KVSerializer implements IKVSerializer { @Override public int serializeKV(DataOutputStream out, SizedWritable<?> key, SizedWritable<?> value) throws IOException { return serializePartitionKV(out, -1, key, value); } KVSerializer(Class<K> kclass, Class<V> vclass); @Override void updateLength(SizedWritable<?> key, SizedWritable<?> value); @Override int serializeKV(DataOutputStream out, SizedWritable<?> key, SizedWritable<?> value); @Override int serializePartitionKV(DataOutputStream out, int partitionId,
      SizedWritable<?> key, SizedWritable<?> value); @Override int deserializeKV(DataInputStream in, SizedWritable<?> key,
      SizedWritable<?> value); static final int KV_HEAD_LENGTH; }
@Test public void testSerializeKV() throws IOException { final DataOutputStream dataOut = Mockito.mock(DataOutputStream.class); Mockito.when(dataOut.hasUnFlushedData()).thenReturn(true); Mockito.when(dataOut.shortOfSpace(key.length + value.length + Constants.SIZEOF_KV_LENGTH)).thenReturn(true); final int written = serializer.serializeKV(dataOut, key, value); Mockito.verify(dataOut, Mockito.times(1)).flush(); Mockito.verify(dataOut, Mockito.times(4)).writeInt(Matchers.anyInt()); Mockito.verify(dataOut, Mockito.times(2)).write(Matchers.any(byte[].class), Matchers.anyInt(), Matchers.anyInt()); Assert.assertEquals(written, key.length + value.length + Constants.SIZEOF_KV_LENGTH); }
@Test public void testSerializeNoFlush() throws IOException { final DataOutputStream dataOut = Mockito.mock(DataOutputStream.class); Mockito.when(dataOut.hasUnFlushedData()).thenReturn(true); Mockito.when(dataOut.shortOfSpace(Matchers.anyInt())).thenReturn(false); final int written = serializer.serializeKV(dataOut, key, value); Mockito.verify(dataOut, Mockito.times(0)).flush(); Mockito.verify(dataOut, Mockito.times(4)).writeInt(Matchers.anyInt()); Mockito.verify(dataOut, Mockito.times(2)).write(Matchers.any(byte[].class), Matchers.anyInt(), Matchers.anyInt()); Assert.assertEquals(written, key.length + value.length + Constants.SIZEOF_KV_LENGTH); }