@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_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);
   }
 }
 /**
  * Return the element being represented by this handle, reconstituting the element if the
  * reference has been set to {@code null}.
  *
  * @return the element being represented by this handle
  */
 protected Element getActualElement() {
   Element element = elementReference.get();
   if (element == null) {
     element = context.getElement(location);
     elementReference = new WeakReference<Element>(element);
   }
   return element;
 }
  public void test_dartEngineAnalysis() throws AnalysisException {
    String svnRootName = System.getProperty("svnRoot");
    assertNotNull("Missing property value: set using -DsvnRoot=...", svnRootName);
    File svnRoot = new File(svnRootName);
    assertTrue("Invalid property value: svnRoot directory does not exist", svnRoot.exists());

    DartSdk sdk = DirectoryBasedDartSdk.getDefaultSdk();
    assertNotNull(
        "Missing or invalid property value: set using -Dcom.google.dart.sdk=...", svnRootName);
    SourceFactory sourceFactory =
        new SourceFactory(new DartUriResolver(sdk), new FileUriResolver());
    AnalysisContext context = AnalysisEngine.getInstance().createAnalysisContext();
    context.setSourceFactory(sourceFactory);

    FileBasedSource engineSource =
        new FileBasedSource(
            sourceFactory.getContentCache(),
            new File(svnRoot, "pkg/analyzer_experimental/lib/src/generated/engine.dart"));
    long startTime = System.currentTimeMillis();
    LibraryElement library = context.computeLibraryElement(engineSource);
    verify(library);
    long endTime = System.currentTimeMillis();
    //
    // Print out timing information.
    //
    System.out.print("Resolved Dart analysis engine in ");
    System.out.print(endTime - startTime);
    System.out.println(" ms");
    System.out.println();
    printStatistics();
    //
    // Print out memory usage information.
    //
    //    MemoryUsage usage = MemoryUtilities.measureMemoryUsage(library);
    //    PrintWriter writer = new PrintWriter(System.out);
    //    usage.writeSummary(writer);
    //    writer.flush();
    //
    // Validate that there were no errors.
    //
    assertValid();
  }
 /** Check if the given {@link Element} is visible in the given {@link Source}. */
 protected static boolean isInTheSameLibrary(
     Element element, AnalysisContext context, Source source) {
   // should be the same AnalysisContext
   if (!isInContext(element, context)) {
     return false;
   }
   // private elements are visible only in their library
   Source[] librarySourcesOfSource = context.getLibrariesContaining(source);
   Source librarySourceOfElement = element.getLibrary().getSource();
   return ArrayUtils.contains(librarySourcesOfSource, librarySourceOfElement);
 }
 public SimpleResourceMapImpl(IContainer container, AnalysisContext context) {
   this.container = container;
   this.containerLocation = container.getLocation();
   this.context = context;
   this.contentCache = context.getSourceFactory().getContentCache();
 }