Search in sources :

Example 1 with XWikiServletRequestStub

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);
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub)

Example 2 with XWikiServletRequestStub

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.");
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiURLFactory(com.xpn.xwiki.web.XWikiURLFactory) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 3 with XWikiServletRequestStub

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;
}
Also used : XWikiServletRequest(com.xpn.xwiki.web.XWikiServletRequest) XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiServletResponse(com.xpn.xwiki.web.XWikiServletResponse) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 4 with XWikiServletRequestStub

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());
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) Invocation(org.jmock.core.Invocation) XWikiHibernateVersioningStore(com.xpn.xwiki.store.XWikiHibernateVersioningStore) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) URL(java.net.URL) Mock(org.jmock.Mock) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) CustomStub(org.jmock.core.stub.CustomStub) BaseObject(com.xpn.xwiki.objects.BaseObject) PropertyClass(com.xpn.xwiki.objects.classes.PropertyClass) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 5 with XWikiServletRequestStub

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);
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) Query(org.xwiki.query.Query) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) Before(org.junit.Before)

Aggregations

XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)10 XWikiContext (com.xpn.xwiki.XWikiContext)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 XWikiServletResponseStub (com.xpn.xwiki.web.XWikiServletResponseStub)5 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)4 URL (java.net.URL)4 XWiki (com.xpn.xwiki.XWiki)2 XWikiConfig (com.xpn.xwiki.XWikiConfig)2 XWikiException (com.xpn.xwiki.XWikiException)2 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)2 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)2 XWikiServletURLFactory (com.xpn.xwiki.web.XWikiServletURLFactory)2 Mock (org.jmock.Mock)2 Invocation (org.jmock.core.Invocation)2 CustomStub (org.jmock.core.stub.CustomStub)2 Before (org.junit.Before)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 JobRequestContext (com.xpn.xwiki.job.JobRequestContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 PropertyClass (com.xpn.xwiki.objects.classes.PropertyClass)1