@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); }
@Before public void configure() throws Exception { Parser parser = mocker.registerMockComponent(Parser.class, Syntax.PLAIN_1_0.toIdString()); when(parser.parse(argThat(any(Reader.class)))) .thenReturn(new XDOM(Collections.<Block>emptyList())); EntityReferenceSerializer<String> serializer = mocker.getInstance(EntityReferenceSerializer.TYPE_STRING); when(serializer.serialize(DOCUMENT_REFERENCE)).thenReturn(SOURCE); }
private void setupMocks(String expectedContent, VelocityContext velocityContext) throws Exception { TargetContentLocator locator = mocker.getInstance(TargetContentLocator.class); when(locator.locate(anyString(), eq("xwiki/2.0"), anyInt())) .thenReturn( new TargetContent( expectedContent, expectedContent.length(), TargetContentType.VELOCITY)); VelocityManager velocityManager = mocker.getInstance(VelocityManager.class); when(velocityManager.getVelocityContext()).thenReturn(velocityContext); }
@Before public void setUp() throws Exception { cacheManager = mocker.getInstance(CacheManager.class); entityReferenceSerializer = mocker.getInstance( new DefaultParameterizedType(null, EntityReferenceSerializer.class, String.class)); cache = mock(Cache.class); CacheFactory cacheFactory = mock(CacheFactory.class); when(cacheManager.getCacheFactory()).thenReturn(cacheFactory); CacheConfiguration configuration = new CacheConfiguration("iconset"); when(cacheFactory.<IconSet>newCache(eq(configuration))).thenReturn(cache); }
@Test public void executeDeleteWikiStatementForPostgreSQLWhenInDatabaseMode() throws Exception { HibernateSessionFactory sessionFactory = mocker.getInstance(HibernateSessionFactory.class); when(sessionFactory.getConfiguration().getProperty("xwiki.virtual_mode")) .thenReturn("database"); Statement statement = mock(Statement.class); DatabaseProduct databaseProduct = DatabaseProduct.POSTGRESQL; store.executeDeleteWikiStatement(statement, databaseProduct, "schema"); verify(mocker.getMockedLogger()) .warn("Subwiki deletion not yet supported in Database mode for PostgreSQL"); verify(statement, never()).execute(any(String.class)); }
@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 testLocksAreReleasedOnLogout() throws Exception { // Capture the event listener. ObservationManager observationManager = getMocker().getInstance(ObservationManager.class); ArgumentCaptor<EventListener> eventListenerCaptor = ArgumentCaptor.forClass(EventListener.class); verify(observationManager).addListener(eventListenerCaptor.capture()); assertEquals("deleteLocksOnLogoutListener", eventListenerCaptor.getValue().getName()); Query query = mock(Query.class); when(session.createQuery("delete from XWikiLock as lock where lock.userName=:userName")) .thenReturn(query); when(context.getUserReference()) .thenReturn(new DocumentReference("xwiki", "XWiki", "LoggerOutter")); when(context.getUser()).thenReturn("XWiki.LoggerOutter"); // Fire the logout event. eventListenerCaptor.getValue().onEvent(new ActionExecutingEvent("logout"), null, context); verify(session, times(2)).setFlushMode(FlushMode.COMMIT); verify(query).setString("userName", "XWiki.LoggerOutter"); verify(query).executeUpdate(); verify(transaction).commit(); verify(session).close(); // setDatabase() is called for each transaction and that calls checkDatabase(). DataMigrationManager dataMigrationManager = mocker.getInstance(DataMigrationManager.class, "hibernate"); verify(dataMigrationManager).checkDatabase(); }
@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 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 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 extractWhenWikiDescriptorButEmptyServerName() throws Exception { setUpConfiguration(WikiNotFoundBehavior.REDIRECT_TO_MAIN_WIKI); WikiDescriptorManager wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); when(wikiDescriptorManager.getByAlias("someWiki")) .thenReturn(new WikiDescriptor("", "someWiki")); testAndAssert("http://localhost/xwiki/wiki/someWiki/view/Main/WebHome", "xwiki"); }
@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 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"))); }
public void setupTest(MockitoComponentMockingRule<CertificateFactory> mocker) throws Exception { // Decode keys once for all tests. if (v1CaCert == null) { BinaryStringEncoder base64encoder = mocker.getInstance(BinaryStringEncoder.class, "Base64"); v1CaCert = base64encoder.decode(V1_CA_CERT); v1Cert = base64encoder.decode(V1_CERT); v3CaCert = base64encoder.decode(V3_CA_CERT); v3InterCaCert = base64encoder.decode(V3_ITERCA_CERT); v3Cert = base64encoder.decode(V3_CERT); } }
@Test public void executeDeleteWikiStatementForPostgreSQLWhenInSchemaMode() throws Exception { HibernateSessionFactory sessionFactory = mocker.getInstance(HibernateSessionFactory.class); when(sessionFactory.getConfiguration().getProperty("xwiki.virtual_mode")).thenReturn("schema"); Statement statement = mock(Statement.class); DatabaseProduct databaseProduct = DatabaseProduct.POSTGRESQL; store.executeDeleteWikiStatement(statement, databaseProduct, "schema"); verify(statement).execute("DROP SCHEMA schema CASCADE"); }
@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 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 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 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()); }
@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 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"))); }
/** * 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 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(); }
@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 getAutoCompletionHintsForScriptServicePropertyWhenNestedCalls() throws Exception { MockitoComponentMockingRule cm = mocker.getInstance(ComponentManager.class); AutoCompletionMethodFinder scriptServiceFinder = cm.registerMockComponent(AutoCompletionMethodFinder.class, "services"); ScriptServiceManager scriptServiceManager = mock(ScriptServiceManager.class); setupMocks("$services.query.m", createTestVelocityContext("services", scriptServiceManager)); when(scriptServiceFinder.findMethodReturnTypes(scriptServiceManager.getClass(), "query")) .thenReturn(Arrays.asList((Class) TestClass.class)); AutoCompletionMethodFinder defaultMethodFinder = mocker.getInstance(AutoCompletionMethodFinder.class); when(defaultMethodFinder.findMethods(TestClass.class, "m")) .thenReturn(new Hints().withHints(new HintData("method", "method"))); String velocity = "{{velocity}}$services.query.m"; Hints hints = this.mocker .getComponentUnderTest() .getAutoCompletionHints(velocity.length(), "xwiki/2.0", velocity); assertEquals(1, hints.getHints().size()); assertTrue(hints.getHints().contains(new HintData("method", "method"))); }
@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()); }