Exemplo n.º 1
0
 private MovesManager.FragmentPair createFragmentPair(
     int baseIndex,
     int baseOffset,
     int baseEnd,
     int witnessIndex,
     int witnessOffset,
     int witnessEnd) {
   JuxtaDocument doc = getDoc(baseIndex);
   JuxtaDocument doc2 = getDoc(witnessIndex);
   MovesManager.FragmentPair fp = MovesManager.newFragmentPair();
   fp.first.set(doc, baseOffset, baseEnd);
   fp.second.set(doc2, witnessOffset, witnessEnd);
   Log("MB: " + insertBrackets(doc.getDocumentText(), baseOffset, baseEnd));
   Log("MW: " + insertBrackets(doc2.getDocumentText(), witnessOffset, witnessEnd));
   return fp;
 }
Exemplo n.º 2
0
  void getDiffs(Node node) throws ReportedException {
    int baseIndex = Integer.parseInt(getAttr(node, "base"));
    int witnessIndex = Integer.parseInt(getAttr(node, "witness"));
    Log(
        "<test id=\""
            + getAttr(node, "id")
            + "\" base=\""
            + baseIndex
            + "\" witness=\""
            + witnessIndex
            + "\" >");

    LinkedList docs = documentManager.getDocumentList();
    JuxtaDocument baseDoc = (JuxtaDocument) docs.get(baseIndex);
    JuxtaDocument witnessDoc = (JuxtaDocument) docs.get(witnessIndex);
    String str = "";
    Collation collation = comparisonSet.getCollation(baseDoc);
    List list = collation.getDifferences(witnessDoc);
    if (list == null) list = new LinkedList();
    dumpAllDifferences(list, baseDoc.getDocumentText(), witnessDoc.getDocumentText());
    int count = 0;
    NodeList nlResult = node.getChildNodes();

    int numResults = (createXml ? list.size() : nlResult.getLength());

    for (int i = 0; i < numResults; ++i) {
      Node nodeResult = (createXml ? null : nlResult.item(i));
      if (createXml
          || nodeResult.getNodeType() == Node.ELEMENT_NODE
              && nodeResult.getNodeName().equals("result")) {
        if (count >= list.size()) {
          DoAssert("Not enough difference entries returned", -1, list.size());
          break;
        }
        Difference difference = (Difference) list.get(count);
        ++count;
        String strBaseFilename =
            documentManager.lookupDocument(difference.getBaseDocumentID()).getDocumentName();
        String strWitnessFilename =
            documentManager.lookupDocument(difference.getWitnessDocumentID()).getDocumentName();
        int iBaseOfs = difference.getOffset(Difference.BASE);
        int iBaseLen = difference.getLength(Difference.BASE);
        int iWitnessOfs = difference.getOffset(Difference.WITNESS);
        int iWitnessLen = difference.getLength(Difference.WITNESS);

        str = "\t<result ";
        str += "basename=\"" + strBaseFilename;
        str += "\" baseoffset=\"" + Integer.toString(iBaseOfs);
        str += "\" baselength=\"" + Integer.toString(iBaseLen);
        str += "\" witnessname=\"" + strWitnessFilename;
        str += "\" witnessoffset=\"" + Integer.toString(iWitnessOfs);
        str += "\" witnesslength=\"" + Integer.toString(iWitnessLen);
        str += "\" type=\"" + Difference.getTypeName(difference.getType());
        str += "\" distance=\"" + Integer.toString(difference.getDistance());
        str += "\" />";
        Log(str);

        if (!createXml) {
          DoAssert(
              "",
              "",
              difference.testContents(
                  Difference.getTypeValue(getAttr(nodeResult, "type")),
                  getAttrInt(nodeResult, "baseoffset"),
                  getAttrInt(nodeResult, "baselength"),
                  getAttrInt(nodeResult, "witnessoffset"),
                  getAttrInt(nodeResult, "witnesslength")));
          DoAssert("basename", getAttr(nodeResult, "basename"), strBaseFilename);
          DoAssert("witnessname", getAttr(nodeResult, "witnessname"), strWitnessFilename);
          DoAssert(
              "distance",
              getAttr(nodeResult, "distance"),
              Integer.toString(difference.getDistance()));
        }
      }
    }
    DoAssert("count", count, (list == null) ? 0 : list.size());

    Log("</test>");
  }
