Пример #1
0
  public List<Patch> build(String[] testClasses) {
    gZoltar = GZoltarSuspiciousProgramStatements.create(this.classpath, testClasses);
    Collection<Statement> statements = gZoltar.sortBySuspiciousness(testClasses);
    if (statements.isEmpty()) {
      throw new RuntimeException("No suspicious statements found.");
    }

    Map<SourceLocation, List<TestResult>> testListPerStatement = getTestListPerStatement();

    if (Config.INSTANCE.getOracle() == Config.NopolOracle.SYMBOLIC) {
      try {
        this.classpath = addJPFLibraryToCassPath(classpath);
        SpoonedProject jpfSpoon = new SpoonedProject(this.sourceFiles, classpath);
        String mainClass = "nopol.repair.NopolTestRunner";
        TestExecutorProcessor.createMainTestClass(jpfSpoon, mainClass);
        jpfSpoon.process(new AssertReplacer());

        final File outputSourceFile = new File("src-gen");
        final File outputCompiledFile = new File("target-gen");
        // generate the output file
        jpfSpoon.generateOutputFile(outputSourceFile);
        jpfSpoon.generateOutputCompiledFile(outputCompiledFile);
      } catch (IOException e) {
        throw new RuntimeException("Unable to write transformed test", e);
      }
    }

    return solveWithMultipleBuild(statements, testListPerStatement);
  }
Пример #2
0
 private Map<SourceLocation, List<TestResult>> getTestListPerStatement() {
   Map<SourceLocation, List<TestResult>> results = new HashMap<>();
   List<TestResult> testResults = gZoltar.getGzoltar().getTestResults();
   for (int i = 0; i < testResults.size(); i++) {
     TestResult testResult = testResults.get(i);
     List<ComponentCount> components = testResult.getCoveredComponents();
     for (int j = 0; j < components.size(); j++) {
       Statement component = (Statement) components.get(j).getComponent();
       SourceLocation sourceLocation =
           new SourceLocation(
               component.getMethod().getParent().getLabel(), component.getLineNumber());
       if (!results.containsKey(sourceLocation)) {
         results.put(sourceLocation, new ArrayList<TestResult>());
       }
       results.get(sourceLocation).add(testResult);
     }
   }
   return results;
 }
Пример #3
0
  /*
   * First algorithm of Nopol,
   * build the initial model
   * apply only one modification
   * build
   * try to find patch
   */
  private List<Patch> solveWithMultipleBuild(
      Collection<Statement> statements,
      Map<SourceLocation, List<TestResult>> testListPerStatement) {
    List<Patch> patches = new ArrayList<>();

    for (Iterator<Statement> iterator = statements.iterator();
        iterator.hasNext()
            &&
            // limit the execution time
            System.currentTimeMillis() - startTime
                <= TimeUnit.MINUTES.toMillis(Config.INSTANCE.getMaxTime()); ) {
      Statement statement = iterator.next();
      if (((StatementExt) statement).getEf() == 0) {
        continue;
      }
      /*if(((StatementExt)statement).getNf() != 0) {
      	continue;
      }*/
      try {
        if (isInTest(statement)) continue;
        NoPol.currentStatement = statement;
        logger.debug("Analysing {}", statement);
        SourceLocation sourceLocation =
            new SourceLocation(
                statement.getMethod().getParent().getName(), statement.getLineNumber());
        Synthesizer synth =
            new SynthesizerFactory(sourceFiles, spooner, type).getFor(sourceLocation);

        if (synth == Synthesizer.NO_OP_SYNTHESIZER) {
          continue;
        }

        List<TestResult> tests = testListPerStatement.get(sourceLocation);

        Set<String> failingClassTest = new HashSet<>();
        for (int i = 0; i < tests.size(); i++) {
          TestResult testResult = tests.get(i);
          if (!testResult.wasSuccessful()) {
            failingClassTest.add(testResult.getName().split("#")[0]);
          }
        }
        Collection<TestCase> failingTest =
            failingTests(failingClassTest.toArray(new String[0]), new URLClassLoader(classpath));

        if (failingTest.isEmpty()) {
          continue;
        }
        Patch patch =
            synth.buildPatch(classpath, tests, failingTest, Config.INSTANCE.getMaxTimeBuildPatch());
        if (isOk(patch, gZoltar.getGzoltar().getTestResults(), synth.getProcessor())) {
          patches.add(patch);
          if (isSinglePatch()) {
            break;
          }
        } else {
          logger.debug("Could not find a patch in {}", statement);
        }
      } catch (RuntimeException re) {
        re.printStackTrace();
      }
    }
    return patches;
  }