1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.concurrent;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.Future;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.TimeoutException;
29 import java.util.stream.Collectors;
30
31 import org.apache.commons.lang3.AbstractLangTest;
32 import org.apache.commons.lang3.exception.UncheckedInterruptedException;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 public class UncheckedFutureTest extends AbstractLangTest {
39
40 private static final class TestFuture<V> extends AbstractFutureProxy<V> {
41
42 private final Exception exception;
43
44 TestFuture(final Exception throwable) {
45 super(ConcurrentUtils.constantFuture(null));
46 this.exception = throwable;
47 }
48
49 TestFuture(final V value) {
50 super(ConcurrentUtils.constantFuture(value));
51 this.exception = null;
52 }
53
54 @SuppressWarnings("unchecked")
55 private <T extends Exception> void checkException() throws T {
56 if (exception != null) {
57 throw (T) exception;
58 }
59 }
60
61 @Override
62 public V get() throws InterruptedException, ExecutionException {
63 checkException();
64 return super.get();
65 }
66
67 @Override
68 public V get(final long timeout, final TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
69 checkException();
70 return super.get(timeout, unit);
71 }
72
73 }
74
75 @Test
76 public void testGetExecutionException() {
77 final ExecutionException e = new ExecutionException(new Exception());
78 assertThrows(UncheckedExecutionException.class, () -> UncheckedFuture.on(new TestFuture<>(e)).get());
79 }
80
81 @Test
82 public void testGetInterruptedException() {
83 final InterruptedException e = new InterruptedException();
84 assertThrows(UncheckedInterruptedException.class, () -> UncheckedFuture.on(new TestFuture<>(e)).get());
85 }
86
87 @Test
88 public void testGetLongExecutionException() {
89 final ExecutionException e = new ExecutionException(new Exception());
90 assertThrows(UncheckedExecutionException.class, () -> UncheckedFuture.on(new TestFuture<>(e)).get(1, TimeUnit.MICROSECONDS));
91 }
92
93 @Test
94 public void testGetLongInterruptedException() {
95 final InterruptedException e = new InterruptedException();
96 assertThrows(UncheckedInterruptedException.class, () -> UncheckedFuture.on(new TestFuture<>(e)).get(1, TimeUnit.MICROSECONDS));
97 }
98
99 @Test
100 public void testGetLongTimeoutException() {
101 final TimeoutException e = new TimeoutException();
102 assertThrows(UncheckedTimeoutException.class, () -> UncheckedFuture.on(new TestFuture<>(e)).get(1, TimeUnit.MICROSECONDS));
103 }
104
105 @Test
106 public void testMap() {
107 final List<String> expected = Arrays.asList("Y", "Z");
108 final List<Future<String>> input = Arrays.asList(new TestFuture<>("Y"), new TestFuture<>("Z"));
109 assertEquals(expected, UncheckedFuture.map(input).map(UncheckedFuture::get).collect(Collectors.toList()));
110 }
111
112 @Test
113 public void testOnCollection() {
114 final List<String> expected = Arrays.asList("Y", "Z");
115 final List<Future<String>> input = Arrays.asList(new TestFuture<>("Y"), new TestFuture<>("Z"));
116 assertEquals(expected, UncheckedFuture.on(input).stream().map(UncheckedFuture::get).collect(Collectors.toList()));
117 }
118
119 @Test
120 public void testOnFuture() {
121 assertEquals("Z", UncheckedFuture.on(new TestFuture<>("Z")).get());
122 }
123
124 }