Prevent improper use of testing framework APIs

Prior to this commit, a lot of work had been done to prevent improper
use of testing Framework APIs throughout the codebase; however, there
were still some loopholes.

This commit addresses these loopholes by introducing additional
Checkstyle rules (and modifying existing rules) to prevent improper use
of testing framework APIs in production code as well as in test code.

- Checkstyle rules for banned imports have been refactored into
  multiple rules specific to JUnit 3, JUnit 4, JUnit Jupiter, and
  TestNG.
- Accidental usage of org.junit.Assume has been switched to
  org.junit.jupiter.api.Assumptions.
- All test classes now reside under org.springframework packages.
- All test classes (including abstract test classes) now conform to the
  `*Tests` naming convention.
  - As an added bonus, tests in the renamed
    ScenariosForSpringSecurityExpressionTests are now included in the
    build.
- Dead JUnit 4 parameterized code has been removed from
  DefaultServerWebExchangeCheckNotModifiedTests.

Closes gh-22962
This commit is contained in:
Sam Brannen
2019-09-12 11:15:30 +02:00
parent 92d3f7e7d7
commit 30cff46e7f
50 changed files with 163 additions and 170 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,12 +14,13 @@
* limitations under the License.
*/
package com.foo;
package org.springframework.beans.factory.xml;
import java.util.ArrayList;
import java.util.List;
public class Component {
private String name;
private List<Component> components = new ArrayList<>();
@@ -39,4 +40,5 @@ public class Component {
public void setName(String name) {
this.name = name;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.foo;
package org.springframework.beans.factory.xml;
import java.util.List;
@@ -24,8 +24,6 @@ import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.CollectionUtils;
import org.springframework.util.xml.DomUtils;

View File

@@ -14,52 +14,49 @@
* limitations under the License.
*/
package com.foo;
package org.springframework.beans.factory.xml;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Costin Leau
*/
public class ComponentBeanDefinitionParserTests {
@TestInstance(Lifecycle.PER_CLASS)
class ComponentBeanDefinitionParserTests {
private final DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
private static DefaultListableBeanFactory bf;
@BeforeAll
public static void setUpBeforeClass() throws Exception {
bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("com/foo/component-config.xml"));
void setUp() throws Exception {
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(
new ClassPathResource("component-config.xml", ComponentBeanDefinitionParserTests.class));
}
@AfterAll
public static void tearDownAfterClass() throws Exception {
void tearDown() {
bf.destroySingletons();
}
private Component getBionicFamily() {
return bf.getBean("bionic-family", Component.class);
}
@Test
public void testBionicBasic() throws Exception {
void testBionicBasic() {
Component cp = getBionicFamily();
assertThat("Bionic-1").isEqualTo(cp.getName());
}
@Test
public void testBionicFirstLevelChildren() throws Exception {
void testBionicFirstLevelChildren() {
Component cp = getBionicFamily();
List<Component> components = cp.getComponents();
assertThat(2).isEqualTo(components.size());
@@ -68,11 +65,17 @@ public class ComponentBeanDefinitionParserTests {
}
@Test
public void testBionicSecondLevelChildren() throws Exception {
void testBionicSecondLevelChildren() {
Component cp = getBionicFamily();
List<Component> components = cp.getComponents().get(0).getComponents();
assertThat(2).isEqualTo(components.size());
assertThat("Karate-1").isEqualTo(components.get(0).getName());
assertThat("Sport-1").isEqualTo(components.get(1).getName());
}
private Component getBionicFamily() {
return bf.getBean("bionic-family", Component.class);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,13 +14,14 @@
* limitations under the License.
*/
package com.foo;
package org.springframework.beans.factory.xml;
import java.util.List;
import org.springframework.beans.factory.FactoryBean;
public class ComponentFactoryBean implements FactoryBean<Component> {
private Component parent;
private List<Component> children;
@@ -51,4 +52,5 @@ public class ComponentFactoryBean implements FactoryBean<Component> {
public boolean isSingleton() {
return true;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -14,14 +14,12 @@
* limitations under the License.
*/
package com.foo;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
package org.springframework.beans.factory.xml;
public class ComponentNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("component",
new ComponentBeanDefinitionParser());
registerBeanDefinitionParser("component", new ComponentBeanDefinitionParser());
}
}