DirectoryEntry extends Entry { @Override public void addChild(Entry child) { checkDoesNotExist(child, child.getName()); unsafeAddChild(child); } DirectoryEntry(int id, String name);  DirectoryEntry(Stream s); @Override void write(Stream s); @Override boolean isDirectory(); @Override void addChild(Entry child); @Override void removeChild(Entry child); @Override List<Entry> getChildren(); @Override boolean hasUnavailableContent(List<Entry> entriesWithUnavailableContent); @Override DirectoryEntry copy(); @Override void collectDifferencesWith(Entry right, List<Difference> result);  }
@Test public void testAddingExistentChildThrowsException() { Entry dir = new DirectoryEntry(-1, "dir"); dir.addChild(new FileEntry(-1, "child", null, -1, false)); Paths.setCaseSensitive(true); try { dir.addChild(new FileEntry(-1, "CHILD", null, -1, false)); } catch (RuntimeException e) { fail(); } try { dir.addChild(new FileEntry(-1, "child", null, -1, false)); fail(); } catch (RuntimeException e) { assertEquals("entry 'child' already exists in 'dir'", e.getMessage()); } Paths.setCaseSensitive(false); try { dir.addChild(new FileEntry(-1, "CHILD", null, -1, false)); fail(); } catch (RuntimeException e) { } }
@Test public void testFindChild() { Entry dir = new DirectoryEntry(-1, null); Entry one = new FileEntry(-1, "one", null, -1, false); Entry two = new FileEntry(-1, "two", null, -1, false); dir.addChild(one); dir.addChild(two); assertSame(one, dir.findChild("one")); assertSame(two, dir.findChild("two")); assertNull(dir.findChild("aaa")); Paths.setCaseSensitive(true); assertNull(dir.findChild("ONE")); Paths.setCaseSensitive(false); assertSame(one, dir.findChild("ONE")); }
@Test public void testIdPath() { Entry dir = new DirectoryEntry(1, null); Entry file = new FileEntry(2, null, null, -1, false); dir.addChild(file); assertEquals(idp(1, 2), file.getIdPath()); }
@Test public void testPath() { Entry dir = new DirectoryEntry(-1, "dir"); Entry file = new FileEntry(-1, "file", null, -1, false); dir.addChild(file); assertEquals("dir/file", file.getPath()); }
@Test public void testRenamingChildToNonExistentNameDoesNotThrowException() { Entry dir = new DirectoryEntry(0, "dir"); Entry child = new FileEntry(1, "child", null, -1, false); dir.addChild(child); child.changeName("new name"); assertEquals("new name", child.getName()); }
@Test public void testRenamingChildToExistingNameThrowsException() { Entry dir = new DirectoryEntry(0, "dir"); Entry child1 = new FileEntry(1, "child1", null, -1, false); Entry child2 = new FileEntry(2, "child2", null, -1, false); dir.addChild(child1); dir.addChild(child2); try { child1.changeName("child2"); fail(); } catch (RuntimeException e) { } }
@Test public void testDifferenceWithCreatedChild() { Entry e1 = new DirectoryEntry(-1, "name"); Entry e2 = new DirectoryEntry(-1, "name"); Entry child = new FileEntry(1, "name", c("content"), -1, false); e2.addChild(child); List<Difference> dd = e1.getDifferencesWith(e2); assertEquals(1, dd.size()); assertFileDifference(dd.get(0), null, child); }
@Test public void testDifferenceWithCreatedChildWithSubChildren() { Entry dir1 = new DirectoryEntry(-1, "name"); Entry dir2 = new DirectoryEntry(-1, "name"); Entry subDir = new DirectoryEntry(-1, "subDir"); Entry subSubFile = new FileEntry(-1, "subSubFile", null, -1, false); dir2.addChild(subDir); subDir.addChild(subSubFile); List<Difference> dd = dir1.getDifferencesWith(dir2); assertEquals(2, dd.size()); assertDirDifference(dd.get(0), null, subDir); assertFileDifference(dd.get(1), null, subSubFile); }
@Test public void testDifferenceWithDeletedChild() { Entry dir1 = new DirectoryEntry(-1, "name"); Entry dir2 = new DirectoryEntry(-1, "name"); Entry subDir = new DirectoryEntry(1, "subDir"); Entry subSubFile = new FileEntry(2, "subSubFile", null, -1, false); dir1.addChild(subDir); subDir.addChild(subSubFile); List<Difference> dd = dir1.getDifferencesWith(dir2); assertEquals(2, dd.size()); assertDirDifference(dd.get(0), subDir, null); assertFileDifference(dd.get(1), subSubFile, null); }
@Test public void testDifferenceWithModifiedChild() { Entry e1 = new DirectoryEntry(-1, "name"); Entry e2 = new DirectoryEntry(-1, "name"); Entry child1 = new FileEntry(1, "name1", c("content"), -1, false); Entry child2 = new FileEntry(1, "name2", c("content"), -1, false); e1.addChild(child1); e2.addChild(child2); List<Difference> dd = e1.getDifferencesWith(e2); assertEquals(1, dd.size()); assertFileDifference(dd.get(0), child1, child2); }
@Test public void testNoesNotIncludeNonModifiedChildDifferences() { Entry e1 = new DirectoryEntry(-1, "name"); Entry e2 = new DirectoryEntry(-1, "name"); e1.addChild(new FileEntry(1, "name", c("content"), -1, false)); e1.addChild(new FileEntry(2, "another name", c("content"), -1, false)); e2.addChild(new FileEntry(1, "name", c("content"), -1, false)); List<Difference> dd = e1.getDifferencesWith(e2); assertEquals("another name", dd.get(0).getLeft().getName()); assertEquals(null, dd.get(0).getRight()); }
@Test public void testDifferenceWithNotModifiedChildWithDifferentIdentity() { Entry e1 = new DirectoryEntry(-1, "name"); Entry e2 = new DirectoryEntry(-1, "name"); e1.addChild(new FileEntry(1, "name", c("content"), -1, false)); e2.addChild(new FileEntry(1, "name", c("content"), -1, false)); assertTrue(e1.getDifferencesWith(e2).isEmpty()); }
@Test public void testDifferenceWithModifiedBothSubjectAndChild() { Entry e1 = new DirectoryEntry(-1, "name1"); Entry e2 = new DirectoryEntry(-1, "name2"); Entry child1 = new FileEntry(1, "name1", c("content"), -1, false); Entry child2 = new FileEntry(1, "name2", c("content"), -1, false); e1.addChild(child1); e2.addChild(child2); List<Difference> dd = e1.getDifferencesWith(e2); assertEquals(2, dd.size()); assertDirDifference(dd.get(0), e1, e2); assertFileDifference(dd.get(1), child1, child2); }
@Test public void testIncludesDifferenceForChilderWhenParentWasModified() { Entry dir1 = new DirectoryEntry(1, "dir1"); Entry dir2 = new DirectoryEntry(1, "dir2"); Entry subDir1 = new DirectoryEntry(2, "subDir"); Entry subDir2 = new DirectoryEntry(2, "subDir"); Entry child1 = new FileEntry(3, "name", c("content"), -1, false); Entry child2 = new FileEntry(3, "name", c("content"), -1, false); dir1.addChild(subDir1); dir2.addChild(subDir2); subDir1.addChild(child1); subDir2.addChild(child2); List<Difference> dd = dir1.getDifferencesWith(dir2); assertEquals(3, dd.size()); assertDirDifference(dd.get(0), dir1, dir2); assertDirDifference(dd.get(1), subDir1, subDir2); assertFileDifference(dd.get(2), child1, child2); }