1 package org.jacorb.test.notification.queue; 2 3 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 import org.easymock.MockControl; 33 import org.jacorb.notification.interfaces.Message; 34 import org.jacorb.notification.queue.BoundedFifoEventQueue; 35 import org.jacorb.notification.queue.EventQueueOverflowStrategy; 36 37 40 41 public class BoundedFifoEventQueueTest extends TestCase 42 { 43 public BoundedFifoEventQueueTest(String name) 44 { 45 super(name); 46 } 47 48 private void addEventsToEventQueue(EventQueueOverflowStrategy strategy, List events) 49 { 50 BoundedFifoEventQueue queue = new BoundedFifoEventQueue(4, strategy); 51 52 Iterator i = events.iterator(); 53 54 while (i.hasNext()) 55 { 56 queue.put((Message) i.next()); 57 } 58 } 59 60 public void testFIFOOverflow() throws Exception 61 { 62 DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.FIFO); 63 64 List _events = new ArrayList (); 65 66 Message mockMessage1 = newMessage(); 67 68 Message mockMessage2 = newMessage(); 69 70 _events.add(mockMessage1); 71 72 _events.add(mockMessage2); 73 74 _events.add(newMessage()); 75 76 _events.add(newMessage()); 77 78 _events.add(newMessage()); 79 80 _events.add(newMessage()); 81 82 addEventsToEventQueue(strategy, _events); 83 84 assertEquals(2, strategy.getRemovedElements().size()); 85 86 assertTrue(strategy.getRemovedElements().contains(mockMessage1)); 87 88 assertTrue(strategy.getRemovedElements().contains(mockMessage2)); 89 } 90 91 public void testLIFOOverflow() throws Exception 92 { 93 DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.LIFO); 94 95 List _events = new ArrayList (); 96 97 _events.add(newMessage()); 98 99 _events.add(newMessage()); 100 101 _events.add(newMessage()); 102 103 Message e1 = newMessage(); 104 105 Message e2 = newMessage(); 106 107 _events.add(e1); 108 109 _events.add(e2); 110 111 _events.add(newMessage()); 112 113 addEventsToEventQueue(strategy, _events); 114 115 assertEquals(2, strategy.getRemovedElements().size()); 116 117 assertTrue(strategy.getRemovedElements().contains(e1)); 118 119 assertTrue(strategy.getRemovedElements().contains(e2)); 120 } 121 122 public void testGetAllClearsQueue() throws Exception 123 { 124 BoundedFifoEventQueue queue = new BoundedFifoEventQueue(10, EventQueueOverflowStrategy.LIFO); 125 126 assertEquals(0, queue.getAllMessages(false).length ); 127 128 Message m = newMessage(); 129 queue.put(m); 130 131 Message[] mesgs = queue.getAllMessages(false); 132 133 assertEquals(1, mesgs.length); 134 assertEquals(m, mesgs[0]); 135 136 assertEquals(0, queue.getAllMessages(false).length); 137 } 138 139 private Message newMessage() 140 { 141 MockControl controlMessage = MockControl.createControl(Message.class); 142 Message mockMessage = (Message) controlMessage.getMock(); 143 controlMessage.replay(); 144 return mockMessage; 145 } 146 147 public static Test suite() 148 { 149 TestSuite suite = new TestSuite(BoundedFifoEventQueueTest.class); 150 151 return suite; 152 } 153 } 154 155 | Popular Tags |