|
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 | +} |
0 commit comments