### Question:
MDSKey implements Comparable<MDSKey> { public Splitter split() { return new Splitter(key); } MDSKey(byte[] key); byte[] getKey(); Splitter split(); @Override boolean equals(Object o); @Override int hashCode(); @Override int compareTo(MDSKey o);  }### Answer:
@Test public void simpleStringKeySplit() { List<String> originalKeyParts = ImmutableList.of("part1", "part2", "part3"); MDSKey.Builder builder = new MDSKey.Builder(); for (String part : originalKeyParts) { builder.add(part); } MDSKey mdsKey = builder.build(); MDSKey.Splitter splitter = mdsKey.split(); for (String originalKeyPart : originalKeyParts) { Assert.assertEquals(originalKeyPart, splitter.getString()); } }
@Test public void testSkipLongAndInt() { MDSKey.Builder builder = new MDSKey.Builder(); builder.add("part1"); builder.add(2L); builder.add(3L); builder.add(4); builder.add(5); MDSKey mdsKey = builder.build(); MDSKey.Splitter splitter = mdsKey.split(); Assert.assertEquals("part1", splitter.getString()); splitter.skipLong(); Assert.assertEquals(3L, splitter.getLong()); splitter.skipInt(); Assert.assertEquals(5, splitter.getInt()); }
@Test public void testGetBytesOverflow() { MDSKey.Builder builder = new MDSKey.Builder(); builder.add(2000); builder.add(2000); MDSKey mdsKey = builder.build(); MDSKey.Splitter splitter = mdsKey.split(); try { splitter.getBytes(); Assert.fail(); } catch (BufferUnderflowException expected) { } try { splitter.getString(); Assert.fail(); } catch (BufferUnderflowException expected) { } }
@Test public void getGetIntOverflow() { MDSKey.Builder builder = new MDSKey.Builder(); builder.add(1); builder.add(2); builder.add(3); MDSKey mdsKey = builder.build(); MDSKey.Splitter splitter = mdsKey.split(); Assert.assertEquals(1, splitter.getInt()); Assert.assertEquals(2, splitter.getInt()); Assert.assertEquals(3, splitter.getInt()); try { splitter.getInt(); Assert.fail(); } catch (BufferUnderflowException expected) { Assert.assertFalse(splitter.hasRemaining()); } }
@Test public void testAppend() { MDSKey mdsKey1 = new MDSKey.Builder().add("ab").add(3L).add(new byte[]{'x', 'y'}).build(); MDSKey mdsKey2 = new MDSKey.Builder().add("bd").add(5).append(mdsKey1).add(new byte[]{'z', 'z'}).build(); MDSKey mdsKey3 = new MDSKey.Builder().add(2).add(new byte[]{'w'}).append(mdsKey2).add(8L).build(); MDSKey.Splitter splitter = mdsKey3.split(); Assert.assertEquals(2, splitter.getInt()); Assert.assertArrayEquals(new byte[]{'w'}, splitter.getBytes()); Assert.assertEquals("bd", splitter.getString()); Assert.assertEquals(5, splitter.getInt()); Assert.assertEquals("ab", splitter.getString()); Assert.assertEquals(3L, splitter.getLong()); Assert.assertArrayEquals(new byte[]{'x', 'y'}, splitter.getBytes()); Assert.assertArrayEquals(new byte[]{'z', 'z'}, splitter.getBytes()); Assert.assertEquals(8L, splitter.getLong()); }