Search in sources :

Example 66 with CtType

use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.

the class MethodsAssertGenerator method addAssertions.

/**
 * Adds new assertions in multiple tests.
 *
 * <p>Instruments the tests to have observation points.
 * Details in {@link AssertGeneratorHelper#createTestWithLog(CtMethod, String)}.
 *
 * <p>Details of the assertion generation in {@link #buildTestWithAssert(CtMethod, Map)}.
 *
 * @param testClass Test class
 * @param testCases Passing test methods
 * @return New tests with new assertions generated from observation points values
 * @throws IOException
 * @throws ClassNotFoundException
 */
private List<CtMethod<?>> addAssertions(CtType<?> testClass, List<CtMethod<?>> testCases) throws IOException, ClassNotFoundException {
    CtType clone = testClass.clone();
    testClass.getPackage().addType(clone);
    LOGGER.info("Add observations points in passing tests.");
    LOGGER.info("Instrumentation...");
    final List<CtMethod<?>> testCasesWithLogs = testCases.stream().map(ctMethod -> {
        DSpotUtils.printProgress(testCases.indexOf(ctMethod), testCases.size());
        return AssertGeneratorHelper.createTestWithLog(ctMethod, this.originalClass.getPackage().getQualifiedName());
    }).collect(Collectors.toList());
    final List<CtMethod<?>> testsToRun = new ArrayList<>();
    IntStream.range(0, 3).forEach(i -> testsToRun.addAll(testCasesWithLogs.stream().map(CtMethod::clone).map(ctMethod -> {
        ctMethod.setSimpleName(ctMethod.getSimpleName() + i);
        return ctMethod;
    }).map(ctMethod -> {
        clone.addMethod(ctMethod);
        return ctMethod;
    }).collect(Collectors.toList())));
    ObjectLog.reset();
    LOGGER.info("Run instrumented tests. ({})", testsToRun.size());
    final TestListener result = TestCompiler.compileAndRun(clone, this.compiler, testsToRun, this.configuration);
    if (result == null || !result.getFailingTests().isEmpty()) {
        return Collections.emptyList();
    } else {
        Map<String, Observation> observations = ObjectLog.getObservations();
        LOGGER.info("Generating assertions...");
        return testCases.stream().map(ctMethod -> this.buildTestWithAssert(ctMethod, observations)).collect(Collectors.toList());
    }
}
Also used : IntStream(java.util.stream.IntStream) TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) java.util(java.util) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) LoggerFactory(org.slf4j.LoggerFactory) CtType(spoon.reflect.declaration.CtType) Counter(fr.inria.diversify.utils.Counter) Observation(fr.inria.diversify.compare.Observation) TestListener(fr.inria.stamp.test.listener.TestListener) Query(spoon.reflect.visitor.Query) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) TestTimedOutException(org.junit.runners.model.TestTimedOutException) Description(org.junit.runner.Description) IOException(java.io.IOException) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) spoon.reflect.code(spoon.reflect.code) CtTypeReference(spoon.reflect.reference.CtTypeReference) ObjectLog(fr.inria.diversify.compare.ObjectLog) CtMethod(spoon.reflect.declaration.CtMethod) CtType(spoon.reflect.declaration.CtType) Observation(fr.inria.diversify.compare.Observation) TestListener(fr.inria.stamp.test.listener.TestListener) CtMethod(spoon.reflect.declaration.CtMethod)

Example 67 with CtType

use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.

the class ChangeDetectorSelector method selectToKeep.

@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
    if (amplifiedTestToBeKept.isEmpty()) {
        return amplifiedTestToBeKept;
    }
    CtType clone = this.currentClassTestToBeAmplified.clone();
    clone.setParent(this.currentClassTestToBeAmplified.getParent());
    this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).forEach(clone::removeMethod);
    amplifiedTestToBeKept.forEach(clone::addMethod);
    DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
    final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/";
    DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getTestClassesDir(), new File(this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir()));
    final TestListener results = TestLauncher.run(this.configuration, classpath + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir(), clone, amplifiedTestToBeKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList()));
    if (!results.getFailingTests().isEmpty()) {
        results.getFailingTests().forEach(failure -> this.failurePerAmplifiedTest.put(amplifiedTestToBeKept.stream().filter(ctMethod -> ctMethod.getSimpleName().equals(failure.getDescription().getMethodName())).findFirst().get(), failure));
    }
    return amplifiedTestToBeKept;
}
Also used : CtType(spoon.reflect.declaration.CtType) TestListener(fr.inria.stamp.test.listener.TestListener) File(java.io.File) CtNamedElement(spoon.reflect.declaration.CtNamedElement)

Example 68 with CtType

use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.

the class Main method run.

