示例#1
0
 /**
  * Tests that spacing is correct.
  *
  * @throws IOException
  */
 @Test
 public void spacing_no_constructor() throws IOException {
   // Class structure
   JClass c = new JClass("sido.test", "Test");
   c.addImport(List.class);
   c.addImport(ArrayList.class);
   c.addField("String", "name");
   c.addField("List<String>", "otherNames").setInitialisation("new ArrayList<String>()");
   c.addMethod("getName", "String").addContent("return name;");
   c.addMethod("setName").addParam("String", "value").addContent("this.name = value;");
   // Output
   StringWriter s = new StringWriter();
   PrintWriter writer = new PrintWriter(s);
   // Generation
   c.write(writer);
   // Output
   List<String> actual = readLines(s.toString());
   // Gets the expected content
   List<String> expected = readReference("/jclass/SpacingNoConstructor.java");
   // Difference between the two sets
   Patch diff = DiffUtils.diff(expected, actual);
   List<Delta> deltas = diff.getDeltas();
   if (!deltas.isEmpty()) {
     // Creates the diff
     String original = StringUtils.join(expected, "\n");
     String revised = StringUtils.join(actual, "\n");
     List<String> unifiedDiff =
         DiffUtils.generateUnifiedDiff(original, revised, expected, diff, 3);
     String diffDisplay = StringUtils.join(unifiedDiff, "\n");
     System.err.println(diffDisplay);
     fail("spacing-no-constructor");
   }
 }
示例#2
0
 public void assertJClass(JClass c, String referenceResourcePath, String testId)
     throws IOException {
   // Output
   StringWriter s = new StringWriter();
   PrintWriter writer = new PrintWriter(s);
   // Generation
   c.write(writer);
   // Output
   List<String> actual = readLines(s.toString());
   // Gets the expected content
   List<String> expected = readReference(referenceResourcePath);
   // Difference between the two sets
   Patch diff = DiffUtils.diff(expected, actual);
   List<Delta> deltas = diff.getDeltas();
   if (!deltas.isEmpty()) {
     // Creates the diff
     String original = StringUtils.join(expected, "\n");
     String revised = StringUtils.join(actual, "\n");
     List<String> unifiedDiff =
         DiffUtils.generateUnifiedDiff(original, revised, expected, diff, 3);
     String diffDisplay = StringUtils.join(unifiedDiff, "\n");
     System.err.println(diffDisplay);
     fail(testId);
   }
 }
示例#3
0
 /**
  * Remove unwanted deltas.
  *
  * @param diff Patch to filter.
  * @return Patch with unwanted deltas removed.
  * @todo #469:30min Remove the method below and find a way to format tags correctly in XML.
  *     Attributes should be indented by 4 spaces, just like XML tags, but in IT xml-violations
  *     there is a tag that our Prettifier want to be indented by 3 spaces which is wrong. Another
  *     problem is that in the parent tag, attributes are indented to match the first attribute,
  *     this is also wrong - all attributes on new line should be indented by 4 spaces.
  */
 private static Patch filter(final Patch diff) {
   final Patch patch = new Patch();
   for (final Delta delta : diff.getDeltas()) {
     final List<?> prev = delta.getOriginal().getLines();
     if (prev.size() != 1
         || delta.getRevised().getLines().size() != 1
         || !XmlValidator.ATTRS_PATTERN.matcher(prev.get(0).toString()).matches()) {
       patch.addDelta(delta);
     }
   }
   return patch;
 }
示例#4
0
  public void runCompareTest() throws Exception {
    LOGGER.info("Diffs saved in: {}", targetDir.getAbsolutePath());
    assertFalse("Dir should not exist: ls -la " + targetDir.getAbsolutePath(), targetDir.exists());
    assertTrue("Unable to create: " + targetDir.getAbsolutePath(), targetDir.mkdirs());
    assertTrue(new File(targetDir, "0_deltas_go_here.txt").createNewFile());

    logVersion(config1);
    logVersion(config2);

    int nrQueries = 0;
    int failedQueries = 0;
    for (final String queryString : queryReader.getQueries()) {
      if (StringUtils.isBlank(queryString) || queryString.startsWith("#")) {
        continue;
      }

      if (logEntries == 1) {
        LOGGER.info("Compare query: {}", queryString);
      } else if (++nrQueries % logEntries == 0) {
        LOGGER.info("Compared {} queries", nrQueries);
      }

      final Future<List<ResponseObject>> queryExecutor1Future =
          executeQuery(queryExecutor1, queryString);
      final Future<List<ResponseObject>> queryExecutor2Future =
          executeQuery(queryExecutor2, queryString);

      final List<ResponseObject> queryExecutor1Result = queryExecutor1Future.get();
      final List<ResponseObject> queryExecutor2Result = queryExecutor2Future.get();

      final KnownDifferencesPredicate knownDifferencesPredicate = new KnownDifferencesPredicate();
      final List<ResponseObject> responseObjects1 =
          Lists.newArrayList(Iterables.filter(queryExecutor1Result, knownDifferencesPredicate));
      final List<ResponseObject> responseObjects2 =
          Lists.newArrayList(Iterables.filter(queryExecutor2Result, knownDifferencesPredicate));

      final Patch patch = DiffUtils.diff(responseObjects1, responseObjects2);
      final List<Delta> deltas = patch.getDeltas();
      if (!deltas.isEmpty()) {
        writeDifferences(queryString, queryExecutor1Result, queryExecutor2Result, deltas);
        failedQueries++;
        LOGGER.error("Query '{}' has differences", queryString);
      }
    }

    assertThat("Number of failed queries", failedQueries, Matchers.is(0));
  }
