Skip to content

Commit 5cd395e

Browse files
committed
fix ArchConfigurationRule default
By default `ArchConfigurationRule` should use the configured `resolveAdditionalDependenciesFromClassPath` instead of the `boolean` default `false`. Unfortunately this masked that `@Before void setUp() { configuration.... }` does actually not do anything, since `@Before` runs after the rule `before` and the rule `before` sets the configuration. Signed-off-by: Peter Gafert <[email protected]>
1 parent b7bf88a commit 5cd395e

File tree

2 files changed

+47
-53
lines changed

2 files changed

+47
-53
lines changed
Lines changed: 46 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,46 @@
1-
package com.tngtech.archunit.core.domain;
2-
3-
import java.util.ArrayList;
4-
5-
import com.tngtech.archunit.testutil.ArchConfigurationRule;
6-
import org.assertj.core.api.Assertions;
7-
import org.junit.Before;
8-
import org.junit.Rule;
9-
import org.junit.Test;
10-
11-
import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext;
12-
import static com.tngtech.archunit.testutil.Assertions.assertThat;
13-
14-
public class SourceCodeLocationTest {
15-
16-
@Rule
17-
public final ArchConfigurationRule configuration = new ArchConfigurationRule();
18-
19-
@Before
20-
public void setUp() {
21-
// We need this to create a JavaClass without source, i.e. a stub because the class is missing and cannot be resolved
22-
configuration.resolveAdditionalDependenciesFromClassPath(false);
23-
}
24-
25-
@Test
26-
public void format_location() {
27-
JavaClass classWithSource = importClassWithContext(Object.class);
28-
29-
assertThat(classWithSource.getSource()).as("source").isPresent();
30-
Assertions.assertThat(SourceCodeLocation.of(classWithSource, 7).toString()).isEqualTo("(Object.java:7)");
31-
32-
JavaClass classWithoutSource = getClassWithoutSource();
33-
34-
assertThat(classWithoutSource.getSource()).as("source").isAbsent();
35-
Assertions.assertThat(SourceCodeLocation.of(classWithoutSource, 7).toString()).isEqualTo(String.format("(%s.java:7)", classWithoutSource.getSimpleName()));
36-
}
37-
38-
private JavaClass getClassWithoutSource() {
39-
for (JavaAccess<?> javaAccess : importClassWithContext(SomeClass.class).getAccessesFromSelf()) {
40-
if (javaAccess.getTargetOwner().isEquivalentTo(ArrayList.class)) {
41-
return javaAccess.getTargetOwner();
42-
}
43-
}
44-
throw new RuntimeException("Could not create any java class without source");
45-
}
46-
47-
private static class SomeClass {
48-
public SomeClass() {
49-
new ArrayList<>();
50-
}
51-
}
52-
}
1+
package com.tngtech.archunit.core.domain;
2+
3+
import java.util.ArrayList;
4+
5+
import com.tngtech.archunit.testutil.ArchConfigurationRule;
6+
import org.assertj.core.api.Assertions;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
10+
import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext;
11+
import static com.tngtech.archunit.testutil.Assertions.assertThat;
12+
13+
public class SourceCodeLocationTest {
14+
15+
// We need this to create a JavaClass without source, i.e. a stub because the class is missing and cannot be resolved
16+
@Rule
17+
public final ArchConfigurationRule configuration = new ArchConfigurationRule().resolveAdditionalDependenciesFromClassPath(false);
18+
19+
@Test
20+
public void format_location() {
21+
JavaClass classWithSource = importClassWithContext(Object.class);
22+
23+
assertThat(classWithSource.getSource()).as("source").isPresent();
24+
Assertions.assertThat(SourceCodeLocation.of(classWithSource, 7).toString()).isEqualTo("(Object.java:7)");
25+
26+
JavaClass classWithoutSource = getClassWithoutSource();
27+
28+
assertThat(classWithoutSource.getSource()).as("source").isAbsent();
29+
Assertions.assertThat(SourceCodeLocation.of(classWithoutSource, 7).toString()).isEqualTo(String.format("(%s.java:7)", classWithoutSource.getSimpleName()));
30+
}
31+
32+
private JavaClass getClassWithoutSource() {
33+
for (JavaAccess<?> javaAccess : importClassWithContext(SomeClass.class).getAccessesFromSelf()) {
34+
if (javaAccess.getTargetOwner().isEquivalentTo(ArrayList.class)) {
35+
return javaAccess.getTargetOwner();
36+
}
37+
}
38+
throw new RuntimeException("Could not create any java class without source");
39+
}
40+
41+
private static class SomeClass {
42+
public SomeClass() {
43+
new ArrayList<>();
44+
}
45+
}
46+
}

archunit/src/test/java/com/tngtech/archunit/testutil/ArchConfigurationRule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.junit.rules.ExternalResource;
55

66
public class ArchConfigurationRule extends ExternalResource {
7-
private boolean resolveMissingDependenciesFromClassPath;
7+
private boolean resolveMissingDependenciesFromClassPath = ArchConfiguration.get().resolveMissingDependenciesFromClassPath();
88

99
public ArchConfigurationRule resolveAdditionalDependenciesFromClassPath(boolean enabled) {
1010
resolveMissingDependenciesFromClassPath = enabled;

0 commit comments

Comments
 (0)