001: package org.objectweb.celtix;
002:
003: import java.util.List;
004:
005: import junit.framework.TestCase;
006:
007: import org.easymock.EasyMock;
008: import static org.easymock.EasyMock.isA;
009:
010: // test bus event handler
011: public class BusEventTest extends TestCase {
012: Bus bus;
013:
014: BusEventListener bel;
015: BusEventFilter bef;
016: BusEvent event;
017:
018: public void setUp() throws Exception {
019: bel = EasyMock.createMock(BusEventListener.class);
020: bef = EasyMock.createMock(BusEventFilter.class);
021: bus = Bus.init();
022: event = new BusEvent("Test for EventListener",
023: BusEvent.BUS_EVENT);
024: }
025:
026: public void tearDown() throws Exception {
027: Thread.sleep(100);
028: bus.shutdown(true);
029: }
030:
031: public void testBusSendEvent() throws BusException {
032: EasyMock.reset(bel);
033: EasyMock.reset(bef);
034: bef.isEventEnabled(isA(BusEvent.class));
035: EasyMock.expectLastCall().andReturn(true);
036: bel.processEvent(isA(BusEvent.class));
037: EasyMock.expectLastCall();
038: EasyMock.replay(bel);
039: EasyMock.replay(bef);
040:
041: bus.addListener(bel, bef);
042: bus.sendEvent(event);
043: bus.removeListener(bel);
044: // this event should not be called
045: bus.sendEvent(event);
046:
047: EasyMock.verify(bel);
048: EasyMock.verify(bef);
049: }
050:
051: public void testBusRemoveListener() throws BusException {
052: bus.addListener(bel, bef);
053:
054: bus.removeListener(bel);
055:
056: bus.sendEvent(new BusEvent("Test for EventListener",
057: BusEvent.BUS_EVENT));
058:
059: }
060:
061: // test for the get event cache
062: public void testBusEventCache() throws BusException {
063:
064: BusEventCache cache = bus.getEventCache();
065:
066: BusEvent event1 = new BusEvent("Test for EventCache",
067: BusEvent.BUS_EVENT);
068: BusEvent event2 = new BusEvent("Test for EventCache", "TEST");
069:
070: cache.flushEvents();
071:
072: bus.sendEvent(event);
073: bus.sendEvent(event1);
074: bus.sendEvent(event2);
075:
076: assertEquals("The event cache getEvents size is not correct",
077: 3, cache.getEvents().size());
078:
079: assertEquals("The event cache getEvents() size is not correct",
080: 1, cache.getEvents("TEST").size());
081:
082: List<BusEvent> events = cache.flushEvents("TEST");
083:
084: assertEquals(
085: "The event cache flushEvent TEST events size is not correct",
086: 1, events.size());
087:
088: assertEquals("The event cache getID is not correct", 0, events
089: .get(0).getID().compareTo("TEST"));
090:
091: assertEquals("The event cache getEvents size is not correct",
092: 2, cache.getEvents().size());
093:
094: cache.flushEvents(BusEvent.class);
095: assertEquals(
096: "After flush the event cache getEvents size is not correct",
097: 0, cache.getEvents().size());
098:
099: }
100:
101: }
|