Search in sources :

Example 26 with InputProgram

use of fr.inria.diversify.utils.sosiefier.InputProgram in project dspot by STAMP-project.

the class StatementAddTest method testWithLoop.

@Test
public void testWithLoop() throws Exception {
    /*
            Test that StatementAdd amplifier is able to add statement inside a loop if this loop has not brackets
         */
    final String packageName = "fr.inria.statementadd";
    InputProgram inputProgram = Utils.getInputProgram();
    final Factory factory = inputProgram.getFactory();
    inputProgram.setFactory(factory);
    AmplificationHelper.setSeedRandom(32L);
    StatementAdd amplifier = new StatementAdd(packageName);
    amplifier.reset(factory.Class().get(packageName + ".ClassTarget"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTarget"), "testWithLoop");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    assertEquals("{" + AmplificationHelper.LINE_SEPARATOR + "    java.util.ArrayList<fr.inria.statementadd.TestClassTarget.Internal> internalList = new java.util.ArrayList<>();" + AmplificationHelper.LINE_SEPARATOR + "    internalList.add(new fr.inria.statementadd.TestClassTarget.Internal());" + AmplificationHelper.LINE_SEPARATOR + "    for (fr.inria.statementadd.TestClassTarget.Internal i : internalList) {" + AmplificationHelper.LINE_SEPARATOR + "        int __DSPOT_i_0 = -1167796541;" + AmplificationHelper.LINE_SEPARATOR + "        i.compute(0);" + AmplificationHelper.LINE_SEPARATOR + "        // StatementAdd: add invocation of a method" + AmplificationHelper.LINE_SEPARATOR + "        i.compute(__DSPOT_i_0);" + AmplificationHelper.LINE_SEPARATOR + "    }" + AmplificationHelper.LINE_SEPARATOR + "}", amplifiedMethods.get(0).getBody().toString());
}
Also used : Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 27 with InputProgram

use of fr.inria.diversify.utils.sosiefier.InputProgram in project dspot by STAMP-project.

the class ChangeMinimizerTest method test.

@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    /*
            ChangeMinimizer keeps only the assertions that trigger the failure on the second version
         */
    final CtClass testClass = Utils.findClass("example.TestSuiteExample");
    final String configurationPath = Utils.getInputConfiguration().getProperty("configPath");
    final String pathToFolder = Utils.getInputConfiguration().getProperty("folderPath");
    InputConfiguration inputConfiguration = new InputConfiguration(configurationPath);
    InputProgram inputProgram = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(inputProgram);
    String pathToChangedVersionOfProgram = pathToFolder + DSpotUtils.shouldAddSeparator.apply(pathToFolder) + (inputConfiguration.getProperty("targetModule") != null ? inputConfiguration.getProperty("targetModule") + DSpotUtils.shouldAddSeparator.apply(pathToFolder) : "");
    inputProgram.setProgramDir(pathToChangedVersionOfProgram);
    Initializer.initialize(Utils.getInputConfiguration(), inputProgram);
    final HashMap<CtMethod<?>, Failure> failurePerAmplifiedTest = new HashMap<>();
    final CtMethod<?> test2 = Utils.findMethod(testClass, "test2");
    failurePerAmplifiedTest.put(test2, new Failure(Description.EMPTY, new StringIndexOutOfBoundsException(-1)));
    final ChangeMinimizer changeMinimizer = new ChangeMinimizer(testClass, inputConfiguration, inputProgram, pathToChangedVersionOfProgram, failurePerAmplifiedTest);
    final CtInvocation assertion = test2.getElements(new TypeFilter<CtInvocation>(CtInvocation.class) {

        @Override
        public boolean matches(CtInvocation element) {
            return AmplificationChecker.isAssert(element);
        }
    }).get(0);
    final CtInvocation clone = assertion.clone();
    clone.getElements(new TypeFilter<CtLiteral>(CtLiteral.class) {

        @Override
        public boolean matches(CtLiteral element) {
            return element.getValue() instanceof Integer && super.matches(element);
        }
    }).get(0).setValue(-1);
    test2.getBody().insertEnd(clone);
    assertEquals(3, test2.getBody().getStatements().size());
    final CtMethod<?> minimize = changeMinimizer.minimize(test2);
    assertEquals(2, minimize.getBody().getStatements().size());
}
Also used : HashMap(java.util.HashMap) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) CtClass(spoon.reflect.declaration.CtClass) CtInvocation(spoon.reflect.code.CtInvocation) CtLiteral(spoon.reflect.code.CtLiteral) CtMethod(spoon.reflect.declaration.CtMethod) Failure(org.junit.runner.notification.Failure) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 28 with InputProgram

use of fr.inria.diversify.utils.sosiefier.InputProgram in project dspot by STAMP-project.

the class TestLauncherTest method testFromSpoonNodes.

@Test
public void testFromSpoonNodes() throws Exception {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setSourceClasspath(System.getProperty("java.class.path").split(System.getProperty("path.separator")));
    launcher.addInputResource("src/test/resources/src/");
    launcher.addInputResource("src/test/resources/test/");
    launcher.buildModel();
    final String classpath = "src/test/resources/MockitoDemo-1.0-SNAPSHOT.jar" + System.getProperty("path.separator") + "src/test/resources/MockitoDemo-1.0-SNAPSHOT-tests.jar" + System.getProperty("path.separator") + "src/test/resources/example-0.0.1-SNAPSHOT.jar" + System.getProperty("path.separator") + "src/test/resources/example-0.0.1-SNAPSHOT-tests.jar";
    /*
			Test that we the same entry point we are able to run all kind of test
		 */
    InputConfiguration configuration = new InputConfiguration("src/test/resources/mockito/mockito.properties");
    configuration.setInputProgram(new InputProgram());
    // Mocked
    TestListener results = TestLauncher.runFromSpoonNodes(configuration, classpath, launcher.getFactory().Class().get("info.sanaulla.dal.BookDALTest"), launcher.getFactory().Class().get("info.sanaulla.dal.BookDALTest").getMethodsByName("testGetBook"));
    assertEquals(1, results.getRunningTests().size());
    assertEquals(0, results.getFailingTests().size());
    assertEquals(1, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
    configuration = new InputConfiguration("src/test/resources/test-projects/test-projects.properties");
    configuration.setInputProgram(new InputProgram());
    // Default
    results = TestLauncher.runFromSpoonNodes(configuration, classpath, launcher.getFactory().Class().get("example.TestSuiteExample"), launcher.getFactory().Class().get("example.TestSuiteExample").getMethodsByName("test1"));
    assertEquals(1, results.getRunningTests().size());
    assertEquals(1, results.getFailingTests().size());
    assertEquals(0, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
    configuration = new InputConfiguration("src/test/resources/sample/sample.properties");
    configuration.setInputProgram(new InputProgram());
    // will use example.InheriteTest to run the test of AbstractTest
    results = TestLauncher.runFromSpoonNodes(configuration, classpath, launcher.getFactory().Class().get("example.AbstractTest"), launcher.getFactory().Class().get("example.AbstractTest").getMethodsByName("abstractTest"));
    assertEquals(1, results.getRunningTests().size());
    assertEquals(0, results.getFailingTests().size());
    assertEquals(1, results.getPassingTests().size());
    assertEquals(0, results.getAssumptionFailingTests().size());
    assertEquals(0, results.getIgnoredTests().size());
}
Also used : Launcher(spoon.Launcher) TestListener(fr.inria.stamp.test.listener.TestListener) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test)

Example 29 with InputProgram

use of fr.inria.diversify.utils.sosiefier.InputProgram in project dspot by STAMP-project.

the class AutomaticBuilderFactoryTest method getAutomaticBuilder_whenUnknown.

@Test
public void getAutomaticBuilder_whenUnknown() throws Exception {
    this.configuration = JSAPOptions.parse(getArgsWithUnknownBuilder());
    InputConfiguration inputConfiguration = new InputConfiguration(configuration.pathToConfigurationFile);
    inputConfiguration.getProperties().setProperty("automaticBuilderName", configuration.automaticBuilderName);
    InputProgram program = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(program);
    assertFalse(inputConfiguration.getProperty("automaticBuilderName") == null);
    assertFalse(inputConfiguration.getProperty("automaticBuilderName").toUpperCase().contains("MAVEN"));
    assertFalse(inputConfiguration.getProperty("automaticBuilderName").toUpperCase().contains("GRADLE"));
    AutomaticBuilder builder = AutomaticBuilderFactory.getAutomaticBuilder(inputConfiguration);
    assertNotNull(builder);
    assertTrue(builder.getClass().equals(MavenAutomaticBuilder.class));
}
Also used : InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test)

Example 30 with InputProgram

use of fr.inria.diversify.utils.sosiefier.InputProgram in project dspot by STAMP-project.

the class DSpotCompilerTest method testDSpotCompiler.

@Test
public void testDSpotCompiler() throws Exception {
    final InputProgram inputProgram = getInputProgram();
    final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(inputProgram, "");
    final CtClass<?> aClass = getClass(compiler.getLauncher().getFactory());
    final List<CtMethod<?>> compile = TestCompiler.compile(compiler, aClass, "");
    assertTrue(compile.isEmpty());
    assertEquals(1, aClass.getMethods().size());
    final List<CtMethod> tests = new UncompilableAmplifier().apply(aClass.getMethods().stream().findAny().get());
    tests.forEach(aClass::addMethod);
    assertEquals(3, aClass.getMethods().size());
    final CtMethod uncompilableTest = tests.stream().filter(ctMethod -> ctMethod.getSimpleName().equals("uncompilableTest")).findFirst().get();
    final List<CtMethod<?>> results = TestCompiler.compile(compiler, aClass, "");
    assertEquals(1, results.size());
    assertEquals("uncompilableTest", results.get(0).getSimpleName());
    assertEquals(uncompilableTest, results.get(0));
    // assertEquals(2, aClass.getMethods().size());
    // The compile methods is now stateless: using a clone class
    assertEquals(3, aClass.getMethods().size());
}
Also used : CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test)

Aggregations

InputProgram (fr.inria.diversify.utils.sosiefier.InputProgram)30 Test (org.junit.Test)19 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)12 AbstractTest (fr.inria.AbstractTest)10 CtMethod (spoon.reflect.declaration.CtMethod)10 File (java.io.File)9 Factory (spoon.reflect.factory.Factory)7 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)6 List (java.util.List)6 IOException (java.io.IOException)5 CtClass (spoon.reflect.declaration.CtClass)5 Utils (fr.inria.Utils)4 Arrays (java.util.Arrays)4 Map (java.util.Map)4 Assert.assertEquals (org.junit.Assert.assertEquals)4 CtInvocation (spoon.reflect.code.CtInvocation)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 AbstractTest (eu.stamp.project.ex2amplifier.AbstractTest)3 TestListener (fr.inria.stamp.test.listener.TestListener)3 Collectors (java.util.stream.Collectors)3