KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > thread > AbstractThreadTest


1 package org.objectweb.petals.jbi.component.thread;
2
3 import junit.framework.TestCase;
4
5 public class AbstractThreadTest extends TestCase {
6
7     /**
8      * The abstract thread to test
9      */

10     protected AbstractThread threadTest;
11
12     /**
13      * If the thread is not started, can not submit a task. The thread throws an
14      * exception
15      *
16      */

17     public void testSubmissionFailure() {
18         threadTest.execute(0);
19         assertNotNull(threadTest.getJbiException());
20     }
21
22     /**
23      * Test multiple task submitter in separate threads.
24      *
25      */

26     public void testMultipleConsumers() {
27         int loopA = 10;
28         long sleepA = 100;
29         int loopB = 5;
30         long sleepB = 120;
31
32         threadTest.start();
33
34         ConsumerThread consumerA = new ConsumerThread(threadTest, loopA, sleepA);
35         ConsumerThread consumerB = new ConsumerThread(threadTest, loopB, sleepB);
36
37         consumerA.start();
38         consumerB.start();
39
40         // wait for tasks to complete
41
try {
42             consumerA.join(loopA * sleepA * 2);
43             consumerB.join(loopB * sleepB * 2);
44         } catch (InterruptedException JavaDoc e) {
45             fail("Task submission failure");
46         }
47
48     }
49
50     /*
51      * (non-Javadoc)
52      *
53      * @see junit.framework.TestCase#setUp()
54      */

55     protected void setUp() throws Exception JavaDoc {
56         super.setUp();
57
58         /*
59          * Create the thread test class overriding the doTask method
60          */

61         threadTest = new AbstractThread() {
62             @Override JavaDoc
63             protected int doTask(int action) {
64                 int result = 0;
65                 switch (action) {
66                     case AbstractThread.SHUTDOWNTHREAD:
67                         result = -1;
68                         break;
69
70                     default:
71                         result = action;
72                         break;
73                 }
74                 return result;
75             }
76         };
77     }
78
79     /**
80      *
81      * @author Christophe Hamerling - EBM WebSourcing
82      *
83      */

84     protected class ConsumerThread extends Thread JavaDoc {
85
86         private AbstractThread abstractThread;
87
88         private long sleepTime;
89
90         private int loop;
91
92         /**
93          * Creates a new instance of ConsumerThread
94          *
95          * @param abstractThread
96          * @param loop
97          * @param sleepTime
98          */

99         public ConsumerThread(AbstractThread abstractThread, int loop,
100             long sleepTime) {
101             this.abstractThread = abstractThread;
102             this.loop = loop;
103             this.sleepTime = sleepTime;
104         }
105
106         /*
107          * (non-Javadoc)
108          *
109          * @see java.lang.Thread#run()
110          */

111         @Override JavaDoc
112         public void run() {
113             for (int i = 0; i < loop; i++) {
114                 abstractThread.execute(i);
115                 try {
116                     Thread.sleep(sleepTime);
117                 } catch (InterruptedException JavaDoc e) {
118                 }
119             }
120         }
121     }
122
123 }
124
Popular Tags