use of org.apache.ignite.binary.BinaryBasicIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testTypeNamesSimpleNameMappers.
/**
* @throws Exception If failed.
*/
public void testTypeNamesSimpleNameMappers() throws Exception {
BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration(Value.class.getName());
customType1.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 300;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.NonExistentClass1");
customType2.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 400;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
BinaryTypeConfiguration customType3 = new BinaryTypeConfiguration("NonExistentClass2");
customType3.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 500;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
BinaryTypeConfiguration customType4 = new BinaryTypeConfiguration("NonExistentClass0");
customType4.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 0;
}
@Override
public int fieldId(int typeId, String fieldName) {
return 0;
}
});
BinaryTypeConfiguration customType5 = new BinaryTypeConfiguration(DateClass1.class.getName());
customType5.setNameMapper(new BinaryBasicNameMapper(false));
customType5.setIdMapper(new BinaryBasicIdMapper(false));
BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(true), new BinaryBasicIdMapper(true), Arrays.asList(new BinaryTypeConfiguration(Key.class.getName()), new BinaryTypeConfiguration("org.gridgain.NonExistentClass3"), new BinaryTypeConfiguration("NonExistentClass4"), customType1, customType2, customType3, customType4, customType5));
BinaryContext ctx = binaryContext(marsh);
assertEquals("notconfiguredclass".hashCode(), ctx.typeId("NotConfiguredClass"));
assertEquals("notconfiguredclass".hashCode(), ctx.typeId("org.blabla.NotConfiguredClass"));
assertEquals("key".hashCode(), ctx.typeId(Key.class.getName()));
assertEquals("nonexistentclass3".hashCode(), ctx.typeId("org.gridgain.NonExistentClass3"));
assertEquals("nonexistentclass4".hashCode(), ctx.typeId("NonExistentClass4"));
assertEquals(300, ctx.typeId(Value.class.getName()));
assertEquals(400, ctx.typeId("org.gridgain.NonExistentClass1"));
assertEquals(500, ctx.typeId("NonExistentClass2"));
assertEquals(DateClass1.class.getName().hashCode(), ctx.typeId(DateClass1.class.getName()));
// BinaryIdMapper.typeId() contract.
assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
}
use of org.apache.ignite.binary.BinaryBasicIdMapper in project ignite by apache.
the class BinaryBasicIdMapperSelfTest method testDefaultCase.
/**
* @throws Exception If failed.
*/
public void testDefaultCase() throws Exception {
BinaryBasicIdMapper mapper = new BinaryBasicIdMapper(false);
assertEquals(GridBinaryTestClass1.class.getName().hashCode(), mapper.typeId(GridBinaryTestClass1.class.getName()));
assertEquals((GridBinaryTestClass1.class.getName() + "$InnerClass").hashCode(), mapper.typeId(GridBinaryTestClass1.class.getName() + "$InnerClass"));
}
use of org.apache.ignite.binary.BinaryBasicIdMapper in project ignite by apache.
the class BinaryBasicIdMapperSelfTest method testLowerCase.
/**
* @throws Exception If failed.
*/
public void testLowerCase() throws Exception {
BinaryBasicIdMapper mapper = new BinaryBasicIdMapper(true);
assertEquals(GridBinaryTestClass1.class.getName().toLowerCase().hashCode(), mapper.typeId(GridBinaryTestClass1.class.getName()));
assertEquals((GridBinaryTestClass1.class.getName() + "$InnerClass").toLowerCase().hashCode(), mapper.typeId(GridBinaryTestClass1.class.getName() + "$InnerClass"));
}
use of org.apache.ignite.binary.BinaryBasicIdMapper in project ignite by apache.
the class BinaryConfigurationConsistencySelfTest method customConfig.
/**
* @return Custom BinaryConfiguration.
* @param compactFooter Compact footer.
*/
private BinaryConfiguration customConfig(boolean compactFooter) {
BinaryConfiguration c = new BinaryConfiguration();
c.setIdMapper(new BinaryBasicIdMapper(true));
c.setSerializer(new BinarySerializer() {
@Override
public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
// No-op.
}
@Override
public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
// No-op.
}
});
c.setCompactFooter(compactFooter);
BinaryTypeConfiguration btc = new BinaryTypeConfiguration("org.MyClass");
btc.setIdMapper(BinaryContext.defaultIdMapper());
btc.setEnum(false);
btc.setSerializer(new BinarySerializer() {
@Override
public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
// No-op.
}
@Override
public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
// No-op.
}
});
c.setTypeConfigurations(Arrays.asList(btc));
return c;
}
use of org.apache.ignite.binary.BinaryBasicIdMapper in project ignite by apache.
the class BinaryMarshallerSelfTest method testSimpleNameLowerCaseMappers.
/**
* @throws Exception If failed.
*/
public void testSimpleNameLowerCaseMappers() throws Exception {
BinaryTypeConfiguration innerClassType = new BinaryTypeConfiguration(InnerMappedObject.class.getName());
BinaryTypeConfiguration publicClassType = new BinaryTypeConfiguration(TestMappedObject.class.getName());
BinaryTypeConfiguration typeWithCustomMapper = new BinaryTypeConfiguration(CustomMappedObject2.class.getName());
typeWithCustomMapper.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 44444;
}
@Override
public int fieldId(int typeId, String fieldName) {
assert typeId == 44444;
if ("val1".equals(fieldName))
return 55555;
else if ("val2".equals(fieldName))
return 66666;
assert false : "Unknown field: " + fieldName;
return 0;
}
});
BinaryMarshaller marsh = binaryMarshaller(new BinaryBasicNameMapper(true), new BinaryBasicIdMapper(true), Arrays.asList(innerClassType, publicClassType, typeWithCustomMapper));
InnerMappedObject innerObj = new InnerMappedObject(10, "str1");
BinaryObjectExImpl innerBo = marshal(innerObj, marsh);
assertEquals("InnerMappedObject".toLowerCase().hashCode(), innerBo.type().typeId());
assertEquals(10, innerBo.<CustomMappedObject1>deserialize().val1);
assertEquals("str1", innerBo.<CustomMappedObject1>deserialize().val2);
TestMappedObject publicObj = new TestMappedObject();
BinaryObjectExImpl publicBo = marshal(publicObj, marsh);
assertEquals("TestMappedObject".toLowerCase().hashCode(), publicBo.type().typeId());
CustomMappedObject2 obj2 = new CustomMappedObject2(20, "str2");
BinaryObjectExImpl po2 = marshal(obj2, marsh);
assertEquals(44444, po2.type().typeId());
assertEquals((Integer) 20, po2.field(55555));
assertEquals("str2", po2.field(66666));
assertEquals(20, po2.<CustomMappedObject2>deserialize().val1);
assertEquals("str2", po2.<CustomMappedObject2>deserialize().val2);
}
Aggregations