I2CDeviceImpl implements I2CDevice { @Override public int read() throws IOException { return getBus().readByteDirect(this); } I2CDeviceImpl(I2CBusImpl bus, int address); @Override int getAddress(); @Override void write(final byte data); @Override void write(final byte[] data, final int offset, final int size); @Override void write(byte[] buffer); @Override void write(final int address, final byte data); @Override void write(final int address, final byte[] data, final int offset, final int size); void write(int address, byte[] buffer); @Override int read(); @Override int read(final byte[] data, final int offset, final int size); @Override int read(final int address); @Override int read(final int address, final byte[] data, final int offset, final int size); @Override void ioctl(long command, int value); @Override void ioctl(long command, ByteBuffer data, IntBuffer offsets); @Override int read(final byte[] writeData, final int writeOffset, final int writeSize, final byte[] readData, final int readOffset, final int readSize);  }
@Test public void testReading() throws IOException { int readResult1 = 50; when(bus.readByteDirect(eq(device))).thenReturn(readResult1); int read1 = device.read(); assertEquals("'read()' does not return the expected value!", readResult1, read1); int readResult2 = 51; when(bus.readByte(eq(device), eq(LOCALADDRESS))).thenReturn(readResult2); int read2 = device.read(LOCALADDRESS); assertEquals("'read(int)' does not return the expected value!", readResult2, read2); when(bus.readBytesDirect(eq(device), anyInt(), anyInt(), any(byte[].class))).thenAnswer(readAnswer); byte[] buffer1 = new byte[2]; int read3 = device.read(buffer1, 0, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read3); assertEquals("Unexpected bytes in buffer!", buffer1[0], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer1[1], READ_SECONDBYTE); byte[] buffer2 = new byte[3]; int read4 = device.read(buffer2, 1, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read4); assertEquals("Unexpected bytes in buffer!", buffer2[1], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer2[2], READ_SECONDBYTE); try { device.read(new byte[1], 0, 1); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IOException e) { } try { device.read(new byte[2], 1, 2); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IndexOutOfBoundsException e) { } when(bus.readBytes(eq(device), eq(LOCALADDRESS), anyInt(), anyInt(), any(byte[].class))).thenAnswer(readAnswer); byte[] buffer3 = new byte[2]; int read5 = device.read(LOCALADDRESS, buffer3, 0, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read5); assertEquals("Unexpected bytes in buffer!", buffer3[0], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer3[1], READ_SECONDBYTE); byte[] buffer4 = new byte[3]; int read6 = device.read(LOCALADDRESS, buffer4, 1, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read6); assertEquals("Unexpected bytes in buffer!", buffer4[1], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer4[2], READ_SECONDBYTE); try { device.read(LOCALADDRESS, new byte[1], 0, 1); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IOException e) { } try { device.read(LOCALADDRESS, new byte[2], 1, 2); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IndexOutOfBoundsException e) { } byte[] dataToBeWritten = new byte[] { WRITE_FIRSTBYTE, WRITE_SECONDBYTE }; when(bus.writeAndReadBytesDirect(eq(device), anyInt(), anyInt(), any(byte[].class), anyInt(), anyInt(), any(byte[].class))).thenAnswer(writeAndReadAnswer); byte[] buffer5 = new byte[2]; int read7 = device.read(dataToBeWritten, 0, 2, buffer5, 0, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read7); assertEquals("Unexpected bytes in buffer!", buffer5[0], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer5[1], READ_SECONDBYTE); byte[] buffer6 = new byte[3]; int read8 = device.read(dataToBeWritten, 0, 2, buffer6, 1, 2); assertEquals("Expected to read one byte but got a different number of bytes!", 2, read8); assertEquals("Unexpected bytes in buffer!", buffer6[1], READ_FIRSTBYTE); assertEquals("Unexpected bytes in buffer!", buffer6[2], READ_SECONDBYTE); try { device.read(new byte[1], 0, 1); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IOException e) { } try { device.read(new byte[2], 1, 2); fail("Expected 'read(...)' to throw an exception but got none!"); } catch (IndexOutOfBoundsException e) { } }