Search in sources :

Example 6 with InputProgram

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

the class MemoryClassLoaderFactory method createMemoryClassLoader.

/*
        Here: the factory has the same behavior than @see fr.inria.stamp.test.runner.TestRunnerFactory
            We will use the parent of the system classloader in special case
     */
public static MemoryClassLoader createMemoryClassLoader(CtType<?> testClass, InputConfiguration configuration) {
    final InputProgram program = configuration.getInputProgram();
    final String classesDirectory = program.getProgramDir() + "/" + program.getClassesDir();
    final String testClassesDirectory = program.getProgramDir() + "/" + program.getTestClassesDir();
    String classpath = AutomaticBuilderFactory.getAutomaticBuilder(configuration).buildClasspath(program.getProgramDir()) + PATH_SEPARATOR + "target/dspot/dependencies/";
    ClassLoader classLoader = null;
    if (testClass != null && TestRunnerFactory.containsSpecificAnnotation.test(testClass)) {
        classLoader = new URLClassLoader(Arrays.stream(classpath.split(":")).map(File::new).map(File::toURI).map(uri -> {
            try {
                return uri.toURL();
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }).toArray(URL[]::new), ClassLoader.getSystemClassLoader().getParent());
    } else {
        classLoader = new URLClassLoader(Arrays.stream(classpath.split(":")).map(File::new).map(File::toURI).map(uri -> {
            try {
                return uri.toURL();
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }).toArray(URL[]::new), ClassLoader.getSystemClassLoader());
    }
    final URL[] urls;
    try {
        urls = new URL[] { new File(classesDirectory).toURI().toURL(), new File(testClassesDirectory).toURI().toURL() };
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
    if (configuration.getProperty("additionalClasspathElements") != null) {
        return new MemoryClassLoader(Stream.concat(Arrays.stream(configuration.getProperty("additionalClasspathElements").split(PATH_SEPARATOR)).map(configuration.getInputProgram().getProgramDir()::concat).map(File::new).map(File::toURI).map(uri -> {
            try {
                return uri.toURL();
            } catch (MalformedURLException e) {
                throw new RuntimeException(e);
            }
        }), Arrays.stream(urls)).toArray(URL[]::new), classLoader);
    } else {
        return new MemoryClassLoader(urls, classLoader);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) Arrays(java.util.Arrays) CtType(spoon.reflect.declaration.CtType) Stream(java.util.stream.Stream) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) PATH_SEPARATOR(fr.inria.diversify.utils.AmplificationHelper.PATH_SEPARATOR) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestRunnerFactory(fr.inria.stamp.test.runner.TestRunnerFactory) File(java.io.File) MalformedURLException(java.net.MalformedURLException) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) File(java.io.File) URL(java.net.URL) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram)

Example 7 with InputProgram

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

the class DSpotUtils method copyResources.

public static void copyResources(InputConfiguration configuration) {
    final InputProgram program = configuration.getInputProgram();
    String resource = configuration.getProperty("srcResources");
    if (resource == null || resource.isEmpty()) {
        resource = "src/main/resources/";
    } else if (!resource.contains("src/main/resources/")) {
        resource += PATH_SEPARATOR + "src/main/resources/";
    }
    copyGivenResources(resource, program.getClassesDir(), configuration);
    resource = configuration.getProperty("testResources");
    if (resource == null || resource.isEmpty()) {
        resource = "src/test/resources/";
    } else if (!resource.contains("src/test/resources/")) {
        resource += PATH_SEPARATOR + "src/test/resources/";
    }
    copyGivenResources(resource, program.getClassesDir(), configuration);
}
Also used : InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram)

Example 8 with InputProgram

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

the class AutomaticBuilderFactoryTest method getAutomaticBuilder_whenConfDoesntContainBuilder.

@Test
public void getAutomaticBuilder_whenConfDoesntContainBuilder() throws Exception {
    this.configuration = JSAPOptions.parse(getArgsWithNoBuilder());
    InputConfiguration inputConfiguration = new InputConfiguration(configuration.pathToConfigurationFile);
    InputProgram program = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(program);
    assertTrue(inputConfiguration.getProperty("automaticBuilderName") == null);
    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 9 with InputProgram

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

the class AutomaticBuilderFactoryTest method getAutomaticBuilder_whenMaven.

@Test
public void getAutomaticBuilder_whenMaven() throws Exception {
    this.configuration = JSAPOptions.parse(getArgsWithMavenBuilder());
    InputConfiguration inputConfiguration = new InputConfiguration(configuration.pathToConfigurationFile);
    InputProgram program = InputConfiguration.initInputProgram(inputConfiguration);
    inputConfiguration.setInputProgram(program);
    inputConfiguration.getProperties().setProperty("automaticBuilderName", configuration.automaticBuilderName);
    assertTrue(inputConfiguration.getProperty("automaticBuilderName").toUpperCase().contains("MAVEN"));
    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 10 with InputProgram

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

the class AssertGeneratorHelperTest method testNoInstrumentationOnGeneratedObject.

/**
 * this test aims at verifying that dspot does not generate assertion for generated object.
 * To do this, it will check that the instrumentation does not add observation points on those objects.
 * If no observation point is added, any assertion would be generated.
 */
@Test
public void testNoInstrumentationOnGeneratedObject() throws Exception {
    final String packageName = "fr.inria.statementaddarray";
    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 + ".ClassTargetAmplify"));
    CtMethod<?> ctMethod = Utils.findMethod(factory.Class().get(packageName + ".TestClassTargetAmplify"), "test");
    List<CtMethod> amplifiedMethods = amplifier.apply(ctMethod);
    final List<CtMethod<?>> instrumentedAmplifiedTests = amplifiedMethods.stream().map(method -> AssertGeneratorHelper.createTestWithLog(method, "fr.inria.statementaddarray")).collect(Collectors.toList());
    assertEquals(5, amplifiedMethods.size());
    final String expectedInstrumentedBodyAfterAmplification_test_sd6_withlog = "{" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.statementaddarray.ClassTargetAmplify clazz = new fr.inria.statementaddarray.ClassTargetAmplify();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1\");" + AmplificationHelper.LINE_SEPARATOR + "    // StatementAdd: generate variable from return value" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.statementaddarray.ClassParameterAmplify __DSPOT_invoc_3 = clazz.methodWithReturn();" + AmplificationHelper.LINE_SEPARATOR + "    // StatementAdd: add invocation of a method" + AmplificationHelper.LINE_SEPARATOR + "    __DSPOT_invoc_3.method1();" + AmplificationHelper.LINE_SEPARATOR + "    fr.inria.diversify.compare.ObjectLog.log(clazz, \"clazz\", \"test_sd6__1___end\");" + AmplificationHelper.LINE_SEPARATOR + "}";
    assertEquals(expectedInstrumentedBodyAfterAmplification_test_sd6_withlog, instrumentedAmplifiedTests.stream().filter(ctMethod1 -> "test_sd6_withlog".equals(ctMethod1.getSimpleName())).findFirst().get().getBody().toString());
}
Also used : AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) Collectors(java.util.stream.Collectors) List(java.util.List) Utils(fr.inria.Utils) CtClass(spoon.reflect.declaration.CtClass) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractTest(fr.inria.AbstractTest) CtMethod(spoon.reflect.declaration.CtMethod) Factory(spoon.reflect.factory.Factory) StatementAdd(fr.inria.diversify.dspot.amplifier.StatementAdd) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

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