Exemplo n.º 3
0
  public void testMovesManager() {
    // This tests the block manager. We add Moves in various orders and with various errors, and we
    // see what blocks are
    // returned. We also remove documents to make sure that the Moves are deleted correctly.
    try {
      addDoc(file3);
      JuxtaDocument doc1 = getDoc(0);
      JuxtaDocument doc2 = getDoc(1);
      JuxtaDocument doc3 = getDoc(2);

      int docId1 = doc1.getID();
      int docId2 = doc2.getID();
      int docId3 = doc3.getID();

      // Make sure that nothing is returned before any manipulation.
      assertMoveIsEmpty(docId1, docId2);
      assertMoveIsEmpty(docId1, docId3);
      assertMoveIsEmpty(docId2, docId1);
      assertMoveIsEmpty(docId2, docId3);
      assertMoveIsEmpty(docId3, docId1);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n</moves>\n");

      MovesManager.Fragment leftFragment = movesManager.new Fragment();
      MovesManager.Fragment rightFragment = movesManager.new Fragment();

      // add a move and try all permutations of retrieval
      leftFragment = movesManager.new Fragment(doc1, 10, 25);
      rightFragment = movesManager.new Fragment(doc2, 17, 27);
      movesManager.createMove(leftFragment, rightFragment);

      assertSingleMoveEntry(docId1, 10, 25, docId2, 17, 27);
      assertMoveIsEmpty(docId1, docId3);
      assertSingleMoveEntry(docId2, 17, 27, docId1, 10, 25);
      assertMoveIsEmpty(docId2, docId3);
      assertMoveIsEmpty(docId3, docId1);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file2.txt\" space2=\"original\" start2=\"17\" end2=\"27\" />\n</moves>\n");

      // add a move to a different pair and try all permutations of retrieval
      rightFragment = movesManager.new Fragment(doc3, 33, 43);
      movesManager.createMove(leftFragment, rightFragment);

      assertSingleMoveEntry(docId1, 10, 25, docId2, 17, 27);
      assertSingleMoveEntry(docId1, 10, 25, docId3, 33, 43);
      assertSingleMoveEntry(docId2, 17, 27, docId1, 10, 25);
      assertMoveIsEmpty(docId2, docId3);
      assertSingleMoveEntry(docId3, 33, 43, docId1, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file2.txt\" space2=\"original\" start2=\"17\" end2=\"27\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n</moves>\n");

      // add a second move (in the opposite base/witness order)
      // Note that the returned moves are sorted by left hand side.
      leftFragment = movesManager.new Fragment(doc2, 48, 52);
      rightFragment = movesManager.new Fragment(doc1, 3, 8);
      movesManager.createMove(leftFragment, rightFragment);

      assertDoubleMoveEntry(docId1, 3, 8, 10, 25, docId2, 48, 52, 17, 27);
      assertSingleMoveEntry(docId1, 10, 25, docId3, 33, 43);
      assertDoubleMoveEntry(docId2, 17, 27, 48, 52, docId1, 10, 25, 3, 8);
      assertMoveIsEmpty(docId2, docId3);
      assertSingleMoveEntry(docId3, 33, 43, docId1, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file2.txt\" space2=\"original\" start2=\"17\" end2=\"27\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"3\" end1=\"8\" doc2=\"file2.txt\" space2=\"original\" start2=\"48\" end2=\"52\" />\n</moves>\n");

      // change a move
      MoveList ml = movesManager.getAllMoves(docId1, docId2);
      MovesManager.FragmentPair fp = ml.get(0);
      leftFragment =
          movesManager
          .new Fragment(
              fp.first.getDocument(),
              fp.first.getStartOffset(OffsetRange.Space.ACTIVE) - 1,
              fp.first.getEndOffset(OffsetRange.Space.ACTIVE) - 1);
      rightFragment =
          movesManager
          .new Fragment(
              fp.second.getDocument(),
              fp.second.getStartOffset(OffsetRange.Space.ACTIVE) - 1,
              fp.second.getEndOffset(OffsetRange.Space.ACTIVE) - 1);
      movesManager.updateBlock(fp, leftFragment, rightFragment);

      assertDoubleMoveEntry(docId1, 2, 7, 10, 25, docId2, 47, 51, 17, 27);
      assertSingleMoveEntry(docId1, 10, 25, docId3, 33, 43);
      assertDoubleMoveEntry(docId2, 17, 27, 47, 51, docId1, 10, 25, 2, 7);
      assertMoveIsEmpty(docId2, docId3);
      assertSingleMoveEntry(docId3, 33, 43, docId1, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file2.txt\" space2=\"original\" start2=\"17\" end2=\"27\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"2\" end1=\"7\" doc2=\"file2.txt\" space2=\"original\" start2=\"47\" end2=\"51\" />\n</moves>\n");

      // delete a move
      ml = movesManager.getAllMoves(docId1, docId2);
      movesManager.deleteMove(ml.get(0));

      assertSingleMoveEntry(docId1, 10, 25, docId2, 17, 27);
      assertSingleMoveEntry(docId1, 10, 25, docId3, 33, 43);
      assertSingleMoveEntry(docId2, 17, 27, docId1, 10, 25);
      assertMoveIsEmpty(docId2, docId3);
      assertSingleMoveEntry(docId3, 33, 43, docId1, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file2.txt\" space2=\"original\" start2=\"17\" end2=\"27\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n</moves>\n");

      // remove a document
      documentManager.removeDocument(getDoc(1)); // this is the second document

      assertMoveIsEmpty(docId1, docId2);
      assertSingleMoveEntry(docId1, 10, 25, docId3, 33, 43);
      assertMoveIsEmpty(docId2, docId1);
      assertMoveIsEmpty(docId2, docId3);
      assertSingleMoveEntry(docId3, 33, 43, docId1, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n</moves>\n");

      // put it back in
      addDoc(file2);
      docId2 = getDoc(2).getID(); // the previous second document is now in the third place.

      assertLastGoodCase(docId1, docId2, docId3);

      //
      // Error cases
      //

      // add an overlapping move
      boolean failed = false;
      try {
        leftFragment = movesManager.new Fragment(doc1, 14, 29);
        rightFragment = movesManager.new Fragment(doc3, 17, 27);
        movesManager.createMove(leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Overlapping move should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // add a move with a zero length
      failed = false;
      try {
        leftFragment = movesManager.new Fragment(doc1, 14, 14);
        rightFragment = movesManager.new Fragment(doc3, 17, 27);
        movesManager.createMove(leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Zero length move should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // add a move with a negative length
      failed = false;
      try {
        leftFragment = movesManager.new Fragment(doc1, 14, 29);
        rightFragment = movesManager.new Fragment(doc3, 17, 12);
        movesManager.createMove(leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Negative length move should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // add a move that is out of bounds
      failed = false;
      try {
        leftFragment = movesManager.new Fragment(doc1, 14, 1229);
        rightFragment = movesManager.new Fragment(doc3, 17, 27);
        movesManager.createMove(leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Out of bounds move should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // change a move that doesn't exist
      failed = false;
      try {
        leftFragment = movesManager.new Fragment(doc1, 14, 19);
        rightFragment = movesManager.new Fragment(doc3, 17, 27);
        fp = movesManager.getAllMoves(docId1, docId3).get(0);
        fp.first.resetDocument(doc2);
        movesManager.updateBlock(fp, leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Non-existent move should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // delete a move that doesn't exist
      failed = false;
      try {
        movesManager.deleteMove(fp);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Non-existent delete should have thrown an exception", true, failed);
      assertLastGoodCase(docId1, docId2, docId3);

      // change a move to one that overlaps
      leftFragment = movesManager.new Fragment(doc1, 30, 35);
      rightFragment = movesManager.new Fragment(doc3, 22, 26);
      movesManager.createMove(leftFragment, rightFragment);

      failed = false;
      try {
        fp = movesManager.getAllMoves(docId1, docId3).get(0);
        leftFragment = movesManager.new Fragment(doc1, 8, 35);
        movesManager.updateBlock(fp, leftFragment, rightFragment);
      } catch (LoggedException e) {
        failed = true;
      }
      assertEquals("Overlapped change should have thrown an exception", true, failed);
      assertMoveIsEmpty(docId1, docId2);
      assertDoubleMoveEntry(docId1, 10, 25, 30, 35, docId3, 33, 43, 22, 26);
      assertMoveIsEmpty(docId2, docId1);
      assertMoveIsEmpty(docId2, docId3);
      assertDoubleMoveEntry(docId3, 22, 26, 33, 43, docId1, 30, 35, 10, 25);
      assertMoveIsEmpty(docId3, docId2);
      assertMovesManagerDump(
          "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<moves>\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"30\" end1=\"35\" doc2=\"file3.txt\" space2=\"original\" start2=\"22\" end2=\"26\" />\n\t<move doc1=\"file1.txt\" space1=\"original\" start1=\"10\" end1=\"25\" doc2=\"file3.txt\" space2=\"original\" start2=\"33\" end2=\"43\" />\n</moves>\n");
    } catch (LoggedException e) {
      fail();
    }
  }