Skip to content

Commit

Permalink
Fix #262 support auto-imports (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
danglotb authored and monperrus committed Dec 8, 2017
1 parent a085fd2 commit ee05c80
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ src/test/resources/sample/log/
src/test/resources/test-projects/log/
src/test/resources/multiple-pom/log/
src/test/resources/multiple-pom/module-1/module-2-1/log/
dspot/src/test/resources/mockito/log/
dspot/src/test/resources/multiple-pom/module-1/module-2-1/log/
dspot/src/test/resources/sample/log/
dspot/src/test/resources/test-projects/log/




Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ install:

script:
- mvn clean install
- cd dspot && java -jar target/dspot-1.0.1-SNAPSHOT-jar-with-dependencies.jar --example
- cd dspot && java -jar target/dspot-1.0.2-SNAPSHOT-jar-with-dependencies.jar --example

after_success:
- mvn clean test jacoco:report coveralls:report
2 changes: 1 addition & 1 deletion dspot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>fr.inria.stamp</groupId>
<artifactId>dspot-parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
</parent>

<artifactId>dspot</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import fr.inria.diversify.mutant.descartes.DescartesChecker;
import fr.inria.diversify.mutant.descartes.DescartesInjector;
import fr.inria.diversify.mutant.pit.MavenPitCommandAndOptions;
import fr.inria.diversify.utils.AmplificationHelper;
import fr.inria.diversify.utils.DSpotUtils;
import fr.inria.diversify.utils.sosiefier.InputConfiguration;
import fr.inria.stamp.Main;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
this.initialCoverage = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified);

this.selectedToBeAmplifiedCoverageResultsMap = null;
this.selectedAmplifiedTest.clear();
}
final List<String> methodNames = testsToBeAmplified.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified)
Expand Down Expand Up @@ -210,8 +211,6 @@ public void report() {
jsonReport(coverageResults);

this.currentClassTestToBeAmplified = null;
this.selectedToBeAmplifiedCoverageResultsMap.clear();
this.selectedAmplifiedTest.clear();
}

