ResourceManager { public ResourceManager() { reinitializeIfNecessary(); } ResourceManager(); int size(); int size(Resource<L> relativeTo); AddResult<L> addResource(Resource<L> newResource); List<Resource<L>> removeResource(Resource<L> doomedResource); List<Resource<L>> findResources(L query, LocationResolver<L> locationResolver); Set<Resource<L>> getChildren(Resource<L> resource); Resource<L> getParent(Resource<L> resource); Resource<L> getResource(ID resourceId); List<Resource<L>> getResourcesBreadthFirst(); List<Resource<L>> getAllResources(Collection<ID> filter); Set<Resource<L>> getRootResources(); void logTreeGraph(String logMsg, long duration); List<Resource<L>> removeResources(L query, LocationResolver<L> locationResolver);  }
@Test public void testResourceManager() { ResourceType<DMRNodeLocation> type = ResourceType .<DMRNodeLocation> builder().id(new ID("resType")).name(new Name("resTypeName")) .location(DMRNodeLocation.empty()) .build(); ResourceManager<DMRNodeLocation> rm = new ResourceManager<>(); Resource<DMRNodeLocation> root1 = Resource .<DMRNodeLocation> builder() .id(new ID("root1")) .name(new Name("root1Name")) .location(DMRNodeLocation.empty()) .type(type) .build(); Resource<DMRNodeLocation> root2 = Resource .<DMRNodeLocation> builder() .id(new ID("root2")).name(new Name("root2Name")).type(type) .location(DMRNodeLocation.empty()) .build(); Resource<DMRNodeLocation> child1 = Resource .<DMRNodeLocation> builder() .id(new ID("child1")).name(new Name("child1Name")).type(type).parent(root1) .location(DMRNodeLocation.of("/child=1")).build(); Resource<DMRNodeLocation> child2 = Resource .<DMRNodeLocation> builder() .id(new ID("child2")).name(new Name("child2Name")).type(type).parent(root1) .location(DMRNodeLocation.of("/child=2")).build(); Resource<DMRNodeLocation> grandChild1 = Resource .<DMRNodeLocation> builder() .id(new ID("grand1")).name(new Name("grand1Name")).type(type).parent(child1) .location(DMRNodeLocation.of("/child=1/grandchild=1")).build(); addResourceAndTest(rm, root1, Effect.ADDED); Assert.assertEquals(1, rm.getResourcesBreadthFirst().size()); Assert.assertTrue(rm.getResourcesBreadthFirst().contains(root1)); Assert.assertEquals(root1, rm.getResource(root1.getID())); Assert.assertEquals(1, rm.getRootResources().size()); Assert.assertTrue(rm.getRootResources().contains(root1)); addResourceAndTest(rm, child1, Effect.ADDED); Assert.assertEquals(2, rm.getResourcesBreadthFirst().size()); Assert.assertTrue(rm.getResourcesBreadthFirst().contains(child1)); Assert.assertEquals(child1, rm.getResource(child1.getID())); addResourceAndTest(rm, grandChild1, Effect.ADDED); Assert.assertEquals(3, rm.getResourcesBreadthFirst().size()); Assert.assertTrue(rm.getResourcesBreadthFirst().contains(grandChild1)); Assert.assertEquals(grandChild1, rm.getResource(grandChild1.getID())); addResourceAndTest(rm, root2, Effect.ADDED); Assert.assertEquals(4, rm.getResourcesBreadthFirst().size()); Assert.assertTrue(rm.getResourcesBreadthFirst().contains(root2)); Assert.assertEquals(root2, rm.getResource(root2.getID())); Assert.assertEquals(2, rm.getRootResources().size()); Assert.assertTrue(rm.getRootResources().contains(root2)); addResourceAndTest(rm, child2, Effect.ADDED); Assert.assertEquals(5, rm.getResourcesBreadthFirst().size()); Assert.assertTrue(rm.getResourcesBreadthFirst().contains(child2)); Assert.assertEquals(child2, rm.getResource(child2.getID())); Assert.assertEquals(2, rm.getChildren(root1).size()); Assert.assertTrue(rm.getChildren(root1).contains(child1)); Assert.assertTrue(rm.getChildren(root1).contains(child2)); Assert.assertEquals(1, rm.getChildren(child1).size()); Assert.assertTrue(rm.getChildren(child1).contains(grandChild1)); Assert.assertEquals(0, rm.getChildren(grandChild1).size()); Assert.assertEquals(0, rm.getChildren(root2).size()); Assert.assertEquals(null, rm.getParent(root1)); Assert.assertEquals(null, rm.getParent(root2)); Assert.assertEquals(root1, rm.getParent(child1)); Assert.assertEquals(root1, rm.getParent(child2)); Assert.assertEquals(child1, rm.getParent(grandChild1)); Iterator<Resource<DMRNodeLocation>> bIter = rm.getResourcesBreadthFirst().iterator(); Assert.assertEquals(root1, bIter.next()); Assert.assertEquals(child1, bIter.next()); Assert.assertEquals(child2, bIter.next()); Assert.assertEquals(grandChild1, bIter.next()); Assert.assertEquals(root2, bIter.next()); Assert.assertFalse(bIter.hasNext()); Collection<Resource<DMRNodeLocation>> removed = rm.removeResources(child2.getLocation(), new DMRLocationResolver()); Assert.assertEquals(1, removed.size()); Assert.assertEquals(child2, removed.iterator().next()); bIter = rm.getResourcesBreadthFirst().iterator(); Assert.assertEquals(root1, bIter.next()); Assert.assertEquals(child1, bIter.next()); Assert.assertEquals(grandChild1, bIter.next()); Assert.assertEquals(root2, bIter.next()); Assert.assertFalse(bIter.hasNext()); removed = rm.removeResources(child1.getLocation(), new DMRLocationResolver()); Assert.assertEquals(2, removed.size()); Assert.assertTrue(removed.contains(child1)); Assert.assertTrue(removed.contains(grandChild1)); bIter = rm.getResourcesBreadthFirst().iterator(); Assert.assertEquals(root1, bIter.next()); Assert.assertEquals(root2, bIter.next()); Assert.assertFalse(bIter.hasNext()); }