use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
this.document = new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis"));
getContext().setRequest(new XWikiServletRequestStub());
getContext().setURL(new URL("http://localhost:8080/xwiki/bin/view/MilkyWay/Fidis"));
Mock mockLocalizationContext = registerMockComponent(LocalizationContext.class);
mockLocalizationContext.stubs().method("getCurrentLocale").will(returnValue(Locale.ROOT));
this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
this.mockWikiDescriptorManager.stubs().method("getCurrentWikiId").will(new CustomStub("Implements WikiDescriptorManager.getCurrentWikiId") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return getContext().getWikiId();
}
});
this.xwiki = new XWiki(new XWikiConfig(), getContext()) {
// Avoid all the error at XWiki initialization
@Override
public String getXWikiPreference(String prefname, String defaultValue, XWikiContext context) {
if (prefname.equals("plugins") || prefname.startsWith("macros_")) {
return defaultValue;
} else {
return super.getXWikiPreference(prefname, defaultValue, context);
}
}
};
getContext().setWiki(this.xwiki);
// Ensure that no Velocity Templates are going to be used when executing Velocity since otherwise
// the Velocity init would fail (since by default the macros.vm templates wouldn't be found as we're
// not providing it in our unit test resources).
getConfigurationSource().setProperty("xwiki.render.velocity.macrolist", "");
this.mockXWikiStore = mock(XWikiHibernateStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { this.xwiki, getContext() });
this.mockXWikiStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
if (XWikiTest.this.docs.containsKey(shallowDoc.getName())) {
return XWikiTest.this.docs.get(shallowDoc.getName());
} else {
return shallowDoc;
}
}
});
this.mockXWikiStore.stubs().method("saveXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
document.setNew(false);
document.setStore((XWikiStoreInterface) XWikiTest.this.mockXWikiStore.proxy());
XWikiTest.this.docs.put(document.getName(), document);
return null;
}
});
this.mockXWikiStore.stubs().method("deleteXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.deleteXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
XWikiTest.this.docs.remove(document.getName());
return null;
}
});
this.mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
this.mockXWikiStore.stubs().method("exists").will(returnValue(true));
this.mockXWikiVersioningStore = mock(XWikiHibernateVersioningStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { this.xwiki, getContext() });
this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
this.mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
this.xwiki.setStore((XWikiStoreInterface) this.mockXWikiStore.proxy());
this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) this.mockXWikiVersioningStore.proxy());
this.xwiki.saveDocument(this.document, getContext());
this.document.setCreator("Condor");
this.document.setAuthor("Albatross");
this.xwiki.saveDocument(this.document, getContext());
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiTest method testGetDocumentWithEntityReference.
public void testGetDocumentWithEntityReference() throws Exception {
Mock mockStore = registerMockComponent(XWikiStoreInterface.class);
this.xwiki.setStore((XWikiStoreInterface) mockStore.proxy());
mockStore.expects(atLeastOnce()).method("loadXWikiDoc").with(NOT_NULL, same(getContext())).will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return invocation.parameterValues.get(0);
}
});
DocumentReference documentReference = new DocumentReference("wiki", "Main", "WebHome");
WikiDescriptor mockWikiDescriptor = new WikiDescriptor("wiki", "wiki");
mockWikiDescriptor.setMainPageReference(documentReference);
this.mockWikiDescriptorManager.stubs().method("getById").with(same("wiki")).will(returnValue(mockWikiDescriptor));
assertEquals(documentReference, this.xwiki.getDocument(new WikiReference("wiki"), getContext()).getDocumentReference());
assertEquals(documentReference, this.xwiki.getDocument(new ObjectReference("object", documentReference), getContext()).getDocumentReference());
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class SyndEntryDocumentSourceTest method mockUp.
private void mockUp() throws Exception {
final Map<String, XWikiDocument> docs = new HashMap<String, XWikiDocument>();
final XWikiContext context = getContext();
// Set URL/Request
context.setRequest(new XWikiServletRequestStub());
context.setURL(new URL("http://localhost:8080/xwiki/bin/view/MilkyWay/Fidis"));
final XWiki xwiki = new XWiki(new XWikiConfig(), context) {
@Override
public String getXWikiPreference(String prefname, String defaultValue, XWikiContext context) {
return defaultValue;
}
};
context.setURLFactory(new XWikiServletURLFactory(new URL("http://www.xwiki.org/"), "xwiki/", "bin/"));
final Mock mockXWikiStore = mock(XWikiHibernateStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { xwiki, context });
mockXWikiStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
if (docs.containsKey(shallowDoc.getName())) {
return docs.get(shallowDoc.getName());
} else {
return shallowDoc;
}
}
});
mockXWikiStore.stubs().method("saveXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
document.setNew(false);
document.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
docs.put(document.getName(), document);
return null;
}
});
mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
mockXWikiStore.stubs().method("exists").will(returnValue(false));
final Mock mockXWikiVersioningStore = mock(XWikiHibernateVersioningStore.class, new Class[] { XWiki.class, XWikiContext.class }, new Object[] { xwiki, context });
mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy());
final Mock mockXWikiRightsService = mock(XWikiRightServiceImpl.class, new Class[] {}, new Object[] {});
mockXWikiRightsService.stubs().method("hasAccessLevel").will(new CustomStub("Implements XWikiRightService.hasAccessLevel") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
// String right = (String) invocation.parameterValues.get(0);
String user = (String) invocation.parameterValues.get(1);
// we give access to all the users with an even name length
return new Boolean(user.length() % 2 == 0);
}
});
xwiki.setRightService((XWikiRightService) mockXWikiRightsService.proxy());
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class AbstractBridgedXWikiComponentTestCase method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
// Statically store the component manager in {@link Utils} to be able to access it without
// the context.
Utils.setComponentManager(getComponentManager());
this.context = new XWikiContext();
this.context.setWikiId("xwiki");
this.context.setMainXWiki("xwiki");
// Make sure response.encodeURL() calls don't fail
Mock xwikiResponse = mock(XWikiResponse.class);
xwikiResponse.stubs().method("setLocale");
xwikiResponse.stubs().method("encodeURL").will(new CustomStub("Implements XWikiResponse.encodeURL") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return invocation.parameterValues.get(0);
}
});
this.context.setResponse((XWikiResponse) xwikiResponse.proxy());
// We need to initialize the Component Manager so that the components can be looked up
getContext().put(ComponentManager.class.getName(), getComponentManager());
// Bridge with old XWiki Context, required for old code.
Execution execution = getComponentManager().getInstance(Execution.class);
this.context.declareInExecutionContext(execution.getContext());
XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
stubContextProvider.initialize(this.context);
// Bridge with XWiki Context Provider, required by newer code.
Mock mockContextProvider = mock(Provider.class);
mockContextProvider.stubs().method("get").will(returnValue(this.context));
DefaultComponentDescriptor<Provider<XWikiContext>> contextProviderDescriptor = new DefaultComponentDescriptor<Provider<XWikiContext>>();
contextProviderDescriptor.setRoleType(new DefaultParameterizedType(null, Provider.class, XWikiContext.class));
contextProviderDescriptor.setRoleHint("default");
getComponentManager().registerComponent(contextProviderDescriptor, (Provider<XWikiContext>) mockContextProvider.proxy());
// Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
// correctly with a Servlet Context.
ServletEnvironment environment = getComponentManager().getInstance(Environment.class);
Mock mockServletContext = mock(ServletContext.class);
environment.setServletContext((ServletContext) mockServletContext.proxy());
mockServletContext.stubs().method("getResourceAsStream").will(returnValue(null));
mockServletContext.stubs().method("getResource").will(returnValue(null));
mockServletContext.stubs().method("getAttribute").with(eq("javax.servlet.context.tempdir")).will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
File testDirectory = new File("target/test-" + new Date().getTime());
this.temporaryDirectory = new File(testDirectory, "temporary-dir");
this.permanentDirectory = new File(testDirectory, "permanent-dir");
environment.setTemporaryDirectory(this.temporaryDirectory);
environment.setPermanentDirectory(this.permanentDirectory);
Mock mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
mockCoreConfiguration.stubs().method("getDefaultDocumentSyntax").will(returnValue(Syntax.XWIKI_1_0));
this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
this.mockWikiDescriptorManager.stubs().method("getCurrentWikiId").will(new CustomStub("Implements WikiDescriptorManager.getCurrentWikiId") {
@Override
public String invoke(Invocation invocation) throws Throwable {
return getContext().getWikiId();
}
});
this.mockWikiDescriptorManager.stubs().method("getMainWikiId").will(new CustomStub("Implements WikiDescriptorManager.getMainWikiId") {
@Override
public String invoke(Invocation invocation) throws Throwable {
return getContext().getMainXWiki();
}
});
// In order not to create a cyclic dependency we have the platform-rendering-xwiki module (which contains
// XWikiWikiModel requires for oldcore testing) not depend on platform-rendering-configuration-default. As a
// consequence we need to provide a mock ExtendedRenderingConfiguration component as otherwise injecting
// WikiModel would fail (since XWikiWikiModel depends on ExtendedRenderingConfiguration).
registerMockComponent(ExtendedRenderingConfiguration.class);
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiDocumentRenderingTest method testGetRenderedContentCleansVelocityMacroCache.
/**
* See XWIKI-5277 for details.
*/
public void testGetRenderedContentCleansVelocityMacroCache() throws Exception {
// Make sure we start not in the rendering engine since this is what happens in real: a document is
// called by a template thus outside of the rendering engine.
getContext().remove("isInRenderingEngine");
// We display a text area since we know that rendering a text area will call getRenderedContent inside our top
// level getRenderedContent call, thus testing that velocity macros are not removed during nested calls to
// getRenderedContent.
this.baseObject.setLargeStringValue("area", "{{velocity}}#macro(testmacrocache)ok#end{{/velocity}}");
this.document.setContent("{{velocity}}$doc.display(\"area\")#testmacrocache{{/velocity}}");
this.document.setSyntax(Syntax.XWIKI_2_0);
// We need to put the current doc in the Velocity Context since it's normally set before the rendering is
// called in the execution flow.
VelocityManager originalVelocityManager = getComponentManager().getInstance(VelocityManager.class);
VelocityContext vcontext = originalVelocityManager.getVelocityContext();
vcontext.put("doc", new Document(this.document, getContext()));
// Register a Mock for the VelocityManager to bypass skin APIs that we would need to mock otherwise.
Mock mockVelocityManager = registerMockComponent(VelocityManager.class);
mockVelocityManager.stubs().method("getCurrentVelocityContext").will(returnValue(vcontext));
VelocityFactory velocityFactory = getComponentManager().getInstance(VelocityFactory.class);
VelocityEngine vengine = velocityFactory.createVelocityEngine("default", new Properties());
// Save the number of cached macro templates in the Velocity engine so that we can compare after the
// document is rendered.
JMXVelocityEngineMBean mbean = new JMXVelocityEngine(vengine);
int cachedMacroNamespaceSize = mbean.getTemplates().values().size();
assertTrue(cachedMacroNamespaceSize > 0);
mockVelocityManager.stubs().method("getVelocityEngine").will(returnValue(vengine));
mockVelocityManager.stubs().method("evaluate").will(new CustomStub("evaluate") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return vengine.evaluate(vcontext, (Writer) invocation.parameterValues.get(0), (String) invocation.parameterValues.get(1), (Reader) invocation.parameterValues.get(2));
}
});
// $doc.display will ask for the syntax of the current document so we need to mock it.
this.mockXWiki.stubs().method("getCurrentContentSyntaxId").with(eq("xwiki/2.0"), ANYTHING).will(returnValue("xwiki/2.0"));
// Verify that the macro located inside the TextArea has been taken into account when executing the doc's
// content.
assertEquals("<p>ok</p>", this.document.getRenderedContent(getContext()));
// Now verify that the Velocity Engine doesn't contain any more cached macro namespace to prove that
// getRenderedContent has correctly cleaned the Velocity macro cache.
assertEquals(cachedMacroNamespaceSize, mbean.getTemplates().values().size());
}
Aggregations