Skip to content

Commit 798c1ef

Browse files
committed
Mods to support split unit and integration tests (#311).
1 parent 053813a commit 798c1ef

37 files changed

+583
-1118
lines changed

pom.xml

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,22 @@
3838
</developers>
3939

4040
<properties>
41-
<java.source.version>1.8</java.source.version>
42-
<java.target.version>1.8</java.target.version>
41+
<java.level>8</java.level>
42+
<java.source.version>1.8</java.source.version>
43+
<java.target.version>1.8</java.target.version>
44+
4345
<jersey.version>2.28</jersey.version>
4446
<jackson.version>2.9.8</jackson.version>
47+
4548
<javaServlet.version>4.0.1</javaServlet.version>
49+
4650
<junit.version>4.12</junit.version>
4751
<mockito.version>2.19.0</mockito.version>
4852
<hamcrest.version>1.3</hamcrest.version>
4953
<systemRules.version>1.18.0</systemRules.version>
5054
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
5155
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
56+
5257
<changelog-lib.version>1.59</changelog-lib.version>
5358
</properties>
5459

@@ -62,6 +67,7 @@
6267
<build>
6368
<defaultGoal>package</defaultGoal>
6469
<plugins>
70+
6571
<plugin>
6672
<groupId>org.apache.maven.plugins</groupId>
6773
<artifactId>maven-release-plugin</artifactId>
@@ -191,22 +197,43 @@
191197
<plugin>
192198
<groupId>org.apache.maven.plugins</groupId>
193199
<artifactId>maven-surefire-plugin</artifactId>
194-
<version>2.22.0</version>
200+
<version>3.0.0-M3</version>
195201
<configuration>
202+
<excludedGroups>org.gitlab4j.api.IntegrationTest</excludedGroups>
196203
<systemPropertyVariables>
197204
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
198205
</systemPropertyVariables>
199206
</configuration>
200207
</plugin>
201208

209+
<plugin>
210+
<groupId>org.apache.maven.plugins</groupId>
211+
<artifactId>maven-failsafe-plugin</artifactId>
212+
<version>3.0.0-M3</version>
213+
<configuration>
214+
<groups>org.gitlab4j.api.IntegrationTest</groups>
215+
<includes>
216+
<include>**/IntegrationTestSuite.java</include>
217+
</includes>
218+
</configuration>
219+
<executions>
220+
<execution>
221+
<goals>
222+
<goal>integration-test</goal>
223+
<goal>verify</goal>
224+
</goals>
225+
</execution>
226+
</executions>
227+
</plugin>
228+
202229
<plugin>
203230
<groupId>org.apache.maven.plugins</groupId>
204231
<artifactId>maven-enforcer-plugin</artifactId>
205232
<version>3.0.0-M2</version>
206233
<configuration>
207234
<rules>
208235
<enforceBytecodeVersion>
209-
<maxJdkVersion>1.8</maxJdkVersion>
236+
<maxJdkVersion>1.8</maxJdkVersion>
210237
<ignoreClasses>
211238
<ignoreClass>module-info</ignoreClass>
212239
</ignoreClasses>
@@ -230,10 +257,31 @@
230257
</dependencies>
231258
</plugin>
232259

260+
<plugin>
261+
<groupId>org.codehaus.mojo</groupId>
262+
<artifactId>animal-sniffer-maven-plugin</artifactId>
263+
<version>1.17</version>
264+
<executions>
265+
<execution>
266+
<goals>
267+
<goal>check</goal>
268+
</goals>
269+
<id>check</id>
270+
</execution>
271+
</executions>
272+
<configuration>
273+
<signature>
274+
<groupId>org.codehaus.mojo.signature</groupId>
275+
<artifactId>java1${java.level}</artifactId>
276+
</signature>
277+
</configuration>
278+
</plugin>
233279
</plugins>
234280

235281
<pluginManagement>
236282
<plugins>
283+
<!-- The se.bjurr.gitchangelog:git-changelog-maven-plugin needs to be in -->
284+
<!-- this section (pluginManagement) for it to work correctly. -->
237285
<plugin>
238286
<groupId>se.bjurr.gitchangelog</groupId>
239287
<artifactId>git-changelog-maven-plugin</artifactId>
@@ -252,7 +300,6 @@
252300
</plugin>
253301
</plugins>
254302
</pluginManagement>
255-
256303
</build>
257304

258305
<dependencies>
@@ -319,7 +366,24 @@
319366
<version>${systemRules.version}</version>
320367
<scope>test</scope>
321368
</dependency>
322-
369+
<dependency>
370+
<groupId>com.googlecode.junit-toolbox</groupId>
371+
<artifactId>junit-toolbox</artifactId>
372+
<version>2.4</version>
373+
<scope>test</scope>
374+
</dependency>
323375
</dependencies>
324376

377+
<dependencyManagement>
378+
<dependencies>
379+
<!-- Needed for animal-sniffer -->
380+
<dependency>
381+
<groupId>org.codehaus.mojo.signature</groupId>
382+
<artifactId>java18</artifactId>
383+
<version>1.0</version>
384+
<type>signature</type>
385+
</dependency>
386+
</dependencies>
387+
</dependencyManagement>
388+
325389
</project>

src/test/java/org/gitlab4j/api/AbstractIntegrationTest.java

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
package org.gitlab4j.api;
22

3+
import java.util.Collections;
4+
import java.util.Map;
5+
import java.util.WeakHashMap;
6+
37
import org.gitlab4j.api.GitLabApi.ApiVersion;
48
import org.gitlab4j.api.models.Project;
59

610
/**
7-
* In order for these tests to run you must set the following properties in
8-
* test-gitlab4j.properties
11+
* In order for the integration tests to run you must set the following properties in test-gitlab4j.properties
12+
* and the resources pointed to must exist.
913
*
1014
* TEST_NAMESPACE
1115
* TEST_PROJECT_NAME
1216
* TEST_HOST_URL
1317
* TEST_PRIVATE_TOKEN
1418
*/
15-
public class AbstractBaseTest {
19+
public class AbstractIntegrationTest {
1620

17-
// The following needs to be set to your test repository
21+
// Get the values of the minimum required test properties.
1822
protected static final String TEST_PROJECT_NAME;
1923
protected static final String TEST_NAMESPACE;
2024
protected static final String TEST_HOST_URL;
@@ -26,14 +30,38 @@ public class AbstractBaseTest {
2630
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
2731
}
2832

29-
protected static Project testProject;
30-
protected static GitLabApi gitLabApi;
33+
protected static class BaseTestResources {
34+
protected GitLabApi gitLabApi;
35+
protected Project testProject;
36+
37+
protected BaseTestResources(GitLabApi gitLabApi) {
38+
this.gitLabApi = gitLabApi;
39+
}
40+
}
41+
42+
// Used to keep track of GitLabApi and Project instances for each test class
43+
private static final Map<String, BaseTestResources> baseTestResourcesMap =
44+
Collections.synchronizedMap(new WeakHashMap<String, BaseTestResources>());
3145

32-
public AbstractBaseTest() {
46+
public AbstractIntegrationTest() {
3347
super();
3448
}
3549

36-
protected static void testSetup() {
50+
/**
51+
* Verifies that the required test properties are present and returns an instance of GitLabApi
52+
* set up to use the TEST_PRIVATE_TOKEN property to authenticate.
53+
*
54+
* @return an instance of GitLabApi set up to use the TEST_PRIVATE_TOKEN property to authenticate
55+
*/
56+
protected static GitLabApi baseTestSetup() {
57+
58+
Throwable t = new Throwable();
59+
StackTraceElement directCaller = t.getStackTrace()[1];
60+
String callingClassName = directCaller.getClassName();
61+
BaseTestResources baseResources = baseTestResourcesMap.get(callingClassName);
62+
if (baseResources != null && baseResources.gitLabApi != null) {
63+
return (baseResources.gitLabApi);
64+
}
3765

3866
String problems = "";
3967
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) {
@@ -54,15 +82,44 @@ protected static void testSetup() {
5482

5583
if (problems.isEmpty()) {
5684
try {
57-
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
58-
testProject = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
85+
GitLabApi gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
86+
baseResources = new BaseTestResources(gitLabApi);
87+
baseTestResourcesMap.put(callingClassName, baseResources);
88+
return (gitLabApi);
5989
} catch (Exception e) {
6090
problems += e.getMessage() + "\n";
6191
}
6292
}
6393

64-
if (!problems.isEmpty()) {
65-
System.err.print(problems);
94+
System.err.print(problems);
95+
return (null);
96+
}
97+
98+
/**
99+
* Get the test Project instance for the calling test class.
100+
*
101+
* @return the test Project instance for the calling test class
102+
*/
103+
protected static Project getTestProject() {
104+
105+
Throwable t = new Throwable();
106+
StackTraceElement directCaller = t.getStackTrace()[1];
107+
String callingClassName = directCaller.getClassName();
108+
BaseTestResources baseResources = baseTestResourcesMap.get(callingClassName);
109+
if (baseResources == null || baseResources.gitLabApi == null) {
110+
System.err.println("Problems fetching test Project instance: GitLabApi instance is null");
111+
return (null);
112+
} else if (baseResources.testProject != null) {
113+
return (baseResources.testProject);
114+
}
115+
116+
try {
117+
Project testProject = (baseResources.gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME));
118+
baseResources.testProject = testProject;
119+
return (testProject);
120+
} catch (Exception e) {
121+
System.err.println("Problems fetching test Project instance: " + e.getMessage());
122+
return (null);
66123
}
67124
}
68125
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.gitlab4j.api;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* This interface is used as a category for JUnit allowing integration tests to be skipped
10+
* while running unit tests.
11+
* <p>To mark a test class as an integration test add the following lines before the class declaration:</p>
12+
*
13+
* <pre><code>
14+
* import org.junit.experimental.categories.Category;
15+
*
16+
* @Category(org.gitlab4j.api.IntegrationTest.class)
17+
* </code></pre>
18+
*/
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Target(ElementType.TYPE)
21+
public @interface IntegrationTest {
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.gitlab4j.api;
2+
3+
import org.junit.BeforeClass;
4+
import org.junit.experimental.categories.Categories.IncludeCategory;
5+
import org.junit.runner.RunWith;
6+
7+
import com.googlecode.junittoolbox.SuiteClasses;
8+
import com.googlecode.junittoolbox.WildcardPatternSuite;
9+
10+
@RunWith(WildcardPatternSuite.class)
11+
@SuiteClasses({"**/Test*.class"})
12+
@IncludeCategory(IntegrationTest.class)
13+
public class IntegrationTestSuite {
14+
15+
@BeforeClass
16+
public static void suiteSetup() {
17+
System.out.println("********************************************************");
18+
System.out.println(" Test Suite Setup");
19+
System.out.println("********************************************************");
20+
21+
// TODO Create default test resources if not present
22+
}
23+
}

0 commit comments

Comments
 (0)