Search in sources :

Example 1 with ClazzFileFinder

use of de.dagere.peass.dependency.ClazzFileFinder in project peass by DaGeRe.

the class PropertyChangeGuesser method getGuesses.

public Set<String> getGuesses(final PeassFolders folders, final Entry<ChangedEntity, ClazzChangeData> changedEntity) throws FileNotFoundException {
    final Set<String> guessedTypes = new HashSet<>();
    // TODO Here, a real config should be passed; since this is rarely used, we go with the default folders
    ClazzFileFinder finder = new ClazzFileFinder(new ExecutionConfig());
    final File file = finder.getSourceFile(folders.getProjectFolder(), changedEntity.getKey());
    final File fileOld = finder.getSourceFile(folders.getOldSources(), changedEntity.getKey());
    if (file != null && fileOld != null && file.exists() && fileOld.exists()) {
        final CompilationUnit clazzUnit = JavaParserProvider.parse(file);
        final CompilationUnit clazzUnitOld = JavaParserProvider.parse(fileOld);
        for (Map.Entry<String, Set<String>> changedClazz : changedEntity.getValue().getChangedMethods().entrySet()) {
            // If only method change..
            if (changedClazz.getValue() != null) {
                for (String method : changedClazz.getValue()) {
                    final String source = FileComparisonUtil.getMethodSource(changedEntity.getKey(), method, clazzUnit);
                    final String sourceOld = FileComparisonUtil.getMethodSource(changedEntity.getKey(), method, clazzUnitOld);
                    final Patch<String> changedLinesMethod = DiffUtils.diff(Arrays.asList(sourceOld.split("\n")), Arrays.asList(source.split("\n")));
                    for (final Delta<String> delta : changedLinesMethod.getDeltas()) {
                        getDeltaGuess(guessedTypes, (delta.getOriginal().getLines()));
                        getDeltaGuess(guessedTypes, (delta.getRevised().getLines()));
                    }
                }
            }
        }
    }
    return guessedTypes;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) Set(java.util.Set) HashSet(java.util.HashSet) ClazzFileFinder(de.dagere.peass.dependency.ClazzFileFinder) ExecutionConfig(de.dagere.peass.config.ExecutionConfig) File(java.io.File) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with ClazzFileFinder

use of de.dagere.peass.dependency.ClazzFileFinder in project peass by DaGeRe.

the class TestClazzFileFinder method testClasses.

@Test
public void testClasses() {
    ExecutionConfig config = new ExecutionConfig();
    config.getClazzFolders().add("src/main/java");
    config.getClazzFolders().add("src/java");
    List<String> clazzes = new ClazzFileFinder(config).getClasses(SOURCE);
    System.out.println(clazzes);
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.TestMe1"));
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.TestMe2"));
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.Second"));
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.TestMe2$Inner"));
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.TestMe2$Inner$InnerInner"));
    MatcherAssert.assertThat(clazzes, Matchers.hasItem("de.TestMe2$InnerEnum"));
}
Also used : ClazzFileFinder(de.dagere.peass.dependency.ClazzFileFinder) ExecutionConfig(de.dagere.peass.config.ExecutionConfig) Test(org.junit.jupiter.api.Test)

Example 3 with ClazzFileFinder

use of de.dagere.peass.dependency.ClazzFileFinder in project peass by DaGeRe.

the class JUnitTestTransformer method findModuleTests.

private TestSet findModuleTests(final ModuleClassMapping mapping, final List<String> includedModules, final File module) {
    final TestSet moduleTests = new TestSet();
    ClazzFileFinder finder = new ClazzFileFinder(config.getExecutionConfig());
    for (final String clazz : finder.getTestClazzes(module)) {
        final String currentModule = mapping.getModuleOfClass(clazz);
        final List<TestCase> testMethodNames = getTestMethodNames(module, new TestCase(clazz, null, currentModule));
        for (TestCase test : testMethodNames) {
            if (includedModules == null || includedModules.contains(test.getModule())) {
                addTestIfIncluded(moduleTests, test);
            }
        }
    }
    return moduleTests;
}
Also used : TestCase(de.dagere.peass.dependency.analysis.data.TestCase) ClazzFileFinder(de.dagere.peass.dependency.ClazzFileFinder) TestSet(de.dagere.peass.dependency.analysis.data.TestSet)

