Skip to content

Commit

Permalink
fixes #38 "enhanced instanceof with final keyword fails"
Browse files Browse the repository at this point in the history
  • Loading branch information
janScheible committed Oct 19, 2022
1 parent c61ac43 commit 8024159
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ The code changes in a local Git working tree are inspected and not covered metho
This is useful for local development iterations before pushing the changes to a remote repository and starting a CI build.

```DOS .bat
mvn com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports^
mvn com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:clean-jacoco-reports^
jacoco:prepare-agent^
surefire:test^
jacoco:report^
com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform
com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:perform
```

By also adding `jacoco:prepare-agent-integration`, `failsafe:integration-test` and `jacoco:report-integration` integration tests are also considered (method coverages of all JaCoCo reports are merged).
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.1</version>

<packaging>pom</packaging>
<name>com.scheible::test-gap-analysis</name>
Expand Down
6 changes: 3 additions & 3 deletions test-gap-analysis-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.1</version>
</parent>

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-maven-plugin</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.1</version>

<packaging>maven-plugin</packaging>
<name>com.scheible::test-gap-analysis maven-plugin</name>
Expand All @@ -21,7 +21,7 @@

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<test-gap-analysis.version>1.2.1-SNAPSHOT</test-gap-analysis.version>
<test-gap-analysis.version>1.2.1</test-gap-analysis.version>
</properties>

<dependencies>
Expand Down
16 changes: 8 additions & 8 deletions test-gap-analysis/nbactions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -220,22 +220,22 @@
<actionName>CUSTOM-Test Gap Analysis of Working Tree Changes</actionName>
<displayName>Test Gap Analysis of Working Tree Changes</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:perform</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-Test Gap Analysis with Reference Commit</actionName>
<displayName>Test Gap Analysis with Reference Commit</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:perform</goal>
</goals>
<properties>
<test-gap-analysis.reference-commit-hash>30640f01d7c116ca0d75ce9285357e5a8053009e</test-gap-analysis.reference-commit-hash>
Expand All @@ -245,11 +245,11 @@
<actionName>CUSTOM-Test Gap Analysis with Previous Tag RegEx</actionName>
<displayName>Test Gap Analysis with Previous Tag RegEx</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:perform</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:perform</goal>
</goals>
<properties>
<test-gap-analysis.previous-tag-regex>[0-9]+\.[0-9]+\.[0-9]+</test-gap-analysis.previous-tag-regex>
Expand All @@ -259,11 +259,11 @@
<actionName>CUSTOM-Debug Coverage Resolution</actionName>
<displayName>Debug Coverage Resolution</displayName>
<goals>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:clean-jacoco-reports</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:clean-jacoco-reports</goal>
<goal>jacoco:prepare-agent</goal>
<goal>surefire:test</goal>
<goal>jacoco:report</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1-SNAPSHOT:debug-coverage-resolution</goal>
<goal>com.scheible.testgapanalysis:test-gap-analysis-maven-plugin:1.2.1:debug-coverage-resolution</goal>
</goals>
</action>
</actions>
6 changes: 3 additions & 3 deletions test-gap-analysis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<parent>
<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis-parent</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.1</version>
</parent>

<groupId>com.scheible.testgapanalysis</groupId>
<artifactId>test-gap-analysis</artifactId>
<version>1.2.1-SNAPSHOT</version>
<version>1.2.1</version>

<packaging>jar</packaging>
<name>com.scheible::test-gap-analysis library</name>
Expand All @@ -30,7 +30,7 @@
<zt-zip.version>1.15</zt-zip.version>

<jgit.version>5.13.1.202206130422-r</jgit.version>
<javaparser.version>3.24.2</javaparser.version>
<javaparser.version>3.24.4</javaparser.version>
<jaCoCo.version>0.8.8</jaCoCo.version>

<!-- NOTE Dependency versions alligned with Spring Boot 2.7.4 (as well spring-boot-maven-plugin version). -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Collections;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand All @@ -19,13 +20,19 @@
*/
public class JavaParser {

private static final Pattern INSTANCEOF_WITH_FINAL_WORKAROUND_PATTERN = Pattern
.compile("\\sinstanceof\\s+?final\\s");

protected final Logger logger = LoggerFactory.getLogger(getClass());

public Set<ParsedMethod> getMethods(String code, String path) {
ParserConfiguration configuration = new ParserConfiguration();
configuration.setLanguageLevel(LanguageLevel.BLEEDING_EDGE);
com.github.javaparser.JavaParser javaParser = new com.github.javaparser.JavaParser(configuration);

// needed as long https://github.com/javaparser/javaparser/issues/2445#issuecomment-964188096 is not fixed
code = INSTANCEOF_WITH_FINAL_WORKAROUND_PATTERN.matcher(code).replaceAll(" instanceof ");

ParseResult<CompilationUnit> parserResult = javaParser.parse(code);
if (!parserResult.isSuccessful()) {
logger.error("Parsing methods of '{}' failed cause: {}", path, parserResult.getProblems().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ public void testRecordParsing() {
});
}

@Test
public void testEnhancedInstanceofParsing() {
// enhanced instanceof requires Java 16, test-gap source code is Java 8 --> parse from string
assertThat(new JavaParser().getMethods("class Test {\n" + //
" void doIt() {\n" + //
" Object object = \":-)\";\n" + //
" if (object instanceof String string) {\n" + //
" }\n" + //
" }\n" + //
"}", "path")).first().satisfies(pm -> {
assertThat(pm.getName()).isEqualTo("doIt");
});
}

@Test
public void testEnhancedInstanceofWithFinalParsing() {
// enhanced instanceof requires Java 16, test-gap source code is Java 8 --> parse from string
assertThat(new JavaParser().getMethods("class Test {\n" + //
" void doIt() {\n" + //
" Object object = \":-)\";\n" + //
" if (object instanceof final String string) {\n" + //
" }\n" + //
" }\n" + //
"}", "path")).first().satisfies(pm -> {
assertThat(pm.getName()).isEqualTo("doIt");
});
}

public static class MethodMasking { // #debug

public String doIt(String arg1, boolean isDebugMode) {
Expand Down

0 comments on commit 8024159

Please sign in to comment.