@Test public void testReadCompatibleVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(2) { @Override public int[] getCompatibleVersions() { return new int[] {1, 2}; } }; try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); } Assert.assertEquals(payload, testWriteable.getData()); }
@Test public void testReadMismatchVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(2); try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); Assert.fail("Version mismatch expected."); } catch (VersionMismatchException ignored) { } Assert.assertEquals(null, testWriteable.getData()); }
@Test public void testReadSameVersion() throws Exception { String payload = "test"; TestWriteable testWriteable = new TestWriteable(1, payload); byte[] serialized; try (ByteArrayOutputStreamWithPos out = new ByteArrayOutputStreamWithPos()) { testWriteable.write(new DataOutputViewStreamWrapper(out)); serialized = out.toByteArray(); } testWriteable = new TestWriteable(1); try (ByteArrayInputStreamWithPos in = new ByteArrayInputStreamWithPos(serialized)) { testWriteable.read(new DataInputViewStreamWrapper(in)); } Assert.assertEquals(payload, testWriteable.getData()); }