@Override protected void write(Schema schema, Object datum, Encoder out) throws IOException { if (datum instanceof Byte) datum = ((Byte)datum).intValue(); else if (datum instanceof Short) datum = ((Short)datum).intValue(); else if (datum instanceof Character) datum = (int)(char)(Character)datum; else if (datum instanceof Map && ReflectData.isNonStringMapSchema(schema)) { // Maps with non-string keys are written as arrays. // Schema for such maps is already changed. Here we // just switch the map to a similar form too. Set entries = ((Map)datum).entrySet(); List<Map.Entry> entryList = new ArrayList<>(entries.size()); for (Object obj: ((Map)datum).entrySet()) { Map.Entry e = (Map.Entry)obj; entryList.add(new MapEntry(e.getKey(), e.getValue())); } datum = entryList; } try { super.write(schema, datum, out); } catch (NullPointerException e) { // improve error message NullPointerException result = new NullPointerException("in "+schema.getFullName()+" "+e.getMessage()); result.initCause(e.getCause() == null ? e : e.getCause()); throw result; } }
@Test public void sourceThrowsIAE() { try { Maybe.unsafeCreate(new MaybeSource<Object>() { @Override public void subscribe(MaybeObserver<? super Object> observer) { throw new IllegalArgumentException("Forced failure"); } }).test(); fail("Should have thrown!"); } catch (NullPointerException ex) { assertTrue(ex.toString(), ex.getCause() instanceof IllegalArgumentException); assertEquals("Forced failure", ex.getCause().getMessage()); } }
fail("Should have thrown!"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof IllegalArgumentException)) { fail(ex.getCause().toString() + ": Should have thrown NullPointerException(IllegalArgumentException)"); fail("Should have thrown!"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof RuntimeException)) { fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))"); if (!(ex.getCause().getCause() instanceof IOException)) { fail(ex.getCause().toString() + ": Should have thrown NullPointerException(RuntimeException(IOException))");
@Test public void subscribeThrows() { try { new Single<Integer>() { @Override protected void subscribeActual(SingleObserver<? super Integer> observer) { throw new IllegalArgumentException(); } }.test(); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof IllegalArgumentException)) { fail(ex.toString() + ": should have thrown NPE(IAE)"); } } }
@Test public void callbackCrash() { try { Observable.just(1) .lift(new ObservableOperator<Object, Integer>() { @Override public Observer<? super Integer> apply(Observer<? super Object> o) throws Exception { throw new TestException(); } }) .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.toString(), ex.getCause() instanceof TestException); } } }
@Test public void badSourceCompleteDisconnect() { BadObservableConnect2 bo = new BadObservableConnect2(); try { bo.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } }
@Test public void badSourceSubscribe() { BadObservableSubscribe bo = new BadObservableSubscribe(); try { bo.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } }
@Test public void badSourceConnect() { BadObservableConnect bo = new BadObservableConnect(); try { bo.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } }
@Test public void badSourceSubscribe2() { BadObservableSubscribe2 bo = new BadObservableSubscribe2(); Observable<Object> o = bo.refCount(); o.test(); try { o.test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } }
@Test(timeout = 5000) public void createOnSubscribeThrowsRuntimeException() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Completable c = Completable.unsafeCreate(new CompletableSource() { @Override public void subscribe(CompletableObserver observer) { throw new TestException(); } }); c.blockingAwait(); Assert.fail("Did not throw exception"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof TestException)) { ex.printStackTrace(); Assert.fail("Did not wrap the TestException but it returned: " + ex); } TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void subscribeActualThrows() { List<Throwable> list = TestHelper.trackPluginErrors(); try { try { new BadObservable().test(); fail("Should have thrown!"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof IllegalArgumentException)) { fail(ex.toString() + ": Should be NPE(IAE)"); } } TestHelper.assertError(list, 0, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void badSourceSubscribe() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { BadFlowableSubscribe bo = new BadFlowableSubscribe(); try { bo.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void badSourceConnect() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { BadFlowableConnect bf = new BadFlowableConnect(); try { bf.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void badSourceCompleteDisconnect() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { BadFlowableConnect2 bf = new BadFlowableConnect2(); try { bf.refCount() .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void subscribeActualThrows() { List<Throwable> list = TestHelper.trackPluginErrors(); try { try { new BadFlowable().test(); fail("Should have thrown!"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof IllegalArgumentException)) { fail(ex.toString() + ": Should be NPE(IAE)"); } } TestHelper.assertError(list, 0, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } } }
@Test public void unsafeCreateThrowsIAE() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Completable.unsafeCreate(new CompletableSource() { @Override public void subscribe(CompletableObserver observer) { throw new IllegalArgumentException(); } }).test(); fail("Should have thrown!"); } catch (NullPointerException ex) { if (!(ex.getCause() instanceof IllegalArgumentException)) { fail(ex.toString() + ": should have thrown NPA(IAE)"); } TestHelper.assertError(errors, 0, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } } }
@Test public void badSourceSubscribe2() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { BadFlowableSubscribe2 bf = new BadFlowableSubscribe2(); Flowable<Object> f = bf.refCount(); f.test(); try { f.test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.getCause() instanceof TestException); } TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void callbackCrash() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Flowable.just(1) .lift(new FlowableOperator<Object, Integer>() { @Override public Subscriber<? super Integer> apply(Subscriber<? super Object> subscriber) throws Exception { throw new TestException(); } }) .test(); fail("Should have thrown"); } catch (NullPointerException ex) { assertTrue(ex.toString(), ex.getCause() instanceof TestException); TestHelper.assertUndeliverable(errors, 0, TestException.class); } finally { RxJavaPlugins.reset(); } } }