001: /*
002: * Bossa Workflow System
003: *
004: * $Id: NotificationBusTest.java,v 1.13 2004/01/30 22:45:50 gdvieira Exp $
005: *
006: * Copyright (C) 2003,2004 OpenBR Sistemas S/C Ltda.
007: *
008: * This file is part of Bossa.
009: *
010: * Bossa is free software; you can redistribute it and/or modify it
011: * under the terms of version 2 of the GNU General Public License as
012: * published by the Free Software Foundation.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public
020: * License along with this program; if not, write to the
021: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
022: * Boston, MA 02111-1307, USA.
023: */
024:
025: package com.bigbross.bossa.notify;
026:
027: import java.util.ArrayList;
028: import java.util.Date;
029: import java.util.HashMap;
030: import java.util.List;
031:
032: import junit.framework.TestCase;
033:
034: import com.bigbross.bossa.Bossa;
035: import com.bigbross.bossa.BossaFactory;
036: import com.bigbross.bossa.PersistenceException;
037: import com.bigbross.bossa.resource.Resource;
038: import com.bigbross.bossa.resource.ResourceManager;
039: import com.bigbross.bossa.wfnet.WFNetEvents;
040:
041: public class NotificationBusTest extends TestCase {
042:
043: private NotificationBus bus;
044:
045: public NotificationBusTest(String name) {
046: super (name);
047: }
048:
049: protected void setUp() {
050: List persistent = new ArrayList();
051: persistent.add(new GoodListener("persist", 0, null));
052: bus = new NotificationBus(null, persistent);
053: }
054:
055: public void testRegisterRemoveListener() {
056: assertTrue(bus.registerListener(new GoodListener("test1", 0,
057: null)));
058: assertFalse(bus.registerListener(new GoodListener("test1", 0,
059: null)));
060: bus.removeListener("test1");
061: assertTrue(bus.registerListener(new GoodListener("test1", 0,
062: null)));
063: assertTrue(bus.registerListener(new GoodListener("persist", 0,
064: null)));
065: }
066:
067: public void testNotify() {
068: TestListener theGood = new GoodListener("The Good", 0, null);
069: TestListener theBad = new BadListener("The Bad", 0, null);
070: assertTrue(bus.registerListener(theGood));
071: assertTrue(bus.registerListener(theBad));
072:
073: try {
074: bus.notifyEvent(new Event("event1", Event.WFNET_EVENT,
075: new HashMap(), new Date()));
076: } catch (Exception e) {
077: fail("This exception should not propagate here.");
078: }
079: assertEquals(1, theGood.runs());
080: assertEquals(1, theBad.runs());
081: }
082:
083: public void testImutableEvent() {
084: BadListener theBad = new BadListener("The Bad", 0, null);
085: assertTrue(bus.registerListener(theBad));
086: Event event = new Event("event1", Event.WFNET_EVENT,
087: new HashMap(), new Date());
088:
089: bus.notifyEvent(event);
090: assertEquals(1, theBad.runs());
091: assertNull(event.getAttributes().get("bad"));
092: assertTrue(theBad.wrongTime() != event.getTime().getTime());
093: }
094:
095: public void testFilterByType() {
096: TestListener theGood = new GoodListener("test1",
097: Event.RESOURCE_EVENT, null);
098: assertTrue(bus.registerListener(theGood));
099:
100: bus.notifyEvent(new Event("event1", Event.WFNET_EVENT,
101: new HashMap(), new Date()));
102: assertEquals(0, theGood.runs());
103: }
104:
105: public void testFilterByResource() throws Exception {
106: Bossa bossa = BossaFactory.transientBossa();
107: NotificationBus bus = bossa.getNotificationBus();
108: ResourceManager resourceManager = bossa.getResourceManager();
109:
110: Resource trumps = resourceManager.createResource("trumps");
111: Resource jdoe = resourceManager.createResource("joedoe");
112: Resource mdoe = resourceManager.createResource("marydoe");
113: trumps.include(jdoe);
114: trumps.include(mdoe);
115:
116: TestListener theGood = new GoodListener("test1", 0, jdoe);
117: assertTrue(bus.registerListener(theGood));
118: HashMap attrib = new HashMap();
119:
120: attrib.put(WFNetEvents.ATTRIB_RESOURCE_ID, trumps.getId());
121: bus.notifyEvent(new Event("event1", Event.WFNET_EVENT, attrib,
122: new Date()));
123: assertEquals(1, theGood.runs());
124:
125: attrib.put(WFNetEvents.ATTRIB_RESOURCE_ID, jdoe.getId());
126: bus.notifyEvent(new Event("event2", Event.WFNET_EVENT, attrib,
127: new Date()));
128: assertEquals(2, theGood.runs());
129:
130: attrib.put(WFNetEvents.ATTRIB_RESOURCE_ID, mdoe.getId());
131: bus.notifyEvent(new Event("event3", Event.WFNET_EVENT, attrib,
132: new Date()));
133: assertEquals(2, theGood.runs());
134: }
135:
136: public void testNotificationQueue() throws PersistenceException {
137: Bossa bossa = BossaFactory.transientBossa();
138: TestListener theGood = new GoodListener("test", 0, null);
139: assertTrue(bossa.getNotificationBus().registerListener(theGood));
140:
141: /* Creates a concrete anonymous inner class */
142: NotificationQueue queue = new NotificationQueue() {
143: };
144: queue.addEvent(new Event("event1", Event.WFNET_EVENT,
145: new HashMap(), new Date()));
146: queue.addEvent(new Event("event2", Event.WFNET_EVENT,
147: new HashMap(), new Date()));
148: queue.notifyAll(bossa);
149: assertEquals(2, theGood.runs());
150: queue.notifyAll(bossa);
151: assertEquals(2, theGood.runs());
152: }
153: }
|