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);
   }
 }
Exemplo n.º 2
0
 /**
  * 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;
 }