1 8 9 package foxtrot.test; 10 11 import java.lang.reflect.Method ; 12 13 import javax.swing.SwingUtilities ; 14 15 import foxtrot.EventPump; 16 import foxtrot.Task; 17 import foxtrot.pumps.QueueEventPump; 18 import foxtrot.pumps.SunJDK14ConditionalEventPump; 19 20 23 public class EventPumpTest extends FoxtrotTestCase 24 { 25 public EventPumpTest(String s) 26 { 27 super(s); 28 } 29 30 public void testJDK13QueueEventPump() throws Exception 31 { 32 if (!isJRE14()) 33 { 34 QueueEventPump pump = new QueueEventPump(); 35 testEventPump(pump); 36 } 37 } 38 39 public void testSunJDK14ConditionalEventPump() throws Exception 40 { 41 if (isJRE14()) 42 { 43 SunJDK14ConditionalEventPump pump = new SunJDK14ConditionalEventPump(); 44 testEventPump(pump); 45 } 46 } 47 48 private void testEventPump(EventPump pump) throws Exception 49 { 50 testPumpEventsBlocks(pump); 51 testPumpEventsDequeues(pump); 52 tesPumpEventsOnThrowException(pump); 53 tesPumpEventsOnThrowError(pump); 54 } 55 56 59 private void testPumpEventsBlocks(final EventPump pump) throws Exception 60 { 61 invokeTest(null, new Runnable () 62 { 63 public void run() 64 { 65 final Task task = new Task() 66 { 67 public Object run() throws Exception 68 { 69 return null; 70 } 71 }; 72 73 final long delay = 5000; 74 75 SwingUtilities.invokeLater(new Runnable () 77 { 78 public void run() 79 { 80 sleep(delay); 81 setTaskCompleted(task); 82 } 83 }); 84 85 long start = System.currentTimeMillis(); 87 pump.pumpEvents(task); 88 long stop = System.currentTimeMillis(); 89 90 long elapsed = stop - start; 91 if (elapsed <= delay) fail("Blocking is not effective: expecting " + delay + ", blocked for only " + elapsed); 92 } 93 }, null); 94 } 95 96 99 private void testPumpEventsDequeues(final EventPump pump) throws Exception 100 { 101 invokeTest(null, new Runnable () 102 { 103 public void run() 104 { 105 final MutableInteger count = new MutableInteger(0); 106 final int value = 13; 107 108 SwingUtilities.invokeLater(new Runnable () 109 { 110 public void run() 111 { 112 count.set(value); 113 } 114 }); 115 116 final Task task = new Task() 117 { 118 public Object run() throws Exception 119 { 120 return null; 121 } 122 }; 123 124 SwingUtilities.invokeLater(new Runnable () 126 { 127 public void run() 128 { 129 setTaskCompleted(task); 130 } 131 }); 132 133 pump.pumpEvents(task); 135 136 if (count.get() != value) fail("Event pump does not dequeue events"); 137 } 138 }, null); 139 } 140 141 144 private void tesPumpEventsOnThrowException(final EventPump pump) throws Exception 145 { 146 invokeTest(null, new Runnable () 147 { 148 public void run() 149 { 150 SwingUtilities.invokeLater(new Runnable () 151 { 152 public void run() 153 { 154 throw new RuntimeException (); 155 } 156 }); 157 158 final Task task = new Task() 159 { 160 public Object run() throws Exception 161 { 162 return null; 163 } 164 }; 165 166 final long delay = 3000; 167 168 SwingUtilities.invokeLater(new Runnable () 170 { 171 public void run() 172 { 173 sleep(delay); 174 setTaskCompleted(task); 175 } 176 }); 177 178 try 179 { 180 long start = System.currentTimeMillis(); 182 pump.pumpEvents(task); 183 long stop = System.currentTimeMillis(); 184 long elapsed = stop - start; 185 if (elapsed <= delay) fail("Blocking is not effective when events throw exceptions: expecting " + delay + ", blocked for only " + elapsed); 186 } 187 catch (RuntimeException x) 188 { 189 fail("Event pump must not throw runtime exceptions thrown by events"); 190 } 191 } 192 }, null); 193 } 194 195 198 private void tesPumpEventsOnThrowError(final EventPump pump) throws Exception 199 { 200 invokeTest(null, new Runnable () 201 { 202 public void run() 203 { 204 SwingUtilities.invokeLater(new Runnable () 205 { 206 public void run() 207 { 208 throw new Error (); 209 } 210 }); 211 212 final Task task = new Task() 213 { 214 public Object run() throws Exception 215 { 216 return null; 217 } 218 }; 219 220 final long delay = 3000; 221 222 SwingUtilities.invokeLater(new Runnable () 224 { 225 public void run() 226 { 227 sleep(delay); 228 setTaskCompleted(task); 229 } 230 }); 231 232 try 233 { 234 long start = System.currentTimeMillis(); 236 pump.pumpEvents(task); 237 long stop = System.currentTimeMillis(); 238 long elapsed = stop - start; 239 if (elapsed <= delay) fail("Blocking is not effective when events throw errors: expecting " + delay + ", blocked for only " + elapsed); 240 } 241 catch (Error x) 242 { 243 fail("Event pump must not throw errors thrown by events"); 244 } 245 } 246 }, null); 247 } 248 249 private void setTaskCompleted(Task task) 250 { 251 try 252 { 253 Method completed = Task.class.getDeclaredMethod("setCompleted", new Class [] {boolean.class}); 254 completed.setAccessible(true); 255 completed.invoke(task, new Object []{Boolean.TRUE}); 256 257 SwingUtilities.invokeLater(new Runnable () 260 { 261 public void run() 262 { 263 } 264 }); 265 } 266 catch (Throwable x) 267 { 268 x.printStackTrace(); 269 fail(); 270 } 271 } 272 } 273 | Popular Tags |