use of com.xpn.xwiki.web.XWikiServletRequestStub in project xwiki-platform by xwiki.
the class XWikiRequestCopier method copy.
@Override
public XWikiRequest copy(XWikiRequest originalRequest) throws CloneFailedException {
if (originalRequest == null) {
return null;
}
XWikiServletRequestStub dummyRequest = new XWikiServletRequestStub();
dummyRequest.setHost(originalRequest.getHeader("x-forwarded-host"));
dummyRequest.setScheme(originalRequest.getScheme());
dummyRequest.setContextPath(originalRequest.getContextPath());
dummyRequest.setrequestURL(originalRequest.getRequestURL());
dummyRequest.setServerName(originalRequest.getServerName());
return new XWikiServletRequest(dummyRequest);
}
use of com.xpn.xwiki.web.XWikiServletRequestStub in project xwiki-platform by xwiki.
the class DefaultXWikiStubContextProvider method initialize.
@Override
public void initialize(XWikiContext context) {
XWikiContext newContext = context.clone();
newContext.setCacheDuration(0);
newContext.setUserReference(null);
newContext.setLocale(null);
newContext.setWikiId(context.getMainXWiki());
// Cleanup
newContext.flushClassCache();
// So we force the dummy request with the current host
if (newContext.getRequest() != null) {
XWikiServletRequestStub initialRequest = new XWikiServletRequestStub();
initialRequest.setHost(newContext.getRequest().getHeader("x-forwarded-host"));
initialRequest.setScheme(newContext.getRequest().getScheme());
XWikiServletRequest request = new XWikiServletRequest(initialRequest);
newContext.setRequest(request);
}
// Get rid of the real response
if (newContext.getResponse() != null) {
XWikiServletResponseStub initialResponse = new XWikiServletResponseStub();
// anything to keep ?
XWikiServletResponse response = new XWikiServletResponse(initialResponse);
newContext.setResponse(response);
}
// We decide arbitrarily to use the Servlet URL Factory since it's the "standard" factory.
if (newContext.getURLFactory() != null) {
XWikiURLFactory urlf = newContext.getWiki().getURLFactoryService().createURLFactory(XWikiContext.MODE_SERVLET, context);
newContext.setURLFactory(urlf);
}
this.initialContext = newContext;
this.logger.debug("Stub context initialized.");
}
use of com.xpn.xwiki.web.XWikiServletRequestStub in project xwiki-platform by xwiki.
the class DefaultXWikiStubContextProvider method createStubContext.
@Override
public XWikiContext createStubContext() {
XWikiContext stubContext;
if (this.initialContext != null) {
stubContext = this.initialContext.clone();
// We make sure to not share the same Request instance with several threads
if (this.initialContext.getRequest() != null) {
XWikiServletRequestStub stubRequest = new XWikiServletRequestStub();
stubRequest.setHost(this.initialContext.getRequest().getHeader("x-forwarded-host"));
stubRequest.setScheme(this.initialContext.getRequest().getScheme());
XWikiServletRequest request = new XWikiServletRequest(stubRequest);
stubContext.setRequest(request);
}
// We make sure to not share the same Response instance with several threads
if (this.initialContext.getResponse() != null) {
XWikiServletResponseStub stubResponse = new XWikiServletResponseStub();
XWikiServletResponse response = new XWikiServletResponse(stubResponse);
stubContext.setResponse(response);
}
// We make sure to not share the same document instance with several threads
stubContext.setDoc(new XWikiDocument(this.defaultDocumentReferenceProvider.get()));
} else {
stubContext = null;
}
return stubContext;
}
use of com.xpn.xwiki.web.XWikiServletRequestStub 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 com.xpn.xwiki.web.XWikiServletRequestStub in project xwiki-platform by xwiki.
the class PageTest method setUpForPageTest.
/**
* Configures the various Components and their mocks with default values for page tests.
*
* @throws Exception in case of errors
*/
@Before
public void setUpForPageTest() throws Exception {
// Configure mocks from OldcoreRule
context = oldcore.getXWikiContext();
xwiki = oldcore.getSpyXWiki();
// We need this one because some component in its init creates a query...
when(oldcore.getQueryManager().createQuery(any(String.class), any(String.class))).thenReturn(mock(Query.class));
// Set up a fake Request
// Configure request so that $!request.outputSyntax" == 'plain
// Need to be executed before ecm.initialize() so that XWikiScriptContextInitializer will initialize the
// script context properly
request = new XWikiServletRequestStub();
request.setScheme("http");
context.setRequest(request);
response = new XWikiServletResponseStub();
context.setResponse(response);
ExecutionContextManager ecm = mocker.getInstance(ExecutionContextManager.class);
ecm.initialize(oldcore.getExecutionContext());
// Let the user have view access to all pages
when(oldcore.getMockRightService().hasAccessLevel(eq("view"), eq("XWiki.XWikiGuest"), any(), eq(context))).thenReturn(true);
// Set up URL Factory
URLFactorySetup.setUp(xwiki, context);
// Set up Localization
LocalizationSetup.setUp(mocker);
// Set up Skin Extensions
SkinExtensionSetup.setUp(xwiki, context);
}
Aggregations