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);
}
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();
}
}
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);
}
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();
}
}
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);
}
Aggregations