@Override
 protected void setUp() throws Exception {
   super.setUp();
   when(contextA.toString()).thenReturn("contextA");
   when(contextB.toString()).thenReturn("contextB");
   when(contextC.toString()).thenReturn("contextC");
   when(sourceA.toString()).thenReturn("sourceA");
   when(sourceB.toString()).thenReturn("sourceB");
   when(sourceC.toString()).thenReturn("sourceC");
   when(sourceD.toString()).thenReturn("sourceD");
   when(elementA.toString()).thenReturn("elementA");
   when(elementB.toString()).thenReturn("elementB");
   when(elementC.toString()).thenReturn("elementC");
   when(elementD.toString()).thenReturn("elementD");
   when(elementA.getContext()).thenReturn(contextA);
   when(elementB.getContext()).thenReturn(contextA);
   when(elementC.getContext()).thenReturn(contextA);
   when(elementD.getContext()).thenReturn(contextA);
   when(elementA.getLocation()).thenReturn(elementLocationA);
   when(elementB.getLocation()).thenReturn(elementLocationB);
   when(elementC.getLocation()).thenReturn(elementLocationC);
   when(elementD.getLocation()).thenReturn(elementLocationD);
   when(elementA.getEnclosingElement()).thenReturn(unitElementA);
   when(elementB.getEnclosingElement()).thenReturn(unitElementB);
   when(elementC.getEnclosingElement()).thenReturn(unitElementC);
   when(elementD.getEnclosingElement()).thenReturn(unitElementD);
   when(elementA.getSource()).thenReturn(sourceA);
   when(elementB.getSource()).thenReturn(sourceB);
   when(elementC.getSource()).thenReturn(sourceC);
   when(elementD.getSource()).thenReturn(sourceD);
   when(elementA.getLibrary()).thenReturn(libraryElement);
   when(elementB.getLibrary()).thenReturn(libraryElement);
   when(elementC.getLibrary()).thenReturn(libraryElement);
   when(elementD.getLibrary()).thenReturn(libraryElement);
   when(unitElementA.getSource()).thenReturn(sourceA);
   when(unitElementB.getSource()).thenReturn(sourceB);
   when(unitElementC.getSource()).thenReturn(sourceC);
   when(unitElementD.getSource()).thenReturn(sourceD);
   when(unitElementA.getLibrary()).thenReturn(libraryElement);
   when(unitElementB.getLibrary()).thenReturn(libraryElement);
   when(unitElementC.getLibrary()).thenReturn(libraryElement);
   when(unitElementD.getLibrary()).thenReturn(libraryElement);
   // library
   when(librarySource.toString()).thenReturn("libSource");
   when(libraryUnitElement.getSource()).thenReturn(librarySource);
   when(libraryElement.getSource()).thenReturn(librarySource);
   when(libraryElement.getDefiningCompilationUnit()).thenReturn(libraryUnitElement);
   // by default index all units
   store.aboutToIndex(contextA, unitElementA);
   store.aboutToIndex(contextA, unitElementB);
   store.aboutToIndex(contextA, unitElementC);
   store.aboutToIndex(contextA, unitElementD);
 }
 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);
   }
 }
Exemplo n.º 3
0
  private static IEditorInput getEditorInput(Element element) {
    CompilationUnitElement cu = getCompilationUnit(element);

    if (cu == null) {
      return null;
    }

    IResource resource = DartCore.getProjectManager().getResource(cu.getSource());

    if (resource instanceof IFile) {
      return new FileEditorInput((IFile) resource);
    }

    Source source = cu.getSource();
    return getEditorInput(source);
  }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   when(store.aboutToIndexHtml(context, htmlElement)).thenReturn(true);
   when(unit.getElement()).thenReturn(htmlElement);
   when(htmlElement.getSource()).thenReturn(unitSource);
   when(htmlElement.getAngularCompilationUnit()).thenReturn(unitElement);
   when(unitElement.getSource()).thenReturn(unitSource);
   operation = new IndexHtmlUnitOperation(store, context, unit);
 }
 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_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_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();
   }
 }