ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +179 -158 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import java.util.concurrent.*;
9     import junit.framework.*;
10     import java.util.List;
11    
12 dl 1.3 public class ThreadPoolExecutorTest extends JSR166TestCase {
13 dl 1.1 public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(ThreadPoolExecutorTest.class);
18     }
19    
20 dl 1.3 /**
21     * For use as ThreadFactory in constructors
22     */
23     static class MyThreadFactory implements ThreadFactory{
24 dl 1.1 public Thread newThread(Runnable r){
25     return new Thread(r);
26     }
27     }
28    
29 dl 1.3 /**
30     * For use as RejectedExecutionHandler in constructors
31     */
32     static class MyREHandler implements RejectedExecutionHandler{
33 dl 1.1 public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
34     }
35    
36     /**
37 dl 1.3 * execute successfully executes a runnable
38 dl 1.1 */
39 dl 1.5 public void testExecute() {
40     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
41 dl 1.1 try {
42 dl 1.5 p1.execute(new Runnable() {
43     public void run() {
44     try {
45 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
46 dl 1.2 } catch(InterruptedException e){
47 dl 1.5 threadUnexpectedException();
48 dl 1.1 }
49     }
50     });
51 dl 1.3 Thread.sleep(SMALL_DELAY_MS);
52 dl 1.2 } catch(InterruptedException e){
53 dl 1.5 unexpectedException();
54 dl 1.3 }
55 dl 1.5 joinPool(p1);
56 dl 1.1 }
57    
58     /**
59 dl 1.3 * getActiveCount gives correct values
60 dl 1.1 */
61 dl 1.5 public void testGetActiveCount() {
62     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
63     assertEquals(0, p2.getActiveCount());
64     p2.execute(new MediumRunnable());
65     try {
66 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
67     } catch(Exception e){
68 dl 1.5 unexpectedException();
69 dl 1.1 }
70 dl 1.5 assertEquals(1, p2.getActiveCount());
71     joinPool(p2);
72 dl 1.1 }
73    
74     /**
75 dl 1.3 * getCompleteTaskCount gives correct values
76 dl 1.1 */
77 dl 1.5 public void testGetCompletedTaskCount() {
78     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
79     assertEquals(0, p2.getCompletedTaskCount());
80     p2.execute(new ShortRunnable());
81     try {
82 dl 1.3 Thread.sleep(MEDIUM_DELAY_MS);
83     } catch(Exception e){
84 dl 1.5 unexpectedException();
85 dl 1.1 }
86 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
87     p2.shutdown();
88     joinPool(p2);
89 dl 1.1 }
90    
91     /**
92 dl 1.3 * getCorePoolSize gives correct values
93 dl 1.1 */
94 dl 1.5 public void testGetCorePoolSize() {
95     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
96     assertEquals(1, p1.getCorePoolSize());
97     joinPool(p1);
98 dl 1.1 }
99    
100     /**
101 dl 1.3 * getKeepAliveTime gives correct values
102 dl 1.1 */
103 dl 1.5 public void testGetKeepAliveTime() {
104     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
105     assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
106     joinPool(p2);
107 dl 1.1 }
108    
109     /**
110 dl 1.3 * getLargestPoolSize gives correct values
111 dl 1.1 */
112 dl 1.5 public void testGetLargestPoolSize() {
113     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
114 dl 1.1 try {
115 dl 1.5 assertEquals(0, p2.getLargestPoolSize());
116     p2.execute(new MediumRunnable());
117     p2.execute(new MediumRunnable());
118 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
119 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
120 dl 1.3 } catch(Exception e){
121 dl 1.5 unexpectedException();
122 dl 1.3 }
123 dl 1.5 joinPool(p2);
124 dl 1.1 }
125    
126     /**
127 dl 1.3 * getMaximumPoolSize gives correct values
128 dl 1.1 */
129 dl 1.5 public void testGetMaximumPoolSize() {
130     ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
131     assertEquals(2, p2.getMaximumPoolSize());
132     joinPool(p2);
133 dl 1.1 }
134    
135     /**
136 dl 1.3 * getPoolSize gives correct values
137 dl 1.1 */
138 dl 1.5 public void testGetPoolSize() {
139     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
140     assertEquals(0, p1.getPoolSize());
141     p1.execute(new MediumRunnable());
142     assertEquals(1, p1.getPoolSize());
143     joinPool(p1);
144 dl 1.1 }
145    
146     /**
147 dl 1.3 * getTaskCount gives correct values
148 dl 1.1 */
149 dl 1.5 public void testGetTaskCount() {
150     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
151 dl 1.1 try {
152 dl 1.5 assertEquals(0, p1.getTaskCount());
153     p1.execute(new MediumRunnable());
154 dl 1.3 Thread.sleep(SHORT_DELAY_MS);
155 dl 1.5 assertEquals(1, p1.getTaskCount());
156 dl 1.3 } catch(Exception e){
157 dl 1.5 unexpectedException();
158 dl 1.3 }
159 dl 1.5 joinPool(p1);
160 dl 1.1 }
161    
162     /**
163 dl 1.3 * isShutDown gives correct values
164 dl 1.1 */
165 dl 1.5 public void testIsShutdown() {
166 dl 1.1
167 dl 1.5 ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
168     assertFalse(p1.isShutdown());
169     p1.shutdown();
170     assertTrue(p1.isShutdown());
171     joinPool(p1);
172 dl 1.1 }
173    
174    
175     /**
176 dl 1.3 * isTerminated gives correct values
177 dl 1.1 * Makes sure termination does not take an innapropriate
178     * amount of time
179     */
180 dl 1.5 public void testIsTerminated() {
181     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
182 dl 1.1 try {
183 dl 1.5 p1.execute(new MediumRunnable());
184 dl 1.1 } finally {
185 dl 1.5 p1.shutdown();
186 dl 1.1 }
187 dl 1.3 try {
188 dl 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
189     assertTrue(p1.isTerminated());
190 dl 1.3 } catch(Exception e){
191 dl 1.5 unexpectedException();
192     }
193     }
194    
195     /**
196     * isTerminating gives correct values
197     */
198     public void testIsTerminating() {
199     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
200     assertFalse(p1.isTerminating());
201     try {
202     p1.execute(new SmallRunnable());
203     assertFalse(p1.isTerminating());
204     } finally {
205     p1.shutdown();
206     }
207     try {
208     assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
209     assertTrue(p1.isTerminated());
210     assertFalse(p1.isTerminating());
211     } catch(Exception e){
212     unexpectedException();
213 dl 1.3 }
214 dl 1.1 }
215    
216     /**
217 dl 1.3 * purge correctly removes cancelled tasks
218 dl 1.1 * from the queue
219     */
220 dl 1.5 public void testPurge() {
221     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
222 dl 1.3 CancellableTask[] tasks = new CancellableTask[5];
223     for(int i = 0; i < 5; i++){
224     tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable());
225 dl 1.5 p1.execute(tasks[i]);
226 dl 1.3 }
227     tasks[4].cancel(true);
228     tasks[3].cancel(true);
229 dl 1.5 p1.purge();
230     long count = p1.getTaskCount();
231 dl 1.3 assertTrue(count >= 2 && count < 5);
232 dl 1.5 joinPool(p1);
233 dl 1.1 }
234    
235     /**
236 dl 1.3 * shutDownNow returns a list
237 dl 1.1 * containing the correct number of elements
238     */
239 dl 1.5 public void testShutDownNow() {
240     ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
241 dl 1.1 List l;
242     try {
243     for(int i = 0; i < 5; i++)
244 dl 1.5 p1.execute(new MediumPossiblyInterruptedRunnable());
245 dl 1.1 }
246     finally {
247 dl 1.5 l = p1.shutdownNow();
248 dl 1.1 }
249 dl 1.5 assertTrue(p1.isShutdown());
250 dl 1.1 assertTrue(l.size() <= 4);
251     }
252    
253     // Exception Tests
254    
255    
256 dl 1.3 /** Throws if corePoolSize argument is less than zero */
257 dl 1.1 public void testConstructor1() {
258 dl 1.5 try {
259 dl 1.3 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
260 dl 1.5 shouldThrow();
261 dl 1.1 }
262 dl 1.3 catch (IllegalArgumentException success){}
263 dl 1.1 }
264    
265 dl 1.3 /** Throws if maximumPoolSize is less than zero */
266 dl 1.1 public void testConstructor2() {
267 dl 1.5 try {
268 dl 1.3 new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
269 dl 1.5 shouldThrow();
270 dl 1.1 }
271 dl 1.3 catch (IllegalArgumentException success){}
272 dl 1.1 }
273    
274 dl 1.3 /** Throws if maximumPoolSize is equal to zero */
275 dl 1.1 public void testConstructor3() {
276 dl 1.5 try {
277 dl 1.3 new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
278 dl 1.5 shouldThrow();
279 dl 1.1 }
280 dl 1.3 catch (IllegalArgumentException success){}
281 dl 1.1 }
282    
283 dl 1.3 /** Throws if keepAliveTime is less than zero */
284 dl 1.1 public void testConstructor4() {
285 dl 1.5 try {
286 dl 1.1 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
287 dl 1.5 shouldThrow();
288 dl 1.1 }
289 dl 1.3 catch (IllegalArgumentException success){}
290 dl 1.1 }
291    
292 dl 1.3 /** Throws if corePoolSize is greater than the maximumPoolSize */
293 dl 1.1 public void testConstructor5() {
294 dl 1.5 try {
295 dl 1.3 new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
296 dl 1.5 shouldThrow();
297 dl 1.1 }
298 dl 1.3 catch (IllegalArgumentException success){}
299 dl 1.1 }
300    
301 dl 1.3 /** Throws if workQueue is set to null */
302 dl 1.1 public void testNullPointerException() {
303 dl 1.5 try {
304 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null);
305 dl 1.5 shouldThrow();
306 dl 1.1 }
307 dl 1.3 catch (NullPointerException success){}
308 dl 1.1 }
309    
310    
311    
312 dl 1.3 /** Throws if corePoolSize argument is less than zero */
313 dl 1.1 public void testConstructor6() {
314 dl 1.5 try {
315 dl 1.3 new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
316 dl 1.5 shouldThrow();
317 dl 1.3 } catch (IllegalArgumentException success){}
318 dl 1.1 }
319    
320 dl 1.3 /** Throws if maximumPoolSize is less than zero */
321 dl 1.1 public void testConstructor7() {
322 dl 1.5 try {
323 dl 1.3 new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
324 dl 1.5 shouldThrow();
325 dl 1.1 }
326 dl 1.3 catch (IllegalArgumentException success){}
327 dl 1.1 }
328    
329 dl 1.3 /** Throws if maximumPoolSize is equal to zero */
330 dl 1.1 public void testConstructor8() {
331 dl 1.5 try {
332 dl 1.3 new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
333 dl 1.5 shouldThrow();
334 dl 1.1 }
335 dl 1.3 catch (IllegalArgumentException success){}
336 dl 1.1 }
337    
338 dl 1.3 /** Throws if keepAliveTime is less than zero */
339 dl 1.1 public void testConstructor9() {
340 dl 1.5 try {
341 dl 1.3 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
342 dl 1.5 shouldThrow();
343 dl 1.1 }
344 dl 1.3 catch (IllegalArgumentException success){}
345 dl 1.1 }
346    
347 dl 1.3 /** Throws if corePoolSize is greater than the maximumPoolSize */
348 dl 1.1 public void testConstructor10() {
349 dl 1.5 try {
350 dl 1.3 new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory());
351 dl 1.5 shouldThrow();
352 dl 1.1 }
353 dl 1.3 catch (IllegalArgumentException success){}
354 dl 1.1 }
355    
356 dl 1.3 /** Throws if workQueue is set to null */
357 dl 1.1 public void testNullPointerException2() {
358 dl 1.5 try {
359 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory());
360 dl 1.5 shouldThrow();
361 dl 1.1 }
362 dl 1.3 catch (NullPointerException success){}
363 dl 1.1 }
364    
365 dl 1.3 /** Throws if threadFactory is set to null */
366 dl 1.1 public void testNullPointerException3() {
367 dl 1.5 try {
368 dl 1.1 ThreadFactory f = null;
369 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
370 dl 1.5 shouldThrow();
371 dl 1.1 }
372 dl 1.3 catch (NullPointerException success){}
373 dl 1.1 }
374    
375    
376 dl 1.3 /** Throws if corePoolSize argument is less than zero */
377 dl 1.1 public void testConstructor11() {
378 dl 1.5 try {
379 dl 1.3 new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
380 dl 1.5 shouldThrow();
381 dl 1.1 }
382 dl 1.3 catch (IllegalArgumentException success){}
383 dl 1.1 }
384    
385 dl 1.3 /** Throws if maximumPoolSize is less than zero */
386 dl 1.1 public void testConstructor12() {
387 dl 1.5 try {
388 dl 1.3 new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
389 dl 1.5 shouldThrow();
390 dl 1.1 }
391 dl 1.3 catch (IllegalArgumentException success){}
392 dl 1.1 }
393    
394 dl 1.3 /** Throws if maximumPoolSize is equal to zero */
395 dl 1.1 public void testConstructor13() {
396 dl 1.5 try {
397 dl 1.3 new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
398 dl 1.5 shouldThrow();
399 dl 1.1 }
400 dl 1.3 catch (IllegalArgumentException success){}
401 dl 1.1 }
402    
403 dl 1.3 /** Throws if keepAliveTime is less than zero */
404 dl 1.1 public void testConstructor14() {
405 dl 1.5 try {
406 dl 1.3 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
407 dl 1.5 shouldThrow();
408 dl 1.1 }
409 dl 1.3 catch (IllegalArgumentException success){}
410 dl 1.1 }
411    
412 dl 1.3 /** Throws if corePoolSize is greater than the maximumPoolSize */
413 dl 1.1 public void testConstructor15() {
414 dl 1.5 try {
415 dl 1.3 new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler());
416 dl 1.5 shouldThrow();
417 dl 1.1 }
418 dl 1.3 catch (IllegalArgumentException success){}
419 dl 1.1 }
420    
421 dl 1.3 /** Throws if workQueue is set to null */
422 dl 1.1 public void testNullPointerException4() {
423 dl 1.5 try {
424 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler());
425 dl 1.5 shouldThrow();
426 dl 1.1 }
427 dl 1.3 catch (NullPointerException success){}
428 dl 1.1 }
429    
430 dl 1.3 /** Throws if handler is set to null */
431 dl 1.1 public void testNullPointerException5() {
432 dl 1.5 try {
433 dl 1.1 RejectedExecutionHandler r = null;
434 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
435 dl 1.5 shouldThrow();
436 dl 1.1 }
437 dl 1.3 catch (NullPointerException success){}
438 dl 1.1 }
439    
440    
441 dl 1.3 /** Throws if corePoolSize argument is less than zero */
442 dl 1.1 public void testConstructor16() {
443 dl 1.5 try {
444 dl 1.3 new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
445 dl 1.5 shouldThrow();
446 dl 1.1 }
447 dl 1.3 catch (IllegalArgumentException success){}
448 dl 1.1 }
449    
450 dl 1.3 /** Throws if maximumPoolSize is less than zero */
451 dl 1.1 public void testConstructor17() {
452 dl 1.5 try {
453 dl 1.3 new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
454 dl 1.5 shouldThrow();
455 dl 1.1 }
456 dl 1.3 catch (IllegalArgumentException success){}
457 dl 1.1 }
458    
459 dl 1.3 /** Throws if maximumPoolSize is equal to zero */
460 dl 1.1 public void testConstructor18() {
461 dl 1.5 try {
462 dl 1.3 new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
463 dl 1.5 shouldThrow();
464 dl 1.1 }
465 dl 1.3 catch (IllegalArgumentException success){}
466 dl 1.1 }
467    
468 dl 1.3 /** Throws if keepAliveTime is less than zero */
469 dl 1.1 public void testConstructor19() {
470 dl 1.5 try {
471 dl 1.3 new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
472 dl 1.5 shouldThrow();
473 dl 1.1 }
474 dl 1.3 catch (IllegalArgumentException success){}
475 dl 1.1 }
476    
477 dl 1.3 /** Throws if corePoolSize is greater than the maximumPoolSize */
478 dl 1.1 public void testConstructor20() {
479 dl 1.5 try {
480 dl 1.3 new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler());
481 dl 1.5 shouldThrow();
482 dl 1.1 }
483 dl 1.3 catch (IllegalArgumentException success){}
484 dl 1.1 }
485    
486 dl 1.3 /** Throws if workQueue is set to null */
487 dl 1.1 public void testNullPointerException6() {
488 dl 1.5 try {
489 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler());
490 dl 1.5 shouldThrow();
491 dl 1.1 }
492 dl 1.3 catch (NullPointerException success){}
493 dl 1.1 }
494    
495 dl 1.3 /** Throws if handler is set to null */
496 dl 1.1 public void testNullPointerException7() {
497 dl 1.5 try {
498 dl 1.1 RejectedExecutionHandler r = null;
499 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),r);
500 dl 1.5 shouldThrow();
501 dl 1.1 }
502 dl 1.3 catch (NullPointerException success){}
503 dl 1.1 }
504    
505 dl 1.3 /** Throws if ThreadFactory is set top null */
506 dl 1.1 public void testNullPointerException8() {
507 dl 1.5 try {
508 dl 1.1 ThreadFactory f = null;
509 dl 1.3 new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new MyREHandler());
510 dl 1.5 shouldThrow();
511 dl 1.1 }
512 dl 1.3 catch (NullPointerException successdn8){}
513 dl 1.1 }
514    
515    
516     /**
517 dl 1.3 * execute will throw RejectedExcutionException
518 dl 1.1 * ThreadPoolExecutor will throw one when more runnables are
519     * executed then will fit in the Queue.
520     */
521 dl 1.5 public void testRejectedExecutionException() {
522 dl 1.1 ThreadPoolExecutor tpe = null;
523 dl 1.5 try {
524 dl 1.3 tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
525 dl 1.2 } catch(Exception e){}
526 dl 1.1 tpe.shutdown();
527 dl 1.5 try {
528 dl 1.3 tpe.execute(new NoOpRunnable());
529 dl 1.5 shouldThrow();
530 dl 1.2 } catch(RejectedExecutionException success){}
531 dl 1.1
532 dl 1.3 joinPool(tpe);
533 dl 1.1 }
534    
535     /**
536 dl 1.3 * setCorePoolSize will throw IllegalArgumentException
537 dl 1.1 * when given a negative
538     */
539 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
540 dl 1.1 ThreadPoolExecutor tpe = null;
541 dl 1.5 try {
542 dl 1.3 tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
543 dl 1.2 } catch(Exception e){}
544 dl 1.5 try {
545 dl 1.1 tpe.setCorePoolSize(-1);
546 dl 1.5 shouldThrow();
547 dl 1.2 } catch(IllegalArgumentException success){
548 dl 1.1 } finally {
549     tpe.shutdown();
550     }
551 dl 1.3 joinPool(tpe);
552 dl 1.1 }
553    
554    
555     /**
556 dl 1.3 * setMaximumPoolSize(int) will throw IllegalArgumentException
557 dl 1.1 * if given a value less the it's actual core pool size
558     */
559 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
560 dl 1.1 ThreadPoolExecutor tpe = null;
561 dl 1.5 try {
562 dl 1.3 tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
563 dl 1.2 } catch(Exception e){}
564 dl 1.5 try {
565 dl 1.1 tpe.setMaximumPoolSize(1);
566 dl 1.5 shouldThrow();
567 dl 1.2 } catch(IllegalArgumentException success){
568 dl 1.1 } finally {
569     tpe.shutdown();
570     }
571 dl 1.3 joinPool(tpe);
572 dl 1.1 }
573    
574     /**
575 dl 1.3 * setMaximumPoolSize will throw IllegalArgumentException
576 dl 1.1 * if given a negative number
577     */
578 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
579 dl 1.1 ThreadPoolExecutor tpe = null;
580 dl 1.5 try {
581 dl 1.3 tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
582 dl 1.2 } catch(Exception e){}
583 dl 1.5 try {
584 dl 1.1 tpe.setMaximumPoolSize(-1);
585 dl 1.5 shouldThrow();
586 dl 1.2 } catch(IllegalArgumentException success){
587 dl 1.1 } finally {
588     tpe.shutdown();
589     }
590 dl 1.3 joinPool(tpe);
591 dl 1.1 }
592    
593    
594     /**
595 dl 1.3 * setKeepAliveTime will throw IllegalArgumentException
596 dl 1.1 * when given a negative value
597     */
598 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
599 dl 1.1 ThreadPoolExecutor tpe = null;
600 dl 1.5 try {
601 dl 1.3 tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
602 dl 1.2 } catch(Exception e){}
603 dl 1.1
604 dl 1.5 try {
605 dl 1.1 tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
606 dl 1.5 shouldThrow();
607 dl 1.2 } catch(IllegalArgumentException success){
608 dl 1.1 } finally {
609     tpe.shutdown();
610     }
611 dl 1.3 joinPool(tpe);
612 dl 1.1 }
613    
614     }