From eeb2a624a99a7ffd667599cc0813567d7f90c20e Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sat, 26 Mar 2016 17:52:50 +0100 Subject: [PATCH] Adding pitest parser #5 --- .gitignore | 1 + CHANGELOG.md | 19 +++++- README.md | 1 + build.gradle | 2 +- .../violations/lib/parsers/PiTestParser.java | 66 +++++++++++++++++++ .../lib/parsers/ViolationsParser.java | 5 ++ .../violations/lib/reports/Reporter.java | 4 +- .../se/bjurr/violations/lib/PiTestTest.java | 41 ++++++++++++ src/test/resources/pitest/mutations.xml | 28 ++++++++ 9 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 src/main/java/se/bjurr/violations/lib/parsers/PiTestParser.java create mode 100644 src/test/java/se/bjurr/violations/lib/PiTestTest.java create mode 100644 src/test/resources/pitest/mutations.xml diff --git a/.gitignore b/.gitignore index 9b5fc3a5..6e6f48de 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ bin temp .gradle node_modules +src/test/resources/tests diff --git a/CHANGELOG.md b/CHANGELOG.md index 115d29a7..72ce6c51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,29 @@ Changelog of Git Changelog. ## Unreleased +### GitHub [#5](https://github.com/tomasbjerre/violations-lib/issues/5) + +**Adding pitest parser** + + +[d92f4e82fc3f4d6](https://github.com/tomasbjerre/git-changelog-lib/commit/d92f4e82fc3f4d6) Tomas Bjerre *2016-03-26 19:20:29* + + +### No issue + +**Adding Jenkins plugin link to README.md** + + +[eb90c644065c9b6](https://github.com/tomasbjerre/git-changelog-lib/commit/eb90c644065c9b6) Tomas Bjerre *2016-03-06 20:20:42* + + +## 1.5 ### GitHub [#3](https://github.com/tomasbjerre/violations-lib/issues/3) **Support PerlCritic and XMLLint** -[c3677c738317827](https://github.com/tomasbjerre/git-changelog-lib/commit/c3677c738317827) Tomas Bjerre *2016-03-06 13:39:48* +[b7c4ea0c66a4c69](https://github.com/tomasbjerre/git-changelog-lib/commit/b7c4ea0c66a4c69) Tomas Bjerre *2016-03-06 13:45:16* **Support CPPLint** diff --git a/README.md b/README.md index a416fe99..5473c326 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ It supports: * [_JSHint_](http://jshint.com/) * _Lint_ A common XML format, used by different linters. * [_PerlCritic_](https://github.com/Perl-Critic) + * [_PiTest_](http://pitest.org/) * [_PMD_](https://pmd.github.io/) * [_ReSharper_](https://www.jetbrains.com/resharper/) * [_XMLLint_](http://xmlsoft.org/xmllint.html) diff --git a/build.gradle b/build.gradle index 166ec9c6..ca8e20fc 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ buildscript { dependencies { classpath 'com.bmuschko:gradle-nexus-plugin:2.3.1' classpath 'net.researchgate:gradle-release:2.2.2' - classpath "gradle.plugin.se.bjurr.gitchangelog:git-changelog-gradle-plugin:1.21" + classpath "gradle.plugin.se.bjurr.gitchangelog:git-changelog-gradle-plugin:1.25" } } diff --git a/src/main/java/se/bjurr/violations/lib/parsers/PiTestParser.java b/src/main/java/se/bjurr/violations/lib/parsers/PiTestParser.java new file mode 100644 index 00000000..6c507909 --- /dev/null +++ b/src/main/java/se/bjurr/violations/lib/parsers/PiTestParser.java @@ -0,0 +1,66 @@ +package se.bjurr.violations.lib.parsers; + +import static com.google.common.base.Charsets.UTF_8; +import static com.google.common.collect.ImmutableMap.of; +import static com.google.common.collect.Lists.newArrayList; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.model.Violation.violationBuilder; +import static se.bjurr.violations.lib.reports.Reporter.PITEST; + +import java.io.File; +import java.util.List; + +import se.bjurr.violations.lib.model.Violation; + +import com.google.common.io.Files; + +public class PiTestParser extends ViolationsParser { + + @Override + public List parseFile(File file) throws Exception { + String string = Files.toString(file, UTF_8); + List violations = newArrayList(); + String mutations = getContent(string, "mutations"); + List mutationChunks = getChunks(mutations, ""); + for (String mutationChunk : mutationChunks) { + String mutatedClass = getContent(mutationChunk, "mutatedClass"); + String sourceFile = getContent(mutationChunk, "sourceFile"); + String filename = toFilename(mutatedClass, sourceFile); + String status = getAttribute(mutationChunk, "status"); + String detected = getAttribute(mutationChunk, "detected"); + String mutatedMethod = getContent(mutationChunk, "mutatedMethod"); + String methodDescription = getContent(mutationChunk, "methodDescription"); + String mutator = getContent(mutationChunk, "mutator"); + String message = status + ", " + mutator + ", " + methodDescription; + Integer startLine = getIntegerContent(mutationChunk, "lineNumber"); + Integer index = getIntegerContent(mutationChunk, "index"); + violations.add(// + violationBuilder()// + .setRule(mutator)// + .setSource(sourceFile)// + .setReporter(PITEST)// + .setStartLine(startLine)// + .setColumn(index)// + .setFile(filename)// + .setSeverity(WARN)// + .setMessage(message)// + .setSpecifics(// + of(// + "detected", detected,// + "mutatedMethod", mutatedMethod,// + "mutatedClass", mutatedClass,// + "status", status,// + "methodDescription", methodDescription))// + .build()// + ); + } + return violations; + } + + /** + * Use package from mutadedClass and assume sourceFile is in that package. + */ + private String toFilename(String mutatedClass, String sourceFile) { + return mutatedClass.substring(0, mutatedClass.lastIndexOf(".")).replaceAll("\\.", "/") + "/" + sourceFile; + } +} \ No newline at end of file diff --git a/src/main/java/se/bjurr/violations/lib/parsers/ViolationsParser.java b/src/main/java/se/bjurr/violations/lib/parsers/ViolationsParser.java index d52e472e..0d6d4797 100644 --- a/src/main/java/se/bjurr/violations/lib/parsers/ViolationsParser.java +++ b/src/main/java/se/bjurr/violations/lib/parsers/ViolationsParser.java @@ -76,6 +76,11 @@ public static String getContent(String in, String tag) { throw new RuntimeException("\"" + tag + "\" not found in " + in); } + public static Integer getIntegerContent(String in, String tag) { + String content = getContent(in, tag); + return parseInt(content); + } + public static List getLines(String string) { return Arrays.asList(string.split("\n")); } diff --git a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java index b96fb345..3140e80a 100644 --- a/src/main/java/se/bjurr/violations/lib/reports/Reporter.java +++ b/src/main/java/se/bjurr/violations/lib/reports/Reporter.java @@ -18,6 +18,7 @@ import se.bjurr.violations.lib.parsers.LintParser; import se.bjurr.violations.lib.parsers.PMDParser; import se.bjurr.violations.lib.parsers.PerlCriticParser; +import se.bjurr.violations.lib.parsers.PiTestParser; import se.bjurr.violations.lib.parsers.ResharperParser; import se.bjurr.violations.lib.parsers.ViolationsParser; import se.bjurr.violations.lib.parsers.XMLLintParser; @@ -34,7 +35,8 @@ public enum Reporter { FLAKE8(new Flake8Parser()), // CPPLINT(new CppLintParser()), // XMLLINT(new XMLLintParser()), // - PERLCRITIC(new PerlCriticParser()); + PERLCRITIC(new PerlCriticParser()), // + PITEST(new PiTestParser()); private static Logger LOG = Logger.getLogger(Reporter.class.getSimpleName()); private ViolationsParser violationsParser; diff --git a/src/test/java/se/bjurr/violations/lib/PiTestTest.java b/src/test/java/se/bjurr/violations/lib/PiTestTest.java new file mode 100644 index 00000000..82121d55 --- /dev/null +++ b/src/test/java/se/bjurr/violations/lib/PiTestTest.java @@ -0,0 +1,41 @@ +package se.bjurr.violations.lib; + +import static org.assertj.core.api.Assertions.assertThat; +import static se.bjurr.violations.lib.TestUtils.getRootFolder; +import static se.bjurr.violations.lib.ViolationsReporterApi.violationsReporterApi; +import static se.bjurr.violations.lib.model.SEVERITY.WARN; +import static se.bjurr.violations.lib.reports.Reporter.PITEST; + +import java.util.List; + +import org.junit.Test; + +import se.bjurr.violations.lib.model.Violation; + +public class PiTestTest { + + @Test + public void testThatViolationsCanBeParsed() { + String rootFolder = getRootFolder(); + + List actual = violationsReporterApi() // + .withPattern(".*/pitest/.*\\.xml$") // + .inFolder(rootFolder) // + .findAll(PITEST) // + .violations(); + + assertThat(actual)// + .hasSize(25); + + assertThat(actual.get(0).getFile())// + .isEqualTo("se/bjurr/violations/lib/example/CopyOfMyClass.java"); + assertThat(actual.get(0).getMessage())// + .isEqualTo("NO_COVERAGE, org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator, (Ljava/lang/Object;)Z"); + assertThat(actual.get(0).getStartLine())// + .isEqualTo(17); + assertThat(actual.get(0).getEndLine())// + .isEqualTo(17); + assertThat(actual.get(0).getSeverity())// + .isEqualTo(WARN); + } +} diff --git a/src/test/resources/pitest/mutations.xml b/src/test/resources/pitest/mutations.xml new file mode 100644 index 00000000..6a209398 --- /dev/null +++ b/src/test/resources/pitest/mutations.xml @@ -0,0 +1,28 @@ + + +CopyOfMyClass.javase.bjurr.violations.lib.example.CopyOfMyClassequals(Ljava/lang/Object;)Z17org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator4 +CopyOfMyClass.javase.bjurr.violations.lib.example.CopyOfMyClassnpe(Ljava/lang/String;Ljava/lang/String;)V7org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator4 +CopyOfMyClass.javase.bjurr.violations.lib.example.CopyOfMyClassnpe(Ljava/lang/String;Ljava/lang/String;)V8org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator8 +OtherClass.javase.bjurr.violations.lib.example.OtherClasshashCode()I17org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator4 +OtherClass.javase.bjurr.violations.lib.example.OtherClassmyMethod()V10org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator6 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator4 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator6 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator8 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator10 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator12 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator14 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator16 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator18 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator20 +OtherClass.javase.bjurr.violations.lib.example.OtherClassnpe(ZZZZZZZZZ)Z26org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator30 +MyClass.javase.bjurr.violations.lib.example.MyClassequals(Ljava/lang/Object;)Z34org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator4 +MyClass.javase.bjurr.violations.lib.example.MyClassnpe(Ljava/lang/String;Ljava/lang/String;)V7org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator4 +MyClass.javase.bjurr.violations.lib.example.MyClassnpe(Ljava/lang/String;Ljava/lang/String;)V8org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator8 +MyClass.javase.bjurr.violations.lib.example.MyClassnpe(Ljava/lang/String;Ljava/lang/String;)V9org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator12 +MyClass.javase.bjurr.violations.lib.example.MyClassnpe2(Ljava/lang/String;Ljava/lang/String;)V24org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator4 +MyClass.javase.bjurr.violations.lib.example.MyClassnpe2(Ljava/lang/String;Ljava/lang/String;)V25org.pitest.mutationtest.engine.gregor.mutators.VoidMethodCallMutator8 +MyClass.javase.bjurr.violations.lib.example.MyClassthisIsTested(II)I17org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator5se.bjurr.violations.lib.example.MyClassTest.test(se.bjurr.violations.lib.example.MyClassTest) +MyClass.javase.bjurr.violations.lib.example.MyClassthisIsTested(II)I17org.pitest.mutationtest.engine.gregor.mutators.NegateConditionalsMutator8se.bjurr.violations.lib.example.MyClassTest.test(se.bjurr.violations.lib.example.MyClassTest) +MyClass.javase.bjurr.violations.lib.example.MyClassthisIsTested(II)I18org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator12se.bjurr.violations.lib.example.MyClassTest.test(se.bjurr.violations.lib.example.MyClassTest) +MyClass.javase.bjurr.violations.lib.example.MyClassthisIsTested(II)I20org.pitest.mutationtest.engine.gregor.mutators.ReturnValsMutator17 +