1
1
/*
2
- * Copyright 2002-2015 the original author or authors.
2
+ * Copyright 2002-2016 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
21
21
import java .util .concurrent .TimeUnit ;
22
22
import java .util .concurrent .TimeoutException ;
23
23
24
- import org .junit .Before ;
25
24
import org .junit .Test ;
26
25
27
26
import static org .hamcrest .Matchers .*;
35
34
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
36
35
public class SettableListenableFutureTests {
37
36
38
- private SettableListenableFuture <String > settableListenableFuture ;
37
+ private final SettableListenableFuture <String > settableListenableFuture = new SettableListenableFuture < String >() ;
39
38
40
- @ Before
41
- public void setUp () {
42
- settableListenableFuture = new SettableListenableFuture <String >();
43
- }
44
39
45
40
@ Test
46
41
public void validateInitialValues () {
@@ -76,6 +71,20 @@ public void throwsSetExceptionWrappedInExecutionException() throws ExecutionExce
76
71
}
77
72
}
78
73
74
+ @ Test
75
+ public void throwsSetErrorWrappedInExecutionException () throws ExecutionException , InterruptedException {
76
+ Throwable exception = new OutOfMemoryError ();
77
+ boolean wasSet = settableListenableFuture .setException (exception );
78
+ assertTrue (wasSet );
79
+ try {
80
+ settableListenableFuture .get ();
81
+ fail ("Expected ExecutionException" );
82
+ }
83
+ catch (ExecutionException ex ) {
84
+ assertThat (ex .getCause (), equalTo (exception ));
85
+ }
86
+ }
87
+
79
88
@ Test
80
89
public void setValueTriggersCallback () {
81
90
String string = "hello" ;
@@ -85,7 +94,6 @@ public void setValueTriggersCallback() {
85
94
public void onSuccess (String result ) {
86
95
callbackHolder [0 ] = result ;
87
96
}
88
-
89
97
@ Override
90
98
public void onFailure (Throwable ex ) {
91
99
fail ("Expected onSuccess() to be called" );
@@ -104,7 +112,6 @@ public void setValueTriggersCallbackOnlyOnce() {
104
112
public void onSuccess (String result ) {
105
113
callbackHolder [0 ] = result ;
106
114
}
107
-
108
115
@ Override
109
116
public void onFailure (Throwable ex ) {
110
117
fail ("Expected onSuccess() to be called" );
@@ -124,7 +131,6 @@ public void setExceptionTriggersCallback() {
124
131
public void onSuccess (String result ) {
125
132
fail ("Expected onFailure() to be called" );
126
133
}
127
-
128
134
@ Override
129
135
public void onFailure (Throwable ex ) {
130
136
callbackHolder [0 ] = ex ;
@@ -143,7 +149,6 @@ public void setExceptionTriggersCallbackOnlyOnce() {
143
149
public void onSuccess (String result ) {
144
150
fail ("Expected onFailure() to be called" );
145
151
}
146
-
147
152
@ Override
148
153
public void onFailure (Throwable ex ) {
149
154
callbackHolder [0 ] = ex ;
@@ -169,7 +174,8 @@ public void run() {
169
174
try {
170
175
Thread .sleep (20L );
171
176
settableListenableFuture .set (string );
172
- } catch (InterruptedException ex ) {
177
+ }
178
+ catch (InterruptedException ex ) {
173
179
throw new RuntimeException (ex );
174
180
}
175
181
}
@@ -183,7 +189,8 @@ public void getWithTimeoutThrowsTimeoutException() throws ExecutionException, In
183
189
try {
184
190
settableListenableFuture .get (1L , TimeUnit .MILLISECONDS );
185
191
fail ("Expected TimeoutException" );
186
- } catch (TimeoutException ex ) {
192
+ }
193
+ catch (TimeoutException ex ) {
187
194
// expected
188
195
}
189
196
}
@@ -197,7 +204,8 @@ public void run() {
197
204
try {
198
205
Thread .sleep (20L );
199
206
settableListenableFuture .set (string );
200
- } catch (InterruptedException ex ) {
207
+ }
208
+ catch (InterruptedException ex ) {
201
209
throw new RuntimeException (ex );
202
210
}
203
211
}
@@ -278,15 +286,17 @@ public void run() {
278
286
try {
279
287
Thread .sleep (20L );
280
288
settableListenableFuture .cancel (true );
281
- } catch (InterruptedException ex ) {
289
+ }
290
+ catch (InterruptedException ex ) {
282
291
throw new RuntimeException (ex );
283
292
}
284
293
}
285
294
}).start ();
286
295
try {
287
296
settableListenableFuture .get (100L , TimeUnit .MILLISECONDS );
288
297
fail ("Expected CancellationException" );
289
- } catch (CancellationException ex ) {
298
+ }
299
+ catch (CancellationException ex ) {
290
300
// expected
291
301
}
292
302
}
@@ -317,6 +327,7 @@ public void cancelDoesNotNotifyCallbacksOnSetException() {
317
327
verifyNoMoreInteractions (callback );
318
328
}
319
329
330
+
320
331
private static class InterruptableSettableListenableFuture extends SettableListenableFuture <String > {
321
332
322
333
private boolean interrupted = false ;
@@ -330,4 +341,5 @@ boolean calledInterruptTask() {
330
341
return interrupted ;
331
342
}
332
343
}
344
+
333
345
}
0 commit comments