ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CancellableTaskTest.java
Revision: 1.3
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.2: +65 -33 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 junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11    
12 dl 1.3 public class CancellableTaskTest 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(CancellableTaskTest.class);
18     }
19    
20 dl 1.3 /**
21     * Subclass to expose protected methods
22     */
23     static class MyCancellableTask extends CancellableTask {
24     public MyCancellableTask() {}
25     public MyCancellableTask(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 dl 1.2
33     public void testConstructor(){
34     try {
35     CancellableTask task = new CancellableTask(null);
36     fail("should throw");
37     }
38     catch(NullPointerException success) {
39     }
40     }
41 dl 1.1
42     public void testIsDone(){
43 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
44 dl 1.1 task.run();
45     assertTrue(task.isDone());
46     assertFalse(task.isCancelled());
47     }
48    
49 dl 1.3 public void testReset(){
50     MyCancellableTask task = new MyCancellableTask(new NoOpRunnable());
51     task.run();
52     assertTrue(task.isDone());
53     assertTrue(task.reset());
54     }
55    
56 dl 1.1 public void testCancelBeforeRun() {
57 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
58     assertTrue(task.cancel(false));
59     task.run();
60     assertTrue(task.isDone());
61     assertTrue(task.isCancelled());
62     }
63    
64     public void testResetAfterCancel() {
65     MyCancellableTask task = new MyCancellableTask(new NoOpRunnable());
66     assertTrue(task.cancel(false));
67     task.run();
68     assertTrue(task.isDone());
69     assertTrue(task.isCancelled());
70     assertFalse(task.reset());
71     }
72    
73     public void testSetRunnable() {
74     MyCancellableTask task = new MyCancellableTask();
75     assertNull(task.getRunnable());
76     Runnable r = new NoOpRunnable();
77     task.setRunnable(r);
78     assertEquals(r, task.getRunnable());
79 dl 1.1 assertTrue(task.cancel(false));
80     task.run();
81     assertTrue(task.isDone());
82     assertTrue(task.isCancelled());
83 dl 1.3 assertFalse(task.reset());
84     }
85    
86     public void testSetDone() {
87     MyCancellableTask task = new MyCancellableTask(new NoOpRunnable());
88     task.setDone();
89     assertTrue(task.isDone());
90     assertFalse(task.isCancelled());
91     }
92    
93     public void testSetCancelled() {
94     MyCancellableTask task = new MyCancellableTask(new NoOpRunnable());
95     assertTrue(task.cancel(false));
96     task.setCancelled();
97     assertTrue(task.isDone());
98     assertTrue(task.isCancelled());
99 dl 1.1 }
100    
101     public void testCancelBeforeRun2() {
102 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
103 dl 1.1 assertTrue(task.cancel(true));
104     task.run();
105     assertTrue(task.isDone());
106     assertTrue(task.isCancelled());
107     }
108    
109     public void testCancelAfterRun() {
110 dl 1.3 CancellableTask task = new CancellableTask(new NoOpRunnable());
111 dl 1.1 task.run();
112     assertFalse(task.cancel(false));
113     assertTrue(task.isDone());
114     assertFalse(task.isCancelled());
115     }
116    
117     public void testCancelInterrupt(){
118 dl 1.3 CancellableTask task = new CancellableTask(new SmallInterruptedRunnable());
119 dl 1.1 Thread t = new Thread(task);
120    
121     try{
122 dl 1.3 t.start();
123 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
124     assertTrue(task.cancel(true));
125     t.join();
126     assertTrue(task.isDone());
127     assertTrue(task.isCancelled());
128     } catch(InterruptedException e){
129     fail("unexpected exception");
130     }
131     }
132    
133    
134     public void testCancelNoInterrupt(){
135 dl 1.3 CancellableTask task = new CancellableTask(new SmallRunnable());
136 dl 1.1 Thread t = new Thread(task);
137     try{
138 dl 1.3 t.start();
139 dl 1.1 Thread.sleep(SHORT_DELAY_MS);
140     assertTrue(task.cancel(false));
141     t.join();
142     assertTrue(task.isDone());
143     assertTrue(task.isCancelled());
144     } catch(InterruptedException e){
145     fail("unexpected exception");
146     }
147     }
148    
149    
150     }