Skip to content

Commit

Permalink
testing archunit #120
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Jan 29, 2021
1 parent 4b645d5 commit 3a92cf4
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/se/bjurr/violations/lib/parsers/JUnitParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ private FileAndLine deriveFileFromClass(final String failureContent) {
fl.line = Integer.parseInt(matcher.group(7));
return fl;
}

final Matcher classNameMatcher =
Pattern.compile("([a-zA-Z\\.]+?):(\\d+)").matcher(failureContentFrontSlash);
final boolean foundClassName = classNameMatcher.find();
if (foundClassName) {
final FileAndLine fl = new FileAndLine();
fl.file = classNameMatcher.group(1);
fl.line = Integer.parseInt(classNameMatcher.group(2));
return fl;
}
return null;
}

Expand Down
67 changes: 67 additions & 0 deletions src/test/java/se/bjurr/violations/lib/JUnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,71 @@ public void testThatViolationsCanBeParsedFromJENKINS64117() {
assertThat(violation0.getMessage()).startsWith("runCalculationScriptTest : The container");
assertThat(violation0.getSeverity()).isEqualTo(ERROR);
}

@Test
public void testThatViolationsCanBeParsedFromArchUnit1() {
final String rootFolder = getRootFolder();

final Set<Violation> actual =
violationsApi() //
.withPattern(".*/junit/archunit1\\.xml$") //
.inFolder(rootFolder) //
.findAll(JUNIT) //
.violations();

assertThat(actual) //
.hasSize(1);

final Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getFile()).isEqualTo("Aufgabe3Test.java");
assertThat(violation0.getMessage())
.startsWith("shouldSplitToEmptyRight(int)[1] : org.opentest4j.AssertionFailedError:");
assertThat(violation0.getSeverity()).isEqualTo(ERROR);
}

@Test
public void testThatViolationsCanBeParsedFromArchUnit2() {
final String rootFolder = getRootFolder();

final Set<Violation> actual =
violationsApi() //
.withPattern(".*/junit/archunit2\\.xml$") //
.inFolder(rootFolder) //
.findAll(JUNIT) //
.violations();

assertThat(actual) //
.hasSize(2);

final Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getFile()).isEqualTo("Aufgabe3Test.java");
assertThat(violation0.getMessage())
.startsWith("shouldSplitToEmptyRight(int)[1] : org.opentest4j.AssertionFailedError:");
assertThat(violation0.getSeverity()).isEqualTo(ERROR);

final Violation violation1 = new ArrayList<>(actual).get(1);
assertThat(violation1.getFile()).isEqualTo("Integers.java");
assertThat(violation1.getMessage()).startsWith("ALL_FINAL : java.lang.AssertionError");
assertThat(violation1.getSeverity()).isEqualTo(ERROR);
}

@Test
public void testThatViolationsCanBeParsedFromArchUnit3() {
final String rootFolder = getRootFolder();

final Set<Violation> actual =
violationsApi() //
.withPattern(".*/junit/archunit3\\.xml$") //
.inFolder(rootFolder) //
.findAll(JUNIT) //
.violations();

assertThat(actual) //
.hasSize(1);

final Violation violation0 = new ArrayList<>(actual).get(0);
assertThat(violation0.getFile()).isEqualTo("Integers.java");
assertThat(violation0.getMessage()).startsWith("ALL_FINAL : java.lang.AssertionError");
assertThat(violation0.getSeverity()).isEqualTo(ERROR);
}
}
17 changes: 17 additions & 0 deletions src/test/resources/junit/archunit1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="Aufgabe3Test" time="0.58" tests="1" errors="0" skipped="0" failures="1">
<testcase name="shouldSplitToEmptyRight(int)[1]" classname="Aufgabe3Test" time="0.092">
<failure type="org.opentest4j.AssertionFailedError"><![CDATA[org.opentest4j.AssertionFailedError:
Expecting:
<[]>
to be equal to:
<[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], []]]]><![CDATA[>
when comparing elements using recursive field/property by field/property comparator on all fields/properties
Comparators used:
- for elements fields (by type): {Double -> DoubleComparator[precision=1.0E-15], Float -> FloatComparator[precision=1.0E-6]}
- for elements (by type): {Double -> DoubleComparator[precision=1.0E-15], Float -> FloatComparator[precision=1.0E-6]}
but was not.
at Aufgabe3Test.shouldSplitToEmptyRight(Aufgabe3Test.java:254)
]]></failure>
</testcase>
</testsuite>
24 changes: 24 additions & 0 deletions src/test/resources/junit/archunit2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="Aufgabe3Test" time="0.58" tests="1" errors="0" skipped="0" failures="1">
<testcase name="shouldSplitToEmptyRight(int)[1]" classname="Aufgabe3Test" time="0.092">
<failure type="org.opentest4j.AssertionFailedError"><![CDATA[org.opentest4j.AssertionFailedError:
Expecting:
<[]>
to be equal to:
<[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], []]]]><![CDATA[>
when comparing elements using recursive field/property by field/property comparator on all fields/properties
Comparators used:
- for elements fields (by type): {Double -> DoubleComparator[precision=1.0E-15], Float -> FloatComparator[precision=1.0E-6]}
- for elements (by type): {Double -> DoubleComparator[precision=1.0E-15], Float -> FloatComparator[precision=1.0E-6]}
but was not.
at Aufgabe3Test.shouldSplitToEmptyRight(Aufgabe3Test.java:254)
]]></failure>
</testcase>
<testcase name="shouldInsertBefore" classname="Aufgabe3Test" time="0.001"/>
<testcase name="ALL_FINAL" classname="Aufgabe3Test" time="0.579">
<failure type="java.lang.AssertionError"><![CDATA[java.lang.AssertionError:
Architecture Violation [Priority: MEDIUM] - Rule 'classes that have simple name 'Integers' should have only final fields' was violated (1 times):
Field <Integers.value> is not final in (Integers.java:0)
]]></failure>
</testcase>
</testsuite>
9 changes: 9 additions & 0 deletions src/test/resources/junit/archunit3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report-3.0.xsd" version="3.0" name="Aufgabe3Test" time="0.58" tests="1" errors="0" skipped="0" failures="1">
<testcase name="ALL_FINAL" classname="Aufgabe3Test" time="0.579">
<failure type="java.lang.AssertionError"><![CDATA[java.lang.AssertionError:
Architecture Violation [Priority: MEDIUM] - Rule 'classes that have simple name 'Integers' should have only final fields' was violated (1 times):
Field <Integers.value> is not final in (Integers.java:0)
]]></failure>
</testcase>
</testsuite>

0 comments on commit 3a92cf4

Please sign in to comment.