Skip to content

Commit b083bbd

Browse files
philwebbcbeams
authored andcommitted
Introduce 'spring-build-junit' subproject
Introduce new 'spring-build-junit' subproject containing shared JUnit utilities and classes to be used by other test cases. This project is for internal use within the framework, and therefore creates no artifacts to be published to any repository. The initial code includes support for JUnit Assumptions that can be used to determine when a test should run. Tests can be skipped based on the running JDK version, logging level or based on specific 'groups' that have activated via a Gradle property. It is intended that sources within the spring-build-junit project be folded into spring-core/src/test/java, pending some Gradle work that will facilitate sharing test output across subprojects; therefore this commit should be seen as a temporary solution. Issue: SPR-9984
1 parent 330457b commit b083bbd

File tree

7 files changed

+415
-1
lines changed

7 files changed

+415
-1
lines changed

build.gradle

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ configure(allprojects.findAll{it.name in ["spring", "spring-jms", "spring-orm",
110110
}
111111
}
112112

113-
configure(subprojects) { subproject ->
113+
configure(subprojects - project(":spring-build-junit")) { subproject ->
114114
apply plugin: "merge"
115115
apply from: "${gradleScriptDir}/publish-maven.gradle"
116116

@@ -159,6 +159,34 @@ configure(subprojects) { subproject ->
159159
}
160160
}
161161

162+
configure(allprojects - project(":spring-build-junit")) {
163+
dependencies {
164+
testCompile(project(":spring-build-junit"))
165+
}
166+
167+
eclipse.classpath.file.whenMerged { classpath ->
168+
classpath.entries.find{it.path == "/spring-build-junit"}.exported = false
169+
}
170+
171+
test.systemProperties.put("testGroups", properties.get("testGroups"))
172+
}
173+
174+
project("spring-build-junit") {
175+
description = "Build-time JUnit dependencies and utilities"
176+
177+
// NOTE: This is an internal project and is not published.
178+
179+
dependencies {
180+
compile("commons-logging:commons-logging:1.1.1")
181+
compile("junit:junit:${junitVersion}")
182+
compile("org.hamcrest:hamcrest-all:1.3")
183+
compile("org.easymock:easymock:${easymockVersion}")
184+
}
185+
186+
// Don't actually generate any artifacts
187+
configurations.archives.artifacts.clear()
188+
}
189+
162190

163191
project("spring-core") {
164192
description = "Spring Core"

settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ include "spring-web"
2222
include "spring-webmvc"
2323
include "spring-webmvc-portlet"
2424
include "spring-webmvc-tiles3"
25+
include "spring-build-junit"
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build.junit;
18+
19+
import static org.junit.Assume.assumeFalse;
20+
21+
import java.util.Set;
22+
23+
import org.apache.commons.logging.Log;
24+
import org.junit.internal.AssumptionViolatedException;
25+
26+
/**
27+
* Provides utility methods that allow JUnit tests to {@link Assume} certain conditions
28+
* hold {@code true}. If the assumption fails, it means the test should be skipped.
29+
*
30+
* <p>For example, if a set of tests require at least JDK 1.7 it can use
31+
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
32+
*
33+
* <pre class="code">
34+
* public void MyTests {
35+
*
36+
* &#064;BeforeClass
37+
* public static assumptions() {
38+
* Assume.atLeast(JdkVersion.JAVA_17);
39+
* }
40+
*
41+
* // ... all the test methods that require at least JDK 1.7
42+
* }
43+
* </pre>
44+
*
45+
* If only a single test requires at least JDK 1.7 it can use the
46+
* {@code Assume#atLeast(JdkVersion.JAVA_17)} as shown below:
47+
*
48+
* <pre class="code">
49+
* public void MyTests {
50+
*
51+
* &#064;Test
52+
* public void requiresJdk17 {
53+
* Assume.atLeast(JdkVersion.JAVA_17);
54+
* // ... perform the actual test
55+
* }
56+
* }
57+
* </pre>
58+
*
59+
* In addition to assumptions based on the JDK version, tests can be categorized into
60+
* {@link TestGroup}s. Active groups are enabled using the 'testGroups' system property,
61+
* usually activated from the gradle command line:
62+
* <pre>
63+
* gradle test -PtestGroups="performance"
64+
* </pre>
65+
*
66+
* Groups can be specified as a comma separated list of values, or using the pseudo group
67+
* 'all'. See {@link TestGroup} for a list of valid groups.
68+
*
69+
* @author Rob Winch
70+
* @author Phillip Webb
71+
* @since 3.2
72+
* @see #atLeast(JavaVersion)
73+
* @see #group(TestGroup)
74+
*/
75+
public abstract class Assume {
76+
77+
78+
private static final Set<TestGroup> GROUPS = TestGroup.parse(System.getProperty("testGroups"));
79+
80+
81+
/**
82+
* Assume a minimum {@link JavaVersion} is running.
83+
* @param version the minimum version for the test to run
84+
*/
85+
public static void atLeast(JavaVersion version) {
86+
if (!JavaVersion.runningVersion().isAtLeast(version)) {
87+
throw new AssumptionViolatedException("Requires JDK " + version + " but running "
88+
+ JavaVersion.runningVersion());
89+
}
90+
}
91+
92+
/**
93+
* Assume that a particular {@link TestGroup} has been specified.
94+
* @param group the group that must be specified.
95+
*/
96+
public static void group(TestGroup group) {
97+
if (!GROUPS.contains(group)) {
98+
throw new AssumptionViolatedException("Requires unspecified group " + group
99+
+ " from " + GROUPS);
100+
}
101+
}
102+
103+
/**
104+
* Assume that the specified log is not set to Trace or Debug.
105+
* @param log the log to test
106+
*/
107+
public static void notLogging(Log log) {
108+
assumeFalse(log.isTraceEnabled());
109+
assumeFalse(log.isDebugEnabled());
110+
}
111+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build.junit;
18+
19+
/**
20+
* Enumeration of known JDK versions.
21+
*
22+
* @author Phillip Webb
23+
* @see #runningVersion()
24+
*/
25+
public enum JavaVersion {
26+
27+
28+
/**
29+
* Java 1.5
30+
*/
31+
JAVA_15("1.5", 15),
32+
33+
/**
34+
* Java 1.6
35+
*/
36+
JAVA_16("1.6", 16),
37+
38+
/**
39+
* Java 1.7
40+
*/
41+
JAVA_17("1.7", 17),
42+
43+
/**
44+
* Java 1.8
45+
*/
46+
JAVA_18("1.8", 18);
47+
48+
49+
private static final JavaVersion runningVersion = findRunningVersion();
50+
51+
private static JavaVersion findRunningVersion() {
52+
String version = System.getProperty("java.version");
53+
for (JavaVersion candidate : values()) {
54+
if (version.startsWith(candidate.version)) {
55+
return candidate;
56+
}
57+
}
58+
return JavaVersion.JAVA_15;
59+
}
60+
61+
62+
private String version;
63+
64+
private int value;
65+
66+
67+
private JavaVersion(String version, int value) {
68+
this.version = version;
69+
this.value = value;
70+
}
71+
72+
73+
@Override
74+
public String toString() {
75+
return version;
76+
}
77+
78+
/**
79+
* Determines if the specified version is the same as or greater than this version.
80+
* @param version the version to check
81+
* @return {@code true} if the specified version is at least this version
82+
*/
83+
public boolean isAtLeast(JavaVersion version) {
84+
return this.value >= version.value;
85+
}
86+
87+
88+
/**
89+
* Returns the current running JDK version. If the current version cannot be
90+
* determined {@link #JAVA_15} will be returned.
91+
* @return the JDK version
92+
*/
93+
public static JavaVersion runningVersion() {
94+
return runningVersion;
95+
}
96+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build.junit;
18+
19+
import java.util.Collections;
20+
import java.util.EnumSet;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
24+
/**
25+
* A test group used to limit when certain tests are run.
26+
*
27+
* @see Assume#group(TestGroup)
28+
* @author Phillip Webb
29+
*/
30+
public enum TestGroup {
31+
32+
33+
/**
34+
* Performance related tests that may take a considerable time to run.
35+
*/
36+
PERFORMANCE;
37+
38+
39+
/**
40+
* Parse the specified comma separates string of groups.
41+
* @param value the comma separated string of groups
42+
* @return a set of groups
43+
*/
44+
public static Set<TestGroup> parse(String value) {
45+
if (value == null || "".equals(value)) {
46+
return Collections.emptySet();
47+
}
48+
if("ALL".equalsIgnoreCase(value)) {
49+
return EnumSet.allOf(TestGroup.class);
50+
}
51+
Set<TestGroup> groups = new HashSet<TestGroup>();
52+
for (String group : value.split(",")) {
53+
try {
54+
groups.add(valueOf(group.trim().toUpperCase()));
55+
} catch (IllegalArgumentException e) {
56+
throw new IllegalArgumentException("Unable to find test group '" + group.trim()
57+
+ "' when parsing '" + value + "'");
58+
}
59+
}
60+
return groups;
61+
}
62+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.build.junit;
18+
19+
import static org.hamcrest.Matchers.startsWith;
20+
import static org.junit.Assert.*;
21+
22+
import org.junit.Test;
23+
24+
/**
25+
* Tests for {@link JavaVersion}.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
public class JavaVersionTest {
30+
31+
@Test
32+
public void runningVersion() {
33+
assertNotNull(JavaVersion.runningVersion());
34+
assertThat(System.getProperty("java.version"), startsWith(JavaVersion.runningVersion().toString()));
35+
}
36+
37+
@Test
38+
public void isAtLeast() throws Exception {
39+
assertTrue(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_15));
40+
assertTrue(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_16));
41+
assertFalse(JavaVersion.JAVA_16.isAtLeast(JavaVersion.JAVA_17));
42+
}
43+
}

0 commit comments

Comments
 (0)