001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.tests;
020:
021: import java.io.File;
022: import java.io.FileWriter;
023: import java.util.LinkedList;
024: import java.util.List;
025: import org.junit.Assert;
026: import org.junit.Test;
027: import org.mactor.brokers.Message;
028: import org.mactor.brokers.MessageSubscriber;
029: import org.mactor.brokers.file.FileMessageBroker;
030: import org.mactor.framework.extensioninterface.MessageSelectorCommand;
031: import org.mactor.framework.spec.MessageBrokersConfig;
032: import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig;
033: import org.mactor.framework.spec.MessageBrokersConfig.MessageBrokerConfig.ChannelConfig;
034:
035: public class FileMessageBrokerTest {
036: @Test
037: public void testPublish() throws Exception {
038: MessageBrokerConfig config = MessageBrokersConfig
039: .readFromFile(
040: "config/mactor_sample_test_config/file-message-broker-config.xml")
041: .getMessageBrokers().get(0);
042: FileMessageBroker fb = new FileMessageBroker(config);
043: ChannelConfig cc = config.getChannelConfig("OutgoingOrder");
044: String expectedOutputDir = "/tmp/mactor/channels/OutgoingOrder"
045: .toLowerCase();
046: Assert.assertEquals(expectedOutputDir, cc.getValue("dir")
047: .toLowerCase());
048: Util.clearDir(expectedOutputDir);
049: Assert.assertNull("Output dir could not be cleared", Util
050: .getFirstFile(expectedOutputDir));
051: fb.publish("OutgoingOrder", Message
052: .createMessage("<x>test</x>"));
053: File f = Util.getFirstFile(expectedOutputDir);
054: Assert.assertNotNull("No output file found", f);
055: Assert.assertEquals("<x>test</x>", Message.createMessage(f)
056: .getContentDocument().getRootElement().asXML());
057: Util.clearDir(expectedOutputDir);
058: }
059:
060: public void testSubscribe() throws Exception {
061: MessageBrokerConfig config = MessageBrokersConfig
062: .readFromFile(
063: "config/mactor_sample_test_config/file-message-broker-config.xml")
064: .getMessageBrokers().get(0);
065: FileMessageBroker fb = new FileMessageBroker(config);
066: ChannelConfig cc = config
067: .getChannelConfig("IncomingOrderStatus");
068: String inputDir = "/tmp/mactor/channels/IncomingOrderStatus"
069: .toLowerCase();
070: Assert.assertEquals(inputDir, cc.getValue("dir").toLowerCase());
071: Util.clearDir(inputDir);
072: Assert.assertNull("Input dir could not be cleared", Util
073: .getFirstFile(inputDir));
074: TestMessageSubscriber sub = new TestMessageSubscriber();
075: fb.subscribe("IncomingOrderStatus", sub,
076: new TestMessageSelectorCommand());
077: FileWriter fw = new FileWriter(inputDir + "/test.xml");
078: fw.write("<x>input test</x>");
079: fw.close();
080: Thread.sleep(3000);
081: fb.unsubscribe("IncomingOrderStatus", sub);
082: Assert.assertEquals(1, sub.messages.size());
083: Assert.assertEquals("<x>input test</x>", sub.messages.get(0)
084: .getContentDocument().getRootElement().asXML());
085: // Util.clearDir(inputDir);
086: }
087:
088: public static class TestMessageSubscriber implements
089: MessageSubscriber {
090: List<Message> messages = new LinkedList<Message>();
091:
092: public Message onMessage(Message message) {
093: messages.add(message);
094: return null;
095: }
096: }
097:
098: public static class TestMessageSelectorCommand implements
099: MessageSelectorCommand {
100: public void setParams(List<String> params) {
101: }
102:
103: public boolean isAcceptableMessage(Message message) {
104: return true;
105: }
106: }
107: }
|