41 |
|
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
– |
import junit.framework.AssertionFailedError; |
44 |
|
import junit.framework.Test; |
45 |
|
import junit.framework.TestSuite; |
46 |
|
|
58 |
|
void checkIncomplete(CompletableFuture<?> f) { |
59 |
|
assertFalse(f.isDone()); |
60 |
|
assertFalse(f.isCancelled()); |
61 |
< |
assertTrue(f.toString().contains("Not completed")); |
61 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
62 |
|
try { |
63 |
|
assertNull(f.getNow(null)); |
64 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
81 |
|
assertTrue(f.isDone()); |
82 |
|
assertFalse(f.isCancelled()); |
83 |
|
assertFalse(f.isCompletedExceptionally()); |
84 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
84 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
85 |
|
} |
86 |
|
|
87 |
|
/** |
137 |
|
assertFalse(f.isCancelled()); |
138 |
|
assertTrue(f.isDone()); |
139 |
|
assertTrue(f.isCompletedExceptionally()); |
140 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
140 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
141 |
|
} |
142 |
|
|
143 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
192 |
|
assertTrue(f.isDone()); |
193 |
|
assertTrue(f.isCompletedExceptionally()); |
194 |
|
assertTrue(f.isCancelled()); |
195 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
195 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
291 |
|
} |
292 |
|
|
293 |
|
f = new CompletableFuture<>(); |
294 |
< |
f.completeExceptionally(ex = new CFException()); |
294 |
> |
f.completeExceptionally(new CFException()); |
295 |
|
f.obtrudeValue(v1); |
296 |
|
checkCompletedNormally(f, v1); |
297 |
|
f.obtrudeException(ex = new CFException()); |
328 |
|
/** |
329 |
|
* toString indicates current completion state |
330 |
|
*/ |
331 |
< |
public void testToString() { |
332 |
< |
CompletableFuture<String> f; |
333 |
< |
|
334 |
< |
f = new CompletableFuture<String>(); |
335 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
331 |
> |
public void testToString_incomplete() { |
332 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
333 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
334 |
> |
if (testImplementationDetails) |
335 |
> |
assertEquals(identityString(f) + "[Not completed]", |
336 |
> |
f.toString()); |
337 |
> |
} |
338 |
|
|
339 |
+ |
public void testToString_normal() { |
340 |
+ |
CompletableFuture<String> f = new CompletableFuture<>(); |
341 |
|
assertTrue(f.complete("foo")); |
342 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
342 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
343 |
> |
if (testImplementationDetails) |
344 |
> |
assertEquals(identityString(f) + "[Completed normally]", |
345 |
> |
f.toString()); |
346 |
> |
} |
347 |
|
|
348 |
< |
f = new CompletableFuture<String>(); |
348 |
> |
public void testToString_exception() { |
349 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
350 |
|
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
351 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
351 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
352 |
> |
if (testImplementationDetails) |
353 |
> |
assertTrue(f.toString().startsWith( |
354 |
> |
identityString(f) + "[Completed exceptionally: ")); |
355 |
> |
} |
356 |
|
|
357 |
+ |
public void testToString_cancelled() { |
358 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
359 |
< |
f = new CompletableFuture<String>(); |
359 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
360 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
361 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
361 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
362 |
> |
if (testImplementationDetails) |
363 |
> |
assertTrue(f.toString().startsWith( |
364 |
> |
identityString(f) + "[Completed exceptionally: ")); |
365 |
|
} |
366 |
|
} |
367 |
|
|
4189 |
|
static void assertZero(CompletableFuture<?> f) { |
4190 |
|
try { |
4191 |
|
f.getNow(null); |
4192 |
< |
throw new AssertionFailedError("should throw"); |
4192 |
> |
throw new AssertionError("should throw"); |
4193 |
|
} catch (CompletionException success) { |
4194 |
|
assertTrue(success.getCause() instanceof ZeroException); |
4195 |
|
} |