public class CounterRollupSerializationTest {
@Test
public void testCounterV1RoundTrip() throws IOException {
CounterRollup c0 = new CounterRollup().withCount(7442245).withSampleCount(1);
CounterRollup c1 = new CounterRollup().withCount(34454722343L).withSampleCount(10);
if (System.getProperty("GENERATE_COUNTER_SERIALIZATION") != null) {
OutputStream os = new FileOutputStream("src/test/resources/serializations/counter_version_" + Constants.VERSION_1_COUNTER_ROLLUP + ".bin", false);
os.write(Base64.encodeBase64(new NumericSerializer.CounterRollupSerializer().toByteBuffer(c0).array()));
os.write("\n".getBytes());
os.write(Base64.encodeBase64(new NumericSerializer.CounterRollupSerializer().toByteBuffer(c1).array()));
os.write("\n".getBytes());
os.close();
}
Assert.assertTrue(new File("src/test/resources/serializations").exists());
int count = 0;
int version = 0;
final int maxVersion = Constants.VERSION_1_COUNTER_ROLLUP;
while (version <= maxVersion) {
BufferedReader reader = new BufferedReader(new FileReader("src/test/resources/serializations/counter_version_" + version + ".bin"));
ByteBuffer bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
CounterRollup cc0 = NumericSerializer.serializerFor(CounterRollup.class).fromByteBuffer(bb);
Assert.assertEquals(c0, cc0);
bb = ByteBuffer.wrap(Base64.decodeBase64(reader.readLine().getBytes()));
CounterRollup cc1 = NumericSerializer.serializerFor(CounterRollup.class).fromByteBuffer(bb);
Assert.assertEquals(c1, cc1);
Assert.assertFalse(cc0.equals(cc1));
version++;
count++;