Search in sources :

Example 1 with XWikiConfig

use of com.xpn.xwiki.XWikiConfig in project xwiki-platform by xwiki.

the class XWikiDocumentArchiveTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    this.mockXWiki = mock(XWiki.class);
    this.mockXWiki.stubs().method("getEncoding").will(returnValue("iso-8859-1"));
    this.mockXWiki.stubs().method("getConfig").will(returnValue(new XWikiConfig()));
    this.context = new XWikiContext();
    this.context.setWiki((XWiki) this.mockXWiki.proxy());
}
Also used : XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiConfig(com.xpn.xwiki.XWikiConfig)

Example 2 with XWikiConfig

use of com.xpn.xwiki.XWikiConfig 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());
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletURLFactory(com.xpn.xwiki.web.XWikiServletURLFactory) Invocation(org.jmock.core.Invocation) HashMap(java.util.HashMap) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) URL(java.net.URL) Mock(org.jmock.Mock) XWikiStoreInterface(com.xpn.xwiki.store.XWikiStoreInterface) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) CustomStub(org.jmock.core.stub.CustomStub) XWikiConfig(com.xpn.xwiki.XWikiConfig)

Example 3 with XWikiConfig

use of com.xpn.xwiki.XWikiConfig 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());
}
Also used : Invocation(org.jmock.core.Invocation) XWikiHibernateVersioningStore(com.xpn.xwiki.store.XWikiHibernateVersioningStore) ArrayList(java.util.ArrayList) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) 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) Iterator(java.util.Iterator) XWikiHibernateRecycleBinStore(com.xpn.xwiki.store.XWikiHibernateRecycleBinStore) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) XWikiConfig(com.xpn.xwiki.XWikiConfig)

Example 4 with XWikiConfig

use of com.xpn.xwiki.XWikiConfig in project xwiki-platform by xwiki.

the class OldCoreHelper method createXWikiContext.

/**
 * @param wikiId id of the wiki for which to prepare the XWiki Context (e.g. {@code xwiki})
 * @param hibernateConfig the Hibernate config fill containing the database definition (JDBC driver, username and
 *            password, etc)
 * @return a valid XWikiContext using the passed Hibernate configuration and passed database name
 * @throws Exception failed to initialize context.
 */
// TODO: Replace the Hibernate config file with a list of parameters required for the packaging operation
private XWikiContext createXWikiContext() throws Exception {
    Utils.setComponentManager(this.componentManager);
    this.xcontext = new XWikiContext();
    this.xcontext.put(ComponentManager.class.getName(), this.componentManager);
    // Initialize the Container fields (request, response, session).
    try {
        ExecutionContext econtext = new ExecutionContext();
        // Bridge with old XWiki Context, required for old code.
        this.xcontext.declareInExecutionContext(econtext);
        this.ecim.initialize(econtext);
    } catch (ExecutionContextException e) {
        throw new Exception("Failed to initialize Execution Context.", e);
    }
    this.xcontext.setWikiId(wikiId);
    this.xcontext.setMainXWiki(wikiId);
    // Use a dummy Request/Response even in daemon mode so that XWiki's initialization can create a Servlet URL
    // Factory and any code requiring those objects will work.
    this.xcontext.setRequest(new XWikiServletRequestStub());
    this.xcontext.setResponse(new XWikiServletResponseStub());
    // Use a dummy URL so that XWiki's initialization can create a Servlet URL Factory. We could also have
    // registered a custom XWikiURLFactory against XWikiURLFactoryService but it's more work.
    this.xcontext.setURL(new URL("http://localhost/xwiki/bin/DummyAction/DumySpace/DummyPage"));
    // Set a dummy Document in the context to act as the current document since when a document containing
    // objects is imported it'll generate Object diff events and the algorithm to compute an object diff
    // currently requires rendering object properties, which requires a current document in the context.
    this.xcontext.setDoc(new XWikiDocument(new DocumentReference(wikiId, "dummySpace", "dummyPage")));
    XWikiConfig config = new XWikiConfig();
    config.put("xwiki.store.class", "com.xpn.xwiki.store.XWikiHibernateStore");
    // The XWikiConfig object requires path to be in unix format (i.e. with forward slashes)
    String hibernateConfigInUnixFormat = hibernateConfig.getPath().replace('\\', '/');
    config.put("xwiki.store.hibernate.path", hibernateConfigInUnixFormat);
    config.put("xwiki.store.hibernate.updateschema", "1");
    // Enable backlinks so that when documents are imported their backlinks will be saved too
    config.put("xwiki.backlinks", "1");
    XWiki xwiki = new XWiki(config, this.xcontext, null, true);
    this.xcontext.setUserReference(new DocumentReference("xwiki", "XWiki", "superadmin"));
    try {
        this.xcontext.setURLFactory(new XWikiServletURLFactory(new URL("http://localhost:8080"), "xwiki/", "bin/"));
    } catch (MalformedURLException e) {
        // doesn't work with external code.
        throw new XWikiException(XWikiException.MODULE_XWIKI_PLUGINS, XWikiException.ERROR_XWIKI_UNKNOWN, "Failed to set up URL Factory", e);
    }
    // Trigger extensions that need to initialize the database (create classes, etc.)
    xwiki.initializeWiki(this.xcontext.getMainXWiki(), true, this.xcontext);
    return this.xcontext;
}
Also used : XWikiServletRequestStub(com.xpn.xwiki.web.XWikiServletRequestStub) XWikiServletURLFactory(com.xpn.xwiki.web.XWikiServletURLFactory) MalformedURLException(java.net.MalformedURLException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) MalformedURLException(java.net.MalformedURLException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ExecutionContextException(org.xwiki.context.ExecutionContextException) URL(java.net.URL) XWikiServletResponseStub(com.xpn.xwiki.web.XWikiServletResponseStub) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) EmbeddableComponentManager(org.xwiki.component.embed.EmbeddableComponentManager) ComponentManager(org.xwiki.component.manager.ComponentManager) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiConfig(com.xpn.xwiki.XWikiConfig) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with XWikiConfig

