@@ -33,29 +33,12 @@ public class TestKeyPrefixContainerCodec {
3333
3434 private final Codec <KeyPrefixContainer > codec = KeyPrefixContainerCodec .get ();
3535
36- @ Test
37- public void testKeyPrefixVersionAndContainer () throws Exception {
38- KeyPrefixContainer original = KeyPrefixContainer .get ("testKey" , 123L , 456L );
39- testCodecBuffer (original );
40- }
41-
42- @ Test
43- public void testEmptyKeyPrefix () throws Exception {
44- KeyPrefixContainer original = KeyPrefixContainer .get ("" , 0L , 0L );
45- testCodecBuffer (original );
46- }
47-
4836 @ Test
4937 public void testKeyPrefixWithDelimiter () throws Exception {
50- KeyPrefixContainer original = KeyPrefixContainer .get ("test_key_with_underscores" , 789L , 101112L );
51- testCodecBuffer (original );
52- }
53-
54- @ Test
55- public void testLongKeyPrefix () throws Exception {
56- KeyPrefixContainer originalWithBoth = KeyPrefixContainer .get (
57- "test___________________________________Key" , 123L , 456L );
58- testCodecBuffer (originalWithBoth );
38+ runTest ("testKey" , 123L , 456L );
39+ runTest ("test_key_with_underscores" , 789L , 101112L );
40+ runTest ("test___________________________________Key" , 1L , 2L );
41+ runTest ("" , 0L , 0L );
5942 }
6043
6144 @ Test
@@ -68,19 +51,42 @@ public void testTypeClass() {
6851 assertEquals (KeyPrefixContainer .class , codec .getTypeClass ());
6952 }
7053
71- private void testCodecBuffer (KeyPrefixContainer original ) throws Exception {
54+ void runTest (String keyPrefix , long version , long containerId ) throws Exception {
55+ final KeyPrefixContainer original = KeyPrefixContainer .get (keyPrefix , version , containerId );
56+ final KeyPrefixContainer keyAndVersion = KeyPrefixContainer .get (keyPrefix , version );
57+ final KeyPrefixContainer keyOnly = KeyPrefixContainer .get (keyPrefix );
58+
59+ final CodecBuffer .Allocator allocator = CodecBuffer .Allocator .getHeap ();
60+ try (CodecBuffer originalBuffer = codec .toCodecBuffer (original , allocator );
61+ CodecBuffer keyOnlyBuffer = codec .toCodecBuffer (keyOnly , allocator );
62+ CodecBuffer keyAndVersionBuffer = codec .toCodecBuffer (keyAndVersion , allocator )) {
63+ assertEquals (original , codec .fromCodecBuffer (originalBuffer ));
64+ assertTrue (originalBuffer .startsWith (keyAndVersionBuffer ));
65+ assertTrue (originalBuffer .startsWith (keyOnlyBuffer ));
66+
67+ final byte [] originalBytes = assertCodecBuffer (original , originalBuffer );
68+ assertEquals (original , codec .fromPersistedFormat (originalBytes ));
69+
70+ final byte [] keyAndVersionBytes = assertCodecBuffer (keyAndVersion , keyAndVersionBuffer );
71+ assertPrefix (originalBytes .length - KeyPrefixContainerCodec .LONG_SERIALIZED_SIZE ,
72+ originalBytes , keyAndVersionBytes );
73+
74+ final byte [] keyOnlyBytes = assertCodecBuffer (keyOnly , keyOnlyBuffer );
75+ assertPrefix (originalBytes .length - 2 * KeyPrefixContainerCodec .LONG_SERIALIZED_SIZE ,
76+ originalBytes , keyOnlyBytes );
77+ }
78+ }
7279
73- final CodecBuffer codecBuffer = codec .toCodecBuffer (
74- original , CodecBuffer .Allocator .getHeap ());
75- final KeyPrefixContainer fromBuffer = codec .fromCodecBuffer (codecBuffer );
80+ static void assertPrefix (int expectedLength , byte [] array , byte [] prefix ) {
81+ assertEquals (expectedLength , prefix .length );
82+ for (int i = 0 ; i < prefix .length ; i ++) {
83+ assertEquals (array [i ], prefix [i ]);
84+ }
85+ }
7686
87+ byte [] assertCodecBuffer (KeyPrefixContainer original , CodecBuffer buffer ) throws Exception {
7788 final byte [] bytes = codec .toPersistedFormat (original );
78- assertArrayEquals (bytes , codecBuffer .getArray ());
79-
80- codecBuffer .release ();
81- assertEquals (original , fromBuffer );
82-
83- KeyPrefixContainer fromPersisted = codec .fromPersistedFormat (bytes );
84- assertEquals (original , fromPersisted );
89+ assertArrayEquals (bytes , buffer .getArray ());
90+ return bytes ;
8591 }
8692}
0 commit comments