/** * Creates a new {@code Blip} object used for this session. A temporary * id will be assigned to the newly created {@code Blip} object. * * @param wavelet the wavelet that owns this blip. * @param initialContent the initial content of the new blip. * @param parentBlipId the parent of this blip. * @return an instance of new {@code Blip} object used for this session. */ private static Blip newBlip(Wavelet wavelet, String initialContent, String parentBlipId, String blipId, String threadId) { Blip newBlip = new Blip(blipId, initialContent, parentBlipId, threadId, wavelet); if (parentBlipId != null) { Blip parentBlip = wavelet.getBlips().get(parentBlipId); if (parentBlip != null) { parentBlip.getChildBlipIds().add(newBlip.getBlipId()); } } wavelet.getBlips().put(newBlip.getBlipId(), newBlip); BlipThread thread = wavelet.getThread(threadId); if (thread != null) { thread.appendBlip(newBlip); } return newBlip; }
/** * Updates an existing shadow blip. * * @param shadowBlip the blip that is shadowing. * @param blipToShadow the blip being shadowed. */ private void updateShadowBlip(Blip shadowBlip, Blip blipToShadow) { Preconditions.checkNotNull(shadowBlip, "Shadow blip can't be null"); Preconditions.checkNotNull(blipToShadow, "Blip to shadow can't be null"); shadowBlip.all().replace(blipToShadow.getContent()); for (Annotation annotation : blipToShadow.getAnnotations()) { if (annotation.getName().equals(ECHOEY_ANNOTATION)) { continue; } shadowBlip.range(annotation.getRange().getStart(), annotation.getRange().getEnd()).annotate( annotation.getName(), annotation.getValue()); } shadowBlip.all().annotate(ECHOEY_ANNOTATION, blipToShadow.getBlipId()); shadowBlipMap.put(blipToShadow.getBlipId(), shadowBlip.getBlipId()); }
/** * Copies the content of the source blip into the target blip. * * @param fromBlip the source blip. * @param toBlip the target blip. */ public static void copyBlipContents(Blip fromBlip, Blip toBlip) { for (BlipContent blipContent: fromBlip.all().values()) { toBlip.append(blipContent); } for (Annotation annotation : fromBlip.getAnnotations()) { Range range = annotation.getRange(); toBlip.range(range.getStart() + 1, range.getEnd() + 1).annotate(annotation.getName(), annotation.getValue()); } }
/** * Creates a new shadow blip. * * Note that this method will not know the id of the new blip since the * server will generate it. * * @param blip the blip to create a new shadow blip for. */ private void createShadowBlip(Blip blip) { Blip newBlip = blip.continueThread(); newBlip.all().replace(blip.getContent()); newBlip.all().annotate(ECHOEY_ANNOTATION, blip.getBlipId()); }
/** * Creates and appends a new operation to the operation queue. * * @param opType the type of the operation. * @param blip the blip to apply this operation to. * @param parameters the parameters that should be added as a property of * the operation. * @return an instance of {@link OperationRequest} that represents the queued * operation. */ OperationRequest appendOperation(OperationType opType, Blip blip, Parameter... parameters) { return appendOperation(opType, blip.getWaveId(), blip.getWaveletId(), blip.getBlipId(), parameters); }
/** * Appends a message followed by a new line to the end of the blip. * * @param blip the blip. * @param msg the message. */ public static void appendLine(Blip blip, String msg) { blip.at(blip.getContent().length()).insert(msg + "\n"); }
public void testSerializeAndDeserialize() throws Exception { SortedMap<Integer, Element> elements = new TreeMap<Integer, Element>(); elements.put(14, new Gadget("http://a/b.xml")); Blip expectedBlip = new Blip("blip1", Arrays.asList("blip2", "blip3"), "\nhello world!\n another line", Arrays.asList("robot@test.com", "user@test.com"), "user@test.com", 1000l, 123l, null, null, Arrays.asList(new Annotation("key", "val", 2, 3)), elements, new ArrayList<String>(), wavelet); Blip actualBlip = Blip.deserialize(wavelet.getOperationQueue(), wavelet, expectedBlip.serialize()); assertEquals(expectedBlip.getWaveId(), actualBlip.getWaveId()); assertEquals(expectedBlip.getWaveletId(), actualBlip.getWaveletId()); assertEquals(expectedBlip.getBlipId(), actualBlip.getBlipId()); assertEquals(expectedBlip.getContent(), actualBlip.getContent()); assertEquals(expectedBlip.getCreator(), actualBlip.getCreator()); assertEquals(expectedBlip.getLastModifiedTime(), actualBlip.getLastModifiedTime()); assertEquals(expectedBlip.getParentBlipId(), actualBlip.getParentBlipId()); assertEquals(expectedBlip.getVersion(), actualBlip.getVersion()); assertEquals(expectedBlip.getContributors(), actualBlip.getContributors()); assertEquals(expectedBlip.getChildBlipIds(), actualBlip.getChildBlipIds()); assertEquals(expectedBlip.getElements().keySet(), actualBlip.getElements().keySet()); assertEquals(expectedBlip.getAnnotations().size(), actualBlip.getAnnotations().size()); }
public void testReplaceInsideAnnotation() { Blip blip = newBlip("\nFoo bar.", Arrays.asList(new Annotation(ANNOTATION_KEY, "bold", 1, 5))); // Replace "oo" with "ooo". blip.range(2, 4).replace("ooo"); assertEquals("\nFooo bar.", blip.getContent()); assertEquals(new Range(1, 6), blip.getAnnotations().get(ANNOTATION_KEY).get(0).getRange()); // Replace "ooo" with "o". blip.range(2, 5).replace("o"); assertEquals("\nFo bar.", blip.getContent()); assertEquals(new Range(1, 4), blip.getAnnotations().get(ANNOTATION_KEY).get(0).getRange()); }
public void testElementHandling() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); int originalLength = blip.getContent().length(); String url = "http://www.test.com/image.png"; blip.append(new Image(url, 100, 100, "Cool pix.")); assertEquals(2, blip.getElements().size()); List<BlipContent> result = blip.all(ElementType.IMAGE, Image.restrictByUrl(url)).values(); assertEquals(1, result.size()); blip.at(1).insert("twelve chars"); assertTrue(blip.getContent().startsWith("\ntwelve charshello")); element = blip.at(originalLength + 12).value().asElement(); assertTrue(element instanceof Image); blip.first("twelve ").delete(); assertTrue(blip.getContent().startsWith("\nchars")); element = blip.at(originalLength + 12 - "twelve ".length()).value().asElement(); assertTrue(element instanceof Image); blip.first("chars").replace(new Image(url, 200, 200, "Yet another cool pix.")); assertEquals(3, blip.getElements().size()); assertTrue(blip.getContent().startsWith("\n hello")); element = blip.at(1).value().asElement(); assertTrue(element instanceof Image); assertEquals("Yet another cool pix.", ((Image) element).getCaption()); blip.all().delete(); blip.append(new Image(url, 100, 100, "Cool pix."));
public void testInlineBlip() throws Exception { Blip blip = newBlip("\n1234", Collections.<Annotation>emptyList()); assertEquals(-1, blip.getInlineBlipOffset()); Blip inlineBlip = blip.insertInlineBlip(3); assertTrue(blip.getChildBlipIds().contains(inlineBlip.getBlipId())); assertEquals(3, inlineBlip.getInlineBlipOffset()); assertEquals("\n12 34", blip.getContent()); assertEquals(ElementType.INLINE_BLIP, blip.getElements().get(3).getType()); }
public void testProxyFor() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); Blip proxiedBlip = blip.proxyFor("proxyuser"); assertEquals(blip.getBlipId(), proxiedBlip.getBlipId()); assertEquals(blip.getWaveId(), proxiedBlip.getWaveId()); assertEquals(blip.getWaveletId(), proxiedBlip.getWaveletId()); proxiedBlip.reply(); List<OperationRequest> pendingOps = proxiedBlip.getOperationQueue().getPendingOperations(); assertEquals(1, pendingOps.size()); assertEquals("proxyuser", pendingOps.get(0).getParameter(ParamsProperty.PROXYING_FOR)); }
if (blip.length() == 0 && (start != 0 || end != 0)) { throw new IndexOutOfBoundsException("Start and end have to be 0 for empty blip."); } else if (start < 0 || end < 1) { throw new IndexOutOfBoundsException("Position outside the blip."); } else if ((start >= blip.length() || end > blip.length()) && modifyHow != ModifyHow.INSERT) { throw new IndexOutOfBoundsException("Position outside the blip."); } else if (start > blip.length() && modifyHow == ModifyHow.INSERT) { throw new IndexOutOfBoundsException("Position outside the blip."); } else if (start >= end){ if (end - start == 1 && blip.getElements().containsKey(start)) { source = blip.getElements().get(start); } else { source = Plaintext.of(blip.getContent().substring(start, end)); blip.getElements().subMap(start, end).keySet().iterator(); while(elementIterator.hasNext()) { elementIterator.next(); blip.deleteAnnotations(start, end); blip.shift(end, start - end); iterator.shift(-1); blip.setContent(blip.getContent().substring(0, start) + blip.getContent().substring(end)); break; case ANNOTATE: Annotation annotation = Annotation.class.cast(next); blip.getAnnotations().add(annotation.getName(), annotation.getValue(), start, end);
public void testDocumentOperations() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); List<BlipContent> newLines = blip.all("\n").values(); assertEquals(2, newLines.size()); assertEquals(Arrays.asList(Plaintext.of("\n"), Plaintext.of("\n")), newLines); blip.first("world").replace("jupiter"); String[] bits = blip.getContent().split("\n"); assertEquals(3, bits.length); assertEquals("hello jupiter!", bits[1]); blip.range(2, 5).delete(); assertTrue(blip.getContent().startsWith("\nho jupiter!")); blip.first("ho").insertAfter("la"); assertTrue(blip.getContent().startsWith("\nhola jupiter!")); blip.at(3).insert(" "); assertTrue(blip.getContent().startsWith("\nho la jupiter!")); blip.all().delete(); blip.at(1).insert("world!"); blip.first("world").insert(new BlipContentFunction() { @Override public BlipContent call(BlipContent source) { return Plaintext.of("Hello " + source.getText().length() + " "); } }); assertEquals("\nHello 5 world!", blip.getContent()); }
public void testTextIteratorDeletingMatches() { Blip blip = new Blip("blip1", "\n1 1 1", null, null, wavelet); TextIterator iterator = new BlipIterator.TextIterator(blip, "1", -1); for (int i = 0; i < 3; ++i) { assertTrue(iterator.hasNext()); Range range = iterator.next(); assertEquals(i + 1, range.getStart()); blip.setContent(blip.getContent().substring(0, range.getStart()) + blip.getContent().substring(range.getEnd())); iterator.shift(-1); } assertFalse(iterator.hasNext()); assertEquals("\n ", blip.getContent()); }
public void testCreateChildOfBlip() throws Exception { Blip blip = mock(Blip.class); when(blip.getBlipId()).thenReturn("blip1"); when(blip.getWavelet()).thenReturn(wavelet); Map<String, Blip> blips = new HashMap<String, Blip>(); blips.put("blip1", blip); when(wavelet.getBlip("blip1")).thenReturn(blip); when(wavelet.getBlips()).thenReturn(blips); OperationQueue queue = new OperationQueue(); Blip newBlip = queue.createChildOfBlip(blip); assertEquals(blip.getBlipId(), newBlip.getParentBlipId()); assertEquals(blip, newBlip.getParentBlip()); assertEquals(2, blips.size()); } }
public void testSetTitle() throws Exception { when(rootBlip.getContent()).thenReturn("\nOld title\n\nContent"); wavelet.setTitle("New title"); verify(opQueue).setTitleOfWavelet(wavelet, "New title"); verify(rootBlip).setContent("\nNew title\n\nContent"); }
public void testBlipOperations() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); assertEquals(1, wavelet.getBlips().size()); Blip other = blip.reply(); other.append("hello world"); assertEquals("\nhello world", other.getContent()); assertEquals(2, wavelet.getBlips().size()); Blip inline = blip.insertInlineBlip(3); assertEquals("\n", inline.getContent()); assertEquals(3, wavelet.getBlips().size()); }
/** * Creates a child blip of another blip. * * @param blip the parent blip. * @return an instance of {@link Blip} that represents the new child blip. */ public Blip createChildOfBlip(Blip blip) { // Create a new thread. String tempBlipId = generateTempBlipId(blip.getWavelet()); Wavelet wavelet = blip.getWavelet(); BlipThread thread = new BlipThread(tempBlipId, -1, new ArrayList<String>(), wavelet.getBlips()); // Add the new thread to the blip and wavelet. blip.addThread(thread); wavelet.addThread(thread); // Create a new blip in the new thread. Blip newBlip = newBlip(blip.getWavelet(), "", blip.getBlipId(), tempBlipId, thread.getId()); appendOperation(OperationType.BLIP_CREATE_CHILD, blip, Parameter.of(ParamsProperty.BLIP_DATA, newBlip.serialize())); return newBlip; }
public void testDocumentModify() throws Exception { Blip blip = newBlip(ROOT_BLIP_ID, Arrays.asList(CHILD_BLIP_ID), null); blip.all().replace("a text with text and then some text"); assertEquals("\na text with text and then some text", blip.getContent()); blip.at(8).insert("text "); blip.all("text").replace("foo", "bar"); assertEquals("\na foo bar with foo and then some bar", blip.getContent()); }
public void testDeleteByBlip() throws Exception { Blip parentBlip = mock(Blip.class); when(parentBlip.getBlipId()).thenReturn("parentblipid"); Blip childBlip = mock(Blip.class); when(childBlip.getBlipId()).thenReturn("childblipid"); when(childBlip.getParentBlip()).thenReturn(parentBlip); Map<String, Blip> blips = new HashMap<String, Blip>(); blips.put("parentblipid", parentBlip); blips.put("childblipid", childBlip); Set<String> participants = new LinkedHashSet<String>(); participants.add("foo@bar.com"); Map<String, String> roles = new HashMap<String, String>(); Map<String, BlipThread> threads = new HashMap<String, BlipThread>(); wavelet = new Wavelet(WAVE_ID, WAVELET_ID, "foo@bar.com", 1l, 1l, "Hello world", "parentblipid", null, roles, participants, new HashMap<String, String>(), new LinkedHashSet<String>(), blips, threads, opQueue); assertEquals(2, wavelet.getBlips().size()); wavelet.delete(childBlip); assertEquals(1, wavelet.getBlips().size()); verify(parentBlip).deleteChildBlipId("childblipid"); verify(opQueue).deleteBlip(wavelet, "childblipid"); }