use of com.xpn.xwiki.XWikiConfig in project xwiki-platform by xwiki.

the class XWikiConfigurationService method getProperty.

public static synchronized String getProperty(String propertyKey, String defaultValue, ServletContext context) {
    if (config == null) {
        // Make XWikiRequestProcessor own configuration loader since XWiki of Configuration component are not
        // initialized at this time
        InputStream xwikicfgis = null;
        String configurationLocation;
        try {
            configurationLocation = XWiki.getConfigPath();
            // First try loading from a file.
            File f = new File(configurationLocation);
            try {
                if (f.exists()) {
                    xwikicfgis = new FileInputStream(f);
                }
            } catch (Exception e) {
                // Error loading the file. Most likely, the Security Manager prevented it.
                // We'll try loading it as a resource below.
                LOGGER.debug("Failed to load the file [" + configurationLocation + "] using direct " + "file access. The error was [" + e.getMessage() + "]. Trying to load it " + "as a resource using the Servlet Context...");
            }
            // Second, try loading it as a resource using the Servlet Context
            if (xwikicfgis == null) {
                if (context != null) {
                    xwikicfgis = context.getResourceAsStream(configurationLocation);
                    LOGGER.debug("Failed to load the file [" + configurationLocation + "] as a resource " + "using the Servlet Context. Trying to load it as classpath resource...");
                } else {
                    LOGGER.debug("No Servlet Context available. Trying to load it as classpath resource...");
                }
            }
            // Third, try loading it from the classloader used to load this current class
            if (xwikicfgis == null) {
                xwikicfgis = XWiki.class.getClassLoader().getResourceAsStream("xwiki.cfg");
            }
            config = new XWikiConfig(xwikicfgis);
        } catch (Exception e) {
            LOGGER.error("Faile to lod configuration", e);
            config = new XWikiConfig();
        }
    }
    return config.getProperty(propertyKey, defaultValue);
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) XWikiConfig(com.xpn.xwiki.XWikiConfig)

Aggregations

XWikiConfig (com.xpn.xwiki.XWikiConfig)7 XWikiContext (com.xpn.xwiki.XWikiContext)5 XWiki (com.xpn.xwiki.XWiki)4 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)4 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)3 Invocation (org.jmock.core.Invocation)3 CustomStub (org.jmock.core.stub.CustomStub)3 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)2 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)2 XWikiServletRequestStub (com.xpn.xwiki.web.XWikiServletRequestStub)2 XWikiServletURLFactory (com.xpn.xwiki.web.XWikiServletURLFactory)2 URL (java.net.URL)2 HashMap (java.util.HashMap)2 Mock (org.jmock.Mock)2 DocumentReference (org.xwiki.model.reference.DocumentReference)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocumentArchive (com.xpn.xwiki.doc.XWikiDocumentArchive)1 XWikiHibernateRecycleBinStore (com.xpn.xwiki.store.XWikiHibernateRecycleBinStore)1 XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)1 XWikiRightServiceImpl (com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl)1