示例#1
0
 @Test
 public void testRandomPositiveInt() throws IOException {
   Random random = new Random(System.nanoTime());
   ObjectInputOutput io = new ObjectInputOutput();
   for (int i = 0; i < NR_RANDOM; ++i) {
     int v = random.nextInt();
     if (v < 0) {
       v = -v;
     }
     io.reset();
     MarshallUtil.marshallInt(io, v);
     Assert.assertEquals("Error for v=" + v, v, MarshallUtil.unmarshallInt(io));
   }
 }
示例#2
0
  @Test
  public void testEnum() throws IOException {
    ObjectInputOutput io = new ObjectInputOutput();
    MarshallUtil.marshallEnum(null, io);
    Assert.assertNull(MarshallUtil.unmarshallEnum(io, ordinal -> TestEnum.values()[ordinal]));
    Assert.assertEquals(0, io.buffer.size());

    for (TestEnum e : TestEnum.values()) {
      io.reset();
      MarshallUtil.marshallEnum(e, io);
      Assert.assertEquals(
          e, MarshallUtil.unmarshallEnum(io, ordinal -> TestEnum.values()[ordinal]));
      Assert.assertEquals(0, io.buffer.size());
    }
  }
示例#3
0
 private static void checkNegativeInt(int i, ObjectInputOutput io) throws IOException {
   io.reset();
   MarshallUtil.marshallInt(io, i);
   Assert.assertEquals("Error for i=" + i, 1, io.buffer.size());
   Assert.assertEquals("Error for i=" + i, -1, MarshallUtil.unmarshallInt(io));
   Assert.assertEquals("Error for i=" + i, 0, io.buffer.size());
 }
示例#4
0
 private static void checkIntAndByteArray(int i, int bytesExpected, ObjectInputOutput io)
     throws IOException {
   io.reset();
   MarshallUtil.marshallInt(io, i);
   Assert.assertEquals("Error for i=" + i, bytesExpected, io.buffer.size());
   Assert.assertEquals("Error for i=" + i, i, MarshallUtil.unmarshallInt(io));
   Assert.assertEquals("Error for i=" + i, 0, io.buffer.size());
 }
示例#5
0
  @Test
  public void testByteArray() throws IOException, ClassNotFoundException {
    ObjectInputOutput io = new ObjectInputOutput();
    MarshallUtil.marshallByteArray(null, io);
    Assert.assertNull(MarshallUtil.unmarshallByteArray(io));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();

    byte[] array = new byte[0];
    MarshallUtil.marshallByteArray(array, io);
    Assert.assertTrue(Arrays.equals(array, MarshallUtil.unmarshallByteArray(io)));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();

    array = new byte[] {1, 2, 3};
    MarshallUtil.marshallByteArray(array, io);
    Assert.assertTrue(Arrays.equals(array, MarshallUtil.unmarshallByteArray(io)));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();
  }
示例#6
0
  @Test
  public void testArray() throws IOException, ClassNotFoundException {
    ObjectInputOutput io = new ObjectInputOutput();
    MarshallUtil.marshallArray(null, io);
    Assert.assertNull(MarshallUtil.unmarshallArray(io, null));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();

    String[] array = new String[0];
    MarshallUtil.marshallArray(array, io);
    Assert.assertTrue(Arrays.equals(array, MarshallUtil.unmarshallArray(io, String[]::new)));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();

    array = new String[] {"a", "b", "c"};
    MarshallUtil.marshallArray(array, io);
    Assert.assertTrue(Arrays.equals(array, MarshallUtil.unmarshallArray(io, String[]::new)));
    Assert.assertEquals(0, io.buffer.size());
    io.reset();
  }
示例#7
0
  @Test
  public void testUUID() throws IOException {
    ObjectInputOutput io = new ObjectInputOutput();
    MarshallUtil.marshallUUID(null, io, true);
    Assert.assertNull(MarshallUtil.unmarshallUUID(io, true));
    Assert.assertEquals(0, io.buffer.size());

    for (int i = 0; i < NR_RANDOM; ++i) {
      io.reset();
      UUID uuid = UUID.randomUUID();
      MarshallUtil.marshallUUID(uuid, io, false);
      Assert.assertEquals(uuid, MarshallUtil.unmarshallUUID(io, false));
      Assert.assertEquals(0, io.buffer.size());
    }

    for (int i = 0; i < NR_RANDOM; ++i) {
      io.reset();
      UUID uuid = UUID.randomUUID();
      MarshallUtil.marshallUUID(uuid, io, true);
      Assert.assertEquals(uuid, MarshallUtil.unmarshallUUID(io, true));
      Assert.assertEquals(0, io.buffer.size());
    }
  }