Skip to content

Commit 12954a6

Browse files
committed
Extract UnsubscribeTester to top level
1 parent 62c29d2 commit 12954a6

File tree

2 files changed

+110
-103
lines changed

2 files changed

+110
-103
lines changed

rxjava-core/src/main/java/rx/subjects/RepeatSubject.java

Lines changed: 7 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import org.junit.Test;
44
import org.mockito.Mockito;
5-
import rx.Observable;
65
import rx.Observer;
76
import rx.Subscription;
87
import rx.subscriptions.Subscriptions;
8+
import rx.testing.UnsubscribeTester;
99
import rx.util.functions.Func1;
1010

1111
import java.util.ArrayList;
@@ -14,7 +14,6 @@
1414
import java.util.List;
1515
import java.util.Map;
1616

17-
import static org.junit.Assert.assertTrue;
1817
import static org.mockito.Matchers.any;
1918
import static org.mockito.Mockito.mock;
2019
import static org.mockito.Mockito.times;
@@ -270,8 +269,8 @@ private void assertObservedUntilTwo(Observer<String> aObserver)
270269
public void testUnsubscribeFromOnNext() {
271270
RepeatSubject<Object> subject = RepeatSubject.create();
272271

273-
UnsubscribeTest test1 = UnsubscribeTest.createOnNext(subject);
274-
UnsubscribeTest test2 = UnsubscribeTest.createOnNext(subject);
272+
UnsubscribeTester test1 = UnsubscribeTester.createOnNext(subject);
273+
UnsubscribeTester test2 = UnsubscribeTester.createOnNext(subject);
275274

276275
subject.onNext("one");
277276

@@ -283,8 +282,8 @@ public void testUnsubscribeFromOnNext() {
283282
public void testUnsubscribeFromOnCompleted() {
284283
RepeatSubject<Object> subject = RepeatSubject.create();
285284

286-
UnsubscribeTest test1 = UnsubscribeTest.createOnCompleted(subject);
287-
UnsubscribeTest test2 = UnsubscribeTest.createOnCompleted(subject);
285+
UnsubscribeTester test1 = UnsubscribeTester.createOnCompleted(subject);
286+
UnsubscribeTester test2 = UnsubscribeTester.createOnCompleted(subject);
288287

289288
subject.onCompleted();
290289

@@ -296,109 +295,14 @@ public void testUnsubscribeFromOnCompleted() {
296295
public void testUnsubscribeFromOnError() {
297296
RepeatSubject<Object> subject = RepeatSubject.create();
298297

299-
UnsubscribeTest test1 = UnsubscribeTest.createOnError(subject);
300-
UnsubscribeTest test2 = UnsubscribeTest.createOnError(subject);
298+
UnsubscribeTester test1 = UnsubscribeTester.createOnError(subject);
299+
UnsubscribeTester test2 = UnsubscribeTester.createOnError(subject);
301300

302301
subject.onError(new Exception());
303302

304303
test1.assertPassed();
305304
test2.assertPassed();
306305
}
307306

308-
private static class UnsubscribeTest
309-
{
310-
private Subscription subscription;
311-
312-
private UnsubscribeTest() {}
313-
314-
public static <T> UnsubscribeTest createOnNext(Observable<T> observable)
315-
{
316-
final UnsubscribeTest test = new UnsubscribeTest();
317-
test.setSubscription(observable.subscribe(new Observer<T>()
318-
{
319-
@Override
320-
public void onCompleted()
321-
{
322-
}
323-
324-
@Override
325-
public void onError(Exception e)
326-
{
327-
}
328-
329-
@Override
330-
public void onNext(T args)
331-
{
332-
test.doUnsubscribe();
333-
}
334-
}));
335-
return test;
336-
}
337-
338-
public static <T> UnsubscribeTest createOnCompleted(Observable<T> observable)
339-
{
340-
final UnsubscribeTest test = new UnsubscribeTest();
341-
test.setSubscription(observable.subscribe(new Observer<T>()
342-
{
343-
@Override
344-
public void onCompleted()
345-
{
346-
test.doUnsubscribe();
347-
}
348-
349-
@Override
350-
public void onError(Exception e)
351-
{
352-
}
353-
354-
@Override
355-
public void onNext(T args)
356-
{
357-
}
358-
}));
359-
return test;
360-
}
361-
362-
public static <T> UnsubscribeTest createOnError(Observable<T> observable)
363-
{
364-
final UnsubscribeTest test = new UnsubscribeTest();
365-
test.setSubscription(observable.subscribe(new Observer<T>()
366-
{
367-
@Override
368-
public void onCompleted()
369-
{
370-
}
371-
372-
@Override
373-
public void onError(Exception e)
374-
{
375-
test.doUnsubscribe();
376-
}
377-
378-
@Override
379-
public void onNext(T args)
380-
{
381-
}
382-
}));
383-
return test;
384-
}
385-
386-
private void setSubscription(Subscription subscription)
387-
{
388-
this.subscription = subscription;
389-
}
390-
391-
private void doUnsubscribe()
392-
{
393-
Subscription subscription = this.subscription;
394-
this.subscription = null;
395-
subscription.unsubscribe();
396-
}
397-
398-
public void assertPassed()
399-
{
400-
assertTrue("expected notification was received", subscription == null);
401-
}
402-
}
403307
}
404308
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package rx.testing;
2+
3+
import rx.Observable;
4+
import rx.Observer;
5+
import rx.Subscription;
6+
7+
import static org.junit.Assert.assertTrue;
8+
9+
public class UnsubscribeTester
10+
{
11+
private Subscription subscription;
12+
13+
public UnsubscribeTester() {}
14+
15+
public static <T> UnsubscribeTester createOnNext(Observable<T> observable)
16+
{
17+
final UnsubscribeTester test = new UnsubscribeTester();
18+
test.setSubscription(observable.subscribe(new Observer<T>()
19+
{
20+
@Override
21+
public void onCompleted()
22+
{
23+
}
24+
25+
@Override
26+
public void onError(Exception e)
27+
{
28+
}
29+
30+
@Override
31+
public void onNext(T args)
32+
{
33+
test.doUnsubscribe();
34+
}
35+
}));
36+
return test;
37+
}
38+
39+
public static <T> UnsubscribeTester createOnCompleted(Observable<T> observable)
40+
{
41+
final UnsubscribeTester test = new UnsubscribeTester();
42+
test.setSubscription(observable.subscribe(new Observer<T>()
43+
{
44+
@Override
45+
public void onCompleted()
46+
{
47+
test.doUnsubscribe();
48+
}
49+
50+
@Override
51+
public void onError(Exception e)
52+
{
53+
}
54+
55+
@Override
56+
public void onNext(T args)
57+
{
58+
}
59+
}));
60+
return test;
61+
}
62+
63+
public static <T> UnsubscribeTester createOnError(Observable<T> observable)
64+
{
65+
final UnsubscribeTester test = new UnsubscribeTester();
66+
test.setSubscription(observable.subscribe(new Observer<T>()
67+
{
68+
@Override
69+
public void onCompleted()
70+
{
71+
}
72+
73+
@Override
74+
public void onError(Exception e)
75+
{
76+
test.doUnsubscribe();
77+
}
78+
79+
@Override
80+
public void onNext(T args)
81+
{
82+
}
83+
}));
84+
return test;
85+
}
86+
87+
private void setSubscription(Subscription subscription)
88+
{
89+
this.subscription = subscription;
90+
}
91+
92+
private void doUnsubscribe()
93+
{
94+
Subscription subscription = this.subscription;
95+
this.subscription = null;
96+
subscription.unsubscribe();
97+
}
98+
99+
public void assertPassed()
100+
{
101+
assertTrue("expected notification was received", subscription == null);
102+
}
103+
}

0 commit comments

Comments
 (0)