Search in sources :

Example 16 with Environment

use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.

the class EmbeddedSolrInstanceInitializationTest method setup.

@Before
public void setup() throws Exception {
    Environment mockEnvironment = this.mocker.registerMockComponent(Environment.class);
    when(mockEnvironment.getPermanentDirectory()).thenReturn(PERMANENT_DIRECTORY);
    FileUtils.deleteDirectory(PERMANENT_DIRECTORY);
    PERMANENT_DIRECTORY.mkdirs();
    this.mockXWikiProperties = this.mocker.registerMockComponent(ConfigurationSource.class, "xwikiproperties");
}
Also used : ConfigurationSource(org.xwiki.configuration.ConfigurationSource) Environment(org.xwiki.environment.Environment) Before(org.junit.Before)

Example 17 with Environment

use of org.xwiki.environment.Environment 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));
        }
    });
}
Also used : XWikiStubContextProvider(com.xpn.xwiki.util.XWikiStubContextProvider) Expectations(org.jmock.Expectations) Execution(org.xwiki.context.Execution) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) Environment(org.xwiki.environment.Environment) ServletContext(javax.servlet.ServletContext) ServletEnvironment(org.xwiki.environment.internal.ServletEnvironment) CoreConfiguration(com.xpn.xwiki.CoreConfiguration) File(java.io.File) Before(org.junit.Before)

Example 18 with Environment

use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.

the class LocalizationScriptServiceTest method setUp.

@Before
public void setUp() throws Exception {
    componentManager = mock(ComponentManager.class);
    Provider<ComponentManager> componentManagerProvider = mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(componentManagerProvider.get()).thenReturn(componentManager);
    renderer = mock(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString());
    when(componentManager.getInstance(BlockRenderer.class, Syntax.PLAIN_1_0.toIdString())).thenReturn(renderer);
    localizationContext = mocker.getInstance(LocalizationContext.class);
    localizationManager = mocker.getInstance(LocalizationManager.class);
    localizationScriptService = (LocalizationScriptService) mocker.getComponentUnderTest();
    translation = mock(Translation.class);
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            WikiPrinter printer = (WikiPrinter) invocation.getArguments()[1];
            printer.print("print result");
            return null;
        }
    }).when(renderer).render(eq(new WordBlock("message")), any(WikiPrinter.class));
    when(translation.render(Locale.ROOT, ArrayUtils.EMPTY_OBJECT_ARRAY)).thenReturn(new WordBlock("message"));
    when(localizationManager.getTranslation("key", Locale.ROOT)).thenReturn(translation);
    when(localizationContext.getCurrentLocale()).thenReturn(Locale.ROOT);
    environment = mocker.getInstance(Environment.class);
}
Also used : Translation(org.xwiki.localization.Translation) LocalizationContext(org.xwiki.localization.LocalizationContext) WordBlock(org.xwiki.rendering.block.WordBlock) WikiPrinter(org.xwiki.rendering.renderer.printer.WikiPrinter) Provider(javax.inject.Provider) BlockRenderer(org.xwiki.rendering.renderer.BlockRenderer) LocalizationManager(org.xwiki.localization.LocalizationManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Before(org.junit.Before)

Example 19 with Environment

use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.

the class XWikiAttachmentContent method getNewFileItem.

/**
 * @return a new FileItem for temporarily storing attachment content.
 * @since 4.2M3
 */
private static FileItem getNewFileItem() {
    final Environment env = Utils.getComponent(Environment.class);
    final File dir = new File(env.getTemporaryDirectory(), "attachment-cache");
    try {
        if (!dir.mkdirs() && !dir.exists()) {
            throw new UnexpectedException("Failed to create directory for attachments " + dir);
        }
        final DiskFileItem dfi = new DiskFileItem(null, null, false, null, 10000, dir);
        // This causes the temp file to be created.
        dfi.getOutputStream().close();
        return dfi;
    } catch (IOException e) {
        throw new UnexpectedException("Failed to create new attachment temporary file.", e);
    }
}
Also used : UnexpectedException(org.xwiki.store.UnexpectedException) Environment(org.xwiki.environment.Environment) IOException(java.io.IOException) File(java.io.File) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem)

Example 20 with Environment

use of org.xwiki.environment.Environment in project xwiki-platform by xwiki.

the class DefaultEntityResourceActionListerTest method listActions.

@Test
public void listActions() throws Exception {
    Environment environment = this.mocker.getInstance(Environment.class);
    when(environment.getResourceAsStream("/WEB-INF/struts-config.xml")).thenReturn(getClass().getClassLoader().getResourceAsStream("WEB-INF/struts-config.xml"));
    this.mocker.registerMockComponent(new DefaultParameterizedType(null, ResourceReferenceHandler.class, EntityResourceAction.class), "testaction");
    assertThat(this.mocker.getComponentUnderTest().listActions(), hasItems("view", "edit", "get", "testaction"));
}
Also used : ResourceReferenceHandler(org.xwiki.resource.ResourceReferenceHandler) EntityResourceAction(org.xwiki.resource.entity.EntityResourceAction) Environment(org.xwiki.environment.Environment) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Test(org.junit.Test)

Aggregations

Environment (org.xwiki.environment.Environment)21 File (java.io.File)15 Test (org.junit.Test)10 ServletEnvironment (org.xwiki.environment.internal.ServletEnvironment)6 Before (org.junit.Before)5 ComponentManager (org.xwiki.component.manager.ComponentManager)5 ServletContext (javax.servlet.ServletContext)4 Attachment (com.xpn.xwiki.api.Attachment)3 CoreConfiguration (com.xpn.xwiki.CoreConfiguration)2 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 MimeBodyPart (javax.mail.internet.MimeBodyPart)2 MimeMessage (javax.mail.internet.MimeMessage)2 Expectations (org.jmock.Expectations)2 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)2 WikiReference (org.xwiki.model.reference.WikiReference)2 XWiki (com.xpn.xwiki.XWiki)1