Skip to content

Commit

Permalink
Test dspot (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored and monperrus committed Dec 13, 2016
1 parent 68ddfbf commit 6c77ce4
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
*/
public class AmplificationHelper {

private static int cloneNumber;
private static Map<CtMethod,CtMethod> ampTestToParent;
private static int cloneNumber = 1;
private static Map<CtMethod,CtMethod> ampTestToParent = new HashMap<>();
private static Map<CtType, Set<CtType>> importByClass = new HashMap<>();
private static Random random = new Random();

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/fr/inria/diversify/dspot/DSpot.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Amplifier> amplifiers) throws InvalidSdkException, Exception {
this(configuration);
this.amplifiers.addAll(amplifiers);
this.amplifiers = amplifiers;
this.numberOfIterations = numberOfIterations;
}

Expand Down
5 changes: 5 additions & 0 deletions src/test/java/fr/inria/diversify/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/";
}
}
84 changes: 84 additions & 0 deletions src/test/java/fr/inria/diversify/dspot/DSpotTest.java
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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());
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/test-projects/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fr.inria.lille.toolset</groupId>
<artifactId>test-projects</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-projects</name>

<properties>
<default.encoding>UTF-8</default.encoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
5 changes: 5 additions & 0 deletions src/test/resources/test-projects/test-projects.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project=src/test/resources/test-projects
src=src/main/java/
testSrc=src/test/java
javaVersion=8
filter=example

0 comments on commit 6c77ce4

Please sign in to comment.