示例#5
0
  @Test
  public void testWholeInsert() {
    String s1 = "";
    String s2 =
        "And this has been added. That quick brown fox jumped over a lazy dog. "
            + "This sentence stays the same. "
            + "And this has been newly added. This will be the same.";

    String[] l1 = s1.split("\\s");
    String[] l2 = s2.split("\\s");
    difflib.Patch patch = DiffUtils.diff(Arrays.asList(l1), Arrays.asList(l2));
    System.out.println(patch.getDeltas().size());

    for (Delta delta : patch.getDeltas()) {
      System.out.println(delta.getRevised().getLines() + "\t" + delta.getType());
    }
  }
示例#6
0
 /**
  * Check formatting of given XML.
  *
  * @param name Name of XML file.
  * @param before XML to check.
  * @throws ValidationException In case of validation error.
  */
 private static void formatting(final String name, final String before)
     throws ValidationException {
   // @checkstyle MultipleStringLiterals (3 lines)
   final String after = new Prettifier(before).prettify().replace("\r\n", "\n");
   final String bnormalized = before.replace("\r\n", "\n");
   final List<String> blines = Arrays.asList(bnormalized.split(XmlValidator.ESCAPED_EOL, -1));
   final Patch filter =
       XmlValidator.filter(
           DiffUtils.diff(blines, Arrays.asList(after.split(XmlValidator.ESCAPED_EOL, -1))));
   if (!filter.getDeltas().isEmpty()) {
     final int context = 5;
     throw new ValidationException(
         // @checkstyle LineLength (1 line)
         "The provided XML %s is not well formatted, it should look like this:\n%s\npatch:\n%s",
         name,
         after,
         StringUtils.join(
             DiffUtils.generateUnifiedDiff("before", "after", blines, filter, context), "\n"));
   }
 }
示例#7
0
  @Test
  public void testNewAlgo() {
    List<String> original = new ArrayList<>();
    original.add("The quick brown fox jumps over the lazy dog");
    original.add("This sentence stays the same");
    original.add("But this will be removed.");
    original.add("This will be the same.");

    List<String> revised = new ArrayList<>();
    revised.add("And this has been added.");
    revised.add("That quick brown fox jumped over a lazy dog.");
    revised.add("This sentence stays the same");
    revised.add("And this has been newly added.");
    revised.add("This will be the same.");

    // Compute diff. Get the Patch object. Patch is the container for computed deltas.
    difflib.Patch patch = DiffUtils.diff(original, revised);

    System.out.println(patch.getDeltas().size());

    for (Delta delta : patch.getDeltas()) {
      System.out.println(delta + "\t" + delta.getType() + delta.getOriginal().getLines());
    }
  }
  @Override
  public List<Diff> perform(SuperResource left, SuperResource right) {
    String left_string = left.getContents();
    String right_string = right.getContents();

    List<String> original = stringToLines(left_string);
    List<String> revised = stringToLines(right_string);

    // Compute diff. Get the Patch object. Patch is the container for computed deltas.
    Patch patch = DiffUtils.diff(original, revised);

    List<Diff> ret = new ArrayList<Diff>();

    for (Delta delta : patch.getDeltas()) {

      if (isDelete(delta)) {
        // If multiple lines were removed, treat it as multiple removals of single lines.

        int pos = delta.getOriginal().getPosition();
        for (Object l : delta.getOriginal().getLines()) {
          String line = (String) l;
          ArrayList<String> line_array = new ArrayList<String>();
          line_array.add(line);

          Chunk c = new Chunk(pos++, line_array);

          Delta new_delta = new DeleteDelta(c, delta.getRevised());

          Diff diff = new SimpleTextDiff(new_delta, original, revised, delimiter);
          diff.setParent(this);
          ret.add(diff);
        }
      } else if (isInsert(delta)) {
        // If multiple lines were inserted, treat it as multiple inserts of single lines.

        int pos = delta.getRevised().getPosition();
        for (Object l : delta.getRevised().getLines()) {
          String line = (String) l;
          ArrayList<String> line_array = new ArrayList<String>();
          line_array.add(line);

          Chunk c = new Chunk(pos++, line_array);

          Delta new_delta = new InsertDelta(delta.getOriginal(), c);

          Diff diff = new SimpleTextDiff(new_delta, original, revised, delimiter);
          diff.setParent(this);
          ret.add(diff);
        }
      } else {
        Diff diff = new SimpleTextDiff(delta, original, revised, delimiter);
        diff.setParent(this);
        ret.add(diff);
      }
    }

    // Now we diff the compiler messages.

    List<CompilerMessage> original_messages = left.getCompilerMessages();
    List<CompilerMessage> revised_messages = right.getCompilerMessages();

    Patch messages_patch = DiffUtils.diff(original_messages, revised_messages);

    for (Delta delta : messages_patch.getDeltas()) {
      Diff diff = new SimpleMessageDiff(delta, original, revised, delimiter);
      diff.setParent(this);
      ret.add(diff);
    }

    return ret;
  }