-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from se2p/more-perfumes
Add Perfumes for Testing and the Java Swing library
- Loading branch information
Showing
48 changed files
with
1,297 additions
and
10 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
72 changes: 72 additions & 0 deletions
72
src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/AssertAllDetector.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,72 @@ | ||
package de.jsilbereisen.perfumator.engine.detector.perfume; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; | ||
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl; | ||
import com.github.javaparser.resolution.types.ResolvedType; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import de.jsilbereisen.perfumator.engine.detector.Detector; | ||
import de.jsilbereisen.perfumator.model.DetectedInstance; | ||
import de.jsilbereisen.perfumator.model.perfume.Perfume; | ||
import lombok.EqualsAndHashCode; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link Detector} for the "Assert all" {@link Perfume}. | ||
* Detects the perfume only if the method is part of the {@link org.junit.jupiter.api.Assertions} class. | ||
*/ | ||
@EqualsAndHashCode | ||
public class AssertAllDetector implements Detector<Perfume> { | ||
|
||
private Perfume perfume; | ||
|
||
private JavaParserFacade analysisContext; | ||
|
||
private static final String QUALIFIED_ASSERT_ALL_METHOD_NAME = "org.junit.jupiter.api.Assertions.assertAll"; | ||
private static final String ASSERTIONS_IMPORT_NAME = "org.junit.jupiter.api.Assertions"; | ||
private static final String ASSERT_ALL = "assertAll"; | ||
|
||
@Override | ||
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) { | ||
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>(); | ||
List<MethodCallExpr> assertAllMethodCallExpressions = getAssertAllMethodCalls(astRoot); | ||
assertAllMethodCallExpressions | ||
.forEach(callExpr -> detectedInstances.add(DetectedInstance.from(callExpr, perfume, astRoot))); | ||
return detectedInstances; | ||
} | ||
|
||
@Override | ||
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) { | ||
this.perfume = concreteDetectable; | ||
} | ||
|
||
@Override | ||
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { | ||
this.analysisContext = analysisContext; | ||
} | ||
|
||
private List<MethodCallExpr> getAssertAllMethodCalls(@NotNull CompilationUnit astRoot) { | ||
return astRoot.findAll(MethodCallExpr.class, expr -> { | ||
// contains instead of equals because of possible 'Assertions.assertAll' calls | ||
if (!expr.getNameAsString().contains(ASSERT_ALL)) { | ||
return false; | ||
} | ||
if (expr.getScope().isPresent()) { | ||
// for non-static imports | ||
ResolvedType resolvedType = expr.getScope().get().calculateResolvedType(); | ||
return resolvedType instanceof ReferenceTypeImpl referenceType | ||
&& referenceType.getQualifiedName().equals(ASSERTIONS_IMPORT_NAME); | ||
} else { | ||
// for static imports | ||
ResolvedMethodDeclaration resolvedMethodDeclaration = expr.resolve(); | ||
String qualifiedName = resolvedMethodDeclaration.getQualifiedName(); | ||
return qualifiedName.equals(QUALIFIED_ASSERT_ALL_METHOD_NAME); | ||
} | ||
}); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/JFrameDisposeDetector.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,63 @@ | ||
package de.jsilbereisen.perfumator.engine.detector.perfume; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.expr.MethodCallExpr; | ||
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import de.jsilbereisen.perfumator.engine.detector.Detector; | ||
import de.jsilbereisen.perfumator.model.DetectedInstance; | ||
import de.jsilbereisen.perfumator.model.perfume.Perfume; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link Detector} for the "JFrame dispose" {@link Perfume}. | ||
* Detects the perfume only if the method is part of the {@link javax.swing.JFrame} class. | ||
*/ | ||
public class JFrameDisposeDetector implements Detector<Perfume> { | ||
|
||
private Perfume perfume; | ||
|
||
private JavaParserFacade analysisContext; | ||
|
||
private static final String DISPOSE_METHOD_NAME = "dispose"; | ||
private static final String QUALIFIED_JFRAME_CLASS_NAME = "javax.swing.JFrame"; | ||
|
||
@Override | ||
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) { | ||
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>(); | ||
List<MethodCallExpr> disposeMethodCallExpressions = getJFrameDisposeMethodCalls(astRoot); | ||
disposeMethodCallExpressions | ||
.forEach(expr -> detectedInstances.add(DetectedInstance.from(expr, perfume, astRoot))); | ||
return detectedInstances; | ||
} | ||
|
||
@Override | ||
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) { | ||
this.perfume = concreteDetectable; | ||
} | ||
|
||
@Override | ||
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { | ||
this.analysisContext = analysisContext; | ||
} | ||
|
||
private List<MethodCallExpr> getJFrameDisposeMethodCalls(@NotNull CompilationUnit astRoot) { | ||
return astRoot.findAll(MethodCallExpr.class, expr -> { | ||
if (!expr.getNameAsString().equals(DISPOSE_METHOD_NAME)) { | ||
return false; | ||
} | ||
var scope = expr.getScope(); | ||
if (scope.isPresent()) { | ||
var resolvedType = scope.get().calculateResolvedType(); | ||
if (resolvedType instanceof ReferenceTypeImpl referenceType) { | ||
return referenceType.getQualifiedName().equals(QUALIFIED_JFRAME_CLASS_NAME); | ||
} | ||
} | ||
return false; | ||
}); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...in/java/de/jsilbereisen/perfumator/engine/detector/perfume/ParameterizedTestDetector.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,56 @@ | ||
package de.jsilbereisen.perfumator.engine.detector.perfume; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.AnnotationExpr; | ||
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import de.jsilbereisen.perfumator.engine.detector.Detector; | ||
import de.jsilbereisen.perfumator.model.DetectedInstance; | ||
import de.jsilbereisen.perfumator.model.perfume.Perfume; | ||
import lombok.EqualsAndHashCode; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link Detector} for the "Parameterized Test" {@link Perfume}. | ||
* Detects the perfume only if the annotation is part of JUnit 5 ({@link org.junit.jupiter.params.ParameterizedTest}). | ||
*/ | ||
@EqualsAndHashCode | ||
public class ParameterizedTestDetector implements Detector<Perfume> { | ||
|
||
private Perfume perfume; | ||
|
||
private JavaParserFacade analysisContext; | ||
|
||
private static final String PARAMETERIZED_TEST_IDENTIFIER = "org.junit.jupiter.params.ParameterizedTest"; | ||
|
||
@Override | ||
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) { | ||
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>(); | ||
List<MethodDeclaration> parameterizedTestMethodDeclarations = getParameterizedTestMethodDeclarations(astRoot); | ||
parameterizedTestMethodDeclarations | ||
.forEach(declaration -> detectedInstances.add(DetectedInstance.from(declaration, perfume, astRoot))); | ||
return detectedInstances; | ||
} | ||
|
||
@Override | ||
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) { | ||
perfume = concreteDetectable; | ||
} | ||
|
||
@Override | ||
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { | ||
this.analysisContext = analysisContext; | ||
} | ||
|
||
private List<MethodDeclaration> getParameterizedTestMethodDeclarations(@NotNull CompilationUnit astRoot) { | ||
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream() | ||
.map(AnnotationExpr::resolve) | ||
.map(ResolvedTypeDeclaration::getQualifiedName) | ||
.anyMatch(name -> name.equals(PARAMETERIZED_TEST_IDENTIFIER))); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...va/de/jsilbereisen/perfumator/engine/detector/perfume/SetupAndTeardownMethodDetector.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,61 @@ | ||
package de.jsilbereisen.perfumator.engine.detector.perfume; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.body.MethodDeclaration; | ||
import com.github.javaparser.ast.expr.AnnotationExpr; | ||
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import de.jsilbereisen.perfumator.engine.detector.Detector; | ||
import de.jsilbereisen.perfumator.model.DetectedInstance; | ||
import de.jsilbereisen.perfumator.model.perfume.Perfume; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* {@link Detector} for the "Setup or teardown method" {@link Perfume}. | ||
* Detects the perfume only if the annotation is part of the JUnit 5 package {@link org.junit.jupiter.api}. | ||
*/ | ||
public class SetupAndTeardownMethodDetector implements Detector<Perfume> { | ||
|
||
private Perfume perfume; | ||
|
||
private JavaParserFacade analysisContext; | ||
|
||
public static final String IMPORT_QUALIFIER = "org.junit.jupiter.api."; | ||
public static final Set<String> TEST_ANNOTATIONS = Set.of("BeforeAll", "BeforeEach", "AfterAll", "AfterEach"); | ||
|
||
@Override | ||
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) { | ||
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>(); | ||
List<MethodDeclaration> setupAndTeardownMethods = getSetupAndTeardownMethodDeclarations(astRoot); | ||
setupAndTeardownMethods | ||
.forEach(declaration -> detectedInstances.add(DetectedInstance.from(declaration, perfume, astRoot))); | ||
return detectedInstances; | ||
} | ||
|
||
@Override | ||
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) { | ||
this.perfume = concreteDetectable; | ||
} | ||
|
||
@Override | ||
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { | ||
this.analysisContext = analysisContext; | ||
} | ||
|
||
private Set<String> getQualifiedAnnotations() { | ||
return TEST_ANNOTATIONS.stream().map(annotation -> IMPORT_QUALIFIER + annotation).collect(Collectors.toSet()); | ||
} | ||
|
||
private List<MethodDeclaration> getSetupAndTeardownMethodDeclarations(@NotNull CompilationUnit astRoot) { | ||
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream() | ||
.map(AnnotationExpr::resolve) | ||
.map(ResolvedTypeDeclaration::getQualifiedName) | ||
.anyMatch(name -> getQualifiedAnnotations().contains(name))); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.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,63 @@ | ||
package de.jsilbereisen.perfumator.engine.detector.perfume; | ||
|
||
import com.github.javaparser.ast.CompilationUnit; | ||
import com.github.javaparser.ast.expr.ObjectCreationExpr; | ||
import com.github.javaparser.resolution.UnsolvedSymbolException; | ||
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl; | ||
import com.github.javaparser.resolution.types.ResolvedType; | ||
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; | ||
import de.jsilbereisen.perfumator.engine.detector.Detector; | ||
import de.jsilbereisen.perfumator.model.DetectedInstance; | ||
import de.jsilbereisen.perfumator.model.perfume.Perfume; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* {@link Detector} for the "Swing timer" {@link Perfume}. | ||
* Detects the perfume only if an object of type {@link javax.swing.Timer} is created. | ||
*/ | ||
public class SwingTimerDetector implements Detector<Perfume> { | ||
|
||
private Perfume perfume; | ||
|
||
private JavaParserFacade analysisContext; | ||
|
||
private static final String QUALIFIED_TIMER_NAME = "javax.swing.Timer"; | ||
|
||
@Override | ||
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) { | ||
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>(); | ||
List<ObjectCreationExpr> newTimerExpressions = getNewTimerExpressions(astRoot); | ||
newTimerExpressions | ||
.forEach(callExpr -> detectedInstances.add(DetectedInstance.from(callExpr, perfume, astRoot))); | ||
return detectedInstances; | ||
} | ||
|
||
@Override | ||
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) { | ||
this.perfume = concreteDetectable; | ||
} | ||
|
||
@Override | ||
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { | ||
this.analysisContext = analysisContext; | ||
} | ||
|
||
private List<ObjectCreationExpr> getNewTimerExpressions(@NotNull CompilationUnit astRoot) { | ||
return astRoot.findAll(ObjectCreationExpr.class, expr -> { | ||
ResolvedType resolvedType; | ||
// when classes from the same package are instantiated, the javaparser cannot resolve the type, therefore | ||
// we simply skip such occurrences of object creation expressions | ||
try { | ||
resolvedType = expr.calculateResolvedType(); | ||
} catch (UnsolvedSymbolException e) { | ||
return false; | ||
} | ||
return resolvedType instanceof ReferenceTypeImpl referenceType | ||
&& referenceType.getQualifiedName().equals(QUALIFIED_TIMER_NAME); | ||
}); | ||
} | ||
} |
Oops, something went wrong.