KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > util > ThreadPoolTest


1 package org.jacorb.test.notification.util;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1999-2003 Gerald Brose
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */

23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.easymock.MockControl;
29 import org.jacorb.notification.engine.DefaultTaskExecutor;
30
31 import EDU.oswego.cs.dl.util.concurrent.Latch;
32
33 /**
34  * @author Alphonse Bendt
35  */

36
37 public class ThreadPoolTest extends TestCase
38 {
39     private DefaultTaskExecutor objectUnderTest_;
40
41     public void setUp() throws Exception JavaDoc
42     {
43         objectUnderTest_ = new DefaultTaskExecutor("Testing", 2);
44     }
45
46     public void tearDown() throws Exception JavaDoc
47     {
48         objectUnderTest_.dispose();
49     }
50
51     public void testExceute() throws Exception JavaDoc
52     {
53         MockControl controlTask = MockControl.createControl(Runnable JavaDoc.class);
54         Runnable JavaDoc mockTask = (Runnable JavaDoc) controlTask.getMock();
55
56         mockTask.run();
57         controlTask.replay();
58
59         objectUnderTest_.execute(mockTask);
60
61         Thread.sleep(100);
62
63         controlTask.verify();
64     }
65
66     public void testDirectExceute() throws Exception JavaDoc
67     {
68         objectUnderTest_.dispose();
69         objectUnderTest_ = new DefaultTaskExecutor("Testing", 0);
70
71         MockControl controlTask = MockControl.createControl(Runnable JavaDoc.class);
72         Runnable JavaDoc mockTask = (Runnable JavaDoc) controlTask.getMock();
73
74         mockTask.run();
75         controlTask.replay();
76
77         objectUnderTest_.execute(mockTask);
78
79         controlTask.verify();
80     }
81
82     public void testNegativeNumberOfThreadsIsInvalid() throws Exception JavaDoc
83     {
84         try
85         {
86             new DefaultTaskExecutor("Testing", -1);
87             fail();
88         } catch (IllegalArgumentException JavaDoc e)
89         {
90             // expected
91
}
92     }
93
94     public void testIsTaskQueued() throws Exception JavaDoc
95     {
96         assertTrue(!objectUnderTest_.isTaskQueued());
97
98         final Latch _latch1 = new Latch();
99         final Latch _latch2 = new Latch();
100
101         objectUnderTest_.execute(new Runnable JavaDoc()
102         {
103             public void run()
104             {
105                 try
106                 {
107                     _latch1.acquire();
108                 } catch (InterruptedException JavaDoc e)
109                 {
110                     // ignore
111
}
112             }
113         });
114
115         objectUnderTest_.execute(new Runnable JavaDoc()
116         {
117             public void run()
118             {
119                 try
120                 {
121                     _latch2.acquire();
122                 } catch (InterruptedException JavaDoc e)
123                 {
124                     // ignore
125
}
126             }
127         });
128
129         objectUnderTest_.execute(new Runnable JavaDoc()
130         {
131             public void run()
132             {
133                 try
134                 {
135                     _latch2.acquire();
136                 } catch (InterruptedException JavaDoc e)
137                 {
138                     // ignore
139
}
140             }
141         });
142
143         assertTrue(objectUnderTest_.isTaskQueued());
144         _latch1.release();
145         Thread.sleep(100);
146         assertTrue(!objectUnderTest_.isTaskQueued());
147     }
148
149     public ThreadPoolTest(String JavaDoc name)
150     {
151         super(name);
152     }
153
154     public static Test suite()
155     {
156         TestSuite suite = new TestSuite(ThreadPoolTest.class, "Tests for Class ThreadPool");
157
158         return suite;
159     }
160 }
Popular Tags