super.setUp(); this.realDoc = createDocument(); elmOne = realDoc.createChildElement(realDoc.getDocumentElement(), "a", noAttribs()); elmTwo = realDoc.createChildElement(realDoc.getDocumentElement(), "b", noAttribs()); this.doc = spy(realDoc); doAnswer(new Answer<Void>() { return null; }).when(doc).addListener(Mockito.<DocHandler>any()); doAnswer(new Answer<Void>() { @SuppressWarnings("unchecked") return null; }).when(doc).removeListener(Mockito.<DocHandler>any()); return realDoc.locate((Integer) invocation.getArguments()[0]); }).when(doc).locate(Mockito.anyInt());
/** * Inserts a first-child element. */ private void remoteInsertCanonicalValue(String value) { Doc.N firstChild = doc.getFirstChild(doc.getDocumentElement()); Point<Doc.N> insertion = Point.<Doc.N>inElement(doc.getDocumentElement(), firstChild); doc.createElement(insertion, TAG, CollectionUtils.immutableMap(ATTR, value)); }
private void remoteClearValue() { Doc.E toDelete = DocHelper.getLastElementWithTagName(doc, TAG, doc.getDocumentElement()); while (toDelete != null) { doc.deleteNode(toDelete); toDelete = DocHelper.getLastElementWithTagName(doc, TAG, doc.getDocumentElement()); } }
/** * Inserts a last-child element. */ private void remoteInsertRedundantValue(String value) { doc.createChildElement(doc.getDocumentElement(), TAG, CollectionUtils.immutableMap(ATTR, value)); }
/** * Test that only the root of a deletion is given child notifications, not * children of parents that are themselves being deleted. */ @SuppressWarnings("unchecked") public void testRecursiveDeletion() { Doc.E parent = elmOne; final Doc.E child = realDoc.createChildElement(parent, "c", noAttribs()); Doc.E grandChild = realDoc.createChildElement(child, "d", noAttribs()); DocumentEventRouter<Doc.N, Doc.E, ?> router = createRouter(doc); ElementListener<Doc.E> childRemovedListener = mock(ElementListener.class); router.addChildListener(parent, childRemovedListener); ElementListener<Doc.E> grandChildRemovedListener = mock(ElementListener.class); router.addChildListener(child, grandChildRemovedListener); realDoc.deleteNode(child); verify(childRemovedListener).onElementRemoved(same(child)); verifyNoMoreInteractions(childRemovedListener); verifyZeroInteractions(grandChildRemovedListener); }
@Override public V set(I initialState) { final Map<String, String> attributes = Initializer.Helper.buildAttributes(initialState, valueFactory); // Insert a new first-child element of the container. Point<Doc.N> insertionPoint = Point.inElement(container, getDocument().getFirstChild(container)); Doc.E element = getDocument().createElement(insertionPoint, valueTagName, attributes); // onElementAdded will create the value object. assert currentElement != null; assert currentValue != null; cleanup(); return currentValue; }
/** * Copies a document from one wavelet to another, appending the contents if the * destination document already exists. * * @param sourceWavelet the source wavelet * @param destWavelet the destination wavelet * @param docId the id of the document to copy */ public static void copyDocument(Wavelet sourceWavelet, Wavelet destWavelet, String docId) { Document document = sourceWavelet.getDocument(docId); DocInitialization docInit = document.toInitialization(); // TODO(user): add a createDocument method to Wavelet so that we can push this ugliness // down the stack. ObservableDocument destDoc = destWavelet.getDocument(docId); destDoc.hackConsume(Nindo.fromDocOp(docInit, false /* don't remove skip */)); }
@Override public void clear() { // Delete non-canonical elements first so no events are generated. cleanup(); // Then delete the canonical, which will remove currentValue. Doc.E element = findCanonicalElement(); if (element != null) { getDocument().deleteNode(element); } assert currentElement == null; assert currentValue == null; }
AttributeListener<Doc.E> attribListenerTwo = mock(AttributeListener.class); router.addAttributeListener(elmTwo, attribListenerTwo); realDoc.setElementAttribute(elmOne, "t", "1"); verify(attribListenerOne).onAttributesChanged(same(elmOne), anyAttribs(), anyAttribs()); verify(attribListenerTwo, never()).onAttributesChanged(anyElement(), anyAttribs(), anyAttribs()); realDoc.setElementAttribute(elmTwo, "s", "2"); Mockito.verify(attribListenerOne).onAttributesChanged(same(elmOne), anyAttribs(), anyAttribs()); Mockito.verify(attribListenerTwo).onAttributesChanged(same(elmTwo), anyAttribs(), anyAttribs()); DeletionListener deleteListenerTwo = mock(DeletionListener.class); router.addDeletionListener(elmTwo, deleteListenerTwo); realDoc.deleteNode(elmTwo); verify(deleteListenerOne, never()).onDeleted(); verify(deleteListenerTwo).onDeleted(); realDoc.deleteNode(elmOne); verify(deleteListenerOne).onDeleted(); verify(deleteListenerTwo).onDeleted();
public static <N> void observe(MutableDocumentListener listener, ObservableDocument doc) { doc.addListener(new MutableDocumentListenerAdapter(listener, doc)); }
/** * Creates a singleton backed by a document root. */ private static DocumentBasedSingleton<Integer, String> createSingleton(DocEventRouter router) { return DocumentBasedSingleton.create(router, router.getDocument().getDocumentElement(), TAG, FACTORY); }
@SuppressWarnings("unchecked") @Override public Void answer(InvocationOnMock invocation) throws Throwable { DocumentHandler<Doc.N, Doc.E, Doc.T> listener = (DocumentHandler<Doc.N, Doc.E, Doc.T>) invocation.getArguments()[0]; docListeners.remove(invocation.getArguments()[0]); realDoc.removeListener(listener); return null; } }).when(doc).removeListener(Mockito.<DocHandler>any());
public void testDeltasAreReturnedInOrder() { // Alex adds a participant to the wavelet OpBasedWavelet waveletAlex = wavelet.getOpBasedWavelet(ALEX); waveletAlex.addParticipant(TRIXIE); // Bob adds a new document to the wavelet OpBasedWavelet waveletBob = wavelet.getOpBasedWavelet(BOB); waveletBob.getDocument("r+randomDocument").insertText(0, "/nHello"); List<WaveletDelta> deltas = wavelet.getDeltas(); assertTrue(deltas.size() == 2); assertEquals("Expected Alex to be first", ALEX, deltas.get(0).getAuthor()); assertEquals("Expected Bob to be the second author", BOB, deltas.get(1).getAuthor()); } }
private void createAttachmentDataDoc(String attachmentId) { String attachmentDataDocId = AttachmentDataDocHelper.dataDocIdFromAttachmentId(attachmentId); wavelet.getDocument(attachmentDataDocId).appendXml(DATA_DOC_XML); }
/** * Test that only the root of a deletion is given child notifications, not * children of parents that are themselves being deleted. */ @SuppressWarnings("unchecked") public void testRecursiveDeletion() { Doc.E parent = elmOne; final Doc.E child = realDoc.createChildElement(parent, "c", noAttribs()); Doc.E grandChild = realDoc.createChildElement(child, "d", noAttribs()); DocumentEventRouter<Doc.N, Doc.E, ?> router = createRouter(doc); ElementListener<Doc.E> childRemovedListener = mock(ElementListener.class); router.addChildListener(parent, childRemovedListener); ElementListener<Doc.E> grandChildRemovedListener = mock(ElementListener.class); router.addChildListener(child, grandChildRemovedListener); realDoc.deleteNode(child); verify(childRemovedListener).onElementRemoved(same(child)); verifyNoMoreInteractions(childRemovedListener); verifyZeroInteractions(grandChildRemovedListener); }
public void testAtomicReplacementOfEquivalentFiresNoEvents() { target.set("42"); DocOp replacement = createReplaceOp(createErasureOp(), createRestoreOp()); target.addListener(listener); doc.hackConsume(Nindo.fromDocOp(replacement, false)); listener.verifyNoEvent(); }
private void remoteRemoveCanonicalValue() { Doc.E canonical = DocHelper.getElementWithTagName(doc, TAG); doc.deleteNode(canonical); }
/** * Inserts a last-child element. */ private void remoteInsertRedundantValue(String value) { doc.createChildElement(doc.getDocumentElement(), TAG, CollectionUtils.immutableMap(ATTR, value)); }
AttributeListener<Doc.E> attribListenerTwo = mock(AttributeListener.class); router.addAttributeListener(elmTwo, attribListenerTwo); realDoc.setElementAttribute(elmOne, "t", "1"); verify(attribListenerOne).onAttributesChanged(same(elmOne), anyAttribs(), anyAttribs()); verify(attribListenerTwo, never()).onAttributesChanged(anyElement(), anyAttribs(), anyAttribs()); realDoc.setElementAttribute(elmTwo, "s", "2"); Mockito.verify(attribListenerOne).onAttributesChanged(same(elmOne), anyAttribs(), anyAttribs()); Mockito.verify(attribListenerTwo).onAttributesChanged(same(elmTwo), anyAttribs(), anyAttribs()); DeletionListener deleteListenerTwo = mock(DeletionListener.class); router.addDeletionListener(elmTwo, deleteListenerTwo); realDoc.deleteNode(elmTwo); verify(deleteListenerOne, never()).onDeleted(); verify(deleteListenerTwo).onDeleted(); realDoc.deleteNode(elmOne); verify(deleteListenerOne).onDeleted(); verify(deleteListenerTwo).onDeleted();
@SuppressWarnings("unchecked") @Override public Void answer(InvocationOnMock invocation) throws Throwable { DocumentHandler<Doc.N, Doc.E, Doc.T> listener = (DocumentHandler<Doc.N, Doc.E, Doc.T>) invocation.getArguments()[0]; docListeners.add(listener); realDoc.addListener(listener); return null; } }).when(doc).addListener(Mockito.<DocHandler>any());