From 6c77ce4fbd9a4fdc477c26491bcbbbdb06893120 Mon Sep 17 00:00:00 2001 From: Benjamin DANGLOT Date: Tue, 13 Dec 2016 16:18:50 +0100 Subject: [PATCH] Test dspot (#21) --- .../diversify/dspot/AmplificationHelper.java | 4 +- .../java/fr/inria/diversify/dspot/DSpot.java | 10 +-- src/test/java/fr/inria/diversify/Utils.java | 5 ++ .../fr/inria/diversify/dspot/DSpotTest.java | 84 +++++++++++++++++++ .../assertGenerator/RemoveBadTestTest.java | 9 +- src/test/resources/test-projects/pom.xml | 21 +++++ .../src/main/java/example/Example.java | 29 +++++++ .../test/java/example/TestSuiteExample.java | 52 ++++++++++++ .../test-projects/test-projects.properties | 5 ++ 9 files changed, 204 insertions(+), 15 deletions(-) create mode 100644 src/test/java/fr/inria/diversify/dspot/DSpotTest.java create mode 100644 src/test/resources/test-projects/pom.xml create mode 100644 src/test/resources/test-projects/src/main/java/example/Example.java create mode 100644 src/test/resources/test-projects/src/test/java/example/TestSuiteExample.java create mode 100644 src/test/resources/test-projects/test-projects.properties diff --git a/src/main/java/fr/inria/diversify/dspot/AmplificationHelper.java b/src/main/java/fr/inria/diversify/dspot/AmplificationHelper.java index 8bdf8b082..b5c0da72f 100644 --- a/src/main/java/fr/inria/diversify/dspot/AmplificationHelper.java +++ b/src/main/java/fr/inria/diversify/dspot/AmplificationHelper.java @@ -21,8 +21,8 @@ */ public class AmplificationHelper { - private static int cloneNumber; - private static Map ampTestToParent; + private static int cloneNumber = 1; + private static Map ampTestToParent = new HashMap<>(); private static Map> importByClass = new HashMap<>(); private static Random random = new Random(); diff --git a/src/main/java/fr/inria/diversify/dspot/DSpot.java b/src/main/java/fr/inria/diversify/dspot/DSpot.java index 04b831482..b5dab8178 100644 --- a/src/main/java/fr/inria/diversify/dspot/DSpot.java +++ b/src/main/java/fr/inria/diversify/dspot/DSpot.java @@ -53,20 +53,20 @@ public DSpot(InputConfiguration inputConfiguration) throws InvalidSdkException, numberOfIterations = 3; amplifiers = new ArrayList<>(); - } - - public DSpot(InputConfiguration configuration, int numberOfIterations) throws InvalidSdkException, Exception { - this(configuration); this.amplifiers.add(new TestDataMutator()); this.amplifiers.add(new TestMethodCallAdder()); this.amplifiers.add(new TestMethodCallRemover()); this.amplifiers.add(new StatementAdderOnAssert()); + } + + public DSpot(InputConfiguration configuration, int numberOfIterations) throws InvalidSdkException, Exception { + this(configuration); this.numberOfIterations = numberOfIterations; } public DSpot(InputConfiguration configuration, int numberOfIterations, List amplifiers) throws InvalidSdkException, Exception { this(configuration); - this.amplifiers.addAll(amplifiers); + this.amplifiers = amplifiers; this.numberOfIterations = numberOfIterations; } diff --git a/src/test/java/fr/inria/diversify/Utils.java b/src/test/java/fr/inria/diversify/Utils.java index b1d46f04f..c8105be7f 100644 --- a/src/test/java/fr/inria/diversify/Utils.java +++ b/src/test/java/fr/inria/diversify/Utils.java @@ -87,4 +87,9 @@ public static CtMethod findMethod(String className, String methodName) throws In public static Factory getFactory() throws InvalidSdkException, Exception { return getInputProgram().getFactory(); } + + public static String buildMavenHome() { + return System.getenv().get("MAVEN_HOME") != null ? System.getenv().get("MAVEN_HOME") : + System.getenv().get("M2_HOME") != null ? System.getenv().get("M2_HOME") : "/usr/share/maven/"; + } } diff --git a/src/test/java/fr/inria/diversify/dspot/DSpotTest.java b/src/test/java/fr/inria/diversify/dspot/DSpotTest.java new file mode 100644 index 000000000..b7780e0ee --- /dev/null +++ b/src/test/java/fr/inria/diversify/dspot/DSpotTest.java @@ -0,0 +1,84 @@ +package fr.inria.diversify.dspot; + +import fr.inria.diversify.Utils; +import fr.inria.diversify.buildSystem.android.InvalidSdkException; +import fr.inria.diversify.runner.InputConfiguration; +import fr.inria.diversify.runner.InputProgram; +import org.junit.Test; +import spoon.reflect.declaration.CtType; + +import java.io.FileWriter; +import java.io.IOException; + +import static org.junit.Assert.assertEquals; + +/** + * Created by Benjamin DANGLOT + * benjamin.danglot@inria.fr + * on 12/13/16 + */ +public class DSpotTest { + + + @Test + public void test() throws Exception, InvalidSdkException { + + /* + Test the whole dspot procedure. + It results with 20 methods: 7 manual + 13 amplified. + The test consist of assert that the manual test remains, and there is an amplified version + */ + + addMavenHomeToPropertiesFile(); + AmplificationHelper.setSeedRandom(23L); + InputConfiguration configuration = new InputConfiguration(pathToPropertiesFile); + InputProgram program = new InputProgram(); + configuration.setInputProgram(program); + DSpot dspot = new DSpot(configuration); + + CtType amplifiedTest = dspot.amplifyTest("example.TestSuiteExample"); + assertEquals(19, amplifiedTest.getMethods().size()); + assertEquals(originalTestBody, amplifiedTest.getMethod("test1").getBody().toString()); + assertEquals(expectedAmplifiedBody, amplifiedTest.getMethod("test1_cf5_cf236").getBody().toString()); + } + + private final String pathToPropertiesFile = "src/test/resources/test-projects/test-projects.properties"; + + private final String nl = System.getProperty("line.separator"); + + private final String originalTestBody = "{" + nl + + " example.Example ex = new example.Example();" + nl + + " org.junit.Assert.assertEquals('a', ex.charAt(\"abcd\", 0));" + nl + + "}"; + + private final String expectedAmplifiedBody = "{" + nl + + " example.Example ex = new example.Example();" + nl + + " int vc_1 = 0;" + nl + + " junit.framework.Assert.assertEquals(vc_1, 0);" + nl + + " java.lang.String s = \"abcd\";" + nl + + " junit.framework.Assert.assertEquals(s, \"abcd\");" + nl + + " char o_test1_cf5_cf236__5 = ex.charAt(s, vc_1);" + nl + + " junit.framework.Assert.assertEquals(o_test1_cf5_cf236__5, 'a');" + nl + + " int vc_35 = 715956334;" + nl + + " junit.framework.Assert.assertEquals(vc_35, 715956334);" + nl + + " java.lang.String vc_10 = \"abcd\";" + nl + + " junit.framework.Assert.assertEquals(vc_10, \"abcd\");" + nl + + " char o_test1_cf5_cf236__8 = ex.charAt(vc_10, vc_35);" + nl + + " junit.framework.Assert.assertEquals(o_test1_cf5_cf236__8, 'd');" + nl + + " org.junit.Assert.assertEquals('a', ex.charAt(\"abcd\", 0));" + nl + + "}"; + + // hack to add maven.home to the properties automatically for travis. + private void addMavenHomeToPropertiesFile() { + final String mavenHome = Utils.buildMavenHome(); + if (mavenHome != null) { + try { + FileWriter writer = new FileWriter(pathToPropertiesFile, true); + writer.write(nl + "maven.home=" + mavenHome + nl); + writer.close(); + } catch (IOException ignored) { + //ignored + } + } + } +} diff --git a/src/test/java/fr/inria/diversify/dspot/assertGenerator/RemoveBadTestTest.java b/src/test/java/fr/inria/diversify/dspot/assertGenerator/RemoveBadTestTest.java index 6fca72a9c..c5740d7aa 100644 --- a/src/test/java/fr/inria/diversify/dspot/assertGenerator/RemoveBadTestTest.java +++ b/src/test/java/fr/inria/diversify/dspot/assertGenerator/RemoveBadTestTest.java @@ -32,9 +32,7 @@ public void testFilterTests() throws Exception, InvalidSdkException { 3 of them failed (on purpose), only one has to be keep. */ - final String mavenHome = buildMavenHome(); - - Log.warn("Using default installation of Maven : " + mavenHome); + final String mavenHome = Utils.buildMavenHome(); InputProgram inputProgram = Utils.getInputProgram(); RemoveBadTest removeBadTest = new RemoveBadTest(inputProgram, mavenHome); @@ -50,11 +48,6 @@ public void testFilterTests() throws Exception, InvalidSdkException { assertEquals("testKeep", ((CtMethod)(ctTypes.get(0).getMethods().stream().findFirst().get())).getSimpleName()); } - private String buildMavenHome() { - return System.getenv().get("MAVEN_HOME") != null ? System.getenv().get("MAVEN_HOME") : - System.getenv().get("M2_HOME") != null ? System.getenv().get("M2_HOME") : "/usr/share/maven/"; - } - @AfterClass public static void tearDown() throws InvalidSdkException, Exception { FileUtils.forceDelete(Utils.getCompiler().getBinaryOutputDirectory()); diff --git a/src/test/resources/test-projects/pom.xml b/src/test/resources/test-projects/pom.xml new file mode 100644 index 000000000..0ff44d7d8 --- /dev/null +++ b/src/test/resources/test-projects/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + fr.inria.lille.toolset + test-projects + 0.0.1-SNAPSHOT + test-projects + + + UTF-8 + 1.7 + 1.7 + + + + + junit + junit + 4.11 + + + \ No newline at end of file diff --git a/src/test/resources/test-projects/src/main/java/example/Example.java b/src/test/resources/test-projects/src/main/java/example/Example.java new file mode 100644 index 000000000..33e0b8148 --- /dev/null +++ b/src/test/resources/test-projects/src/main/java/example/Example.java @@ -0,0 +1,29 @@ +package example; + +public class Example { + + /* + * Return the index char of s + * or the last if index > s.length + * or the first if index < 0 + */ + public char charAt(String s, int index){ + + if ( index <= 0 ) // Fix index <= 0 + return s.charAt(0); + + if ( index < s.length() ) + return s.charAt(index); + + return s.charAt(s.length()-1); + } + + public Example() { + int variableInsideConstructor; + variableInsideConstructor = 15; + index = 2 * variableInsideConstructor; + } + + private int index = 419382; + private static String s = "Overloading field name with parameter name"; +} diff --git a/src/test/resources/test-projects/src/test/java/example/TestSuiteExample.java b/src/test/resources/test-projects/src/test/java/example/TestSuiteExample.java new file mode 100644 index 000000000..d20c70cd2 --- /dev/null +++ b/src/test/resources/test-projects/src/test/java/example/TestSuiteExample.java @@ -0,0 +1,52 @@ +package example; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class TestSuiteExample { + + @Test + public void test1(){ + Example ex = new Example(); + assertEquals('a', ex.charAt("abcd", 0)); + } + + @Test + public void test2(){ + Example ex = new Example(); + assertEquals('d', ex.charAt("abcd", 3)); + } + + @Test + public void test3(){ + Example ex = new Example(); + String s = "abcd"; + assertEquals('d', ex.charAt(s, s.length()-1)); + } + + @Test + public void test4(){ + Example ex = new Example(); + String s = "abcd"; + assertEquals('d', ex.charAt(s, 12)); + } + + @Test + public void test7(){ + Example ex = new Example(); + assertEquals('c', ex.charAt("abcd", 2)); + } + + @Test + public void test8(){ + Example ex = new Example(); + assertEquals('b', ex.charAt("abcd", 1)); + } + + @Test + public void test9(){ + Example ex = new Example(); + assertEquals('f', ex.charAt("abcdefghijklm", 5)); + } +} diff --git a/src/test/resources/test-projects/test-projects.properties b/src/test/resources/test-projects/test-projects.properties new file mode 100644 index 000000000..be72bf816 --- /dev/null +++ b/src/test/resources/test-projects/test-projects.properties @@ -0,0 +1,5 @@ +project=src/test/resources/test-projects +src=src/main/java/ +testSrc=src/test/java +javaVersion=8 +filter=example \ No newline at end of file