-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Non-Java source files were reported in a wrong way, since formatLocat…
…ion(..) assumed that the file name will always be simpleName of enclosing class + ".java". ASM allows to read the source file name from Java bytecode, provided it was compiled into the class-file. This patch adds Optional<sourceFileName> to Source and a 2-step approach within formatLocation(..) that will first try to derive the location from the real source file name, and only if that is not present will use the old heuristic to determine the source file name. Issue: #77 Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
- Loading branch information
1 parent
9e8af8c
commit d7fef52
Showing
8 changed files
with
127 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
archunit/src/test/java/com/tngtech/archunit/core/domain/FormattersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.tngtech.archunit.core.domain; | ||
|
||
import java.util.ArrayList; | ||
|
||
import com.tngtech.archunit.testutil.ArchConfigurationRule; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
|
||
import static com.tngtech.archunit.core.domain.Formatters.formatLocation; | ||
import static com.tngtech.archunit.core.domain.TestUtils.importClassWithContext; | ||
import static com.tngtech.archunit.testutil.Assertions.assertThat; | ||
|
||
public class FormattersTest { | ||
@Rule | ||
public final ArchConfigurationRule configuration = new ArchConfigurationRule(); | ||
|
||
@Before | ||
public void setUp() { | ||
// We need this to create a JavaClass without source, i.e. a stub because the class is missing and cannot be resolved | ||
configuration.resolveAdditionalDependenciesFromClassPath(false); | ||
} | ||
|
||
@Test | ||
public void format_location() { | ||
JavaClass classWithSource = importClassWithContext(Object.class); | ||
|
||
assertThat(classWithSource.getSource()).as("source").isPresent(); | ||
assertThat(formatLocation(classWithSource, 7)).isEqualTo("(Object.java:7)"); | ||
|
||
JavaClass classWithoutSource = getClassWithoutSource(); | ||
|
||
assertThat(classWithoutSource.getSource()).as("source").isAbsent(); | ||
assertThat(formatLocation(classWithoutSource, 7)).isEqualTo(String.format("(%s.java:7)", classWithoutSource.getSimpleName())); | ||
} | ||
|
||
private JavaClass getClassWithoutSource() { | ||
for (JavaAccess<?> javaAccess : importClassWithContext(SomeClass.class).getAccessesFromSelf()) { | ||
if (javaAccess.getTargetOwner().isEquivalentTo(ArrayList.class)) { | ||
return javaAccess.getTargetOwner(); | ||
} | ||
} | ||
throw new RuntimeException("Could not create any java class without source"); | ||
} | ||
|
||
private static class SomeClass { | ||
public SomeClass() { | ||
new ArrayList<>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters