Search in sources :

Example 6 with Environment

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

the class JGroupsNetworkAdapter method loadChannelConfiguration.

/**
 * Load channel configuration.
 *
 * @param channelId the identifier of the channel
 * @return the channel configuration
 * @throws IOException failed to load configuration file
 */
private ProtocolStackConfigurator loadChannelConfiguration(String channelId) throws IOException {
    String channelFile = channelId + ".xml";
    String path = "/WEB-INF/" + CONFIGURATION_PATH + channelFile;
    InputStream is = null;
    try {
        Environment environment = this.componentManager.getInstance(Environment.class);
        is = environment.getResourceAsStream(path);
    } catch (ComponentLookupException e) {
        // Environment not found, continue by fallbacking on JGroups's standard configuration.
        this.logger.debug("Failed to lookup the Environment component.", e);
    }
    if (is == null) {
        // Fallback on JGroups standard configuration locations
        is = ConfiguratorFactory.getConfigStream(channelFile);
        if (is == null && !Global.DEFAULT_PROTOCOL_STACK.equals(channelFile)) {
            // Fallback on default JGroups configuration
            is = ConfiguratorFactory.getConfigStream(Global.DEFAULT_PROTOCOL_STACK);
        }
    }
    return XmlConfigurator.getInstance(is);
}
Also used : InputStream(java.io.InputStream) Environment(org.xwiki.environment.Environment) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 7 with Environment

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

the class TempResourceActionTest method setUp.

@Before
public void setUp() throws Exception {
    base = new File(getClass().getResource("/").toURI());
    // Configure Servlet Environment defined in AbstractBridgedComponentTestCase so that it returns a good
    // temporary directory
    Environment environment = oldcore.getMocker().registerMockComponent(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(base);
    action = new TempResourceAction();
}
Also used : Environment(org.xwiki.environment.Environment) TemporaryFile(com.xpn.xwiki.internal.file.TemporaryFile) File(java.io.File) Before(org.junit.Before)

Example 8 with Environment

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

the class SerializedFilesMimeMessageIteratorTest method createMessageWhenFileNoLongerExists.

/**
 * Error that can happen if the file has been locally deleted between the time the time the user executes a
 * resend and the time the Mail Sender Thread reaches that file for processing (i.e. deserializing it).
 */
@Test
public void createMessageWhenFileNoLongerExists() throws Exception {
    Environment environment = mock(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
    // Create a serialized file before the iterator is initialized
    String mailID = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    createSerializedMessage(mailID);
    SerializedFilesMimeMessageIterator iterator = new SerializedFilesMimeMessageIterator(this.batchId, Collections.<String, Object>emptyMap(), componentManager);
    // Remove the file before next() is called to generate the error
    File messageFile = new File(this.batchDirectory, URLEncoder.encode(mailID, "UTF-8"));
    messageFile.delete();
    MimeMessage message = iterator.next();
    // Verify that:
    // 1) the returned message is null since there was an error
    // 2) that the log contains the error
    assertNull(message);
    assertEquals("Failed to create Mime Message", this.logRule.getMessage(0));
}
Also used : MimeMessage(javax.mail.internet.MimeMessage) ComponentManager(org.xwiki.component.manager.ComponentManager) Environment(org.xwiki.environment.Environment) File(java.io.File) Test(org.junit.Test)

Example 9 with Environment

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

the class SerializedFilesMimeMessageIteratorTest method createMessage.

@Test
public void createMessage() throws Exception {
    String mailID1 = "<1128820400.0.1419205781342.JavaMail.contact@xwiki.org>";
    String mailID2 = "<1128820400.1.1419205781342.JavaMail.contact@xwiki.org>";
    String mailID3 = "<1128820400.2.1419205781342.JavaMail.contact@xwiki.org>";
    createSerializedMessage(mailID1);
    createSerializedMessage(mailID2);
    createSerializedMessage(mailID3);
    Map<String, Object> parameters = new HashMap<>();
    parameters.put("parameters", Collections.EMPTY_MAP);
    Environment environment = mock(Environment.class);
    when(environment.getPermanentDirectory()).thenReturn(new File(TEMPORARY_DIRECTORY));
    ComponentManager componentManager = mock(ComponentManager.class);
    when(componentManager.getInstance(eq(Environment.class))).thenReturn(environment);
    SerializedFilesMimeMessageIterator iterator = new SerializedFilesMimeMessageIterator(this.batchId, parameters, componentManager);
    ArrayList<String> listID = new ArrayList<>();
    listID.add(mailID1);
    listID.add(mailID2);
    listID.add(mailID3);
    assertTrue(iterator.hasNext());
    MimeMessage message1 = iterator.next();
    assertTrue(listID.contains(message1.getMessageID()));
    listID.remove(message1.getMessageID());
    assertTrue(iterator.hasNext());
    MimeMessage message2 = iterator.next();
    assertTrue(listID.contains(message2.getMessageID()));
    listID.remove(message1.getMessageID());
    assertTrue(iterator.hasNext());
    MimeMessage message3 = iterator.next();
    assertTrue(listID.contains(message2.getMessageID()));
    listID.remove(message3.getMessageID());
    assertFalse(iterator.hasNext());
}
Also used : HashMap(java.util.HashMap) MimeMessage(javax.mail.internet.MimeMessage) ComponentManager(org.xwiki.component.manager.ComponentManager) ArrayList(java.util.ArrayList) Environment(org.xwiki.environment.Environment) File(java.io.File) Test(org.junit.Test)

Example 10 with Environment

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

the class TemporaryChartImageWriterTest method getStorageLocation.

@Test
public void getStorageLocation() throws Exception {
    WikiReference currentWikiReference = new WikiReference("wiki");
    ModelContext modelContext = this.componentManager.getInstance(ModelContext.class);
    when(modelContext.getCurrentEntityReference()).thenReturn(currentWikiReference);
    Environment environment = this.componentManager.getInstance(Environment.class);
    when(environment.getTemporaryDirectory()).thenReturn(new File("/tmpdir"));
    File location = this.componentManager.getComponentUnderTest().getStorageLocation(new ImageId(new ChartMacroParameters()));
    Assert.assertTrue("Got: " + location.toString(), location.toString().matches("/tmpdir/temp/chart/wiki/space/page/.*\\.png"));
}
Also used : ModelContext(org.xwiki.model.ModelContext) ChartMacroParameters(org.xwiki.rendering.macro.chart.ChartMacroParameters) Environment(org.xwiki.environment.Environment) WikiReference(org.xwiki.model.reference.WikiReference) File(java.io.File) 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