ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:41 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +204 -241 lines
Log Message:
New base class JSR166TestCase

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