Example usage for io.netty.buffer ByteBuf duplicate

List of usage examples for io.netty.buffer ByteBuf duplicate

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf duplicate.

Prototype

public abstract ByteBuf duplicate();

Source Link

Document

Returns a buffer which shares the whole region of this buffer.

Usage

From source file:io.datty.spring.converter.embedded.ArrayEntityTest.java

License:Apache License

@Test
public void testEmbeddedNull() {

    ArrayEntity entity = new ArrayEntity();
    entity.setId(123L);//from w  ww .  j a  v a2 s. c  om

    EmbeddedEntity embedded = new EmbeddedEntity();
    entity.setEmbedded(new EmbeddedEntity[] { embedded });

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(Arrays.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof List);

    List<Map> list = (List<Map>) value;
    Assert.assertEquals(1, list.size());
    Assert.assertNotNull(list.get(0));
    Assert.assertNull(list.get(0).get("innerField"));

    ArrayEntity actual = DattyConverterUtil.read(ArrayEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().length);
    Assert.assertNull(actual.getEmbedded()[0].getInnerField());

}

From source file:io.datty.spring.converter.embedded.ArrayEntityTest.java

License:Apache License

@Test
public void testEmbedded() {

    ArrayEntity entity = new ArrayEntity();
    entity.setId(123L);/*from   w  w  w  .  j a v a2  s  .  c  om*/

    EmbeddedEntity embedded = new EmbeddedEntity();
    embedded.setInnerField("inner");
    entity.setEmbedded(new EmbeddedEntity[] { embedded });

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(Arrays.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof List);

    List<Map> list = (List<Map>) value;
    Assert.assertEquals(1, list.size());
    Assert.assertNotNull(list.get(0));
    Assert.assertEquals("inner", list.get(0).get("innerField"));

    ArrayEntity actual = DattyConverterUtil.read(ArrayEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().length);
    Assert.assertEquals("inner", actual.getEmbedded()[0].getInnerField());

}

From source file:io.datty.spring.converter.embedded.IntMapEntityTest.java

License:Apache License

