private void assertSingleMoveEntry( int lhsDoc, int lhsOfs, int lhsEnd, int rhsDoc, int rhsOfs, int rhsEnd) { MoveList ml = movesManager.getAllMoves(lhsDoc, rhsDoc); assertEquals("Size of move list is wrong", 1, ml.size()); FragmentPair fp = ml.get(0); assertMove(fp, lhsDoc, lhsOfs, lhsEnd, rhsDoc, rhsOfs, rhsEnd); }
private void assertDoubleMoveEntry( int lhsDoc, int lhsOfs1, int lhsEnd1, int lhsOfs2, int lhsEnd2, int rhsDoc, int rhsOfs1, int rhsEnd1, int rhsOfs2, int rhsEnd2) { MoveList ml = movesManager.getAllMoves(lhsDoc, rhsDoc); assertEquals("Size of move list is wrong", 2, ml.size()); FragmentPair fp = ml.get(0); assertMove(fp, lhsDoc, lhsOfs1, lhsEnd1, rhsDoc, rhsOfs1, rhsEnd1); fp = ml.get(1); assertMove(fp, lhsDoc, lhsOfs2, lhsEnd2, rhsDoc, rhsOfs2, rhsEnd2); }
private void assertMoveIsEmpty(int lhs, int rhs) { MoveList ml = movesManager.getAllMoves(lhs, rhs); assertEquals("Size of move list is wrong", 0, ml.size()); }
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(); } }