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 junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class CancellableTaskTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(CancellableTaskTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* Subclass to expose protected methods |
22 |
*/ |
23 |
static class PublicCancellableTask extends CancellableTask { |
24 |
public PublicCancellableTask() {} |
25 |
public PublicCancellableTask(Runnable r) { super(r); } |
26 |
public boolean reset() { return super.reset(); } |
27 |
public Runnable getRunnable() { return super.getRunnable(); } |
28 |
public void setRunnable(Runnable r) { super.setRunnable(r); } |
29 |
public void setCancelled() { super.setCancelled(); } |
30 |
public void setDone() { super.setDone(); } |
31 |
} |
32 |
|
33 |
/** |
34 |
* creating task with null runnable throws NPE |
35 |
*/ |
36 |
public void testConstructor(){ |
37 |
try { |
38 |
CancellableTask task = new CancellableTask(null); |
39 |
shouldThrow(); |
40 |
} |
41 |
catch(NullPointerException success) { |
42 |
} |
43 |
} |
44 |
|
45 |
/** |
46 |
* isDone is true after task is run |
47 |
*/ |
48 |
public void testIsDone(){ |
49 |
CancellableTask task = new CancellableTask(new NoOpRunnable()); |
50 |
task.run(); |
51 |
assertTrue(task.isDone()); |
52 |
assertFalse(task.isCancelled()); |
53 |
} |
54 |
|
55 |
/** |
56 |
* Cancelling before running succeeds |
57 |
*/ |
58 |
public void testCancelBeforeRun() { |
59 |
CancellableTask task = new CancellableTask(new NoOpRunnable()); |
60 |
assertTrue(task.cancel(false)); |
61 |
task.run(); |
62 |
assertTrue(task.isDone()); |
63 |
assertTrue(task.isCancelled()); |
64 |
} |
65 |
|
66 |
/** |
67 |
* reset of a done task succeeds and changes status to not done |
68 |
*/ |
69 |
public void testReset(){ |
70 |
PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable()); |
71 |
task.run(); |
72 |
assertTrue(task.isDone()); |
73 |
assertTrue(task.reset()); |
74 |
assertFalse(task.isDone()); |
75 |
} |
76 |
|
77 |
/** |
78 |
* Resetting after cancellation fails |
79 |
*/ |
80 |
public void testResetAfterCancel() { |
81 |
PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable()); |
82 |
assertTrue(task.cancel(false)); |
83 |
task.run(); |
84 |
assertTrue(task.isDone()); |
85 |
assertTrue(task.isCancelled()); |
86 |
assertFalse(task.reset()); |
87 |
} |
88 |
|
89 |
/** |
90 |
* Setting the runnable causes it to be used |
91 |
*/ |
92 |
public void testSetRunnable() { |
93 |
PublicCancellableTask task = new PublicCancellableTask(); |
94 |
assertNull(task.getRunnable()); |
95 |
Runnable r = new NoOpRunnable(); |
96 |
task.setRunnable(r); |
97 |
assertEquals(r, task.getRunnable()); |
98 |
assertTrue(task.cancel(false)); |
99 |
task.run(); |
100 |
assertTrue(task.isDone()); |
101 |
assertTrue(task.isCancelled()); |
102 |
assertFalse(task.reset()); |
103 |
} |
104 |
|
105 |
/** |
106 |
* setDone of new task causes isDone to be true |
107 |
*/ |
108 |
public void testSetDone() { |
109 |
PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable()); |
110 |
task.setDone(); |
111 |
assertTrue(task.isDone()); |
112 |
assertFalse(task.isCancelled()); |
113 |
} |
114 |
|
115 |
/** |
116 |
* setCancelled of a new task causes isCancelled to be true |
117 |
*/ |
118 |
public void testSetCancelled() { |
119 |
PublicCancellableTask task = new PublicCancellableTask(new NoOpRunnable()); |
120 |
assertTrue(task.cancel(false)); |
121 |
task.setCancelled(); |
122 |
assertTrue(task.isDone()); |
123 |
assertTrue(task.isCancelled()); |
124 |
} |
125 |
|
126 |
/** |
127 |
* Cancel(true) before run succeeds |
128 |
*/ |
129 |
public void testCancelBeforeRun2() { |
130 |
CancellableTask task = new CancellableTask(new NoOpRunnable()); |
131 |
assertTrue(task.cancel(true)); |
132 |
task.run(); |
133 |
assertTrue(task.isDone()); |
134 |
assertTrue(task.isCancelled()); |
135 |
} |
136 |
|
137 |
/** |
138 |
* cancel of a completed task fails |
139 |
*/ |
140 |
public void testCancelAfterRun() { |
141 |
CancellableTask task = new CancellableTask(new NoOpRunnable()); |
142 |
task.run(); |
143 |
assertFalse(task.cancel(false)); |
144 |
assertTrue(task.isDone()); |
145 |
assertFalse(task.isCancelled()); |
146 |
} |
147 |
|
148 |
/** |
149 |
* cancel(true) interrupts a running task |
150 |
*/ |
151 |
public void testCancelInterrupt(){ |
152 |
CancellableTask task = new CancellableTask(new SmallInterruptedRunnable()); |
153 |
Thread t = new Thread(task); |
154 |
|
155 |
try{ |
156 |
t.start(); |
157 |
Thread.sleep(SHORT_DELAY_MS); |
158 |
assertTrue(task.cancel(true)); |
159 |
t.join(); |
160 |
assertTrue(task.isDone()); |
161 |
assertTrue(task.isCancelled()); |
162 |
} catch(InterruptedException e){ |
163 |
unexpectedException(); |
164 |
} |
165 |
} |
166 |
|
167 |
|
168 |
/** |
169 |
* cancel(false) does not interrupt a running task |
170 |
*/ |
171 |
public void testCancelNoInterrupt(){ |
172 |
CancellableTask task = new CancellableTask(new SmallRunnable()); |
173 |
Thread t = new Thread(task); |
174 |
try{ |
175 |
t.start(); |
176 |
Thread.sleep(SHORT_DELAY_MS); |
177 |
assertTrue(task.cancel(false)); |
178 |
t.join(); |
179 |
assertTrue(task.isDone()); |
180 |
assertTrue(task.isCancelled()); |
181 |
} catch(InterruptedException e){ |
182 |
unexpectedException(); |
183 |
} |
184 |
} |
185 |
|
186 |
|
187 |
} |