forked from ReactiveX/RxJava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRxJavaHooksTest.java
More file actions
165 lines (129 loc) · 5.26 KB
/
Copy pathRxJavaHooksTest.java
File metadata and controls
165 lines (129 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* Copyright 2016 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.plugins;
import java.lang.reflect.Method;
import org.junit.*;
import rx.*;
import rx.exceptions.*;
import rx.functions.*;
import rx.internal.util.UtilityFunctions;
import rx.observers.TestSubscriber;
public class RxJavaHooksTest {
static Observable<Integer> createObservable() {
return Observable.range(1, 5).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer t) {
throw new TestException();
}
});
}
@Test
public void assemblyTrackingObservable() {
RxJavaHooks.enableAssemblyTracking();
try {
TestSubscriber<Integer> ts = TestSubscriber.create();
createObservable().subscribe(ts);
ts.assertError(AssemblyStackTraceException.class);
Throwable ex = ts.getOnErrorEvents().get(0);
Assert.assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException);
Assert.assertTrue("" + ex, ex instanceof AssemblyStackTraceException);
Assert.assertTrue(ex.getMessage(), ex.getMessage().contains("createObservable"));
} finally {
RxJavaHooks.resetAssemblyTracking();
}
}
static Single<Integer> createSingle() {
return Single.just(1).map(new Func1<Integer, Integer>() {
@Override
public Integer call(Integer t) {
throw new TestException();
}
});
}
@Test
public void assemblyTrackingSingle() {
RxJavaHooks.enableAssemblyTracking();
try {
TestSubscriber<Integer> ts = TestSubscriber.create();
createSingle().subscribe(ts);
ts.assertError(AssemblyStackTraceException.class);
Throwable ex = ts.getOnErrorEvents().get(0);
Assert.assertTrue("" + ex, ex instanceof AssemblyStackTraceException);
Assert.assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException);
Assert.assertTrue(ex.getMessage(), ex.getMessage().contains("createSingle"));
} finally {
RxJavaHooks.resetAssemblyTracking();
}
}
static Completable createCompletable() {
return Completable.error(new Func0<Throwable>() {
@Override
public Throwable call() {
return new TestException();
}
});
}
@Test
public void assemblyTrackingCompletable() {
RxJavaHooks.enableAssemblyTracking();
try {
TestSubscriber<Integer> ts = TestSubscriber.create();
createCompletable().subscribe(ts);
ts.assertError(AssemblyStackTraceException.class);
Throwable ex = ts.getOnErrorEvents().get(0);
Assert.assertTrue("" + ex, ex instanceof AssemblyStackTraceException);
Assert.assertTrue("" + ex.getCause(), ex.getCause() instanceof TestException);
Assert.assertTrue(ex.getMessage(), ex.getMessage().contains("createCompletable"));
} finally {
RxJavaHooks.resetAssemblyTracking();
}
}
@SuppressWarnings("rawtypes")
@Test
public void lockdown() throws Exception {
RxJavaHooks.reset();
RxJavaHooks.lockdown();
try {
Action1 a1 = Actions.empty();
Func1 f1 = UtilityFunctions.identity();
Func2 f2 = new Func2() {
@Override
public Object call(Object t1, Object t2) {
return t2;
}
};
for (Method m : RxJavaHooks.class.getMethods()) {
if (m.getName().startsWith("setOn")) {
Method getter = RxJavaHooks.class.getMethod("get" + m.getName().substring(3));
Object before = getter.invoke(null);
if (m.getParameterTypes()[0].isAssignableFrom(Func1.class)) {
m.invoke(null, f1);
} else
if (m.getParameterTypes()[0].isAssignableFrom(Action1.class)) {
m.invoke(null, a1);
} else {
m.invoke(null, f2);
}
Object after = getter.invoke(null);
Assert.assertSame(m.toString(), before, after);
}
}
} finally {
RxJavaHooks.lockdown = false;
RxJavaHooks.reset();
}
}
}