@Test
  public void testKeyValueSerialization() throws Exception {
    KeyValue[] keyValues = new KeyValue[] {
      new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
        Bytes.toBytes("1")),
      new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
        Bytes.toBytes("2")),
      new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
        EnvironmentEdgeManager.currentTime(), Bytes.toBytes("2"),
        new Tag[] { new ArrayBackedTag((byte) 120, "tagA"),
          new ArrayBackedTag((byte) 121, Bytes.toBytes("tagB")) }),
      new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes("qualA"),
        EnvironmentEdgeManager.currentTime(), Bytes.toBytes("2"),
        new Tag[] { new ArrayBackedTag((byte) 0, "tagA") }),
      new KeyValue(Bytes.toBytes("key"), Bytes.toBytes("cf"), Bytes.toBytes(""),
        Bytes.toBytes("1")) };
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    for (KeyValue kv : keyValues) {
      DataOutputStream os = new DataOutputStream(byteArrayOutputStream);
      ByteBufferUtils.putInt(os, kv.getSerializedSize(true));
      kv.write(os, true);
    }
    DataInputStream is =
      new DataInputStream(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    for (int i = 0; i < keyValues.length; i++) {
      LOG.info("Case#" + i + ": deserialize the kv: " + keyValues[i]);
      KeyValue destKv = KeyValueUtil.createKeyValueFromInputStream(is, true);
      assertEquals(keyValues[i], destKv);
      assertArrayEquals(CellUtil.cloneValue(keyValues[i]), CellUtil.cloneValue(destKv));
      assertArrayEquals(PrivateCellUtil.cloneTags(keyValues[i]), PrivateCellUtil.cloneTags(destKv));
    }
  }
Tests that getTimestamp() does always return the proper timestamp, even after updating it. See
HBASE-6265.
hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java