From f53e64dd09129da38b42c10f6661be25ea6cea6c Mon Sep 17 00:00:00 2001 From: danglotb Date: Wed, 4 Dec 2019 14:30:49 +0100 Subject: [PATCH 1/5] feat: extract test-projects from jar --- .../dspot/common/configuration/UserInput.java | 63 ++++++++++++++++--- 1 file changed, 54 insertions(+), 9 deletions(-) diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java index b72802081..fa68c44d8 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java @@ -5,16 +5,19 @@ import eu.stamp_project.dspot.common.miscellaneous.AmplificationHelper; import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; import eu.stamp_project.dspot.common.configuration.options.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import picocli.CommandLine; import spoon.reflect.factory.Factory; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; +import java.io.*; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.*; +import java.util.jar.JarEntry; +import java.util.jar.JarFile; import java.util.stream.Collectors; import static eu.stamp_project.dspot.common.miscellaneous.AmplificationHelper.PATH_SEPARATOR; @@ -1015,7 +1018,11 @@ public String getPathToTestListCSV() { public void configureExample() { try { - this.setAbsolutePathToProjectRoot("src/test/resources/test-projects/"); + if (new File("src/main/resources/test-projects/").exists()) { + this.setAbsolutePathToProjectRoot("src/main/resources/test-projects/"); + } else { + this.setAbsolutePathToProjectRoot(extractTestProjectsToRunExampleForJar()); + } this.setNbIteration(1); this.setAmplifiers(Collections.singletonList(AmplifierEnum.FastLiteralAmplifier)); this.setSelector(SelectorEnum.JacocoCoverageSelector); @@ -1028,8 +1035,46 @@ public void configureExample() { } } + private String extractTestProjectsToRunExampleForJar() { + try { + Path destDirectory = Files.createTempDirectory("dspot-example"); + String pathToExeJar = new File(UserInput.class.getProtectionDomain().getCodeSource().getLocation().toURI()).getPath(); + unzipJar(destDirectory.toString(), pathToExeJar); + return destDirectory.toString() + "/test-projects/"; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + private void unzipJar(String destinationDir, String jarPath) throws IOException { + File file = new File(jarPath); + JarFile jar = new JarFile(file); + for (Enumeration enums = jar.entries(); enums.hasMoreElements();) { + JarEntry entry = (JarEntry) enums.nextElement(); + String fileName = destinationDir + File.separator + entry.getName(); + File f = new File(fileName); + if (fileName.contains(destinationDir.toString() + "/test-projects/") && fileName.endsWith("/")) { + f.mkdirs(); + } + } + for (Enumeration enums = jar.entries(); enums.hasMoreElements();) { + JarEntry entry = (JarEntry) enums.nextElement(); + String fileName = destinationDir + File.separator + entry.getName(); + File f = new File(fileName); + if (fileName.contains(destinationDir.toString() + "/test-projects/") && !fileName.endsWith("/")) { + InputStream is = jar.getInputStream(entry); + FileOutputStream fos = new FileOutputStream(f); + while (is.available() > 0) { + fos.write(is.read()); + } + fos.close(); + is.close(); + } + } + } + public void setDependencies(String dependencies) { - this.dependencies =dependencies; + this.dependencies = dependencies; } public void initTestsToBeAmplified() { From 0532342f2e08ea52e7a245e888d141060c6a5aec Mon Sep 17 00:00:00 2001 From: danglotb Date: Wed, 4 Dec 2019 14:31:14 +0100 Subject: [PATCH 2/5] feat: add test-projects in dspot's resources --- .../main/resources/test-projects/build.gradle | 20 +++++ .../test-projects/originalpit/mutations.csv | 25 +++++++ .../src/main/resources/test-projects/pom.xml | 37 ++++++++++ .../src/main/java/example/Example.java | 29 ++++++++ .../example/ParametrizedTestSuiteExample.java | 74 +++++++++++++++++++ .../src/test/java/example/TestResources.java | 11 +++ .../test/java/example/TestSuiteExample.java | 46 ++++++++++++ .../test/java/example/TestSuiteExample2.java | 48 ++++++++++++ 8 files changed, 290 insertions(+) create mode 100644 dspot/src/main/resources/test-projects/build.gradle create mode 100644 dspot/src/main/resources/test-projects/originalpit/mutations.csv create mode 100644 dspot/src/main/resources/test-projects/pom.xml create mode 100644 dspot/src/main/resources/test-projects/src/main/java/example/Example.java create mode 100644 dspot/src/main/resources/test-projects/src/test/java/example/ParametrizedTestSuiteExample.java create mode 100644 dspot/src/main/resources/test-projects/src/test/java/example/TestResources.java create mode 100644 dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample.java create mode 100644 dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample2.java diff --git a/dspot/src/main/resources/test-projects/build.gradle b/dspot/src/main/resources/test-projects/build.gradle new file mode 100644 index 000000000..987485d11 --- /dev/null +++ b/dspot/src/main/resources/test-projects/build.gradle @@ -0,0 +1,20 @@ +description = """ +Test Gradle project for DSpot test cases + +Project name: dspot.gradle.java.example + +""" +apply plugin: 'java' + +version = "0.0.1" +sourceCompatibility = 1.7 + +repositories { + mavenCentral() +} + +dependencies { + compile 'org.apache.logging.log4j:log4j-api:2.8.2' + compile 'org.apache.logging.log4j:log4j-core:2.8.2' + testCompile 'junit:junit:4.12' +} \ No newline at end of file diff --git a/dspot/src/main/resources/test-projects/originalpit/mutations.csv b/dspot/src/main/resources/test-projects/originalpit/mutations.csv new file mode 100644 index 000000000..e3e27072d --- /dev/null +++ b/dspot/src/main/resources/test-projects/originalpit/mutations.csv @@ -0,0 +1,25 @@ +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.InlineConstantMutator,,27,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.InlineConstantMutator,,23,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.InlineConstantMutator,,24,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.MathMutator,,24,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator,,27,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.experimental.MemberVariableMutator,,24,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.ConditionalsBoundaryMutator,charAt,12,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.ConditionalsBoundaryMutator,charAt,15,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.InlineConstantMutator,charAt,18,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.MathMutator,charAt,18,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator,charAt,12,KILLED,example.TestSuiteExample.test3(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator,charAt,15,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator,charAt,15,KILLED,example.TestSuiteExample.test7(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator,charAt,16,KILLED,example.TestSuiteExample.test3(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator,charAt,18,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator,charAt,18,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_ORDER_ELSE,charAt,12,SURVIVED,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_ORDER_ELSE,charAt,15,KILLED,example.TestSuiteExample.test7(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_ORDER_IF,charAt,12,KILLED,example.TestSuiteExample.test3(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_ORDER_IF,charAt,15,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator,charAt,16,KILLED,example.TestSuiteExample.test3(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator,charAt,18,KILLED,example.TestSuiteExample.test4(example.TestSuiteExample) +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.InlineConstantMutator,charAt,13,NO_COVERAGE,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.NonVoidMethodCallMutator,charAt,13,NO_COVERAGE,none +Example.java,example.Example,org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator,charAt,13,NO_COVERAGE,none diff --git a/dspot/src/main/resources/test-projects/pom.xml b/dspot/src/main/resources/test-projects/pom.xml new file mode 100644 index 000000000..cbcb8b8bc --- /dev/null +++ b/dspot/src/main/resources/test-projects/pom.xml @@ -0,0 +1,37 @@ + + 4.0.0 + example + example + 0.0.1-SNAPSHOT + test-projects + + + UTF-8 + 1.7 + 1.7 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.7.0 + + 1.8 + 1.8 + + + + + + + + junit + junit + 4.11 + + + \ No newline at end of file diff --git a/dspot/src/main/resources/test-projects/src/main/java/example/Example.java b/dspot/src/main/resources/test-projects/src/main/java/example/Example.java new file mode 100644 index 000000000..b378eec69 --- /dev/null +++ b/dspot/src/main/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 ) + 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/dspot/src/main/resources/test-projects/src/test/java/example/ParametrizedTestSuiteExample.java b/dspot/src/main/resources/test-projects/src/test/java/example/ParametrizedTestSuiteExample.java new file mode 100644 index 000000000..5680fcba2 --- /dev/null +++ b/dspot/src/main/resources/test-projects/src/test/java/example/ParametrizedTestSuiteExample.java @@ -0,0 +1,74 @@ +package example; + +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.util.Arrays; +import java.util.Collection; + +/** + * created by Benjamin DANGLOT + * benjamin.danglot@inria.fr + * on 26/11/18 + */ +@RunWith(Parameterized.class) +public class ParametrizedTestSuiteExample { + + private String string; + + public ParametrizedTestSuiteExample(String string) { + this.string = string; + } + + + @Parameterized.Parameters + public static Collection strategies() { + return Arrays.asList( + new Object[]{ + "abcd" + }, + new Object[]{ + "abcd" + } + ); + } + + @org.junit.Test + public void test3() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, ((s.length()) - 1))); + } + + @org.junit.Test + public void test4() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, 12)); + } + + @org.junit.Test + public void test7() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('c', ex.charAt("abcd", 2)); + } + + @org.junit.Test + public void test8() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('b', ex.charAt("abcd", 1)); + } + + @org.junit.Test + public void test9() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('f', ex.charAt("abcdefghijklm", 5)); + } + + @org.junit.Test + public void test2() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('d', ex.charAt("abcd", 3)); + } + +} \ No newline at end of file diff --git a/dspot/src/main/resources/test-projects/src/test/java/example/TestResources.java b/dspot/src/main/resources/test-projects/src/test/java/example/TestResources.java new file mode 100644 index 000000000..de9130959 --- /dev/null +++ b/dspot/src/main/resources/test-projects/src/test/java/example/TestResources.java @@ -0,0 +1,11 @@ + + +package example; + + +public class TestResources { + + public static int integer = 23; + +} + diff --git a/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample.java b/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample.java new file mode 100644 index 000000000..70d19b783 --- /dev/null +++ b/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample.java @@ -0,0 +1,46 @@ + + +package example; + + +public class TestSuiteExample { + + @org.junit.Test + public void test3() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, ((s.length()) - 1))); + } + + @org.junit.Test + public void test4() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, 12)); + } + + @org.junit.Test + public void test7() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('c', ex.charAt("abcd", 2)); + } + + @org.junit.Test + public void test8() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('b', ex.charAt("abcd", 1)); + } + + @org.junit.Test + public void test9() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('f', ex.charAt("abcdefghijklm", 5)); + } + + @org.junit.Test + public void test2() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('d', ex.charAt("abcd", 3)); + } +} + diff --git a/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample2.java b/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample2.java new file mode 100644 index 000000000..f9d402357 --- /dev/null +++ b/dspot/src/main/resources/test-projects/src/test/java/example/TestSuiteExample2.java @@ -0,0 +1,48 @@ + + +package example; + + +public class TestSuiteExample2 { + + private static int integer = example.TestResources.integer; + + @org.junit.Test + public void test3() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, ((s.length()) - 1))); + } + + @org.junit.Test + public void test4() { + example.Example ex = new example.Example(); + java.lang.String s = "abcd"; + org.junit.Assert.assertEquals('d', ex.charAt(s, 12)); + } + + @org.junit.Test + public void test7() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('c', ex.charAt("abcd", 2)); + } + + @org.junit.Test + public void test8() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('b', ex.charAt("abcd", 1)); + } + + @org.junit.Test + public void test9() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('f', ex.charAt("abcdefghijklm", 5)); + } + + @org.junit.Test + public void test2() { + example.Example ex = new example.Example(); + org.junit.Assert.assertEquals('d', ex.charAt("abcd", 3)); + } +} + From 8675b4dc578bb9281cb5179242b5991b0f95153b Mon Sep 17 00:00:00 2001 From: danglotb Date: Wed, 4 Dec 2019 14:31:36 +0100 Subject: [PATCH 3/5] pom: add test-projects target and generated poms in the clean chore of dspot --- dspot/pom.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/dspot/pom.xml b/dspot/pom.xml index fd2d40c0d..a895e9044 100644 --- a/dspot/pom.xml +++ b/dspot/pom.xml @@ -198,6 +198,14 @@ 3.0.0 + + src/main/resources/ + + test-projects/target/ + test-projects/.dspot_pom.xml + test-projects/.dspot_junit5_pom.xml + + src/test/resources/ From e3ec1a80ea52f7280ab7458b13ef31692db16848 Mon Sep 17 00:00:00 2001 From: danglotb Date: Wed, 4 Dec 2019 14:50:36 +0100 Subject: [PATCH 4/5] style: remove unused imports --- .../stamp_project/dspot/common/configuration/UserInput.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java index fa68c44d8..d8e284b73 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java @@ -5,16 +5,12 @@ import eu.stamp_project.dspot.common.miscellaneous.AmplificationHelper; import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; import eu.stamp_project.dspot.common.configuration.options.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import picocli.CommandLine; import spoon.reflect.factory.Factory; import java.io.*; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; From 7998ab0f6b80ac5c0e40fd64a67282b6f2039f86 Mon Sep 17 00:00:00 2001 From: danglotb Date: Wed, 4 Dec 2019 14:51:04 +0100 Subject: [PATCH 5/5] ci: run now the --example in /tmp, away from the src/test/resources folder --- .travis/travis-jar.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis/travis-jar.sh b/.travis/travis-jar.sh index 88c86834d..b7035adac 100755 --- a/.travis/travis-jar.sh +++ b/.travis/travis-jar.sh @@ -1,4 +1,6 @@ #!/usr/bin/env bash DPSOT_VERSION=${1} -cd dspot && java -jar target/dspot-${DSPOT_VERSION}-jar-with-dependencies.jar --example \ No newline at end of file +cp dspot/target/dspot-${DSPOT_VERSION}-jar-with-dependencies.jar /tmp +cd /tmp +java -jar dspot-${DSPOT_VERSION}-jar-with-dependencies.jar --example \ No newline at end of file