@Test public void createAndExecuteJob() throws Exception { // Mocks WikiProvisioningJob provisioningJob = mock(WikiProvisioningJob.class); mocker.registerComponent(Job.class, "wikiprovisioning.test", provisioningJob); ExecutionContextManager executionContextManager = mock(ExecutionContextManager.class); mocker.registerComponent(ExecutionContextManager.class, executionContextManager); Execution execution = mock(Execution.class); mocker.registerComponent(Execution.class, execution); DocumentReference user = new DocumentReference("xwiki", "XWiki", "User"); when(xcontext.getUserReference()).thenReturn(user); // Execute WikiProvisioningJob job = mocker .getComponentUnderTest() .createAndExecuteJob("wikiid", "wikiprovisioning.test", "templateid"); // Verify // Id of the job. List<String> jobId = new ArrayList<String>(); jobId.add("wiki"); jobId.add("provisioning"); jobId.add("wikiprovisioning.test"); jobId.add("wikiid"); verify(provisioningJob) .initialize(eq(new WikiProvisioningJobRequest(jobId, "wikiid", "templateid", user))); Thread.sleep(100); verify(provisioningJob).run(); // getJobs also works assertEquals(mocker.getComponentUnderTest().getJob(jobId), job); }
@Test public void clearByDocRef() throws Exception { DocumentReference docRef = new DocumentReference("a", "b", "c"); when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c"); mocker.getComponentUnderTest().clear(docRef); verify(cache).remove("DOC:a:b.c"); }
@Test public void getAutoCompletionHintsWhenOnlyDollarSign() throws Exception { // Note that we create nested Velocity Context in order to verify that we support that in // getAutoCompletionHints Context innerContext = new VelocityContext(); innerContext.put("key1", "value1"); VelocityContext vcontext = new VelocityContext(innerContext); vcontext.put("key2", "value2"); setupMocks("$", vcontext); String velocity = "{{velocity}}$"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); // Verify we can get keys from the Velocity Context and its inner context too. We test this // since our // Velocity tools are put in an inner Velocity Context. assertEquals(5, hints.getHints().size()); // Also verifies that hints are sorted SortedSet<HintData> expected = new TreeSet<HintData>(); expected.addAll( Arrays.asList( new HintData("doc", "doc"), new HintData("key1", "key1"), new HintData("key2", "key2"), new HintData("sdoc", "sdoc"), new HintData("tdoc", "tdoc"))); assertEquals(expected, hints.getHints()); }
@Test public void testMissingParserException() throws Exception { thrown.expect(MissingParserException.class); thrown.expectMessage("Failed to find a parser for syntax [XWiki 2.1]"); thrown.expectCause(any(ComponentLookupException.class)); mocker.getComponentUnderTest().parse("", Syntax.XWIKI_2_1, DOCUMENT_REFERENCE); }
@Test public void getAutoCompletionHintsForMethodWhenJustAfterTheDot() throws Exception { setupMocks("$key.", createTestVelocityContext("key", new TestClass())); Hints expectedMethods = new Hints() .withHints( new HintData("doWork", "doWork(...) AncillaryTestClass"), new HintData("something", "something String"), new HintData("getSomething", "getSomething(...) String"), new HintData("method1", "method1(...)"), new HintData("method2", "method2(...) String")); setupMethodFinderMock(expectedMethods, "", TestClass.class); String velocity = "{{velocity}}$key."; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(5, hints.getHints().size()); // Verify methods are returned sorted SortedSet<HintData> expected = new TreeSet<HintData>(); expected.add(new HintData("doWork", "doWork(...) AncillaryTestClass")); expected.add(new HintData("getSomething", "getSomething(...) String")); expected.add(new HintData("method1", "method1(...)")); expected.add(new HintData("method2", "method2(...) String")); expected.add(new HintData("something", "something String")); assertEquals(expected, hints.getHints()); }
@Test public void putByDocRef() throws Exception { IconSet iconSet = new IconSet("key"); DocumentReference docRef = new DocumentReference("a", "b", "c"); when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c"); mocker.getComponentUnderTest().put(docRef, iconSet); verify(cache).set("DOC:a:b.c", iconSet); }
@Test public void getByNameAndWiki() throws Exception { IconSet iconSet = new IconSet("key"); when(cache.get("NAMED:6wikiId_key")).thenReturn(iconSet); IconSet result = mocker.getComponentUnderTest().get("key", "wikiId"); assertTrue(iconSet == result); }
@Test public void getByDocRef() throws Exception { IconSet iconSet = new IconSet("key"); DocumentReference docRef = new DocumentReference("a", "b", "c"); when(entityReferenceSerializer.serialize(docRef)).thenReturn("a:b.c"); when(cache.get("DOC:a:b.c")).thenReturn(iconSet); IconSet result = mocker.getComponentUnderTest().get(docRef); assertTrue(iconSet == result); }
@Test public void getAutoCompletionHintsWhenNoDollarSign() throws Exception { setupMocks("whatever", new VelocityContext()); String velocity = "{{velocity}}whatever"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(0, hints.getHints().size()); }
@Test public void getAutoCompletionHintsWhenInvalidAutoCompletion() throws Exception { setupMocks("$k ", createTestVelocityContext()); String velocity = "{{velocity}}$k "; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(0, hints.getHints().size()); }
@Test public void getSimpleDocument() throws Exception { // Mock // No objects (and no comments). when(mockDocument.getComments()).thenReturn(new Vector<BaseObject>()); when(mockDocument.getXObjects()).thenReturn(new HashMap<DocumentReference, List<BaseObject>>()); // Call DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); SolrInputDocument solrDocument = extractor.getSolrDocument(documentReference); // Assert and verify Assert.assertEquals( String.format("%s_%s", documentReferenceString, language), solrDocument.getFieldValue(Fields.ID)); Assert.assertEquals( documentReference.getWikiReference().getName(), solrDocument.getFieldValue(Fields.WIKI)); Assert.assertEquals( documentReference.getLastSpaceReference().getName(), solrDocument.getFieldValue(Fields.SPACE)); Assert.assertEquals(documentReference.getName(), solrDocument.getFieldValue(Fields.NAME)); Assert.assertEquals(language, solrDocument.getFieldValue(Fields.LANGUAGE)); Assert.assertEquals(hidden, solrDocument.getFieldValue(Fields.HIDDEN)); Assert.assertEquals(EntityType.DOCUMENT.name(), solrDocument.getFieldValue(Fields.TYPE)); Assert.assertEquals(documentReferenceLocalString, solrDocument.getFieldValue(Fields.FULLNAME)); Assert.assertEquals( title, solrDocument.getFieldValue( String.format(Fields.MULTILIGNUAL_FORMAT, Fields.TITLE, language))); Assert.assertEquals( renderedContent, solrDocument.getFieldValue( String.format(Fields.MULTILIGNUAL_FORMAT, Fields.DOCUMENT_CONTENT, language))); Assert.assertEquals(version, solrDocument.getFieldValue(Fields.VERSION)); Assert.assertEquals(authorString, solrDocument.getFieldValue(Fields.AUTHOR)); Assert.assertEquals(authorDisplay, solrDocument.getFieldValue(Fields.AUTHOR_DISPLAY)); Assert.assertEquals(creatorString, solrDocument.getFieldValue(Fields.CREATOR)); Assert.assertEquals(creatorDisplay, solrDocument.getFieldValue(Fields.CREATOR_DISPLAY)); Assert.assertEquals(creationDate, solrDocument.getFieldValue(Fields.CREATIONDATE)); Assert.assertEquals(date, solrDocument.get(Fields.DATE).getValue()); }
@Test public void getAutoCompletionHintsWhenDollarSignFollowedByCurlyBracketSymbol() throws Exception { setupMocks("${ke", createTestVelocityContext("key", "value", "otherKey", "otherValue")); String velocity = "{{velocity}}${ke"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); assertTrue(hints.getHints().contains(new HintData("key", "key"))); }
@Test public void getIdLanguageInDatabase() throws Exception { DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); // Call String id = extractor.getId(documentReference); // Assert and verify Assert.assertEquals("wiki:space.name_en", id); verify(mockDab, atMost(2)).getDocument(documentReference); verify(mockDocument, atMost(2)).getRealLanguage(); }
/** * See http://jira.xwiki.org/browse/WIKIEDITOR-18 * * @throws Exception */ @Test public void getAutoCompletionHintsWhenDollarSignFollowedBySomeNonMatchingLettersThenDot() throws Exception { setupMocks("$o.", createTestVelocityContext("key", "value")); String velocity = "{{velocity}}$o"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(0, hints.getHints().size()); }
@Test public void getAutoCompletionHintsWhenOnlyDollarBangAndCurlyBracketSigns() throws Exception { setupMocks("$!{", createTestVelocityContext("key", "value")); String velocity = "{{velocity}}$!{"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(4, hints.getHints().size()); assertTrue(hints.getHints().contains(new HintData("key", "key"))); }
@Test @Ignore("Not working yet") public void getAutoCompletionHintsWhenSetDoneAbove() throws Exception { String velocity = "#set ($mydoc = $key.doWork())\n$mydoc."; setupMocks(velocity, createTestVelocityContext("key", new TestClass())); String content = "{{velocity}}" + velocity; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(content.length(), "xwiki/2.0", content); assertEquals(2, hints.getHints().size()); }
@Test public void getIdLanguageInLocale() throws Exception { DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); // Locale provided in the reference DocumentReference reference = new DocumentReference("wiki", "space", "name", new Locale("en")); // Call String id = extractor.getId(reference); // Assert Assert.assertEquals("wiki:space.name_en", id); }
@Test public void getAutoCompletionHintsForMethodsWhenGetter() throws Exception { setupMocks("$key.s", createTestVelocityContext("key", new TestClass())); Hints expectedMethods = new Hints().withHints(new HintData("something", "something String")); setupMethodFinderMock(expectedMethods, "s", TestClass.class); String velocity = "{{velocity}}$key.s"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); }
@Test public void getAutoCompletionHintsWhenSeveralDollar() throws Exception { setupMocks("$var\n$key.", createTestVelocityContext("key", new TestClass())); Hints expectedMethods = new Hints().withHints(new HintData("method1", "method1")); setupMethodFinderMock(expectedMethods, "", TestClass.class); String velocity = "{{velocity}}$var\n$key."; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); assertEquals(new HintData("method1", "method1"), hints.getHints().first()); }
@Test public void getIdLanguageNotAvailable() throws Exception { DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); // Mock // Empty string returned as language. The default "en" will be used. when(mockDocument.getRealLanguage()).thenReturn(""); // Call String id = extractor.getId(documentReference); // Assert and verify Assert.assertEquals("wiki:space.name_en", id); verify(mockDab, times(1)).getDocument(documentReference); verify(mockDocument, times(1)).getRealLanguage(); }
@Test public void getAutoCompletionHintsForScriptServiceProperty() throws Exception { MockitoComponentMockingRule cm = mocker.getInstance(ComponentManager.class); AutoCompletionMethodFinder scriptServiceFinder = cm.registerMockComponent(AutoCompletionMethodFinder.class, "services"); ScriptServiceManager scriptServiceManager = mock(ScriptServiceManager.class); setupMocks("$services.t", createTestVelocityContext("services", scriptServiceManager)); when(scriptServiceFinder.findMethods(scriptServiceManager.getClass(), "t")) .thenReturn(new Hints().withHints(new HintData("test", "test"))); String velocity = "{{velocity}}$services.t"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); assertTrue(hints.getHints().contains(new HintData("test", "test"))); }
@Test public void initializeWhenError() throws Exception { DefaultIconSetCache cache = mocker.getComponentUnderTest(); CacheFactory cacheFactory = mock(CacheFactory.class); when(cacheManager.getCacheFactory()).thenReturn(cacheFactory); Exception exception = new CacheException("ERROR"); when(cacheFactory.newCache(any(CacheConfiguration.class))).thenThrow(exception); Exception exceptionCaught = null; try { cache.initialize(); } catch (InitializationException e) { exceptionCaught = e; } assertNotNull(exceptionCaught); assertEquals("Failed to initialize the IconSet Cache.", exceptionCaught.getMessage()); assertEquals(exception, exceptionCaught.getCause()); }
@Test public void getAutoCompletionHintsForMethodWhenThereIsContentAfterCursorWithCapitalLetters() throws Exception { setupMocks("$key.doW", createTestVelocityContext("key", new TestClass())); Hints expectedMethods = new Hints().withHints(new HintData("doWork", "doWork(...) AncillaryTestClass")); setupMethodFinderMock(expectedMethods, "doW", TestClass.class); String velocity = "{{velocity}}$key.doWork"; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length() - 3, "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); // Verify methods are returned sorted SortedSet<HintData> expected = new TreeSet<HintData>(); expected.add(new HintData("doWork", "doWork(...) AncillaryTestClass")); assertEquals(expected, hints.getHints()); }
@Test public void getAutoCompletionHintsForDeepChainedMethod() throws Exception { setupMocks("$key.doWork().method1().spl", createTestVelocityContext("key", new TestClass())); Hints expectedMethods = new Hints().withHints(new HintData("split", "split")); AutoCompletionMethodFinder methodFinder = mocker.getInstance(AutoCompletionMethodFinder.class); when(methodFinder.findMethodReturnTypes(TestClass.class, "doWork")) .thenReturn(Arrays.asList((Class) AncillaryTestClass.class)); when(methodFinder.findMethodReturnTypes(AncillaryTestClass.class, "method1")) .thenReturn(Arrays.asList((Class) String.class)); when(methodFinder.findMethods(String.class, "spl")).thenReturn(expectedMethods); String velocity = "{{velocity}}$key.doWork().method1()."; Hints hints = mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); assertEquals(new HintData("split", "split"), hints.getHints().first()); assertEquals(velocity.length() - "spl".length(), hints.getStartOffset()); }
@Before public void configure() throws Exception { factory = mocker.getComponentUnderTest(); setupTest(mocker); }
@Before public void setUp() throws Exception { super.setUp(); store = (XWikiHibernateStore) mocker.getComponentUnderTest(); }
@Test public void testAddingMetadataSource() throws Exception { XDOM xdom = mocker.getComponentUnderTest().parse("", Syntax.PLAIN_1_0, DOCUMENT_REFERENCE); assertThat((String) xdom.getMetaData().getMetaData(MetaData.SOURCE), equalTo(SOURCE)); }
@Test public void testNoMetadataSource() throws Exception { XDOM xdom = mocker.getComponentUnderTest().parse("", Syntax.PLAIN_1_0); assertThat(xdom.getMetaData().getMetaData(MetaData.SOURCE), nullValue()); }
@Test public void getDocumentWithObjects() throws Exception { DocumentReference commentsClassReference = new DocumentReference("wiki", "space", "commentsClass"); String commentContent = "This is a comment"; String commentAuthor = "wiki:space.commentAuthor"; Date commentDate = new Date(); // Adding a fake password field to the comments class just to test the branch in the code. String commentPassword = "******"; List<String> commentList = Arrays.asList("a", "list"); List<BaseProperty<EntityReference>> commentFields = new ArrayList<BaseProperty<EntityReference>>(); // Mock BaseProperty<EntityReference> mockCommentField = mock(BaseProperty.class); when(mockCommentField.getName()).thenReturn("comment"); when(mockCommentField.getValue()).thenReturn(commentContent); commentFields.add(mockCommentField); BaseProperty<EntityReference> mockAuthorField = mock(BaseProperty.class); when(mockAuthorField.getName()).thenReturn("author"); when(mockAuthorField.getValue()).thenReturn(commentAuthor); commentFields.add(mockAuthorField); BaseProperty<EntityReference> mockDateField = mock(BaseProperty.class); when(mockDateField.getName()).thenReturn("date"); when(mockDateField.getValue()).thenReturn(commentDate); commentFields.add(mockDateField); BaseProperty<EntityReference> mockPasswordField = mock(BaseProperty.class); when(mockPasswordField.getName()).thenReturn("password"); when(mockPasswordField.getValue()).thenReturn(commentPassword); commentFields.add(mockPasswordField); BaseProperty<EntityReference> mockListField = mock(BaseProperty.class); when(mockListField.getName()).thenReturn("list"); when(mockListField.getValue()).thenReturn(commentList); commentFields.add(mockListField); BaseClass mockXClass = mock(BaseClass.class); BaseObject mockComment = mock(BaseObject.class); // When handled as a comment Vector<BaseObject> comments = new Vector<BaseObject>(); comments.add(mockComment); when(mockDocument.getComments()).thenReturn(comments); when(mockComment.getStringValue("comment")).thenReturn(commentContent); when(mockComment.getStringValue("author")).thenReturn(commentAuthor); when(mockComment.getDateValue("date")).thenReturn(commentDate); // When handled as a general object HashMap<DocumentReference, List<BaseObject>> xObjects = new HashMap<DocumentReference, List<BaseObject>>(); xObjects.put(commentsClassReference, Arrays.asList(mockComment)); when(mockDocument.getXObjects()).thenReturn(xObjects); when(mockComment.getXClass(mockContext)).thenReturn(mockXClass); when(mockComment.getFieldList()).thenReturn(commentFields); PropertyClass passwordClass = mock(PasswordClass.class); when(mockXClass.get("password")).thenReturn(passwordClass); when(passwordClass.getClassType()).thenReturn("Password"); // Call DocumentSolrMetadataExtractor extractor = (DocumentSolrMetadataExtractor) mocker.getComponentUnderTest(); SolrInputDocument solrDocument = extractor.getSolrDocument(documentReference); // Assert and verify Assert.assertEquals( String.format("%s by %s on %s", commentContent, commentAuthor, commentDate), solrDocument.getFieldValue( String.format(Fields.MULTILIGNUAL_FORMAT, Fields.COMMENT, language))); Collection<Object> objectProperties = solrDocument.getFieldValues( String.format(Fields.MULTILIGNUAL_FORMAT, Fields.OBJECT_CONTENT, language)); MatcherAssert.assertThat( objectProperties, Matchers.containsInAnyOrder( (Object) ("comment:" + commentContent), (Object) ("author:" + commentAuthor), (Object) ("date:" + commentDate.toString()), (Object) ("list:" + commentList.get(0)), (Object) ("list:" + commentList.get(1)))); Assert.assertEquals(5, objectProperties.size()); }
@Test public void getJobWhenNoJob() throws Exception { List<String> jobId = new ArrayList<String>(); assertNull(mocker.getComponentUnderTest().getJob(jobId)); }