@Test
public void testNull() {

    IntMapEntity entity = new IntMapEntity();
    entity.setId(123L);// w  w w  .  ja va 2  s .co m

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    Assert.assertNull(rec.get("embedded"));

    IntMapEntity actual = DattyConverterUtil.read(IntMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNull(actual.getEmbedded());

}

From source file:io.datty.spring.converter.embedded.IntMapEntityTest.java

License:Apache License

@Test
public void testEmbeddedEmpty() {

    IntMapEntity entity = new IntMapEntity();
    entity.setId(123L);/*from   ww  w  .  ja va 2 s  . c  om*/
    entity.setEmbedded(Collections.<Integer, EmbeddedEntity>emptyMap());

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<Integer, Map> map = (Map<Integer, Map>) value;
    Assert.assertEquals(0, map.size());

    IntMapEntity actual = DattyConverterUtil.read(IntMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(0, actual.getEmbedded().size());

}

From source file:io.datty.spring.converter.embedded.IntMapEntityTest.java

License:Apache License

@Test
public void testEmbeddedNull() {

    IntMapEntity entity = new IntMapEntity();
    entity.setId(123L);//from ww  w  . j  av a 2  s . c o  m

    EmbeddedEntity embedded = new EmbeddedEntity();
    entity.setEmbedded(Collections.singletonMap(555, embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(StringMaps.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<Integer, Map> map = (Map<Integer, Map>) value;
    Assert.assertEquals(1, map.size());
    Assert.assertNotNull(map.get(555));
    Assert.assertNull(map.get(555).get("innerField"));

    IntMapEntity actual = DattyConverterUtil.read(IntMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertNull(actual.getEmbedded().get(555).getInnerField());

}

From source file:io.datty.spring.converter.embedded.IntMapEntityTest.java

License:Apache License

@Test
public void testEmbedded() {

    IntMapEntity entity = new IntMapEntity();
    entity.setId(123L);/*from w w  w. j av a 2s  .  co  m*/

    EmbeddedEntity embedded = new EmbeddedEntity();
    embedded.setInnerField("inner");
    entity.setEmbedded(Collections.singletonMap(555, embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(StringMaps.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof Map);

    Map<Integer, Map> map = (Map<Integer, Map>) value;
    Assert.assertEquals(1, map.size());
    Assert.assertNotNull(map.get(555));
    Assert.assertEquals("inner", map.get(555).get("innerField"));

    IntMapEntity actual = DattyConverterUtil.read(IntMapEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertEquals("inner", actual.getEmbedded().get(555).getInnerField());

}

From source file:io.datty.spring.converter.embedded.ListEntityTest.java

License:Apache License

@Test
public void testNull() {

    ListEntity entity = new ListEntity();
    entity.setId(123L);/*from   w w  w  .  ja  v a  2 s  .  c o  m*/

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    Assert.assertNull(rec.get("embedded"));

    ListEntity actual = DattyConverterUtil.read(ListEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNull(actual.getEmbedded());

}

From source file:io.datty.spring.converter.embedded.ListEntityTest.java

License:Apache License

@Test
public void testEmbeddedEmpty() {

    ListEntity entity = new ListEntity();
    entity.setId(123L);/*from  www .ja v  a  2 s  .  com*/
    entity.setEmbedded(Collections.<EmbeddedEntity>emptyList());

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof List);

    List<Map> list = (List<Map>) value;
    Assert.assertEquals(0, list.size());

    ListEntity actual = DattyConverterUtil.read(ListEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(0, actual.getEmbedded().size());

}

From source file:io.datty.spring.converter.embedded.ListEntityTest.java

License:Apache License

@Test
public void testEmbeddedNull() {

    ListEntity entity = new ListEntity();
    entity.setId(123L);/*from   w w w  .j  av  a 2s  .  c o m*/

    EmbeddedEntity embedded = new EmbeddedEntity();
    entity.setEmbedded(Arrays.asList(embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(Lists.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof List);

    List<Map> list = (List<Map>) value;
    Assert.assertEquals(1, list.size());
    Assert.assertNotNull(list.get(0));
    Assert.assertNull(list.get(0).get("innerField"));

    ListEntity actual = DattyConverterUtil.read(ListEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertNull(actual.getEmbedded().get(0).getInnerField());

}

From source file:io.datty.spring.converter.embedded.ListEntityTest.java

License:Apache License

@Test
public void testEmbedded() {

    ListEntity entity = new ListEntity();
    entity.setId(123L);/*from w w w. j a va  2  s.  c o m*/

    EmbeddedEntity embedded = new EmbeddedEntity();
    embedded.setInnerField("inner");
    entity.setEmbedded(Arrays.asList(embedded));

    DattyRecord rec = new DattyRecord();

    DattyConverterUtil.write(entity, rec);

    ByteBuf bb = rec.get("id").asByteBuf();
    Assert.assertNotNull(bb);

    Long id = LongReader.INSTANCE.read(bb.duplicate(), true);
    Assert.assertNotNull(id);
    Assert.assertEquals(123L, id.longValue());

    bb = rec.get("embedded").asByteBuf();
    Assert.assertNotNull(bb);

    //System.out.println(Lists.toString(ByteBufUtil.getBytes(bb)));

    Object value = MessageIO.readValue(bb.duplicate(), true);
    Assert.assertNotNull(value);
    Assert.assertTrue(value instanceof List);

    List<Map> list = (List<Map>) value;
    Assert.assertEquals(1, list.size());
    Assert.assertNotNull(list.get(0));
    Assert.assertEquals("inner", list.get(0).get("innerField"));

    ListEntity actual = DattyConverterUtil.read(ListEntity.class, rec);
    Assert.assertEquals(entity.getId(), actual.getId());
    Assert.assertNotNull(actual.getEmbedded());
    Assert.assertEquals(1, actual.getEmbedded().size());
    Assert.assertEquals("inner", actual.getEmbedded().get(0).getInnerField());

}