@Override public Range next() { if (hitsLeft == 0) { return null; } hitsLeft--; String searchIn = apiView.apiContents(); int next = searchIn.indexOf(searchFor, from + 1); if (next == -1) { return null; } from = next; return new Range(from, from + searchFor.length()); }
public void testComposeDocUsingAppend() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello"); api.insert(1, new Image("id", "caption")); assertEquals(2, api.getElements().size()); assertEquals("\n hello", api.apiContents()); assertInSync(document, api); api.insert(api.apiContents().length(), " world"); assertEquals("\n hello world", api.apiContents()); assertInSync(document, api); api.insert(api.apiContents().length(), new Image("id", "caption")); assertInSync(document, api); api.insert(api.apiContents().length(), "!"); assertEquals("\n hello world !", api.apiContents()); assertInSync(document, api); }
/** * Assert that the api view and document are still in sync. */ private void assertInSync(Document document, ApiView api) { ApiView alt = new ApiView(document, mock(Wavelet.class)); assertEquals(alt.apiContents(), api.apiContents()); List<ElementInfo> apiElements = api.getElements(); List<ElementInfo> altElements = alt.getElements(); assertEquals(altElements.size(), apiElements.size()); for (int i = 0; i < apiElements.size(); i++) { ElementInfo altInfo = altElements.get(i); ElementInfo apiInfo = apiElements.get(i); assertEquals(altInfo.element.getType(), apiInfo.element.getType()); assertEquals(altInfo.apiPosition, apiInfo.apiPosition); assertEquals(altInfo.xmlPosition, apiInfo.xmlPosition); } } }
public void testInsertAfter() throws Exception { String toInsert = "insertedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.INSERT_AFTER, Collections.singletonList(toInsert), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("Content should be insterted after the first character", INITIAL_CONTENT.charAt(0) + toInsert + INITIAL_CONTENT.substring(1), after); }
public void testInsertAfter() throws Exception { String toInsert = "insertedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.INSERT_AFTER, Collections.singletonList(toInsert), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("Content should be insterted after the first character", INITIAL_CONTENT.charAt(0) + toInsert + INITIAL_CONTENT.substring(1), after); }
public void testStringAppend() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "world"); assertEquals("\nworld", api.apiContents()); assertInSync(document, api); api.insert(1, "hello "); assertEquals("\nhello world", api.apiContents()); assertInSync(document, api); }
public void testStringAppend() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "world"); assertEquals("\nworld", api.apiContents()); assertInSync(document, api); api.insert(1, "hello "); assertEquals("\nhello world", api.apiContents()); assertInSync(document, api); }
public void testDelete() throws Exception { OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.DELETE, NO_VALUES, NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("First character should be deleted", INITIAL_CONTENT.substring(1), after); }
public void testDeleteString() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "helllo"); api.delete(3, 4); assertEquals("\nhello", api.apiContents()); assertInSync(document, api); api.delete(0, 2); assertEquals("ello", api.apiContents()); assertInSync(document, api); }
public void testDeleteString() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "helllo"); api.delete(3, 4); assertEquals("\nhello", api.apiContents()); assertInSync(document, api); api.delete(0, 2); assertEquals("ello", api.apiContents()); assertInSync(document, api); }
public void testDelete() throws Exception { OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.DELETE, NO_VALUES, NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("First character should be deleted", INITIAL_CONTENT.substring(1), after); }
public void testReplace() throws Exception { String replacement = "replacedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.REPLACE, Collections.singletonList(replacement), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.RANGE, new Range(CONTENT_START_TEXT, CONTENT_START_TEXT + INITIAL_CONTENT.length()))); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("The entire text should be replaced", replacement, after); }
public void testReplace() throws Exception { String replacement = "replacedText"; OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.REPLACE, Collections.singletonList(replacement), NO_ANNOTATION_KEY, NO_ELEMENTS, NO_BUNDLED_ANNOTATIONS, false)), Parameter.of(ParamsProperty.RANGE, new Range(CONTENT_START_TEXT, CONTENT_START_TEXT + INITIAL_CONTENT.length()))); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String after = getApiView().apiContents().substring(1); assertEquals("The entire text should be replaced", replacement, after); }
public void testInsertTextWithNewline() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello\nworld"); assertEquals("\nhello\nworld", api.apiContents()); assertInSync(document, api); List<ElementInfo> elements = api.getElements(); assertEquals(2, elements.size()); assertEquals(ElementType.LINE, elements.get(0).element.getType()); assertEquals(ElementType.LINE, elements.get(1).element.getType()); }
public void testInsertAndDeleteElementInString() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello"); api.insert(3, new Image("id", "caption")); assertEquals("\nhe llo", api.apiContents()); assertInSync(document, api); api.delete(3, 5); assertEquals("\nhelo", api.apiContents()); assertInSync(document, api); }
public void testInsertTextWithNewline() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello\nworld"); assertEquals("\nhello\nworld", api.apiContents()); assertInSync(document, api); List<ElementInfo> elements = api.getElements(); assertEquals(2, elements.size()); assertEquals(ElementType.LINE, elements.get(0).element.getType()); assertEquals(ElementType.LINE, elements.get(1).element.getType()); }
public void testInsertAndDeleteElementInString() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "hello"); api.insert(3, new Image("id", "caption")); assertEquals("\nhe llo", api.apiContents()); assertInSync(document, api); api.delete(3, 5); assertEquals("\nhelo", api.apiContents()); assertInSync(document, api); }
public void testInsertBeforeElementThenDeleteElement() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "0123456789"); api.insert(4, new Image("id1", "caption")); api.insert(4, "4"); assertInSync(document, api); api.delete(5, 6); assertEquals(1, api.getElements().size()); assertEquals("\n01243456789", api.apiContents()); assertInSync(document, api); }
public void testInsertBeforeElementThenDeleteElement() { Document document = BasicFactories.documentProvider().parse(LineContainers.debugContainerWrap("")); ApiView api = new ApiView(document, mock(Wavelet.class)); api.insert(1, "0123456789"); api.insert(4, new Image("id1", "caption")); api.insert(4, "4"); assertInSync(document, api); api.delete(5, 6); assertEquals(1, api.getElements().size()); assertEquals("\n01243456789", api.apiContents()); assertInSync(document, api); }
public void testInsert() throws Exception { String toInsert = "insertedText"; // Insert a new piece of annotated text before the current text. OperationRequest operation = operationRequest(OperationType.DOCUMENT_MODIFY, rootBlipId, Parameter.of(ParamsProperty.MODIFY_ACTION, new DocumentModifyAction(ModifyHow.INSERT, Collections.singletonList(toInsert), NO_ANNOTATION_KEY, NO_ELEMENTS, BundledAnnotation.listOf(ANNOTATION_KEY, ANNOTATION_VALUE), false)), Parameter.of(ParamsProperty.INDEX, CONTENT_START_TEXT)); service.execute(operation, helper.getContext(), ALEX); // Cut off the /n String result = getApiView().apiContents().substring(1); assertEquals( "The result should start with the inserted text", toInsert + INITIAL_CONTENT, result); String annotation = getRootBlip().getContent().getAnnotation(CONTENT_START_XML, ANNOTATION_KEY); assertEquals("Expected the text to be annotated", ANNOTATION_VALUE, annotation); }