public static void run(Configuration configuration, InputConfiguration inputConfiguration) throws Exception {
    AmplificationHelper.setSeedRandom(23L);
    AmplificationHelper.minimize = configuration.minimize;
    InputProgram program = new InputProgram();
    inputConfiguration.setInputProgram(program);
    inputConfiguration.getProperties().setProperty("automaticBuilderName", configuration.automaticBuilderName);
    AmplificationHelper.MAX_NUMBER_OF_TESTS = configuration.maxTestAmplified;
    if (configuration.mavenHome != null) {
        inputConfiguration.getProperties().put("maven.home", configuration.mavenHome);
    }
    if (configuration.pathToOutput != null) {
        inputConfiguration.getProperties().setProperty("outputDirectory", configuration.pathToOutput);
    }
    DSpot dspot = new DSpot(inputConfiguration, configuration.nbIteration, configuration.amplifiers, configuration.selector);
    AmplificationHelper.setSeedRandom(configuration.seed);
    AmplificationHelper.setTimeOutInMs(configuration.timeOutInMs);
    createOutputDirectories(inputConfiguration, configuration.clean);
    final long startTime = System.currentTimeMillis();
    final List<CtType> amplifiedTestClasses;
    if ("all".equals(configuration.testClasses.get(0))) {
        amplifiedTestClasses = dspot.amplifyAllTests();
    } else if ("diff".equals(configuration.testClasses.get(0))) {
        amplifiedTestClasses = dspot.amplifyAllTests(SelectorOnDiff.findTestClassesAccordingToADiff(inputConfiguration));
    } else {
        if (configuration.testCases.isEmpty()) {
            amplifiedTestClasses = dspot.amplifyAllTestsNames(configuration.testClasses);
        } else {
            amplifiedTestClasses = configuration.testClasses.stream().map(testClasses -> dspot.amplifyTest(testClasses, configuration.testCases)).collect(Collectors.toList());
        }
    }
    LOGGER.info("Amplification {}.", amplifiedTestClasses.isEmpty() ? "failed" : "succeed");
    final long elapsedTime = System.currentTimeMillis() - startTime;
    LOGGER.info("Elapsed time {} ms", elapsedTime);
}
Also used : TestDataMutator(fr.inria.diversify.dspot.amplifier.TestDataMutator) Logger(org.slf4j.Logger) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) JacocoCoverageSelector(fr.inria.diversify.dspot.selector.JacocoCoverageSelector) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Collectors(java.util.stream.Collectors) SelectorOnDiff(fr.inria.stamp.diff.SelectorOnDiff) DSpot(fr.inria.diversify.dspot.DSpot) File(java.io.File) List(java.util.List) CtType(spoon.reflect.declaration.CtType) Collections(java.util.Collections) DSpot(fr.inria.diversify.dspot.DSpot) CtType(spoon.reflect.declaration.CtType) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram)

Example 69 with CtType

use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.

the class AssertGenerator method generateAsserts.

/**
 * Adds new assertions in multiple tests.
 *
 * <p>Details of the assertions generation in {@link MethodsAssertGenerator#generateAsserts(CtType, List)}.
 *
 * @param testClass Test class
 * @param tests Test methods to amplify
 * @return New amplified tests
 * @throws IOException
 * @throws ClassNotFoundException
 */
public List<CtMethod<?>> generateAsserts(CtType<?> testClass, List<CtMethod<?>> tests) throws IOException, ClassNotFoundException {
    if (tests.isEmpty()) {
        return tests;
    }
    CtType cloneClass = testClass.clone();
    cloneClass.setParent(testClass.getParent());
    List<CtMethod<?>> testsWithoutAssertions = tests.stream().map(this.assertionRemover::removeAssertion).collect(Collectors.toList());
    testsWithoutAssertions.forEach(cloneClass::addMethod);
    MethodsAssertGenerator ags = new MethodsAssertGenerator(testClass, this.configuration, compiler);
    final List<CtMethod<?>> amplifiedTestsWithAssertions = ags.generateAsserts(cloneClass, testsWithoutAssertions);
    if (amplifiedTestsWithAssertions.isEmpty()) {
        LOGGER.info("Could not generate any test with assertions");
    } else {
        LOGGER.info("{} new tests with assertions generated", amplifiedTestsWithAssertions.size());
    }
    return amplifiedTestsWithAssertions;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtMethod(spoon.reflect.declaration.CtMethod)

Example 70 with CtType

use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.

the class AmplificationHelper method cloneTestClassAndAddGivenTest.

/**
 * Clones the test class and adds the test methods.
 *
 * @param original Test class
 * @param methods Test methods
 * @return Test class with new methods
 */
public static CtType cloneTestClassAndAddGivenTest(CtType original, List<CtMethod<?>> methods) {
    CtType clone = original.clone();
    original.getPackage().addType(clone);
    methods.forEach(clone::addMethod);
    return clone;
}
Also used : CtType(spoon.reflect.declaration.CtType)

Aggregations

CtType (spoon.reflect.declaration.CtType)134 Test (org.junit.Test)67 Launcher (spoon.Launcher)60 ArrayList (java.util.ArrayList)42 CtMethod (spoon.reflect.declaration.CtMethod)38 CtTypeReference (spoon.reflect.reference.CtTypeReference)30 DefaultJavaPrettyPrinter (spoon.reflect.visitor.DefaultJavaPrettyPrinter)20 File (java.io.File)19 Factory (spoon.reflect.factory.Factory)19 PrettyPrinter (spoon.reflect.visitor.PrettyPrinter)19 List (java.util.List)18 Collectors (java.util.stream.Collectors)17 CtField (spoon.reflect.declaration.CtField)17 CtElement (spoon.reflect.declaration.CtElement)16 CtPackage (spoon.reflect.declaration.CtPackage)16 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)14 IOException (java.io.IOException)12 SpoonException (spoon.SpoonException)12 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)11 Set (java.util.Set)11