use of com.xpn.xwiki.util.XWikiStubContextProvider in project xwiki-platform by xwiki.
the class XWikiStubContextInitializerTest method testWithAndWithoutXWikiContext.
public void testWithAndWithoutXWikiContext() throws Exception {
XWikiContext xcontext = new XWikiContext();
xcontext.put("key", "value");
this.mockXWiki = mock(XWiki.class);
this.mockXWiki.stubs().method("prepareResources");
xcontext.setWiki((XWiki) this.mockXWiki.proxy());
ExecutionContext context = new ExecutionContext();
xcontext.declareInExecutionContext(context);
XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
stubContextProvider.initialize(xcontext);
final ExecutionContext daemonContext = new ExecutionContext();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
executionContextManager.initialize(daemonContext);
} catch (ExecutionContextException e) {
fail("Failed to initialize execution context: " + e.getStackTrace());
}
}
});
thread.start();
thread.join();
XWikiContext daemonXcontext = (XWikiContext) daemonContext.getProperty("xwikicontext");
assertNotNull(daemonXcontext);
assertNotSame(xcontext, daemonXcontext);
assertEquals("value", daemonXcontext.get("key"));
assertNotNull(daemonXcontext.getWiki());
}
use of com.xpn.xwiki.util.XWikiStubContextProvider in project xwiki-platform by xwiki.
the class AbstractBridgedComponentTestCase method setUp.
@Override
@Before
public 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");
// 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);
execution.getContext().setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.context);
XWikiStubContextProvider stubContextProvider = getComponentManager().getInstance(XWikiStubContextProvider.class);
stubContextProvider.initialize(this.context);
// 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 = (ServletEnvironment) getComponentManager().getInstance(Environment.class);
final ServletContext mockServletContext = getMockery().mock(ServletContext.class);
environment.setServletContext(mockServletContext);
getMockery().checking(new Expectations() {
{
allowing(mockServletContext).getResourceAsStream("/WEB-INF/cache/infinispan/config.xml");
will(returnValue(null));
allowing(mockServletContext).getResourceAsStream("/WEB-INF/xwiki.cfg");
will(returnValue(null));
allowing(mockServletContext).getAttribute("javax.servlet.context.tempdir");
will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
}
});
final CoreConfiguration mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
getMockery().checking(new Expectations() {
{
allowing(mockCoreConfiguration).getDefaultDocumentSyntax();
will(returnValue(Syntax.XWIKI_1_0));
}
});
}
use of com.xpn.xwiki.util.XWikiStubContextProvider in project xwiki-platform by xwiki.
the class XWiki method getMainXWiki.
/**
* @param wait true if the method should way for {@link XWiki} instance to be initialized
* @param context see {@link XWikiContext}
*/
public static XWiki getMainXWiki(boolean wait, XWikiContext context) throws XWikiException {
String xwikiname = DEFAULT_MAIN_WIKI;
context.setMainXWiki(xwikiname);
XWiki xwiki;
try {
XWikiEngineContext econtext = context.getEngineContext();
xwiki = (XWiki) econtext.getAttribute(xwikiname);
if (xwiki == null) {
// Start XWiki initialization
synchronized (XWiki.class) {
xwiki = (XWiki) econtext.getAttribute(xwikiname);
if (xwiki == null && job == null) {
job = Utils.getComponent((Type) Job.class, XWikiInitializerJob.JOBTYPE);
if (job.getStatus() == null) {
// "Pre-initialize" XWikiStubContextProvider so that XWiki initializer can find one
Utils.<XWikiStubContextProvider>getComponent(XWikiStubContextProvider.class).initialize(context);
job.startAsync();
}
}
}
// Wait until XWiki is initialized
if (wait) {
job.join();
xwiki = (XWiki) econtext.getAttribute(xwikiname);
}
}
context.setWiki(xwiki);
return xwiki;
} catch (Exception e) {
throw new XWikiException(XWikiException.MODULE_XWIKI, XWikiException.ERROR_XWIKI_INIT_FAILED, "Could not initialize main XWiki instance", e);
}
}
Aggregations