Search in sources :

Example 1 with OObjectSerializerContext

use of com.orientechnologies.orient.object.serialization.OObjectSerializerContext in project orientdb by orientechnologies.

the class CustomDatatypeTest method reproduce.

@Test
public void reproduce() throws Exception {
    final OObjectDatabaseTx db = new OObjectDatabaseTx("memory:CustomDatatypeTest");
    db.create();
    // WrappedString custom datatype registration (storing it as
    // OType.STRING)
    OObjectSerializerContext serializerContext = new OObjectSerializerContext();
    serializerContext.bind(new OObjectSerializer<WrappedString, String>() {

        @Override
        public String serializeFieldValue(Class<?> iClass, WrappedString iFieldValue) {
            return iFieldValue.getValue();
        }

        @Override
        public WrappedString unserializeFieldValue(Class<?> iClass, String iFieldValue) {
            final WrappedString result = new WrappedString();
            result.setValue(iFieldValue);
            return result;
        }
    }, db);
    OObjectSerializerHelper.bindSerializerContext(WrappedString.class, serializerContext);
    // we want schema to be generated
    db.setAutomaticSchemaGeneration(true);
    // register our entity
    db.getEntityManager().registerEntityClass(Entity.class);
    // Validate DB did figure out schema properly
    Assert.assertEquals(db.getMetadata().getSchema().getClass(Entity.class).getProperty("data").getType(), OType.STRING);
}
Also used : OObjectSerializerContext(com.orientechnologies.orient.object.serialization.OObjectSerializerContext) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) Test(org.testng.annotations.Test)

Example 2 with OObjectSerializerContext

use of com.orientechnologies.orient.object.serialization.OObjectSerializerContext in project orientdb by orientechnologies.

the class ObjectTreeTest method testEnumListWithCustomTypes.

@Test(dependsOnMethods = "testCustomTypesDatabaseNewInstance")
public void testEnumListWithCustomTypes() {
    OObjectDatabaseTx database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    ORID rid = null;
    try {
        OObjectSerializerContext serializerContext = new OObjectSerializerContext();
        serializerContext.bind(new OObjectSerializer<SecurityRole, String>() {

            @Override
            public Object serializeFieldValue(Class<?> type, SecurityRole role) {
                return role.name();
            }

            @Override
            public Object unserializeFieldValue(Class<?> type, String str) {
                return SecurityRole.getByName(str);
            }
        }, database);
        OObjectSerializerHelper.bindSerializerContext(null, serializerContext);
        database.getEntityManager().registerEntityClasses("com.orientechnologies.orient.test.domain.customserialization");
        Sec s = new Sec();
        s.getSecurityRoleList().add(SecurityRole.LOGIN);
        Assert.assertTrue(s.getSecurityRoleList().contains(SecurityRole.LOGIN));
        s = database.save(s);
        rid = database.getRecordByUserObject(s, false).getIdentity();
        database.close();
        database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
        s = database.load(rid);
        Assert.assertTrue(s.getSecurityRoleList().contains(SecurityRole.LOGIN));
    } finally {
        database.close();
    }
}
Also used : Sec(com.orientechnologies.orient.test.domain.customserialization.Sec) OObjectSerializerContext(com.orientechnologies.orient.object.serialization.OObjectSerializerContext) SecurityRole(com.orientechnologies.orient.test.domain.customserialization.SecurityRole) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) OSchemaProxyObject(com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject) ORID(com.orientechnologies.orient.core.id.ORID) Test(org.testng.annotations.Test)

Example 3 with OObjectSerializerContext

use of com.orientechnologies.orient.object.serialization.OObjectSerializerContext in project orientdb by orientechnologies.

the class ObjectTreeTestSchemaFull method testCustomTypes.

