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

@@ -27,7 +27,7 @@ import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.AbstractLeakCheckingTestCase;
import org.springframework.core.io.buffer.AbstractLeakCheckingTests;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
@@ -44,7 +44,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
* @since 5.1.3
*/
@SuppressWarnings("ProtectedField")
public abstract class AbstractDecoderTestCase<D extends Decoder<?>> extends AbstractLeakCheckingTestCase {
public abstract class AbstractDecoderTests<D extends Decoder<?>> extends AbstractLeakCheckingTests {
/**
* The decoder to test.
@@ -52,10 +52,10 @@ public abstract class AbstractDecoderTestCase<D extends Decoder<?>> extends Abst
protected D decoder;
/**
* Construct a new {@code AbstractDecoderTestCase} for the given decoder.
* Construct a new {@code AbstractDecoderTests} instance for the given decoder.
* @param decoder the decoder
*/
protected AbstractDecoderTestCase(D decoder) {
protected AbstractDecoderTests(D decoder) {
Assert.notNull(decoder, "Encoder must not be null");
this.decoder = decoder;

View File

@@ -25,7 +25,7 @@ import reactor.core.publisher.Flux;
import reactor.test.StepVerifier;
import org.springframework.core.ResolvableType;
import org.springframework.core.io.buffer.AbstractLeakCheckingTestCase;
import org.springframework.core.io.buffer.AbstractLeakCheckingTests;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.lang.Nullable;
@@ -45,7 +45,7 @@ import static org.springframework.core.io.buffer.DataBufferUtils.release;
* @since 5.1.3
*/
@SuppressWarnings("ProtectedField")
public abstract class AbstractEncoderTestCase<E extends Encoder<?>> extends AbstractLeakCheckingTestCase {
public abstract class AbstractEncoderTests<E extends Encoder<?>> extends AbstractLeakCheckingTests {
/**
* The encoder to test.
@@ -57,7 +57,7 @@ public abstract class AbstractEncoderTestCase<E extends Encoder<?>> extends Abst
* Construct a new {@code AbstractEncoderTestCase} for the given parameters.
* @param encoder the encoder
*/
protected AbstractEncoderTestCase(E encoder) {
protected AbstractEncoderTests(E encoder) {
Assert.notNull(encoder, "Encoder must not be null");

View File

@@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
class ByteArrayDecoderTests extends AbstractDecoderTestCase<ByteArrayDecoder> {
class ByteArrayDecoderTests extends AbstractDecoderTests<ByteArrayDecoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -29,7 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
class ByteArrayEncoderTests extends AbstractEncoderTestCase<ByteArrayEncoder> {
class ByteArrayEncoderTests extends AbstractEncoderTests<ByteArrayEncoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sebastien Deleuze
*/
class ByteBufferDecoderTests extends AbstractDecoderTestCase<ByteBufferDecoder> {
class ByteBufferDecoderTests extends AbstractDecoderTests<ByteBufferDecoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sebastien Deleuze
*/
class ByteBufferEncoderTests extends AbstractEncoderTestCase<ByteBufferEncoder> {
class ByteBufferEncoderTests extends AbstractEncoderTests<ByteBufferEncoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sebastien Deleuze
*/
class CharSequenceEncoderTests extends AbstractEncoderTestCase<CharSequenceEncoder> {
class CharSequenceEncoderTests extends AbstractEncoderTests<CharSequenceEncoder> {
private final String foo = "foo";

View File

@@ -32,7 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sebastien Deleuze
*/
class DataBufferDecoderTests extends AbstractDecoderTestCase<DataBufferDecoder> {
class DataBufferDecoderTests extends AbstractDecoderTests<DataBufferDecoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Sebastien Deleuze
*/
class DataBufferEncoderTests extends AbstractEncoderTestCase<DataBufferEncoder> {
class DataBufferEncoderTests extends AbstractEncoderTests<DataBufferEncoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -37,7 +37,7 @@ import static org.springframework.core.ResolvableType.forClass;
/**
* @author Arjen Poutsma
*/
class ResourceDecoderTests extends AbstractDecoderTestCase<ResourceDecoder> {
class ResourceDecoderTests extends AbstractDecoderTests<ResourceDecoder> {
private final byte[] fooBytes = "foo".getBytes(StandardCharsets.UTF_8);

View File

@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Arjen Poutsma
*/
class ResourceEncoderTests extends AbstractEncoderTestCase<ResourceEncoder> {
class ResourceEncoderTests extends AbstractEncoderTests<ResourceEncoder> {
private final byte[] bytes = "foo".getBytes(UTF_8);

View File

@@ -43,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Brian Clozel
* @author Mark Paluch
*/
class StringDecoderTests extends AbstractDecoderTestCase<StringDecoder> {
class StringDecoderTests extends AbstractDecoderTests<StringDecoder> {
private static final ResolvableType TYPE = ResolvableType.forClass(String.class);

View File

@@ -27,7 +27,7 @@ import org.junit.jupiter.api.AfterEach;
* @since 5.1.3
* @see LeakAwareDataBufferFactory
*/
public abstract class AbstractLeakCheckingTestCase {
public abstract class AbstractLeakCheckingTests {
/**
* The data buffer factory.

View File

@@ -35,7 +35,7 @@ import org.springframework.util.Assert;
* Implementation of the {@code DataBufferFactory} interface that keeps track of
* memory leaks.
* <p>Useful for unit tests that handle data buffers. Simply inherit from
* {@link AbstractLeakCheckingTestCase} or call {@link #checkForLeaks()} in
* {@link AbstractLeakCheckingTests} or call {@link #checkForLeaks()} in
* a JUnit <em>after</em> method yourself, and any buffers that have not been
* released will result in an {@link AssertionError}.
*

View File

@@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Arjen Poutsma
* @author Sam Brannen
*/
abstract class AbstractStaxHandlerTestCase {
abstract class AbstractStaxHandlerTests {
private static final String COMPLEX_XML =
"<?xml version='1.0' encoding='UTF-8'?>" +

View File

@@ -54,7 +54,7 @@ import static org.mockito.Mockito.mock;
/**
* @author Arjen Poutsma
*/
abstract class AbstractStaxXMLReaderTestCase {
abstract class AbstractStaxXMLReaderTests {
protected static XMLInputFactory inputFactory;

View File

@@ -24,7 +24,7 @@ import javax.xml.transform.Result;
/**
* @author Arjen Poutsma
*/
class StaxEventHandlerTests extends AbstractStaxHandlerTestCase {
class StaxEventHandlerTests extends AbstractStaxHandlerTests {
@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {

View File

@@ -33,7 +33,7 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTestCase {
class StaxEventXMLReaderTests extends AbstractStaxXMLReaderTests {
public static final String CONTENT = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";

View File

@@ -24,7 +24,7 @@ import javax.xml.transform.Result;
/**
* @author Arjen Poutsma
*/
class StaxStreamHandlerTests extends AbstractStaxHandlerTestCase {
class StaxStreamHandlerTests extends AbstractStaxHandlerTests {
@Override
protected AbstractStaxHandler createStaxHandler(Result result) throws XMLStreamException {

View File

@@ -36,7 +36,7 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTestCase {
class StaxStreamXMLReaderTests extends AbstractStaxXMLReaderTests {
public static final String CONTENT = "<root xmlns='http://springframework.org/spring-ws'><child/></root>";