18
18
import static org .junit .Assert .*;
19
19
import static org .mockito .Mockito .*;
20
20
21
- import java .lang .reflect .Array ;
22
- import java .util .ArrayList ;
23
- import java .util .List ;
24
- import java .util .concurrent .CountDownLatch ;
25
-
21
+ import org .junit .Before ;
26
22
import org .junit .Test ;
27
23
28
24
import rx .Observable ;
@@ -47,9 +43,10 @@ public final class OperationFinally {
47
43
* @param sequence An observable sequence of elements
48
44
* @param action An action to be taken when the sequence is complete or throws an exception
49
45
* @return An observable sequence with the same elements as the input.
50
- * After the last element is consumed (just before {@link Observer#onComplete} is called),
51
- * or when an exception is thrown (just before {@link Observer#onError}), the action will be called.
52
- * @see http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx
46
+ * After the last element is consumed (and {@link Observer#onCompleted} has been called),
47
+ * or after an exception is thrown (and {@link Observer#onError} has been called),
48
+ * the given action will be called.
49
+ * @see <a href="http://msdn.microsoft.com/en-us/library/hh212133(v=vs.103).aspx">MSDN Observable.Finally method</a>
53
50
*/
54
51
public static <T > Func1 <Observer <T >, Subscription > finally0 (final Observable <T > sequence , final Action0 action ) {
55
52
return new Func1 <Observer <T >, Subscription >() {
@@ -94,14 +91,14 @@ private class FinallyObserver implements Observer<T> {
94
91
95
92
@ Override
96
93
public void onCompleted () {
97
- finalAction .call ();
98
94
observer .onCompleted ();
95
+ finalAction .call ();
99
96
}
100
97
101
98
@ Override
102
99
public void onError (Exception e ) {
103
- finalAction .call ();
104
100
observer .onError (e );
101
+ finalAction .call ();
105
102
}
106
103
107
104
@ Override
@@ -112,30 +109,24 @@ public void onNext(T args) {
112
109
}
113
110
114
111
public static class UnitTest {
115
- private static class TestAction implements Action0 {
116
- public int called = 0 ;
117
- @ Override public void call () {
118
- called ++;
119
- }
112
+ private Action0 aAction0 ;
113
+ private Observer <String > aObserver ;
114
+ @ Before
115
+ public void before () {
116
+ aAction0 = mock (Action0 .class );
117
+ aObserver = mock (Observer .class );
118
+ }
119
+ private void checkActionCalled (Observable <String > input ) {
120
+ Observable .create (finally0 (input , aAction0 )).subscribe (aObserver );
121
+ verify (aAction0 , times (1 )).call ();
122
+ }
123
+ @ Test
124
+ public void testFinallyCalledOnComplete () {
125
+ checkActionCalled (Observable .toObservable (new String [] {"1" , "2" , "3" }));
120
126
}
121
-
122
127
@ Test
123
- public void testFinally () {
124
- final String [] n = {"1" , "2" , "3" };
125
- final Observable <String > nums = Observable .toObservable (n );
126
- TestAction action = new TestAction ();
127
- action .called = 0 ;
128
- Observable <String > fin = Observable .create (finally0 (nums , action ));
129
- @ SuppressWarnings ("unchecked" )
130
- Observer <String > aObserver = mock (Observer .class );
131
- fin .subscribe (aObserver );
132
- assertEquals (1 , action .called );
133
-
134
- action .called = 0 ;
135
- Observable <String > error = Observable .<String >error (new RuntimeException ("expected" ));
136
- fin = Observable .create (finally0 (error , action ));
137
- fin .subscribe (aObserver );
138
- assertEquals (1 , action .called );
128
+ public void testFinallyCalledOnError () {
129
+ checkActionCalled (Observable .<String >error (new RuntimeException ("expected" )));
139
130
}
140
131
}
141
132
}
0 commit comments