@Test(dependsOnMethods = "testPool")
public void testCustomTypes() {
    OObjectSerializerContext serializerContext = new OObjectSerializerContext();
    serializerContext.bind(new OObjectSerializer<CustomType, Long>() {

        @Override
        public Long serializeFieldValue(Class<?> itype, CustomType iFieldValue) {
            serialized++;
            return iFieldValue.value;
        }

        @Override
        public CustomType unserializeFieldValue(Class<?> itype, Long iFieldValue) {
            unserialized++;
            return new CustomType(iFieldValue);
        }
    }, database);
    OObjectSerializerHelper.bindSerializerContext(null, serializerContext);
    database.getEntityManager().registerEntityClass(CustomClass.class);
    if (!database.getMetadata().getSchema().existsClass("CustomClass"))
        database.getMetadata().getSchema().createClass("CustomClass");
    List<CustomType> customTypesList = new ArrayList<CustomType>();
    customTypesList.add(new CustomType(102L));
    Set<CustomType> customTypeSet = new HashSet<CustomType>();
    customTypeSet.add(new CustomType(103L));
    Map<Long, CustomType> customTypeMap = new HashMap<Long, CustomType>();
    customTypeMap.put(1L, new CustomType(104L));
    CustomClass pojo = new CustomClass("test", 33L, new CustomType(101L), customTypesList, customTypeSet, customTypeMap);
    // init counters
    serialized = 0;
    unserialized = 0;
    pojo = database.save(pojo);
    Assert.assertEquals(serialized, 4);
    Assert.assertEquals(unserialized, 0);
    pojo = database.reload(pojo);
    Assert.assertEquals(unserialized, 0);
    pojo.getCustom();
    Assert.assertEquals(unserialized, 1);
    Assert.assertTrue(pojo.getCustom() instanceof CustomType);
    pojo.getCustomTypeList().iterator().next();
    Assert.assertEquals(unserialized, 2);
    Assert.assertTrue(pojo.getCustomTypeList().iterator().next() instanceof CustomType);
    unserialized--;
    pojo.getCustomTypeSet().iterator().next();
    Assert.assertEquals(unserialized, 3);
    Assert.assertTrue(pojo.getCustomTypeSet().iterator().next() instanceof CustomType);
    unserialized--;
    pojo.getCustomTypeMap().get(1L);
    Assert.assertEquals(serialized, 4);
    Assert.assertEquals(unserialized, 4);
    Assert.assertTrue(pojo.getCustomTypeMap().get(1L) instanceof CustomType);
}
Also used : OObjectSerializerContext(com.orientechnologies.orient.object.serialization.OObjectSerializerContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 4 with OObjectSerializerContext

use of com.orientechnologies.orient.object.serialization.OObjectSerializerContext in project orientdb by orientechnologies.

the class ObjectTreeTestSchemaFull method testEnumListWithCustomTypes.

@Test(dependsOnMethods = "testCustomTypesDatabaseNewInstance")
public void testEnumListWithCustomTypes() {
    OObjectDatabaseTx database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
    ORID rid = null;
    try {
        OObjectSerializerContext serializerContext = new OObjectSerializerContext();
        serializerContext.bind(new OObjectSerializer<SecurityRole, String>() {

            @Override
            public Object serializeFieldValue(Class<?> type, SecurityRole role) {
                return role.name();
            }

            @Override
            public Object unserializeFieldValue(Class<?> type, String str) {
                return SecurityRole.getByName(str);
            }
        }, database);
        OObjectSerializerHelper.bindSerializerContext(null, serializerContext);
        database.getEntityManager().registerEntityClasses("com.orientechnologies.orient.test.domain.customserialization");
        Sec s = new Sec();
        s.getSecurityRoleList().add(SecurityRole.LOGIN);
        Assert.assertTrue(s.getSecurityRoleList().contains(SecurityRole.LOGIN));
        s = database.save(s);
        rid = database.getRecordByUserObject(s, false).getIdentity();
        database.close();
        database = OObjectDatabasePool.global().acquire(url, "admin", "admin");
        s = database.load(rid);
        Assert.assertTrue(s.getSecurityRoleList().contains(SecurityRole.LOGIN));
    } finally {
        database.close();
    }
}
Also used : Sec(com.orientechnologies.orient.test.domain.customserialization.Sec) OObjectSerializerContext(com.orientechnologies.orient.object.serialization.OObjectSerializerContext) SecurityRole(com.orientechnologies.orient.test.domain.customserialization.SecurityRole) OObjectDatabaseTx(com.orientechnologies.orient.object.db.OObjectDatabaseTx) ORID(com.orientechnologies.orient.core.id.ORID) Test(org.testng.annotations.Test)

Example 5 with OObjectSerializerContext

use of com.orientechnologies.orient.object.serialization.OObjectSerializerContext in project orientdb by orientechnologies.

the class ObjectTreeTest method testCustomTypes.

@Test(dependsOnMethods = "testPool")
public void testCustomTypes() {
    OObjectSerializerContext serializerContext = new OObjectSerializerContext();
    serializerContext.bind(new OObjectSerializer<CustomType, Long>() {

        @Override
        public Long serializeFieldValue(Class<?> itype, CustomType iFieldValue) {
            serialized++;
            return iFieldValue.value;
        }

        @Override
        public CustomType unserializeFieldValue(Class<?> itype, Long iFieldValue) {
            unserialized++;
            return new CustomType(iFieldValue);
        }
    }, database);
    OObjectSerializerHelper.bindSerializerContext(null, serializerContext);
    database.getEntityManager().registerEntityClass(CustomClass.class);
    if (!database.getMetadata().getSchema().existsClass("CustomClass"))
        database.getMetadata().getSchema().createClass("CustomClass");
    List<CustomType> customTypesList = new ArrayList<CustomType>();
    customTypesList.add(new CustomType(102L));
    Set<CustomType> customTypeSet = new HashSet<CustomType>();
    customTypeSet.add(new CustomType(103L));
    Map<Long, CustomType> customTypeMap = new HashMap<Long, CustomType>();
    customTypeMap.put(1L, new CustomType(104L));
    CustomClass pojo = new CustomClass("test", 33L, new CustomType(101L), customTypesList, customTypeSet, customTypeMap);
    // init counters
    serialized = 0;
    unserialized = 0;
    pojo = database.save(pojo);
    Assert.assertEquals(serialized, 4);
    Assert.assertEquals(unserialized, 0);
    pojo = database.reload(pojo);
    Assert.assertEquals(unserialized, 0);
    pojo.getCustom();
    Assert.assertEquals(unserialized, 1);
    Assert.assertTrue(pojo.getCustom() instanceof CustomType);
    pojo.getCustomTypeList().iterator().next();
    Assert.assertEquals(unserialized, 2);
    Assert.assertTrue(pojo.getCustomTypeList().iterator().next() instanceof CustomType);
    unserialized--;
    pojo.getCustomTypeSet().iterator().next();
    Assert.assertEquals(unserialized, 3);
    Assert.assertTrue(pojo.getCustomTypeSet().iterator().next() instanceof CustomType);
    unserialized--;
    pojo.getCustomTypeMap().get(1L);
    Assert.assertEquals(serialized, 4);
    Assert.assertEquals(unserialized, 4);
    Assert.assertTrue(pojo.getCustomTypeMap().get(1L) instanceof CustomType);
}
Also used : OObjectSerializerContext(com.orientechnologies.orient.object.serialization.OObjectSerializerContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Aggregations

OObjectSerializerContext (com.orientechnologies.orient.object.serialization.OObjectSerializerContext)5 Test (org.testng.annotations.Test)5 OObjectDatabaseTx (com.orientechnologies.orient.object.db.OObjectDatabaseTx)3 ORID (com.orientechnologies.orient.core.id.ORID)2 Sec (com.orientechnologies.orient.test.domain.customserialization.Sec)2 SecurityRole (com.orientechnologies.orient.test.domain.customserialization.SecurityRole)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 OSchemaProxyObject (com.orientechnologies.orient.object.metadata.schema.OSchemaProxyObject)1