use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testCustomSerializer.
/**
* @throws Exception If failed.
*/
public void testCustomSerializer() throws Exception {
BinaryTypeConfiguration type = new BinaryTypeConfiguration(CustomSerializedObject1.class.getName());
type.setSerializer(new CustomSerializer1());
BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(type));
CustomSerializedObject1 obj1 = new CustomSerializedObject1(10);
BinaryObject po1 = marshal(obj1, marsh);
assertEquals(20, po1.<CustomSerializedObject1>deserialize().val);
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testTypeNamesSimpleNameMapper.
/**
* @throws Exception If failed.
*/
public void testTypeNamesSimpleNameMapper() 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;
}
});
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));
BinaryContext ctx = binaryContext(marsh);
// Full name hashCode.
assertEquals("notconfiguredclass".hashCode(), ctx.typeId("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"));
// BinaryIdMapper.typeId() contract.
assertEquals("nonexistentclass0".hashCode(), ctx.typeId("NonExistentClass0"));
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testCustomIdMapper.
/**
* @throws Exception If failed.
*/
public void testCustomIdMapper() throws Exception {
BinaryTypeConfiguration type = new BinaryTypeConfiguration(CustomMappedObject1.class.getName());
type.setIdMapper(new BinaryIdMapper() {
@Override
public int typeId(String clsName) {
return 11111;
}
@Override
public int fieldId(int typeId, String fieldName) {
assert typeId == 11111;
if ("val1".equals(fieldName))
return 22222;
else if ("val2".equals(fieldName))
return 33333;
assert false : "Unknown field: " + fieldName;
return 0;
}
});
BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(type));
CustomMappedObject1 obj1 = new CustomMappedObject1(10, "str");
BinaryObjectExImpl po1 = marshal(obj1, marsh);
assertEquals(11111, po1.type().typeId());
assertEquals((Integer) 10, po1.field(22222));
assertEquals("str", po1.field(33333));
assertEquals(10, po1.<CustomMappedObject1>deserialize().val1);
assertEquals("str", po1.<CustomMappedObject1>deserialize().val2);
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryFooterOffsetsAbstractSelfTest method beforeTest.
/** {@inheritDoc} */
@Override
protected void beforeTest() throws Exception {
super.beforeTest();
ctx = new BinaryContext(BinaryCachingMetadataHandler.create(), new IgniteConfiguration(), new NullLogger());
marsh = new BinaryMarshaller();
IgniteConfiguration iCfg = new IgniteConfiguration();
BinaryConfiguration bCfg = new BinaryConfiguration();
bCfg.setTypeConfigurations(Arrays.asList(new BinaryTypeConfiguration(TestObject.class.getName())));
bCfg.setCompactFooter(compactFooter());
iCfg.setBinaryConfiguration(bCfg);
marsh.setContext(new MarshallerContextTestImpl(null));
IgniteUtils.invoke(BinaryMarshaller.class, marsh, "setBinaryContext", ctx, iCfg);
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testBinaryCopyStringArray.
/**
* @throws Exception If failed.
*/
public void testBinaryCopyStringArray() throws Exception {
BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
SimpleObject obj = simpleObject();
BinaryObject po = marshal(obj, marsh);
BinaryObject copy = copy(po, F.<String, Object>asMap("strArr", new String[] { "str1", "str2" }));
assertArrayEquals(new String[] { "str1", "str2" }, copy.<String[]>field("strArr"));
SimpleObject obj0 = copy.deserialize();
assertArrayEquals(new String[] { "str1", "str2" }, obj0.strArr);
}
Aggregations