Skip to content

Commit d500440

Browse files
committed
Rewrite integration tests on package cycles
1 parent 7a3faee commit d500440

File tree

8 files changed

+82
-338
lines changed

8 files changed

+82
-338
lines changed

src/test/java/org/scijava/maven/plugin/enforcer/NoPackageCyclesRuleIntegrationTest.java

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
* %%
77
* Redistribution and use in source and binary forms, with or without
88
* modification, are permitted provided that the following conditions are met:
9-
*
9+
*
1010
* 1. Redistributions of source code must retain the above copyright notice,
1111
* this list of conditions and the following disclaimer.
1212
* 2. Redistributions in binary form must reproduce the above copyright notice,
1313
* this list of conditions and the following disclaimer in the documentation
1414
* and/or other materials provided with the distribution.
15-
*
15+
*
1616
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1717
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1818
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@@ -56,54 +56,67 @@
5656
import java.io.IOException;
5757
import java.net.URISyntaxException;
5858
import java.net.URL;
59+
import java.nio.charset.Charset;
5960

6061
import org.apache.commons.io.IOUtils;
6162
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
6263
import org.junit.Before;
6364
import org.junit.Test;
6465

66+
/**
67+
* Tests {@link NoPackageCyclesRule} on a dummy project
68+
* @author Gabriel Selzer
69+
*/
6570
public class NoPackageCyclesRuleIntegrationTest {
66-
private static final URL FITNESSE_TARGET_FOLDER = getResource("fitnesse-target");
67-
private static final URL FITNESSE_EXPECTED_OUTPUT = getResource("fitnesse-expected-output-package-cycle.txt");
68-
private static final URL JUNIT_TARGET_FOLDER = getResource("junit-target");
69-
private static final URL JUNIT_EXPECTED_OUTPUT = getResource("junit-expected-output-package-cycle.txt");
70-
71-
private NoPackageCyclesRule rule;
72-
private EnforcerRuleHelperMock helper;
73-
74-
@Before
75-
public void setUp() throws Exception {
76-
rule = new NoPackageCyclesRule();
77-
helper = new EnforcerRuleHelperMock();
78-
}
71+
// Cyclic dependency test files
72+
private static final URL CYCLIC_TARGET_FOLDER = getResource("cyclic-target");
73+
private static final URL CYCLIC_EXPECTED_OUTPUT = getResource("cyclic-expected-output.txt");
74+
// Control test files
75+
private static final URL CONTROL_TARGET_FOLDER = getResource("control-target");
7976

80-
@Test
81-
public void fitnesseIntegrationTest() throws Exception {
82-
assertPackageCycles(FITNESSE_TARGET_FOLDER, FITNESSE_EXPECTED_OUTPUT);
83-
}
77+
private NoPackageCyclesRule rule;
78+
private EnforcerRuleHelperMock helper;
8479

85-
@Test
86-
public void junitIntegrationTest() throws Exception {
87-
assertPackageCycles(JUNIT_TARGET_FOLDER, JUNIT_EXPECTED_OUTPUT);
88-
}
80+
@Before
81+
public void setUp() throws Exception {
82+
rule = new NoPackageCyclesRule();
83+
helper = new EnforcerRuleHelperMock();
84+
}
8985

90-
private void assertPackageCycles(URL targetFolder, URL expectedOutput) throws URISyntaxException, IOException {
91-
helper.setTestClassesDir(new File("non-existent"));
92-
helper.setClassesDir(new File(targetFolder.toURI()));
93-
try {
94-
rule.execute(helper);
95-
fail("expected EnforcerRuleException");
96-
} catch (EnforcerRuleException e) {
97-
// using assertEquals to get a nice comparison editor in eclipse
98-
assertEquals(getExpectedOutput(expectedOutput), e.getMessage());
99-
}
100-
}
86+
/**
87+
* Test that package cycles throw an error
88+
*/
89+
@Test
90+
public void cyclicIntegrationTest() throws URISyntaxException, IOException {
91+
helper.setTestClassesDir(new File("non-existent"));
92+
helper.setClassesDir(new File(CYCLIC_TARGET_FOLDER.toURI()));
93+
try {
94+
rule.execute(helper);
95+
fail("expected EnforcerRuleException");
96+
} catch (EnforcerRuleException e) {
97+
String expected = IOUtils.toString(CYCLIC_EXPECTED_OUTPUT.openStream(), (Charset) null) //
98+
.replaceAll("\r", "") //
99+
.trim();
100+
String actual = e.getMessage().trim();
101+
assertEquals(expected, actual);
102+
}
103+
}
101104

102-
private String getExpectedOutput(URL expectedOutput) throws IOException {
103-
return IOUtils.toString(expectedOutput.openStream()).replaceAll("\r", "");
104-
}
105+
/**
106+
* Test no error thrown for acyclic package structure
107+
*/
108+
@Test
109+
public void controlIntegrationTest() throws URISyntaxException {
110+
helper.setTestClassesDir(new File("non-existent"));
111+
helper.setClassesDir(new File(CONTROL_TARGET_FOLDER.toURI()));
112+
try {
113+
rule.execute(helper);
114+
} catch (EnforcerRuleException e) {
115+
fail("expected EnforcerRuleException");
116+
}
117+
}
105118

106-
private static URL getResource(String path) {
107-
return Thread.currentThread().getContextClassLoader().getResource(path);
108-
}
119+
private static URL getResource(String path) {
120+
return Thread.currentThread().getContextClassLoader().getResource(path);
121+
}
109122
}

src/test/java/org/scijava/maven/plugin/enforcer/NoSubpackageDependenceRuleIntegrationTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
import org.junit.Before;
6464
import org.junit.Test;
6565

66+
/**
67+
* Tests {@link NoSubpackageDependenceRule} on a dummy project
68+
* @author Gabriel Selzer
69+
*/
6670
public class NoSubpackageDependenceRuleIntegrationTest {
6771
// Subpackage dependence test files
6872
private static final URL SUBPACKAGE_DEPENDENT_TARGET_FOLDER = //
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
There are package cycles:
2+
3+
Package-cycle found involving com.example.bar, com.example.foo:
4+
com.example.bar depends on:
5+
com.example.foo (Bar)
6+
com.example.foo depends on:
7+
com.example.bar (Foo)
242 Bytes
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.bar;
2+
3+
import com.example.foo.Foo;
4+
5+
public class Bar {
6+
public Bar() {
7+
final Foo foo = new Foo();
8+
}
9+
}
242 Bytes
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.foo;
2+
3+
import com.example.bar.Bar;
4+
5+
public class Foo {
6+
public Foo() {
7+
final Bar bar = new Bar();
8+
}
9+
}

0 commit comments

Comments
 (0)