Debian Codegraph @

src/test/java/io/reactivex/rxjava3/single/SingleNullTests.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.single;
15
16 import static org.junit.Assert.assertTrue;
17
18 import java.lang.reflect.*;
19 import java.util.*;
20 import java.util.concurrent.*;
21
22 import org.junit.*;
23 import org.reactivestreams.*;
24
25 import io.reactivex.rxjava3.core.*;
26 import io.reactivex.rxjava3.exceptions.*;
27 import io.reactivex.rxjava3.functions.*;
28 import io.reactivex.rxjava3.internal.functions.Functions;
29
30 public class SingleNullTests extends RxJavaTest {
31
32 Single<Integer> just1 = Single.just(1);
33
34 Single<Integer> error = Single.error(new TestException());
35
36 @Test
37 public void ambIterableIteratorNull() {
38 Single.amb(new Iterable<Single<Object>>() {
39 @Override
40 public Iterator<Single<Object>> iterator() {
41 return null;
42 }
43 }).test().assertError(NullPointerException.class);
44 }
45
46 @Test
47 public void ambIterableOneIsNull() {
48 Single.amb(Arrays.asList(null, just1))
49 .test()
50 .assertError(NullPointerException.class);
51 }
52
53 @Test
54 public void ambArrayOneIsNull() {
55 Single.ambArray(null, just1)
56 .test()
57 .assertError(NullPointerException.class);
58 }
59
60 @Test(expected = NullPointerException.class)
61 public void concatIterableIteratorNull() {
62 Single.concat(new Iterable<Single<Object>>() {
63 @Override
64 public Iterator<Single<Object>> iterator() {
65 return null;
66 }
67 }).blockingSubscribe();
68 }
69
70 @Test(expected = NullPointerException.class)
71 public void concatIterableOneIsNull() {
72 Single.concat(Arrays.asList(just1, null)).blockingSubscribe();
73 }
74
75 @Test
76 public void concatNull() throws Exception {
77 int maxArgs = 4;
78
79 @SuppressWarnings("rawtypes")
80 Class<Single> clazz = Single.class;
81 for (int argCount = 2; argCount <= maxArgs; argCount++) {
82 for (int argNull = 1; argNull <= argCount; argNull++) {
83 Class<?>[] params = new Class[argCount];
84 Arrays.fill(params, SingleSource.class);
85
86 Object[] values = new Object[argCount];
87 Arrays.fill(values, just1);
88 values[argNull - 1] = null;
89
90 Method m = clazz.getMethod("concat", params);
91
92 try {
93 m.invoke(null, values);
94 Assert.fail("No exception for argCount " + argCount + " / argNull " + argNull);
95 } catch (InvocationTargetException ex) {
96 if (!(ex.getCause() instanceof NullPointerException)) {
97 Assert.fail("Unexpected exception for argCount " + argCount + " / argNull " + argNull + ": " + ex);
98 }
99 }
100 }
101 }
102 }
103
104 @Test(expected = NullPointerException.class)
105 public void deferReturnsNull() {
106 Single.defer(Functions.<Single<Object>>nullSupplier()).blockingGet();
107 }
108
109 @Test(expected = NullPointerException.class)
110 public void errorSupplierReturnsNull() {
111 Single.error(Functions.<Throwable>nullSupplier()).blockingGet();
112 }
113
114 @Test(expected = NullPointerException.class)
115 public void fromCallableReturnsNull() {
116 Single.fromCallable(new Callable<Object>() {
117 @Override
118 public Object call() throws Exception {
119 return null;
120 }
121 }).blockingGet();
122 }
123
124 @Test(expected = NullPointerException.class)
125 public void fromFutureReturnsNull() {
126 FutureTask<Object> f = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
127 f.run();
128 Single.fromFuture(f).blockingGet();
129 }
130
131 @Test(expected = NullPointerException.class)
132 public void fromFutureTimedReturnsNull() {
133 FutureTask<Object> f = new FutureTask<>(Functions.EMPTY_RUNNABLE, null);
134 f.run();
135 Single.fromFuture(f, 1, TimeUnit.SECONDS).blockingGet();
136 }
137
138 @Test(expected = NullPointerException.class)
139 public void mergeIterableIteratorNull() {
140 Single.merge(new Iterable<Single<Object>>() {
141 @Override
142 public Iterator<Single<Object>> iterator() {
143 return null;
144 }
145 }).blockingSubscribe();
146 }
147
148 @Test(expected = NullPointerException.class)
149 public void mergeIterableOneIsNull() {
150 Single.merge(Arrays.asList(null, just1)).blockingSubscribe();
151 }
152
153 @Test
154 public void mergeNull() throws Exception {
155 int maxArgs = 4;
156
157 @SuppressWarnings("rawtypes")
158 Class<Single> clazz = Single.class;
159 for (int argCount = 2; argCount <= maxArgs; argCount++) {
160 for (int argNull = 1; argNull <= argCount; argNull++) {
161 Class<?>[] params = new Class[argCount];
162 Arrays.fill(params, SingleSource.class);
163
164 Object[] values = new Object[argCount];
165 Arrays.fill(values, just1);
166 values[argNull - 1] = null;
167
168 Method m = clazz.getMethod("merge", params);
169
170 try {
171 m.invoke(null, values);
172 Assert.fail("No exception for argCount " + argCount + " / argNull " + argNull);
173 } catch (InvocationTargetException ex) {
174 if (!(ex.getCause() instanceof NullPointerException)) {
175 Assert.fail("Unexpected exception for argCount " + argCount + " / argNull " + argNull + ": " + ex);
176 }
177 }
178 }
179 }
180 }
181
182 @Test(expected = NullPointerException.class)
183 public void usingSingleSupplierReturnsNull() {
184 Single.using(new Supplier<Object>() {
185 @Override
186 public Object get() {
187 return 1;
188 }
189 }, new Function<Object, Single<Object>>() {
190 @Override
191 public Single<Object> apply(Object d) {
192 return null;
193 }
194 }, Functions.emptyConsumer()).blockingGet();
195 }
196
197 @Test(expected = NullPointerException.class)
198 public void zipIterableIteratorNull() {
199 Single.zip(new Iterable<Single<Object>>() {
200 @Override
201 public Iterator<Single<Object>> iterator() {
202 return null;
203 }
204 }, new Function<Object[], Object>() {
205 @Override
206 public Object apply(Object[] v) {
207 return 1;
208 }
209 }).blockingGet();
210 }
211
212 @Test(expected = NullPointerException.class)
213 public void zipIterableOneIsNull() {
214 Single.zip(Arrays.asList(null, just1), new Function<Object[], Object>() {
215 @Override
216 public Object apply(Object[] v) {
217 return 1;
218 }
219 }).blockingGet();
220 }
221
222 @Test(expected = NullPointerException.class)
223 public void zipIterableOneFunctionReturnsNull() {
224 Single.zip(Arrays.asList(just1, just1), new Function<Object[], Object>() {
225 @Override
226 public Object apply(Object[] v) {
227 return null;
228 }
229 }).blockingGet();
230 }
231
232 @SuppressWarnings("unchecked")
233 @Test
234 public void zipNull() throws Exception {
235 @SuppressWarnings("rawtypes")
236 Class<Single> clazz = Single.class;
237 for (int argCount = 3; argCount < 10; argCount++) {
238 for (int argNull = 1; argNull <= argCount; argNull++) {
239 Class<?>[] params = new Class[argCount + 1];
240 Arrays.fill(params, SingleSource.class);
241 Class<?> fniClass = Class.forName("io.reactivex.rxjava3.functions.Function" + argCount);
242 params[argCount] = fniClass;
243
244 Object[] values = new Object[argCount + 1];
245 Arrays.fill(values, just1);
246 values[argNull - 1] = null;
247 values[argCount] = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { fniClass }, new InvocationHandler() {
248 @Override
249 public Object invoke(Object o, Method m, Object[] a) throws Throwable {
250 return 1;
251 }
252 });
253
254 Method m = clazz.getMethod("zip", params);
255
256 try {
257 m.invoke(null, values);
258 Assert.fail("No exception for argCount " + argCount + " / argNull " + argNull);
259 } catch (InvocationTargetException ex) {
260 if (!(ex.getCause() instanceof NullPointerException)) {
261 Assert.fail("Unexpected exception for argCount " + argCount + " / argNull " + argNull + ": " + ex);
262 }
263 }
264
265 values[argCount] = Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { fniClass }, new InvocationHandler() {
266 @Override
267 public Object invoke(Object o, Method m1, Object[] a) throws Throwable {
268 return null;
269 }
270 });
271 try {
272 ((Single<Object>)m.invoke(null, values)).blockingGet();
273 Assert.fail("No exception for argCount " + argCount + " / argNull " + argNull);
274 } catch (InvocationTargetException ex) {
275 if (!(ex.getCause() instanceof NullPointerException)) {
276 Assert.fail("Unexpected exception for argCount " + argCount + " / argNull " + argNull + ": " + ex);
277 }
278 }
279
280 }
281
282 Class<?>[] params = new Class[argCount + 1];
283 Arrays.fill(params, SingleSource.class);
284 Class<?> fniClass = Class.forName("io.reactivex.rxjava3.functions.Function" + argCount);
285 params[argCount] = fniClass;
286
287 Object[] values = new Object[argCount + 1];
288 Arrays.fill(values, just1);
289 values[argCount] = null;
290
291 Method m = clazz.getMethod("zip", params);
292
293 try {
294 m.invoke(null, values);
295 Assert.fail("No exception for argCount " + argCount + " / zipper function ");
296 } catch (InvocationTargetException ex) {
297 if (!(ex.getCause() instanceof NullPointerException)) {
298 Assert.fail("Unexpected exception for argCount " + argCount + " / zipper function: " + ex);
299 }
300 }
301 }
302 }
303
304 @Test(expected = NullPointerException.class)
305 public void zipIterableTwoIsNull() {
306 Single.zip(Arrays.asList(just1, null), new Function<Object[], Object>() {
307 @Override
308 public Object apply(Object[] v) {
309 return 1;
310 }
311 })
312 .blockingGet();
313 }
314
315 @Test(expected = NullPointerException.class)
316 public void zipArrayOneIsNull() {
317 Single.zipArray(new Function<Object[], Object>() {
318 @Override
319 public Object apply(Object[] v) {
320 return 1;
321 }
322 }, just1, null)
323 .blockingGet();
324 }
325
326 @Test(expected = NullPointerException.class)
327 public void zipArrayFunctionReturnsNull() {
328 Single.zipArray(new Function<Object[], Object>() {
329 @Override
330 public Object apply(Object[] v) {
331 return null;
332 }
333 }, just1, just1).blockingGet();
334 }
335
336 //**************************************************
337 // Instance methods
338 //**************************************************
339
340 @Test(expected = NullPointerException.class)
341 public void flatMapFunctionReturnsNull() {
342 just1.flatMap(new Function<Integer, Single<Object>>() {
343 @Override
344 public Single<Object> apply(Integer v) {
345 return null;
346 }
347 }).blockingGet();
348 }
349
350 @Test(expected = NullPointerException.class)
351 public void flatMapPublisherFunctionReturnsNull() {
352 just1.flatMapPublisher(new Function<Integer, Publisher<Object>>() {
353 @Override
354 public Publisher<Object> apply(Integer v) {
355 return null;
356 }
357 }).blockingSubscribe();
358 }
359
360 @Test(expected = NullPointerException.class)
361 public void liftFunctionReturnsNull() {
362 just1.lift(new SingleOperator<Object, Integer>() {
363 @Override
364 public SingleObserver<? super Integer> apply(SingleObserver<? super Object> observer) {
365 return null;
366 }
367 }).blockingGet();
368 }
369
370 @Test(expected = NullPointerException.class)
371 public void onErrorReturnsSupplierReturnsNull() {
372 error.onErrorReturn(new Function<Throwable, Integer>() {
373 @Override
374 public Integer apply(Throwable t) throws Exception {
375 return null;
376 }
377 }).blockingGet();
378 }
379
380 @Test
381 public void onErrorResumeNextFunctionReturnsNull() {
382 try {
383 error.onErrorResumeNext(new Function<Throwable, Single<Integer>>() {
384 @Override
385 public Single<Integer> apply(Throwable e) {
386 return null;
387 }
388 }).blockingGet();
389 } catch (CompositeException ex) {
390 assertTrue(ex.toString(), ex.getExceptions().get(1) instanceof NullPointerException);
391 }
392 }
393
394 @Test(expected = NullPointerException.class)
395 public void repeatWhenFunctionReturnsNull() {
396 error.repeatWhen(new Function<Flowable<Object>, Publisher<Object>>() {
397 @Override
398 public Publisher<Object> apply(Flowable<Object> v) {
399 return null;
400 }
401 }).blockingSubscribe();
402 }
403
404 @Test(expected = NullPointerException.class)
405 public void retryWhenFunctionReturnsNull() {
406 error.retryWhen(new Function<Flowable<? extends Throwable>, Publisher<Object>>() {
407 @Override
408 public Publisher<Object> apply(Flowable<? extends Throwable> e) {
409 return null;
410 }
411 }).blockingGet();
412 }
413
414 @Test(expected = NullPointerException.class)
415 public void subscribeOnSuccessNull() {
416 just1.subscribe(null, new Consumer<Throwable>() {
417 @Override
418 public void accept(Throwable e) { }
419 });
420 }
421
422 @Test(expected = NullPointerException.class)
423 public void zipWithFunctionReturnsNull() {
424 just1.zipWith(just1, new BiFunction<Integer, Integer, Object>() {
425 @Override
426 public Object apply(Integer a, Integer b) {
427 return null;
428 }
429 }).blockingGet();
430 }
431 }