Example 4 with ClazzFileFinder

use of de.dagere.peass.dependency.ClazzFileFinder in project peass by DaGeRe.

the class JUnitTestTransformer method getTestMethodNames.

@Override
public List<TestCase> getTestMethodNames(final File module, final TestCase clazzname) {
    final List<TestCase> methods = new LinkedList<>();
    ClazzFileFinder finder = new ClazzFileFinder(config.getExecutionConfig());
    final File clazzFile = finder.getClazzFile(module, clazzname);
    final CompilationUnit unit = loadedFiles.get(clazzFile);
    if (unit != null) {
        final Integer junit = junitVersions.get(clazzFile);
        if (junit != null) {
            final ClassOrInterfaceDeclaration clazz = ParseUtil.getClass(unit);
            if (junit == 3) {
                for (final MethodDeclaration method : clazz.getMethods()) {
                    if (method.getNameAsString().toLowerCase().contains("test")) {
                        methods.add(new TestCase(clazzname.getClazz(), method.getNameAsString(), clazzname.getModule()));
                    }
                }
            } else if (junit == 4) {
                for (String junit4method : getAnnotatedMethods(clazz, 4)) {
                    TestCase test = new TestCase(clazzname.getClazz(), junit4method, clazzname.getModule());
                    methods.add(test);
                }
            } else if (junit == 5) {
                for (String junit5method : getAnnotatedMethods(clazz, 5)) {
                    TestCase test = new TestCase(clazzname.getClazz(), junit5method, clazzname.getModule());
                    methods.add(test);
                }
            }
        } else {
            LOG.warn("Clazz {} has no JUnit version", clazzFile);
        }
    } else {
        /**
         * By default, the dependency selection adds all changed clazzes as tests (since a class not containing a test may contain a new test), so this is mostly not a real error
         */
        LOG.error("Did not find {} for {} - class not loaded (since it is not a test class?)", clazzFile, clazzname);
    }
    return methods;
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) TestCase(de.dagere.peass.dependency.analysis.data.TestCase) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ClazzFileFinder(de.dagere.peass.dependency.ClazzFileFinder) File(java.io.File) LinkedList(java.util.LinkedList)

Example 5 with ClazzFileFinder

use of de.dagere.peass.dependency.ClazzFileFinder in project peass by DaGeRe.

the class TestPackageFinder method testDependencyModule.

@Test
public void testDependencyModule() {
    ExecutionConfig config = new ExecutionConfig();
    config.getClazzFolders().add("src/main/java");
    config.getClazzFolders().add("src/java");
    final List<String> lowestPackage = new ClazzFileFinder(config).getClasses(new File("."));
    System.out.println(lowestPackage);
    MatcherAssert.assertThat(lowestPackage, IsIterableContaining.hasItem("de.dagere.peass.RegressionTestSelectionStarter"));
    MatcherAssert.assertThat(lowestPackage, Matchers.not(IsIterableContaining.hasItem("de.dagere.peass.RegressionTestSelectionStarter.RegressionTestSelectionStarter")));
    MatcherAssert.assertThat(lowestPackage, IsIterableContaining.hasItem("de.dagere.peass.dependency.statistics.DependencyStatisticAnalyzer"));
    MatcherAssert.assertThat(lowestPackage, IsIterableContaining.hasItem("de.dagere.peass.dependency.statistics.DependencyStatistics"));
    MatcherAssert.assertThat(lowestPackage, IsIterableContaining.hasItem("de.dagere.peass.TestPackageFinder"));
}
Also used : ClazzFileFinder(de.dagere.peass.dependency.ClazzFileFinder) ExecutionConfig(de.dagere.peass.config.ExecutionConfig) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

ClazzFileFinder (de.dagere.peass.dependency.ClazzFileFinder)11 File (java.io.File)8 CompilationUnit (com.github.javaparser.ast.CompilationUnit)4 ExecutionConfig (de.dagere.peass.config.ExecutionConfig)4 TestCase (de.dagere.peass.dependency.analysis.data.TestCase)4 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 ChangedEntity (de.dagere.peass.dependency.analysis.data.ChangedEntity)2 TestSet (de.dagere.peass.dependency.analysis.data.TestSet)2 LinkedList (java.util.LinkedList)2 Test (org.junit.jupiter.api.Test)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1