All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.bjurr.violations.lib.parsers.PiTestParser Maven / Gradle / Ivy

There is a newer version: 1.157.3
Show newest version
package se.bjurr.violations.lib.parsers;

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.Parser.PITEST;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getAttribute;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getChunks;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getContent;
import static se.bjurr.violations.lib.util.ViolationParserUtils.getIntegerContent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import se.bjurr.violations.lib.model.Violation;

public class PiTestParser implements ViolationsParser {

  @Override
  public List parseReportOutput(String string) throws Exception {
    List violations = new ArrayList<>();
    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");
      Map specifics = new HashMap<>();
      specifics.put("detected", detected);
      specifics.put("mutatedMethod", mutatedMethod);
      specifics.put("mutatedClass", mutatedClass);
      specifics.put("status", status);
      specifics.put("methodDescription", methodDescription);
      violations.add( //
          violationBuilder() //
              .setRule(mutator) //
              .setSource(sourceFile) //
              .setParser(PITEST) //
              .setStartLine(startLine) //
              .setColumn(index) //
              .setFile(filename) //
              .setSeverity(WARN) //
              .setMessage(message) //
              .setSpecifics(specifics) //
              .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;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy