KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > javacoding > jspider > core > threading > WorkerThreadTest


1 package net.javacoding.jspider.core.threading;
2
3 import junit.framework.TestCase;
4 import net.javacoding.jspider.core.task.WorkerTask;
5 import net.javacoding.jspider.core.util.config.ConfigurationFactory;
6 import net.javacoding.jspider.mockobjects.*;
7
8 import java.net.URL JavaDoc;
9
10 /**
11  * $Id: WorkerThreadTest.java,v 1.6 2003/04/09 17:08:14 vanrogu Exp $
12  */

13 public class WorkerThreadTest extends TestCase {
14
15     protected WorkerThread thread;
16     protected WorkerThreadPool pool;
17
18     public WorkerThreadTest ( ) {
19         super ( "WorkerThreadTest" );
20         // make sure we're using the 'unittest' configuration
21
ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
22     }
23
24     protected void setUp() throws Exception JavaDoc {
25         pool = new MockWorkerThreadPool ( );
26         thread = new WorkerThread(pool, "testThread", 1);
27     }
28
29     public void testDoubleAssign ( ) {
30         thread.start();
31         sleep();
32         try {
33             thread.assign(new WaitTask(null, 1000));
34         } catch (Exception JavaDoc e) {
35             e.printStackTrace();
36             fail("thread throw an exception during the first assign");
37         }
38         try {
39             thread.assign(new WaitTask(null, 1000));
40         } catch (Exception JavaDoc e) {
41             // thread telling us he's already busy doing something else
42
return;
43         }
44         fail("thread threw no exception during the first assign");
45     }
46
47     public void testAvailable ( ) {
48         thread.start();
49         sleep();
50         assertTrue ( "idle thread claiming not to be available", thread.isAvailable() );
51         assertFalse ( "idle thread claiming not to be occupied", thread.isOccupied() );
52         try {
53             thread.assign(new WaitTask(null, 1000));
54             assertFalse ( "busy thread claiming to be available", thread.isAvailable() );
55             assertTrue ( "busy thread claiming to be occupied", thread.isOccupied() );
56         } catch (Exception JavaDoc e) {
57             fail("thread throw an exception during the first assign");
58         }
59         try {
60             thread.assign(new WaitTask(null, 1000));
61         } catch (Exception JavaDoc e) {
62             assertFalse ( "busy thread claiming to be available", thread.isAvailable() );
63             assertTrue ( "busy thread claiming to be occupied", thread.isOccupied() );
64             // thread telling us he's already busy doing something else
65
return;
66         }
67         fail("thread threw no exception during the first assign");
68     }
69
70     public void testStopRunning ( ) {
71         thread.start();
72         sleep();
73         thread.stopRunning();
74         assertFalse ( "stopped thread claiming to be available", thread.isAvailable() );
75         assertFalse ( "stopped thread claiming to be occupied", thread.isOccupied() );
76     }
77
78     public void testStopRunningNotStarted ( ) {
79         try {
80             thread.stopRunning();
81         } catch (Exception JavaDoc e) {
82             return;
83         }
84         fail ( "not started thread accepted to be stopped" );
85     }
86
87     public void testAssignNotStarted ( ) {
88         try {
89             thread.assign(new WaitTask(null, 1000));
90         } catch (Exception JavaDoc e) {
91             return;
92         }
93         fail("not started thread accepted an assigned task");
94     }
95
96     public void testAssignAfterStop ( ) {
97         thread.start();
98         sleep();
99         thread.stopRunning();
100         try {
101             thread.assign(new WaitTask(null, 1000));
102         } catch (Exception JavaDoc e) {
103             return;
104         }
105         fail("stopped thread accepted an assigned task");
106     }
107
108     public void testInitialState ( ) {
109         int state = thread.getState();
110         int expected = WorkerThread.WORKERTHREAD_IDLE;
111         assertEquals ( "newly created thread reports another state than IDLE", expected, state);
112     }
113
114     public void testBlockedState ( ) throws Exception JavaDoc {
115         URL JavaDoc url = new URL JavaDoc ("http://j-spider.sourceforge.net");
116         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 1000, 1000 );
117         thread.start();
118         sleep();
119         thread.assign(task);
120         try {
121             Thread.sleep(100);
122         } catch (InterruptedException JavaDoc e) {
123             Thread.currentThread().interrupt();
124         }
125         int state = thread.getState();
126         int expected = WorkerThread.WORKERTHREAD_BLOCKED;
127         assertEquals ( "thread waiting in prepare() did report another state than BLOCKED", expected, state);
128     }
129
130     public void testBusyState ( ) throws Exception JavaDoc {
131         URL JavaDoc url = new URL JavaDoc ("http://j-spider.sourceforge.net");
132         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 1000, 0 );
133         thread.start();
134         sleep();
135         thread.assign(task);
136         sleep();
137         int state = thread.getState();
138         int expected = WorkerThread.WORKERTHREAD_BUSY;
139         assertEquals ( "thread waiting in execute() did report another state than BUSY", expected, state);
140     }
141
142     public void testIdleStateAfterTask ( ) throws Exception JavaDoc {
143         URL JavaDoc url = new URL JavaDoc ("http://j-spider.sourceforge.net");
144         WorkerTask task = new WaitTask ( new SimpleSpiderContext(url), 10, 0 );
145         thread.start();
146         sleep();
147         thread.assign(task);
148         sleep();
149         int state = thread.getState();
150         int expected = WorkerThread.WORKERTHREAD_IDLE;
151         assertEquals ( "thread that should have completed it's job reported another state than IDLE", expected, state);
152     }
153
154
155     // give the thread the time to start the run() method.
156
protected void sleep(){
157         try {
158             Thread.sleep(100);
159         } catch (InterruptedException JavaDoc e) {
160             Thread.currentThread().interrupt();
161         }
162     }
163 }
164
Popular Tags