11/*
2- * Copyright 2002-2015 the original author or authors.
2+ * Copyright 2002-2016 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
2121import java .util .concurrent .TimeUnit ;
2222import java .util .concurrent .TimeoutException ;
2323
24- import org .junit .Before ;
2524import org .junit .Test ;
2625
2726import static org .hamcrest .Matchers .*;
3534@ SuppressWarnings ({ "rawtypes" , "unchecked" })
3635public class SettableListenableFutureTests {
3736
38- private SettableListenableFuture <String > settableListenableFuture ;
37+ private final SettableListenableFuture <String > settableListenableFuture = new SettableListenableFuture < String >() ;
3938
40- @ Before
41- public void setUp () {
42- settableListenableFuture = new SettableListenableFuture <String >();
43- }
4439
4540 @ Test
4641 public void validateInitialValues () {
@@ -76,6 +71,20 @@ public void throwsSetExceptionWrappedInExecutionException() throws ExecutionExce
7671 }
7772 }
7873
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+
7988 @ Test
8089 public void setValueTriggersCallback () {
8190 String string = "hello" ;
@@ -85,7 +94,6 @@ public void setValueTriggersCallback() {
8594 public void onSuccess (String result ) {
8695 callbackHolder [0 ] = result ;
8796 }
88-
8997 @ Override
9098 public void onFailure (Throwable ex ) {
9199 fail ("Expected onSuccess() to be called" );
@@ -104,7 +112,6 @@ public void setValueTriggersCallbackOnlyOnce() {
104112 public void onSuccess (String result ) {
105113 callbackHolder [0 ] = result ;
106114 }
107-
108115 @ Override
109116 public void onFailure (Throwable ex ) {
110117 fail ("Expected onSuccess() to be called" );
@@ -124,7 +131,6 @@ public void setExceptionTriggersCallback() {
124131 public void onSuccess (String result ) {
125132 fail ("Expected onFailure() to be called" );
126133 }
127-
128134 @ Override
129135 public void onFailure (Throwable ex ) {
130136 callbackHolder [0 ] = ex ;
@@ -143,7 +149,6 @@ public void setExceptionTriggersCallbackOnlyOnce() {
143149 public void onSuccess (String result ) {
144150 fail ("Expected onFailure() to be called" );
145151 }
146-
147152 @ Override
148153 public void onFailure (Throwable ex ) {
149154 callbackHolder [0 ] = ex ;
@@ -169,7 +174,8 @@ public void run() {
169174 try {
170175 Thread .sleep (20L );
171176 settableListenableFuture .set (string );
172- } catch (InterruptedException ex ) {
177+ }
178+ catch (InterruptedException ex ) {
173179 throw new RuntimeException (ex );
174180 }
175181 }
@@ -183,7 +189,8 @@ public void getWithTimeoutThrowsTimeoutException() throws ExecutionException, In
183189 try {
184190 settableListenableFuture .get (1L , TimeUnit .MILLISECONDS );
185191 fail ("Expected TimeoutException" );
186- } catch (TimeoutException ex ) {
192+ }
193+ catch (TimeoutException ex ) {
187194 // expected
188195 }
189196 }
@@ -197,7 +204,8 @@ public void run() {
197204 try {
198205 Thread .sleep (20L );
199206 settableListenableFuture .set (string );
200- } catch (InterruptedException ex ) {
207+ }
208+ catch (InterruptedException ex ) {
201209 throw new RuntimeException (ex );
202210 }
203211 }
@@ -278,15 +286,17 @@ public void run() {
278286 try {
279287 Thread .sleep (20L );
280288 settableListenableFuture .cancel (true );
281- } catch (InterruptedException ex ) {
289+ }
290+ catch (InterruptedException ex ) {
282291 throw new RuntimeException (ex );
283292 }
284293 }
285294 }).start ();
286295 try {
287296 settableListenableFuture .get (100L , TimeUnit .MILLISECONDS );
288297 fail ("Expected CancellationException" );
289- } catch (CancellationException ex ) {
298+ }
299+ catch (CancellationException ex ) {
290300 // expected
291301 }
292302 }
@@ -317,6 +327,7 @@ public void cancelDoesNotNotifyCallbacksOnSetException() {
317327 verifyNoMoreInteractions (callback );
318328 }
319329
330+
320331 private static class InterruptableSettableListenableFuture extends SettableListenableFuture <String > {
321332
322333 private boolean interrupted = false ;
@@ -330,4 +341,5 @@ boolean calledInterruptTask() {
330341 return interrupted ;
331342 }
332343 }
344+
333345}
0 commit comments