ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
Revision: 1.4
Committed: Sun Feb 10 21:18:25 2013 UTC (11 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +53 -4 lines
Log Message:
add testAllOf_empty, testAnyOf_empty

File Contents

# Content
1 /*
2 * Written by Doug Lea and Martin Buchholz with assistance from
3 * members of JCP JSR-166 Expert Group and released to the public
4 * domain, as explained at
5 * http://creativecommons.org/publicdomain/zero/1.0/
6 */
7
8 import junit.framework.*;
9 import java.util.concurrent.Callable;
10 import java.util.concurrent.CancellationException;
11 import java.util.concurrent.CountDownLatch;
12 import java.util.concurrent.ExecutionException;
13 import java.util.concurrent.Future;
14 import java.util.concurrent.CompletableFuture;
15 import java.util.concurrent.TimeoutException;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import static java.util.concurrent.TimeUnit.MILLISECONDS;
18 import static java.util.concurrent.TimeUnit.SECONDS;
19 import java.util.*;
20
21 public class CompletableFutureTest extends JSR166TestCase {
22
23 public static void main(String[] args) {
24 junit.textui.TestRunner.run(suite());
25 }
26 public static Test suite() {
27 return new TestSuite(CompletableFutureTest.class);
28 }
29
30 void checkIncomplete(CompletableFuture<?> f) {
31 assertFalse(f.isDone());
32 assertFalse(f.isCancelled());
33 assertTrue(f.toString().contains("[Not completed]"));
34 try {
35 assertNull(f.getNow(null));
36 } catch (Throwable fail) { threadUnexpectedException(fail); }
37 try {
38 f.get(0L, SECONDS);
39 shouldThrow();
40 }
41 catch (TimeoutException success) {}
42 catch (Throwable fail) { threadUnexpectedException(fail); }
43 }
44
45 void checkCompletedNormally(CompletableFuture<?> f, Object value) {
46 assertTrue(f.isDone());
47 assertFalse(f.isCancelled());
48 assertTrue(f.toString().contains("[Completed normally]"));
49 try {
50 assertSame(value, f.join());
51 } catch (Throwable fail) { threadUnexpectedException(fail); }
52 try {
53 assertSame(value, f.getNow(null));
54 } catch (Throwable fail) { threadUnexpectedException(fail); }
55 try {
56 assertSame(value, f.get());
57 } catch (Throwable fail) { threadUnexpectedException(fail); }
58 try {
59 assertSame(value, f.get(0L, SECONDS));
60 } catch (Throwable fail) { threadUnexpectedException(fail); }
61 }
62
63 // XXXX Just a skeleton implementation for now.
64 public void testTODO() {
65 fail("Please add some real tests!");
66 }
67
68 public void testToString() {
69 CompletableFuture<String> f;
70
71 f = new CompletableFuture<String>();
72 assertTrue(f.toString().contains("[Not completed]"));
73
74 f.complete("foo");
75 assertTrue(f.toString().contains("[Completed normally]"));
76
77 f = new CompletableFuture<String>();
78 f.completeExceptionally(new IndexOutOfBoundsException());
79 assertTrue(f.toString().contains("[Completed exceptionally]"));
80 }
81
82 /**
83 * allOf(no component futures) returns a future completed normally
84 * with the value null
85 */
86 public void testAllOf_empty() throws Exception {
87 CompletableFuture<?> f = CompletableFuture.allOf();
88 checkCompletedNormally(f, null);
89 }
90
91 /**
92 * anyOf(no component futures) returns an incomplete future
93 */
94 public void testAnyOf_empty() throws Exception {
95 CompletableFuture<?> f = CompletableFuture.anyOf();
96 checkIncomplete(f);
97 }
98 }