InstanceV2 extends Instance { @Override public void writeToMmv(ByteBuffer byteBuffer) { byteBuffer.position(offset); byteBuffer.putLong(instanceDomain.getOffset()); byteBuffer.putInt(0); byteBuffer.putInt(id); byteBuffer.putLong(getStringOffset(nameAsString)); } InstanceV2(InstanceDomain domain, String name, int id, PcpString nameAsString); @Override void writeToMmv(ByteBuffer byteBuffer); @Override int byteSize();  }
@Test public void writeToMmvShouldWriteTheCorrectContentToTheByteBuffer() { InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpString pcpString = mock(PcpString.class); ByteBuffer byteBuffer = ByteBuffer.allocate(EXPECTED_BYTE_SIZE); InstanceV2 instanceV2 = new InstanceV2(instanceDomain, INSTANCE_NAME, INSTANCE_DOMAIN_ID, pcpString); when(instanceDomain.getOffset()).thenReturn(INSTANCE_DOMAIN_OFFSET); when(pcpString.getOffset()).thenReturn(STRING_OFFSET); instanceV2.writeToMmv(byteBuffer); byte[] expected = { 0, 0, 0, 0, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 30, }; assertArrayEquals(expected, byteBuffer.array()); }
@Test public void shouldPositionTheByteBufferBeforeWriting() { InstanceV2 instanceV2 = new InstanceV2(mock(InstanceDomain.class), INSTANCE_NAME, INSTANCE_DOMAIN_ID, mock(PcpString.class)); ByteBuffer byteBuffer = mock(ByteBuffer.class); instanceV2.setOffset(MY_OFFSET); instanceV2.writeToMmv(byteBuffer); InOrder inOrder = Mockito.inOrder(byteBuffer); inOrder.verify(byteBuffer).position(MY_OFFSET); inOrder.verify(byteBuffer).putLong(anyLong()); }