Move spring-build-junit into spring-core

Move code from spring-build-junit into spring-core/src/test along with
several other test utility classes. This commit removes the temporary
spring-build-junit project introduced in commit
b083bbdec7.
This commit is contained in:
Phillip Webb
2013-01-01 19:41:55 -08:00
parent db2b00a2a5
commit 65fb26f847
26 changed files with 75 additions and 74 deletions

View File

@@ -35,8 +35,6 @@ import java.util.UUID;
import org.junit.Test;
import org.springframework.build.junit.Assume;
import org.springframework.build.junit.TestGroup;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
@@ -46,6 +44,8 @@ import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.core.io.Resource;
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils;

View File

@@ -0,0 +1,112 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
import static org.junit.Assume.assumeFalse;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.junit.internal.AssumptionViolatedException;
/**
* Provides utility methods that allow JUnit tests to {@link org.junit.Assume} certain
* conditions hold {@code true}. If the assumption fails, it means the test should be
* skipped.
*
* <p>For example, if a set of tests require at least JDK 1.7 it can use
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
*
* <pre class="code">
* public void MyTests {
*
* &#064;BeforeClass
* public static assumptions() {
* Assume.atLeast(JdkVersion.JAVA_17);
* }
*
* // ... all the test methods that require at least JDK 1.7
* }
* </pre>
*
* If only a single test requires at least JDK 1.7 it can use the
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
*
* <pre class="code">
* public void MyTests {
*
* &#064;Test
* public void requiresJdk17 {
* Assume.atLeast(JdkVersion.JAVA_17);
* // ... perform the actual test
* }
* }
* </pre>
*
* In addition to assumptions based on the JDK version, tests can be categorized into
* {@link TestGroup}s. Active groups are enabled using the 'testGroups' system property,
* usually activated from the gradle command line:
* <pre>
* gradle test -PtestGroups="performance"
* </pre>
*
* Groups can be specified as a comma separated list of values, or using the pseudo group
* 'all'. See {@link TestGroup} for a list of valid groups.
*
* @author Rob Winch
* @author Phillip Webb
* @since 3.2
* @see #atLeast(JavaVersion)
* @see #group(TestGroup)
*/
public abstract class Assume {
private static final Set<TestGroup> GROUPS = TestGroup.parse(System.getProperty("testGroups"));
/**
* Assume a minimum {@link JavaVersion} is running.
* @param version the minimum version for the test to run
*/
public static void atLeast(JavaVersion version) {
if (!JavaVersion.runningVersion().isAtLeast(version)) {
throw new AssumptionViolatedException("Requires JDK " + version + " but running "
+ JavaVersion.runningVersion());
}
}
/**
* Assume that a particular {@link TestGroup} has been specified.
* @param group the group that must be specified.
*/
public static void group(TestGroup group) {
if (!GROUPS.contains(group)) {
throw new AssumptionViolatedException("Requires unspecified group " + group
+ " from " + GROUPS);
}
}
/**
* Assume that the specified log is not set to Trace or Debug.
* @param log the log to test
*/
public static void notLogging(Log log) {
assumeFalse(log.isTraceEnabled());
assumeFalse(log.isDebugEnabled());
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
/**
* Enumeration of known JDK versions.
*
* @author Phillip Webb
* @see #runningVersion()
*/
public enum JavaVersion {
/**
* Java 1.5
*/
JAVA_15("1.5", 15),
/**
* Java 1.6
*/
JAVA_16("1.6", 16),
/**
* Java 1.7
*/
JAVA_17("1.7", 17),
/**
* Java 1.8
*/
JAVA_18("1.8", 18);
private static final JavaVersion runningVersion = findRunningVersion();
private static JavaVersion findRunningVersion() {
String version = System.getProperty("java.version");
for (JavaVersion candidate : values()) {
if (version.startsWith(candidate.version)) {
return candidate;
}
}
return JavaVersion.JAVA_15;
}
private String version;
private int value;
private JavaVersion(String version, int value) {
this.version = version;
this.value = value;
}
@Override
public String toString() {
return version;
}
/**
* Determines if the specified version is the same as or greater than this version.
* @param version the version to check
* @return {@code true} if the specified version is at least this version
*/
public boolean isAtLeast(JavaVersion version) {
return this.value >= version.value;
}
/**
* Returns the current running JDK version. If the current version cannot be
* determined {@link #JAVA_15} will be returned.
* @return the JDK version
*/
public static JavaVersion runningVersion() {
return runningVersion;
}
}

View File

@@ -0,0 +1,43 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Tests for {@link JavaVersion}.
*
* @author Phillip Webb
*/
public class JavaVersionTests {
@Test
public void runningVersion() {
assertNotNull(JavaVersion.runningVersion());
assertThat(System.getProperty("java.version"), startsWith(JavaVersion.runningVersion().toString()));
}
@Test
public void isAtLeast() throws Exception {
assertTrue(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_15));
assertTrue(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_16));
assertFalse(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_17));
}
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2002-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
/**
* Additional hamcrest matchers.
*
* @author Phillip Webb
*/
public class Matchers {
/**
* Create a matcher that wrapps the specified matcher and tests against the
* {@link Throwable#getCause() cause} of an exception. If the item tested
* is {@code null} not a {@link Throwable} the wrapped matcher will be called
* with a {@code null} item.
*
* <p>Often useful when working with JUnit {@link ExpectedException}
* {@link Rule @Rule}s, for example:
* <pre>
* thrown.expect(DataAccessException.class);
* thrown.except(exceptionCause(isA(SQLException.class)));
* </pre>
*
* @param matcher the matcher to wrap (must not be null)
* @return a matcher that tests using the exception cause
*/
@SuppressWarnings("unchecked")
public static <T> Matcher<T> exceptionCause(final Matcher<T> matcher) {
return (Matcher<T>) new BaseMatcher<Object>() {
@Override
public boolean matches(Object item) {
Throwable cause = null;
if(item != null && item instanceof Throwable) {
cause = ((Throwable)item).getCause();
}
return matcher.matches(cause);
}
@Override
public void describeTo(Description description) {
description.appendText("exception cause ").appendDescriptionOf(matcher);
}
};
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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 org.springframework.build.test.mockito;
package org.springframework.tests;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
/**
* A test group used to limit when certain tests are run.
*
* @see Assume#group(TestGroup)
* @author Phillip Webb
*/
public enum TestGroup {
/**
* Performance related tests that may take a considerable time to run.
*/
PERFORMANCE;
/**
* Parse the specified comma separates string of groups.
* @param value the comma separated string of groups
* @return a set of groups
*/
public static Set<TestGroup> parse(String value) {
if (value == null || "".equals(value)) {
return Collections.emptySet();
}
if("ALL".equalsIgnoreCase(value)) {
return EnumSet.allOf(TestGroup.class);
}
Set<TestGroup> groups = new HashSet<TestGroup>();
for (String group : value.split(",")) {
try {
groups.add(valueOf(group.trim().toUpperCase()));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("Unable to find test group '" + group.trim()
+ "' when parsing '" + value + "'");
}
}
return groups;
}
}

View File

@@ -0,0 +1,73 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.tests;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
/**
* Tests for {@link TestGroup}.
*
* @author Phillip Webb
*/
public class TestGroupTests {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void parseNull() throws Exception {
assertThat(TestGroup.parse(null), is(Collections.<TestGroup> emptySet()));
}
@Test
public void parseEmptyString() throws Exception {
assertThat(TestGroup.parse(""), is(Collections.<TestGroup> emptySet()));
}
@Test
public void parseWithSpaces() throws Exception {
assertThat(TestGroup.parse("PERFORMANCE, PERFORMANCE"),
is((Set<TestGroup>) EnumSet.of(TestGroup.PERFORMANCE)));
}
@Test
public void parseInMixedCase() throws Exception {
assertThat(TestGroup.parse("performance, PERFormaNCE"),
is((Set<TestGroup>) EnumSet.of(TestGroup.PERFORMANCE)));
}
@Test
public void parseMissing() throws Exception {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Unable to find test group 'missing' when parsing 'performance, missing'");
TestGroup.parse("performance, missing");
}
@Test
public void parseAll() throws Exception {
assertThat(TestGroup.parse("all"), is((Set<TestGroup>)EnumSet.allOf(TestGroup.class)));
}
}

View File

@@ -0,0 +1,22 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Shared utilities that are used internally throughout the test suite but are not
* published. This package should not be confused with {@code org.springframework.test}
* which contains published code from the 'spring-test' module.
*/
package org.springframework.tests;

View File

@@ -31,10 +31,10 @@ import org.junit.Before;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.build.test.mockito.MockitoUtils;
import org.springframework.build.test.mockito.MockitoUtils.InvocationArgumentsAdapter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.tests.MockitoUtils;
import org.springframework.tests.MockitoUtils.InvocationArgumentsAdapter;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;