use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testTypeNamesFullNameMappers.
/**
* @throws Exception If failed.
*/
public void testTypeNamesFullNameMappers() 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(false), new BinaryBasicIdMapper(false), 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.class.getName().hashCode(), ctx.typeId(Key.class.getName()));
assertEquals("org.gridgain.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 testCycleLink.
/**
* @throws Exception If failed.
*/
public void testCycleLink() throws Exception {
BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(CycleLinkObject.class.getName())));
CycleLinkObject obj = new CycleLinkObject();
obj.self = obj;
BinaryObject po = marshal(obj, marsh);
CycleLinkObject obj0 = po.deserialize();
assert obj0.self == obj0;
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testDecimalFields.
/**
*/
public void testDecimalFields() throws Exception {
Collection<BinaryTypeConfiguration> clsNames = new ArrayList<>();
clsNames.add(new BinaryTypeConfiguration(DecimalReflective.class.getName()));
clsNames.add(new BinaryTypeConfiguration(DecimalMarshalAware.class.getName()));
BinaryMarshaller marsh = binaryMarshaller(clsNames);
// 1. Test reflective stuff.
DecimalReflective obj1 = new DecimalReflective();
BigDecimal[] valArr = new BigDecimal[] { BigDecimal.ONE, BigDecimal.TEN, new BigDecimal("-100.5"), BigDecimal.valueOf(Long.MAX_VALUE, 0), BigDecimal.valueOf(Long.MIN_VALUE, 0), BigDecimal.valueOf(Long.MAX_VALUE, 8), BigDecimal.valueOf(Long.MIN_VALUE, 8) };
obj1.val = BigDecimal.ZERO;
obj1.valArr = valArr;
BinaryObjectImpl portObj = marshal(obj1, marsh);
assertArrayEquals(obj1.valArr, portObj.<BigDecimal[]>field("valArr"));
assertArrayEquals(obj1.valArr, portObj.<DecimalReflective>deserialize().valArr);
assertArrayEquals(obj1.valArr, (BigDecimal[]) portObj.type().field("valArr").value(portObj));
obj1.valArr = null;
for (BigDecimal v : valArr) {
obj1.val = v;
portObj = marshal(obj1, marsh);
assertEquals(obj1.val, portObj.field("val"));
assertEquals(obj1.val, portObj.<DecimalReflective>deserialize().val);
assertEquals(obj1.val, portObj.type().field("val").value(portObj));
}
// 2. Test marshal aware stuff.
DecimalMarshalAware obj2 = new DecimalMarshalAware();
obj2.val = BigDecimal.ZERO;
obj2.valArr = new BigDecimal[] { BigDecimal.ONE, BigDecimal.TEN.negate() };
obj2.rawVal = BigDecimal.TEN;
obj2.rawValArr = new BigDecimal[] { BigDecimal.ZERO, BigDecimal.ONE };
portObj = marshal(obj2, marsh);
assertEquals(obj2.val, portObj.field("val"));
assertArrayEquals(obj2.valArr, portObj.<BigDecimal[]>field("valArr"));
assertEquals(obj2.val, portObj.<DecimalMarshalAware>deserialize().val);
assertArrayEquals(obj2.valArr, portObj.<DecimalMarshalAware>deserialize().valArr);
assertEquals(obj2.rawVal, portObj.<DecimalMarshalAware>deserialize().rawVal);
assertArrayEquals(obj2.rawValArr, portObj.<DecimalMarshalAware>deserialize().rawValArr);
assertEquals(obj2.val, portObj.type().field("val").value(portObj));
assertArrayEquals(obj2.valArr, (BigDecimal[]) portObj.type().field("valArr").value(portObj));
for (BigDecimal v : valArr) {
obj2.val = v;
portObj = marshal(obj2, marsh);
assertEquals(obj2.val, portObj.field("val"));
assertEquals(obj2.val, portObj.<DecimalMarshalAware>deserialize().val);
assertEquals(obj2.val, portObj.type().field("val").value(portObj));
}
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class BinaryMarshallerSelfTest method testBinaryCopyIntArray.
/**
* @throws Exception If failed.
*/
public void testBinaryCopyIntArray() 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("iArr", new int[] { 1, 2, 3 }));
assertArrayEquals(new int[] { 1, 2, 3 }, copy.<int[]>field("iArr"));
SimpleObject obj0 = copy.deserialize();
assertArrayEquals(new int[] { 1, 2, 3 }, obj0.iArr);
}
use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.
the class GridCacheBinaryObjectUserClassloaderSelfTest method getConfiguration.
/**
* {@inheritDoc}
*/
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
TcpDiscoverySpi disco = new TcpDiscoverySpi();
disco.setIpFinder(ipFinder);
cfg.setDiscoverySpi(disco);
cfg.setCacheConfiguration(cacheConfiguration(igniteInstanceName));
cfg.setMarshaller(new BinaryMarshaller());
cfg.setClassLoader(useWrappingLoader ? new WrappingClassLoader(getExternalClassLoader()) : getExternalClassLoader());
if (customBinaryConf) {
BinarySerializer bs = new BinarySerializer() {
/**
* {@inheritDoc}
*/
@Override
public void writeBinary(Object obj, BinaryWriter writer) throws BinaryObjectException {
// No-op.
}
/**
* {@inheritDoc}
*/
@Override
public void readBinary(Object obj, BinaryReader reader) throws BinaryObjectException {
deserialized = true;
}
};
BinaryTypeConfiguration btcfg1 = new BinaryTypeConfiguration();
btcfg1.setTypeName("org.apache.ignite.tests.p2p.CacheDeploymentTestValue");
btcfg1.setSerializer(bs);
BinaryTypeConfiguration btcfg2 = new BinaryTypeConfiguration();
btcfg2.setTypeName("org.apache.ignite.internal.processors.cache.binary." + "GridCacheBinaryObjectUserClassloaderSelfTest$TestValue1");
btcfg2.setSerializer(bs);
BinaryConfiguration bcfg = new BinaryConfiguration();
Set<BinaryTypeConfiguration> set = new HashSet<>();
set.add(btcfg1);
set.add(btcfg2);
bcfg.setTypeConfigurations(set);
cfg.setBinaryConfiguration(bcfg);
}
return cfg;
}
Aggregations