コード例 #1
0
  @Test
  public void testSerialize() {
    SFSSerializer sfsSerializer = new SFSSerializer();

    SubEntity subEntity = new SubEntity();
    subEntity.setLongField(10L);
    subEntity.setStringsList(Arrays.asList("testString"));

    Entity entity = new Entity();
    entity.setIntField(100);
    entity.setStringField("string");
    entity.setSubEntity(subEntity);
    entity.setSubEntities(Arrays.asList(new SubEntity(100L), new SubEntity(100L)));
    entity.setNotDeserializable("value1");
    entity.fieldWithoutGetter = "value";
    entity.setNotSerializable("value2");
    entity.setEnumField(Entity.Color.black);
    entity.setColors(Arrays.asList(Entity.Color.black, Entity.Color.white));
    entity.fieldCustomSerializable = 20L;
    entity.totallyIgnoredField = "value";

    ISFSObject sObj = sfsSerializer.serialize(entity);

    assertEquals(entity.getIntField(), sObj.getInt("intField"));
    assertEquals(entity.fieldWithoutGetter, sObj.getUtfString("fieldWithoutGetter"));
    assertEquals(entity.getEnumField().name(), sObj.getUtfString("enumField"));
    assertEquals(entity.getNameToBeChanged(), sObj.getUtfString("changedName"));
    assertEquals(entity.getNotDeserializable(), sObj.getUtfString("notDeserializable"));
    assertEquals(20, sObj.getInt("fieldCustomSerializable").intValue());
    Collection<String> colors = sObj.getUtfStringArray("colors");
    assertTrue(colors.contains(entity.getColors().get(1).name()));
    assertNull(sObj.getUtfString("totallyIgnoredField"));
    assertNull(sObj.getUtfString("notSerializable"));
    assertNull(sObj.getUtfString("ignoredField"));
    assertEquals(entity.getStringField(), sObj.getUtfString("stringField"));
    assertNotNull(sObj.getSFSObject("subEntity"));
    assertEquals(subEntity.getLongField(), sObj.getSFSObject("subEntity").getLong("longField"));
    assertNotNull(sObj.getSFSArray("subEntities"));
    Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("subEntities").iterator();
    assertEquals(100L, ((SFSObject) iterator.next().getObject()).getLong("longField").longValue());

    assertEquals(
        "testString",
        sObj.getSFSObject("subEntity").getUtfStringArray("stringsList").iterator().next());
  }
コード例 #2
0
  @Test
  public void testSerializeCustom() {
    SFSSerializer sfsSerializer = new SFSSerializer();
    Entity entity = new Entity();
    Entity subEntity = new Entity();
    Entity subEntityCustom = new Entity();
    subEntity.setIntField(10);
    subEntityCustom.setIntField(99);
    entity.setWildcardList(Arrays.asList(subEntity));
    entity.setWildcardedListCustom(Arrays.asList(subEntityCustom));

    sfsSerializer.registerPostProcessor(
        new SerializePostProcessor() {
          @Override
          public <ST, DT extends TransportObject> void process(ST result, DT sourceObject) {
            if (sourceObject instanceof Entity)
              ((ISFSObject) result).putBool("postProcessed", true);
          }
        });
    sfsSerializer.registerPreProcessor(
        new SerializePreProcessor() {
          @Override
          public <DT> void process(DT sourceObject) {
            if (sourceObject instanceof Entity) ((Entity) sourceObject).setPreProcessed(true);
          }
        });

    ISFSObject sObj = sfsSerializer.serialize(entity);

    assertNotNull(sObj.getSFSArray("wildcardList"));
    assertTrue(sObj.getBool("postProcessed"));
    assertTrue(sObj.getBool("preProcessed"));
    Iterator<SFSDataWrapper> iterator = sObj.getSFSArray("wildcardList").iterator();
    assertEquals(
        subEntity.getIntField() * 2,
        ((SFSObject) iterator.next().getObject()).getInt("intField").intValue());

    Iterator<SFSDataWrapper> customIterator = sObj.getSFSArray("wildcardedListCustom").iterator();
    assertEquals(
        subEntityCustom.getIntField(),
        ((SFSObject) customIterator.next().getObject()).getInt("intField"));
  }