@Test public void testReadWithoutRewinding() { parcel.writeInt(123); try { parcel.readInt(); fail("should have thrown"); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Did you forget to setDataPosition(0) before reading the parcel?"); } }
@Test public void finderThrowsNiceError() { Context context = InstrumentationRegistry.getContext(); View view = new View(context); try { Utils.findRequiredView(view, android.R.id.button1, "yo mama"); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Required view 'button1' with ID " + android.R.id.button1 + " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation."); } }
@Test public void testInvalidReadFromMiddleOfObject() { parcel.writeLong(111L); parcel.writeLong(222L); parcel.setDataPosition(0); parcel.setDataPosition(4); try { parcel.readInt(); fail("should have thrown UnreliableBehaviorError"); } catch (UnreliableBehaviorError e) { assertThat(e) .hasMessage( "Looking for Integer at position 4, found Long [111] taking 8 bytes, but " + "[222] interrupts it at position 8"); } }
@Test public void testInvalidReadFromOverwrittenObject() { parcel.writeString("hello all"); parcel.setDataPosition(4); parcel.writeInt(5); parcel.setDataPosition(0); try { parcel.readString(); fail("should have thrown UnreliableBehaviorError"); } catch (UnreliableBehaviorError e) { assertThat(e) .hasMessage( "Looking for String at position 0, found String [hello all] taking 24 bytes, but " + "[5] interrupts it at position 4"); } }
@Test public void testWriteAndReadByteArray_justLengthButNoContents() { parcel.writeInt(3); parcel.setDataPosition(0); try { parcel.createByteArray(); fail("expected exception"); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Byte array's length prefix is 3 but real length is 0"); } }
@Test public void testMarshallFailsFastReadingInterruptedObject() { parcel.writeString("hello all"); parcel.setDataPosition(4); parcel.writeInt(1); try { parcel.marshall(); fail(); } catch (UnreliableBehaviorError e) { assertThat(e) .hasMessage( "Looking for Object at position 0, found String [hello all] taking 24 bytes, but " + "[1] interrupts it at position 4"); } }
@Test public void testWriteStringReadInt() { String val = "test"; parcel.writeString(val); parcel.setDataPosition(0); try { parcel.readInt(); fail("should have thrown"); } catch (RuntimeException e) { assertThat(e) .hasCauseThat() .hasMessage( "Looking for Integer at position 0, found String [test] taking 16 bytes, " + "and it is non-portable to reinterpret it"); } }
@Test public void mapToOneOrDefaultDisallowsNullDefault() { try { Query.mapToOneOrDefault(MAPPER, null); fail(); } catch (NullPointerException e) { assertThat(e).hasMessage("defaultValue == null"); } }
@Test public void testWriteIntReadString() { int val = 9; parcel.writeInt(val); parcel.setDataPosition(0); try { parcel.readString(); fail("should have thrown"); } catch (RuntimeException e) { assertThat(e) .hasCauseThat() .hasMessage( "Looking for String at position 0, found Integer [9] taking 4 bytes, " + "and it is non-portable to reinterpret it"); } }
@Test public void testMarshallFailsFastReadingTruncatedObject() { parcel.writeString("hello all"); parcel.setDataSize(8); try { parcel.marshall(); fail(); } catch (UnreliableBehaviorError e) { assertThat(e) .hasMessage( "Looking for Object at position 0, found String [hello all] taking 24 bytes, but " + "[uninitialized data or the end of the buffer] interrupts it at position 8"); } }
@Test public void finderThrowsLessNiceErrorInEditMode() { Context context = InstrumentationRegistry.getContext(); View view = new EditModeView(context); try { Utils.findRequiredView(view, android.R.id.button1, "yo mama"); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Required view '<unavailable while editing>' " + "with ID " + android.R.id.button1 + " for yo mama was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation."); } }
@Test public void builderDisallowsNull() { SqlBrite.Builder builder = new SqlBrite.Builder(); try { builder.logger(null); fail(); } catch (NullPointerException e) { assertThat(e).hasMessage("logger == null"); } try { builder.queryTransformer(null); fail(); } catch (NullPointerException e) { assertThat(e).hasMessage("queryTransformer == null"); } }
@Test public void testInvalidReadFromTruncatedObjectEvenAfterBufferRegrows() { parcel.writeString("hello all"); parcel.setDataSize(12); // Restore the original size, but the data should be lost. parcel.setDataSize(100); parcel.setDataPosition(0); try { parcel.readString(); fail("should have thrown UnreliableBehaviorError"); } catch (UnreliableBehaviorError e) { assertThat(e) .hasMessage( "Looking for String at position 0, found String [hello all] taking 24 bytes, but " + "[uninitialized data or the end of the buffer] interrupts it at position 12"); } }
@Test public void testInvalidReadFromUninitializedData() { // Write two longs with an 8-byte gap in the middle: parcel.writeLong(333L); parcel.setDataSize(parcel.dataSize() + 8); parcel.setDataPosition(parcel.dataSize()); parcel.writeLong(444L); parcel.setDataPosition(0); assertThat(parcel.readLong()).isEqualTo(333L); try { parcel.readLong(); fail("should have thrown UnreliableBehaviorError"); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Reading uninitialized data at position 8"); } }
@Test public void testMarshallFailsFastReadingUninitializedData() { parcel.writeString("hello everyone"); parcel.setDataSize(parcel.dataSize() + 4); parcel.setDataPosition(parcel.dataSize()); parcel.writeInt(1); try { parcel.marshall(); fail(); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Reading uninitialized data at position 36"); } }
@Test public void callingEndMultipleTimesThrows() { Transaction transaction = db.newTransaction(); transaction.end(); try { transaction.end(); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Not in transaction."); } }
@Test public void testWriteAndReadByteArray_overwrittenLength() { byte[] bytes = new byte[] {-1, 2, 3, 127}; parcel.writeByteArray(bytes); assertThat(parcel.dataSize()).isEqualTo(8); parcel.setDataPosition(0); parcel.writeInt(3); parcel.setDataPosition(0); try { parcel.createByteArray(); fail("expected exception"); } catch (UnreliableBehaviorError e) { assertThat(e).hasMessage("Byte array's length prefix is 3 but real length is 4"); } }
@Test public void duplicateSubscriptions() { SubscriptionManagerCallbackAdapter<Operation.Data> subscriptionManagerCallback1 = new SubscriptionManagerCallbackAdapter<>(); subscriptionManager.subscribe(subscription1, subscriptionManagerCallback1); SubscriptionManagerCallbackAdapter<Operation.Data> subscriptionManagerCallback2 = new SubscriptionManagerCallbackAdapter<>(); subscriptionManager.subscribe(subscription1, subscriptionManagerCallback2); assertThat(subscriptionManagerCallback2.error).hasMessage("Already subscribed"); }
@Test public void mapToOneThrowsOnMultipleRows() { Observable<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") // .lift(Query.mapToOne(MAPPER)); try { employees.blockingFirst(); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Cursor returned more than 1 row"); } }
@Test public void mapToOneOrDefaultThrowsOnMultipleRows() { Observable<Employee> employees = db.createQuery(TABLE_EMPLOYEE, SELECT_EMPLOYEES + " LIMIT 2") // .lift(Query.mapToOneOrDefault( MAPPER, new Employee("fred", "Fred Frederson"))); try { employees.blockingFirst(); fail(); } catch (IllegalStateException e) { assertThat(e).hasMessage("Cursor returned more than 1 row"); } }