private void jsonReport(CoverageResults coverageResults) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public class PitMutantScoreSelector extends TakeAllSelector {

private static final Logger LOGGER = LoggerFactory.getLogger(JacocoExecutor.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PitMutantScoreSelector.class);

private int numberOfMutant;

Expand Down Expand Up @@ -179,10 +179,10 @@ private void reportStdout() {
long nbOfTotalMutantKilled = getNbTotalNewMutantKilled();
string.append(nl).append("======= REPORT =======").append(nl);
string.append("PitMutantScoreSelector: ").append(nl);
string.append("The original test suite kill ").append(this.originalKilledMutants.size()).append(" mutants").append(nl);
string.append("The original test suite kills ").append(this.originalKilledMutants.size()).append(" mutants").append(nl);
string.append("The amplification results with ").append(
this.testThatKilledMutants.size()).append(" new tests").append(nl);
string.append("it kill ")
string.append("it kills ")
.append(nbOfTotalMutantKilled).append(" more mutants").append(nl);
System.out.println(string.toString());

Expand Down
2 changes: 2 additions & 0 deletions dspot/src/main/java/fr/inria/diversify/utils/DSpotUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,12 @@ public static void addBranchLogger(InputProgram inputProgram, Factory factory) {
public static void printJavaFileWithComment(CtType<?> type, File directory) {
Factory factory = type.getFactory();
Environment env = factory.getEnvironment();
env.setAutoImports(true);
env.setCommentEnabled(true);
JavaOutputProcessor processor = new JavaOutputProcessor(directory, new DefaultJavaPrettyPrinter(env));
processor.setFactory(factory);
processor.createJavaFile(type);
env.setAutoImports(false);
}

public static void printAmplifiedTestClass(CtType<?> type, File directory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static Description copyDescriptionFromObject(Object descriptionToBeCopie
}
}

public static TestListener copyFromObject(Object listenerToBeCopied) {
public static TestListener copyFromObject(Object listenerToBeCopied, Collection<String> testMethodNames) {
try {

Class<?> listenerClass = listenerToBeCopied.getClass();
Expand All @@ -138,27 +138,32 @@ public static TestListener copyFromObject(Object listenerToBeCopied) {
.invoke(listenerToBeCopied))
.stream()
.map(TestListener::copyDescriptionFromObject)
.filter(description -> testMethodNames.contains(description.getMethodName()) || testMethodNames.isEmpty())
.collect(Collectors.toList()));

copy.getFailingTests().addAll(((Collection<?>) listenerClass
.getMethod("getFailingTests")
.invoke(listenerToBeCopied))
.stream()
.map(TestListener::copyFailurefromObject)
.filter(failure -> failure.getDescription().getMethodName() == null ||
testMethodNames.contains(failure.getDescription().getMethodName()) || testMethodNames.isEmpty())
.collect(Collectors.toList()));

copy.getIgnoredTests().addAll(((Collection<?>) listenerClass
.getMethod("getIgnoredTests")
.invoke(listenerToBeCopied))
.stream()
.map(TestListener::copyDescriptionFromObject)
.filter(description -> testMethodNames.contains(description.getMethodName()) || testMethodNames.isEmpty())
.collect(Collectors.toList()));

copy.getAssumptionFailingTests().addAll(((Collection<?>) listenerClass
.getMethod("getAssumptionFailingTests")
.invoke(listenerToBeCopied))
.stream()
.map(TestListener::copyFailurefromObject)
.filter(failure -> testMethodNames.contains(failure.getDescription().getMethodName()) || testMethodNames.isEmpty())
.collect(Collectors.toList()));

return copy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class ReflectiveTestRunner extends AbstractTestRunner {
}).toArray(URL[]::new), ClassLoader.getSystemClassLoader().getParent());
}

private TestListener runUsingReflection(Class<?> classTest) {
private Object runUsingReflection(Class<?> classTest) {
try {
Class<?> requestClass = this.loadClass("org.junit.runner.Request");
Object request = requestClass.getMethod("aClass", Class.class)
Expand All @@ -66,7 +66,7 @@ private TestListener runUsingReflection(Class<?> classTest) {
runner.getClass()
.getMethod("run", notifier.getClass())
.invoke(runner, notifier);
return TestListener.copyFromObject(listenerInstance);
return listenerInstance;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -85,7 +85,7 @@ public TestListener run(Class<?> testClass, Collection<String> testMethodNames,
try {
Object listener = submit.get(10000000 * (testMethodNames.size() + 1),
TimeUnit.MILLISECONDS);
return TestListener.copyFromObject(listener);
return TestListener.copyFromObject(listener, testMethodNames);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
Expand Down
Binary file modified dspot/src/main/resources/listener/TestListener.class
Binary file not shown.
42 changes: 35 additions & 7 deletions dspot/src/test/java/fr/inria/stamp/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,22 @@ public void testNoneAmplifier() throws Exception {
}
}

private static final String expectedReportOnDefaultMode = "\n" +
"======= REPORT =======\n" +
"Initial instruction coverage: 33 / 37\n" +
"89.19%\n" +
"Amplification results with 0 amplified tests.\n" +
"Amplified instruction coverage: 33 / 37\n" +
"89.19%\n";
private static final String expectedReportOnDefaultMode = "" + nl +
"======= REPORT =======" + nl +
"Initial instruction coverage: 33 / 37" + nl +
"89.19%" + nl +
"Amplification results with 0 amplified tests." + nl +
"Amplified instruction coverage: 33 / 37" + nl +
"89.19%" + nl ;

@Test
public void testExample() throws Exception {

/*
Test the --example option. It runs a specific predefined example of amplification.
It also checks the auto imports output of DSpot.
*/

Main.main(new String[]{"--verbose", "--example"});
final File reportFile = new File("target/trash/example.TestSuiteExample_jacoco_instr_coverage_report.txt");
assertTrue(reportFile.exists());
Expand All @@ -120,8 +126,30 @@ public void testExample() throws Exception {
} catch (IOException e) {
throw new RuntimeException(e);
}
try (BufferedReader reader = new BufferedReader(new FileReader(new File("target/trash/example/TestSuiteExampleAmpl.java")))) {
String content = reader.lines().reduce("", (acc, line) -> acc + line + nl);
assertTrue(content.startsWith(expectedAmplifiedTestClass));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

//we don't test the whole file, but only the begin of it. It is sufficient to detect the auto import.
private static final String expectedAmplifiedTestClass = "package example;" + nl +
nl +
nl +
"import org.junit.Assert;" + nl +
"import org.junit.Test;" + nl +
nl +
nl +
"public class TestSuiteExampleAmpl {" + nl +
" @Test" + nl +
" public void test3() {" + nl +
" Example ex = new Example();" + nl +
" String s = \"abcd\";" + nl +
" Assert.assertEquals('d', ex.charAt(s, ((s.length()) - 1)));" + nl +
" }" + nl ;

@Test
public void testTwoClasses() throws Exception {
Main.main(new String[]{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package fr.inria.multiple.pom;

public class HelloWorld {

public static String run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package fr.inria.multiple.pom;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>fr.inria.stamp</groupId>
<artifactId>dspot-parent</artifactId>
<version>1.0.1-SNAPSHOT</version>
<version>1.0.2-SNAPSHOT</version>
<packaging>pom</packaging>

<properties>
Expand Down

0 comments on commit ee05c80

Please sign in to comment.