public void test_removeContext_withDeclaration() throws Exception { when(elementB.getContext()).thenReturn(contextB); when(elementC.getContext()).thenReturn(contextC); // configure B and C Location locationB = mockLocation(elementB); Location locationC = mockLocation(elementC); // record: [B -> A] and [C -> A] { store.recordRelationship(elementA, relationship, locationB); store.recordRelationship(elementA, relationship, locationC); assertEquals(2, store.internalGetLocationCount()); assertEquals(1, store.internalGetKeyCount()); assertEquals(0, store.internalGetLocationCount(contextA)); assertEquals(1, store.internalGetLocationCount(contextB)); assertEquals(1, store.internalGetLocationCount(contextC)); // we get locations from all contexts Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationB, locationC); } // remove A, so no relations anymore // remove B, 1 relation and 1 location left store.removeContext(contextA); assertEquals(0, store.internalGetLocationCount()); assertEquals(0, store.internalGetLocationCount(contextA)); assertEquals(0, store.internalGetLocationCount(contextB)); assertEquals(0, store.internalGetLocationCount(contextC)); { Location[] locations = store.getRelationships(elementA, relationship); assertThat(locations).isEmpty(); } }
public void test_aboutToIndex_unitExcluded() throws Exception { // build library with defining unit Source librarySource = mock(Source.class); LibraryElement library = mock(LibraryElement.class); CompilationUnitElement libraryUnit = mock(CompilationUnitElement.class); when(library.getContext()).thenReturn(contextA); when(library.getDefiningCompilationUnit()).thenReturn(libraryUnit); when(library.getSource()).thenReturn(librarySource); when(libraryUnit.getContext()).thenReturn(contextA); when(libraryUnit.getSource()).thenReturn(librarySource); when(libraryUnit.getLibrary()).thenReturn(library); // build 2 library units CompilationUnitElement unitA = mock(CompilationUnitElement.class); CompilationUnitElement unitB = mock(CompilationUnitElement.class); when(unitA.getContext()).thenReturn(contextA); when(unitB.getContext()).thenReturn(contextA); when(unitA.getSource()).thenReturn(sourceA); when(unitB.getSource()).thenReturn(sourceB); when(unitA.getLibrary()).thenReturn(library); when(unitB.getLibrary()).thenReturn(library); // prepare locations Location locationA = mockLocation(unitA); Location locationB = mockLocation(unitB); // initially A and B in library when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA, unitB}); store.aboutToIndex(contextA, libraryUnit); store.aboutToIndex(contextA, unitA); store.aboutToIndex(contextA, unitB); store.recordRelationship(elementA, relationship, locationA); store.recordRelationship(elementA, relationship, locationB); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA, locationB); } // exclude A from library when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA}); boolean mayIndex = store.aboutToIndex(contextA, libraryUnit); assertTrue(mayIndex); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA); } // exclude B from library, empty now when(library.getParts()).thenReturn(new CompilationUnitElement[] {}); store.aboutToIndex(contextA, libraryUnit); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations); } }
public void test_writeRead() throws Exception { when(contextA.getElement(eq(elementLocationA))).thenReturn(elementA); when(contextB.getElement(eq(elementLocationB))).thenReturn(elementB); when(elementA.getContext()).thenReturn(contextA); when(elementB.getContext()).thenReturn(contextB); // fill store Location locationA = new Location(elementA, 0, 0); Location locationB = new Location(elementB, 0, 0); store.aboutToIndex(contextA, unitElementA); store.aboutToIndex(contextB, unitElementB); store.recordRelationship(elementA, relationship, locationA); store.recordRelationship(elementB, relationship, locationB); assertEquals(2, store.internalGetKeyCount()); assertEquals(2, store.internalGetLocationCount()); assertLocations(store.getRelationships(elementA, relationship), locationA); assertLocations(store.getRelationships(elementB, relationship), locationB); // write byte[] content; { ByteArrayOutputStream baos = new ByteArrayOutputStream(); store.writeIndex(contextA, baos); content = baos.toByteArray(); } // clear store.removeContext(contextA); store.removeContext(contextB); assertEquals(0, store.internalGetKeyCount()); assertEquals(0, store.internalGetLocationCount()); // we need to re-create AnalysisContext, current instance was marked as removed { contextA = mock(AnalysisContext.class); when(contextA.getElement(eq(elementLocationA))).thenReturn(elementA); when(elementA.getContext()).thenReturn(contextA); } // read { ByteArrayInputStream bais = new ByteArrayInputStream(content); store.readIndex(contextA, bais); } // validate after read assertEquals(1, store.internalGetKeyCount()); assertEquals(1, store.internalGetLocationCount()); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA); } }
public void test_getRelationships_hasTwo() throws Exception { Location locationA = mockLocation(elementA); Location locationB = mockLocation(elementB); store.recordRelationship(elementA, relationship, locationA); store.recordRelationship(elementA, relationship, locationB); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA, locationB); }
public void test_removeSource_withRelationship() throws Exception { Location locationB = mockLocation(elementB); Location locationC = mockLocation(elementC); // record: [B -> A] and [C -> A] { store.recordRelationship(elementA, relationship, locationB); store.recordRelationship(elementA, relationship, locationC); assertEquals(2, store.internalGetLocationCount()); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationB, locationC); } // remove B, 1 relation and 1 location left store.removeSource(contextA, sourceB); assertEquals(1, store.internalGetLocationCount()); assertEquals(1, store.internalGetLocationCount(contextA)); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationC); }
public void test_recordRelationship_member() throws Exception { Member member = mock(Member.class); when(member.getBaseElement()).thenReturn(elementA); // no relationships initially assertEquals(0, store.internalGetLocationCount()); // record relationship store.recordRelationship(member, relationship, location); // no location for "member" { Location[] locations = store.getRelationships(member, relationship); assertLocations(locations); } // has location for "elementA" { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, location); } }
public void test_removeSource_withRelationship_instrumented() throws Exception { Location locationB = mockLocation(elementB); Location locationC = mockLocation(elementC); // record: [B -> A] and [C -> A] { store.recordRelationship(elementA, relationship, locationB); store.recordRelationship(elementA, relationship, locationC); assertEquals(2, store.internalGetLocationCount()); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationB, locationC); } // remove B, 1 relation and 1 location left InstrumentedAnalysisContextImpl iContextA = mock(InstrumentedAnalysisContextImpl.class); when(iContextA.getBasis()).thenReturn(contextA); store.removeSource(iContextA, sourceB); assertEquals(1, store.internalGetLocationCount()); assertEquals(1, store.internalGetLocationCount(contextA)); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationC); }
public void test_getRelationships_twoContexts_oneSource() throws Exception { when(unitElementB.getSource()).thenReturn(sourceB); when(unitElementC.getSource()).thenReturn(sourceB); when(elementA.getContext()).thenReturn(contextA); when(elementB.getContext()).thenReturn(contextB); Location locationA = mockLocation(elementA); Location locationB = mockLocation(elementB); store.recordRelationship(elementA, relationship, locationA); store.recordRelationship(elementB, relationship, locationB); // "elementA" { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA); } // "elementB" { Location[] locations = store.getRelationships(elementB, relationship); assertLocations(locations, locationB); } }
public void test_removeSource_withRelationship_twoContexts_oneSource() throws Exception { when(elementB.getContext()).thenReturn(contextB); when(elementC.getContext()).thenReturn(contextC); when(elementC.getSource()).thenReturn(sourceB); when(unitElementC.getSource()).thenReturn(sourceB); // configure B and C Location locationB = mockLocation(elementB); Location locationC = mockLocation(elementC); // record: [B -> A] and [C -> A] { store.aboutToIndex(contextB, unitElementB); store.aboutToIndex(contextC, unitElementC); store.recordRelationship(elementA, relationship, locationB); store.recordRelationship(elementA, relationship, locationC); assertEquals(2, store.internalGetLocationCount()); assertEquals(1, store.internalGetLocationCount(contextB)); assertEquals(1, store.internalGetLocationCount(contextC)); // we get locations from all contexts Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationB, locationC); } // remove "B" in B, 1 relation and 1 location left store.removeSource(contextB, sourceB); assertEquals(1, store.internalGetLocationCount()); assertEquals(0, store.internalGetLocationCount(contextB)); assertEquals(1, store.internalGetLocationCount(contextC)); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationC); } // now remove "B" in C, empty store.removeSource(contextC, sourceB); assertEquals(0, store.internalGetLocationCount()); assertEquals(0, store.internalGetLocationCount(contextB)); assertEquals(0, store.internalGetLocationCount(contextC)); { Location[] locations = store.getRelationships(elementA, relationship); assertThat(locations).isEmpty(); } }
public void test_removeSources_withDeclaration() throws Exception { Location locationB = mockLocation(elementB); Location locationC = mockLocation(elementC); // record: A, [B -> A], [C -> A] and [B -> C] { store.recordRelationship(elementA, relationship, locationB); store.recordRelationship(elementA, relationship, locationC); store.recordRelationship(elementC, relationship, locationB); assertEquals(3, store.internalGetLocationCount()); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationB, locationC); } // remove container with [A], only [B -> C] left SourceContainer containerA = mockSourceContainer(sourceA); store.removeSources(contextA, containerA); assertEquals(1, store.internalGetLocationCount()); assertEquals(1, store.internalGetLocationCount(contextA)); { Location[] locations = store.getRelationships(elementC, relationship); assertLocations(locations, locationB); } }
public void test_aboutToIndex_sharedSource_inTwoLibraries() throws Exception { Source librarySourceA = mock(Source.class); Source librarySourceB = mock(Source.class); LibraryElement libraryA = mock(LibraryElement.class); LibraryElement libraryB = mock(LibraryElement.class); CompilationUnitElement libraryAUnit = mock(CompilationUnitElement.class); CompilationUnitElement libraryBUnit = mock(CompilationUnitElement.class); when(libraryA.getDefiningCompilationUnit()).thenReturn(libraryAUnit); when(libraryB.getDefiningCompilationUnit()).thenReturn(libraryBUnit); when(libraryA.getSource()).thenReturn(librarySourceA); when(libraryB.getSource()).thenReturn(librarySourceB); when(libraryAUnit.getSource()).thenReturn(librarySourceA); when(libraryBUnit.getSource()).thenReturn(librarySourceB); when(libraryAUnit.getLibrary()).thenReturn(libraryA); when(libraryBUnit.getLibrary()).thenReturn(libraryB); // build 2 units in different libraries CompilationUnitElement unitA = mock(CompilationUnitElement.class); CompilationUnitElement unitB = mock(CompilationUnitElement.class); when(unitA.getContext()).thenReturn(contextA); when(unitB.getContext()).thenReturn(contextA); when(unitA.getSource()).thenReturn(sourceA); when(unitB.getSource()).thenReturn(sourceA); when(unitA.getLibrary()).thenReturn(libraryA); when(unitB.getLibrary()).thenReturn(libraryB); when(libraryA.getParts()).thenReturn(new CompilationUnitElement[] {unitA}); when(libraryB.getParts()).thenReturn(new CompilationUnitElement[] {unitB}); // record relationships in both A and B Location locationA = mockLocation(unitA); Location locationB = mockLocation(unitB); store.aboutToIndex(contextA, libraryAUnit); store.aboutToIndex(contextA, libraryBUnit); store.aboutToIndex(contextA, unitA); store.aboutToIndex(contextA, unitB); store.recordRelationship(elementA, relationship, locationA); store.recordRelationship(elementA, relationship, locationB); { Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, locationA, locationB); } }
public void test_getRelationships_noRelations() throws Exception { store.recordRelationship(elementA, relationship, location); Location[] locations = store.getRelationships(elementA, Relationship.getRelationship("no-such-relationship")); assertThat(locations).isEmpty(); }
public void test_getRelationships_hasOne() throws Exception { store.recordRelationship(elementA, relationship, location); Location[] locations = store.getRelationships(elementA, relationship); assertLocations(locations, location); }