use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiDocumentTest method testBackupRestoreContextUpdatesVContext.
/**
* XWIKI-8025: XWikiDocument#backup/restoreContext doesn't update the reference to the Velocity context stored on
* the XWiki context
*/
public void testBackupRestoreContextUpdatesVContext() throws Exception {
final Execution execution = getComponentManager().getInstance(Execution.class);
this.mockVelocityManager.stubs().method("getVelocityContext").will(new CustomStub("Implements VelocityManager.getVelocityContext") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return (VelocityContext) execution.getContext().getProperty("velocityContext");
}
});
VelocityContext oldVelocityContext = new VelocityContext();
execution.getContext().setProperty("velocityContext", oldVelocityContext);
Map<String, Object> backup = new HashMap<String, Object>();
XWikiDocument.backupContext(backup, getContext());
VelocityContext newVelocityContext = (VelocityContext) execution.getContext().getProperty("velocityContext");
assertNotNull(newVelocityContext);
assertNotSame(oldVelocityContext, newVelocityContext);
assertSame(newVelocityContext, getContext().get("vcontext"));
XWikiDocument.restoreContext(backup, getContext());
assertSame(oldVelocityContext, execution.getContext().getProperty("velocityContext"));
assertSame(oldVelocityContext, getContext().get("vcontext"));
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiDocumentTest method registerComponents.
@Override
protected void registerComponents() throws Exception {
super.registerComponents();
// Mock xwiki.cfg
getComponentManager().registerComponent(MockConfigurationSource.getDescriptor("xwikicfg"), getConfigurationSource());
// Setup the mock Velocity engine.
this.mockVelocityManager = registerMockComponent(VelocityManager.class);
this.mockVelocityEngine = mock(VelocityEngine.class);
this.mockVelocityManager.stubs().method("getVelocityContext").will(returnValue(null));
this.mockVelocityManager.stubs().method("getVelocityEngine").will(returnValue(this.mockVelocityEngine.proxy()));
velocityEngineEvaluateStub = new CustomStub("Implements VelocityEngine.evaluate") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
// Output the given text without changes.
StringWriter writer = (StringWriter) invocation.parameterValues.get(1);
String text = (String) invocation.parameterValues.get(3);
writer.append(text);
return true;
}
};
this.mockVelocityEngine.stubs().method("evaluate").will(velocityEngineEvaluateStub);
this.mockVelocityEngine.stubs().method("startedUsingMacroNamespace");
this.mockVelocityEngine.stubs().method("stoppedUsingMacroNamespace");
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class ImportTest method setUp.
@Override
protected void setUp() throws Exception {
super.setUp();
this.pack = new Package();
this.xwiki = new XWiki();
getContext().setWiki(this.xwiki);
this.xwiki.setConfig(new XWikiConfig());
Mock mockLocalizationContext = registerMockComponent(LocalizationContext.class);
mockLocalizationContext.stubs().method("getCurrentLocale").will(returnValue(Locale.ROOT));
// mock a store that would also handle translations
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);
String documentKey = shallowDoc.getFullName();
if (!shallowDoc.getLanguage().equals("")) {
documentKey += "." + shallowDoc.getLanguage();
}
if (docs.containsKey(documentKey)) {
return docs.get(documentKey);
} 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) mockXWikiStore.proxy());
// if this is a translated document, append a language prefix
String documentKey = document.getFullName();
if (!document.getLanguage().equals("")) {
documentKey += "." + document.getLanguage();
}
docs.put(documentKey, 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);
// delete the document from the map
String documentKey = document.getFullName();
if (!document.getLanguage().equals("")) {
documentKey += "." + document.getLanguage();
}
docs.remove(documentKey);
return null;
}
});
this.mockXWikiStore.stubs().method("getTranslationList").will(new CustomStub("Implements XWikiStoreInterface.getTranslationList") {
@Override
public Object invoke(Invocation invocation) throws Throwable {
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
// search for this document in the map and return it's translations
List translationList = new ArrayList();
for (Iterator pairsIt = docs.entrySet().iterator(); pairsIt.hasNext(); ) {
Map.Entry currentEntry = (Map.Entry) pairsIt.next();
if (((String) currentEntry.getKey()).startsWith(document.getFullName()) && !((XWikiDocument) currentEntry.getValue()).getLanguage().equals("")) {
// yeeey, it's a translation
translationList.add(((XWikiDocument) currentEntry.getValue()).getLanguage());
}
}
return translationList;
}
});
this.mockXWikiStore.stubs().method("injectCustomMapping").will(returnValue(false));
this.mockRecycleBinStore = mock(XWikiHibernateRecycleBinStore.class, new Class[] { XWikiContext.class }, new Object[] { getContext() });
this.mockRecycleBinStore.stubs().method("saveToRecycleBin").will(VoidStub.INSTANCE);
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) mockXWikiStore.proxy());
this.xwiki.setRecycleBinStore((XWikiHibernateRecycleBinStore) this.mockRecycleBinStore.proxy());
this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy());
// mock the right service
this.mockRightService = mock(XWikiRightService.class);
this.mockRightService.stubs().method("checkAccess").will(returnValue(true));
this.mockRightService.stubs().method("hasWikiAdminRights").will(returnValue(true));
this.mockRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
this.xwiki.setRightService((XWikiRightService) this.mockRightService.proxy());
}
use of org.jmock.core.stub.CustomStub in project xwiki-platform by xwiki.
the class XWikiNotificationTest method setUp.
@Override
public void setUp() throws Exception {
super.setUp();
this.xwiki = new XWiki();
getContext().setWiki(this.xwiki);
Mock mockStore = mock(XWikiStoreInterface.class);
mockStore.expects(atLeastOnce()).method("saveXWikiDoc");
mockStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWiki.getDocument") {
public Object invoke(Invocation invocation) throws Throwable {
return invocation.parameterValues.get(0);
}
});
this.xwiki.setStore((XWikiStoreInterface) mockStore.proxy());
}
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.xwiki = new com.xpn.xwiki.XWiki();
getContext().setWiki(this.xwiki);
this.xwiki.setConfig(new XWikiConfig());
this.apiXWiki = new XWiki(this.xwiki, getContext());
this.mockXWikiStore = mock(XWikiHibernateStore.class, new java.lang.Class[] { com.xpn.xwiki.XWiki.class, XWikiContext.class }, new java.lang.Object[] { this.xwiki, getContext() });
this.mockXWikiStore.stubs().method("loadXWikiDoc").will(new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") {
@Override
public java.lang.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 java.lang.Object invoke(Invocation invocation) throws Throwable {
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0);
document.setNew(false);
document.setStore((XWikiStoreInterface) XWikiTest.this.mockXWikiStore.proxy());
document.setId(rand.nextLong());
XWikiTest.this.docs.put(document.getName(), document);
return null;
}
});
this.mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
this.mockXWikiVersioningStore = mock(XWikiHibernateVersioningStore.class, new java.lang.Class[] { com.xpn.xwiki.XWiki.class, XWikiContext.class }, new java.lang.Object[] { this.xwiki, getContext() });
this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(new XWikiDocumentArchive()));
this.mockXWikiVersioningStore.stubs().method("saveXWikiDocArchive").will(returnValue(null));
this.mockXWikiRightService = mock(XWikiRightServiceImpl.class, new java.lang.Class[] {}, new java.lang.Object[] {});
this.mockXWikiRightService.stubs().method("hasAccessLevel").will(returnValue(true));
this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
this.xwiki.setStore((XWikiStoreInterface) this.mockXWikiStore.proxy());
this.xwiki.setVersioningStore((XWikiVersioningStoreInterface) this.mockXWikiVersioningStore.proxy());
this.xwiki.setRightService((XWikiRightService) this.mockXWikiRightService.proxy());
getContext().setUser("Redtail");
this.apiDocument = new Document(new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis")), getContext());
this.apiDocument.getDocument().setCreator("c" + getContext().getUser());
this.apiDocument.getDocument().setAuthor("a" + getContext().getUser());
this.apiDocument.save();
getContext().setUser("Earth");
}
Aggregations