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.AbstractBoundedEventQueue; 35 import org.jacorb.notification.queue.BoundedPriorityEventQueue; 36 import org.jacorb.notification.queue.MessageQueue; 37 import org.jacorb.notification.queue.EventQueueOverflowStrategy; 38 39 42 43 public class BoundedPriorityEventQueueTest extends TestCase 44 { 45 public BoundedPriorityEventQueueTest(String name) 46 { 47 super(name); 48 } 49 50 public static Test suite() throws Exception 51 { 52 return new TestSuite(BoundedPriorityEventQueueTest.class); 53 } 54 55 public void testPriorityOrder_ascendingInsert() throws Exception 56 { 57 BoundedPriorityEventQueue _queue = new BoundedPriorityEventQueue(20, 58 EventQueueOverflowStrategy.FIFO); 59 60 for (int x = 0; x < 10; ++x) 61 { 62 Message mockMessage = newMessage(x); 63 _queue.put(mockMessage); 64 } 65 66 for (int x = 9; x >= 0; --x) 67 { 68 Message _event = _queue.getMessage(false); 69 assertEquals(x, _event.getPriority()); 70 } 71 } 72 73 public void testPriorityOrder_descendingInsert() throws Exception 74 { 75 MessageQueue _queue = new BoundedPriorityEventQueue(20, EventQueueOverflowStrategy.FIFO); 76 77 for (int x = 0; x < 10; ++x) 78 { 79 int prio = 10 - x; 80 81 Message mockMessage = newMessage(prio); 82 _queue.put(mockMessage); 83 } 84 85 for (int x = 0; x < 10; ++x) 86 { 87 Message _event = _queue.getMessage(false); 88 assertEquals(10 - x, _event.getPriority()); 89 } 90 91 } 92 93 public void testFIFOOverflow() throws Exception 94 { 95 DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.FIFO); 96 97 List _events = new ArrayList (); 98 99 Message e1 = newMessage(); 100 Message e2 = newMessage(); 101 Message e3 = newMessage(); 102 Message e4 = newMessage(); 103 Message e5 = newMessage(); 104 105 _events.add(e1); 106 107 _events.add(e2); 108 109 _events.add(e3); 110 111 _events.add(e4); 112 113 _events.add(e5); 114 115 addEventsToEventQueue(strategy, _events); 116 117 assertEquals(1, strategy.getRemovedElements().size()); 118 119 assertTrue(strategy.getRemovedElements().contains(e1)); 120 } 121 122 public void testLIFOOverflow() throws Exception 123 { 124 DelegatingOverflowStrategy strategy = new DelegatingOverflowStrategy(EventQueueOverflowStrategy.LIFO); 125 126 List _events = new ArrayList (); 127 128 _events.add(newMessage()); 129 130 _events.add(newMessage()); 131 132 _events.add(newMessage()); 133 134 Message e1 = newMessage(); 135 136 Message e2 = newMessage(); 137 138 _events.add(e1); 139 140 _events.add(e2); 141 142 _events.add(newMessage()); 143 144 addEventsToEventQueue(strategy, _events); 145 146 assertEquals(2, strategy.getRemovedElements().size()); 147 148 assertTrue(strategy.getRemovedElements().contains(e1)); 149 150 assertTrue(strategy.getRemovedElements().contains(e2)); 151 } 152 153 public void testGetAllClearsQueue() throws Exception 154 { 155 BoundedPriorityEventQueue queue = new BoundedPriorityEventQueue(10, EventQueueOverflowStrategy.LEAST_PRIORITY); 156 157 Message m = newMessage(); 158 159 assertEquals(0, queue.getAllMessages(false).length); 160 161 queue.put(m); 162 163 Message[] mesgs = queue.getAllMessages(false); 164 165 assertEquals(1, mesgs.length); 166 assertEquals(m, mesgs[0]); 167 168 queue.getAllMessages(false); 169 170 assertEquals(0, queue.getAllMessages(false).length); 171 } 172 173 private void addEventsToEventQueue(EventQueueOverflowStrategy strategy, List events) 174 { 175 AbstractBoundedEventQueue queue = new BoundedPriorityEventQueue(4, strategy); 176 177 Iterator i = events.iterator(); 178 179 while (i.hasNext()) 180 { 181 queue.put((Message) i.next()); 182 } 183 } 184 185 private Message newMessage() 186 { 187 return newMessage(0); 188 } 189 190 private Message newMessage(int priority) 191 { 192 MockControl controlMessage = MockControl.createControl(Message.class); 193 Message mockMessage = (Message) controlMessage.getMock(); 194 mockMessage.getPriority(); 195 controlMessage.setDefaultReturnValue(priority); 196 controlMessage.replay(); 197 198 return mockMessage; 199 } 200 } | Popular Tags |