use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testSimpleTypeFieldRead.
/**
* @throws Exception If failed.
*/
public void testSimpleTypeFieldRead() throws Exception {
GridBinaryTestClasses.TestObjectAllTypes exp = new GridBinaryTestClasses.TestObjectAllTypes();
exp.setDefaultData();
BinaryObjectBuilder mutPo = wrap(exp);
for (Field field : GridBinaryTestClasses.TestObjectAllTypes.class.getDeclaredFields()) {
Object expVal = field.get(exp);
Object actVal = mutPo.getField(field.getName());
switch(field.getName()) {
case "anEnum":
assertEquals(((BinaryBuilderEnum) actVal).getOrdinal(), ((Enum) expVal).ordinal());
break;
case "enumArr":
{
BinaryBuilderEnum[] actArr = (BinaryBuilderEnum[]) actVal;
Enum[] expArr = (Enum[]) expVal;
assertEquals(expArr.length, actArr.length);
for (int i = 0; i < actArr.length; i++) assertEquals(expArr[i].ordinal(), actArr[i].getOrdinal());
break;
}
}
}
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testCollectionsSerialization.
/**
* Check that correct type is stored in binary object.
*/
public void testCollectionsSerialization() {
final BinaryObjectBuilder root = newWrapper(BigInteger.class);
final List<Integer> arrList = new ArrayList<>();
arrList.add(Integer.MAX_VALUE);
final List<Integer> linkedList = new LinkedList<>();
linkedList.add(Integer.MAX_VALUE);
final Set<Integer> hashSet = new HashSet<>();
hashSet.add(Integer.MAX_VALUE);
final Set<Integer> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add(Integer.MAX_VALUE);
final Map<String, String> hashMap = new HashMap<>();
hashMap.put("key", "val");
final Map<String, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put("key", "val");
// collections
root.setField("arrayList", arrList);
root.setField("linkedList", linkedList);
root.setField("hashSet", hashSet);
root.setField("linkedHashSet", linkedHashSet);
root.setField("singletonList", Collections.singletonList(Integer.MAX_VALUE), Collection.class);
root.setField("singletonSet", Collections.singleton(Integer.MAX_VALUE), Collection.class);
// maps
root.setField("hashMap", hashMap);
root.setField("linkedHashMap", linkedHashMap);
root.setField("singletonMap", Collections.singletonMap("key", "val"), Map.class);
// objects
root.setField("asList", Collections.singletonList(Integer.MAX_VALUE));
root.setField("asSet", Collections.singleton(Integer.MAX_VALUE));
root.setField("asMap", Collections.singletonMap("key", "val"));
root.setField("asListHint", Collections.singletonList(Integer.MAX_VALUE), List.class);
root.setField("asSetHint", Collections.singleton(Integer.MAX_VALUE), Set.class);
root.setField("asMapHint", (AbstractMap) Collections.singletonMap("key", "val"), AbstractMap.class);
BinaryObject binaryObj = root.build();
final String COL = "Collection";
final String MAP = "Map";
final String OBJ = "Object";
assert COL.equals(binaryObj.type().fieldTypeName("arrayList"));
assert COL.equals(binaryObj.type().fieldTypeName("linkedList"));
assert COL.equals(binaryObj.type().fieldTypeName("hashSet"));
assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
assert COL.equals(binaryObj.type().fieldTypeName("linkedHashSet"));
assert COL.equals(binaryObj.type().fieldTypeName("singletonList"));
assert COL.equals(binaryObj.type().fieldTypeName("singletonSet"));
assert MAP.equals(binaryObj.type().fieldTypeName("singletonMap"));
assert COL.equals(binaryObj.type().fieldTypeName("asList"));
assert OBJ.equals(binaryObj.type().fieldTypeName("asSet"));
assert OBJ.equals(binaryObj.type().fieldTypeName("asMap"));
assert OBJ.equals(binaryObj.type().fieldTypeName("asListHint"));
assert OBJ.equals(binaryObj.type().fieldTypeName("asSetHint"));
assert OBJ.equals(binaryObj.type().fieldTypeName("asMapHint"));
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testWrongMetadataNullField2.
/**
*/
public void testWrongMetadataNullField2() {
BinaryObjectBuilder builder = binaries().builder("SomeType1");
builder.setField("dateField", null);
builder.setField("objectField", null, Integer.class);
BinaryObject obj = builder.build();
try {
builder = binaries().builder(obj);
builder.setField("dateField", new Date());
builder.build();
} catch (BinaryObjectException ex) {
assertTrue(ex.getMessage().startsWith("Wrong value has been set"));
}
builder = binaries().builder(obj);
try {
builder.setField("objectField", new GridBinaryTestClasses.Company());
builder.build();
fail("BinaryObjectBuilder accepted wrong metadata");
} catch (BinaryObjectException ex) {
assertTrue(ex.getMessage().startsWith("Wrong value has been set"));
}
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinaryObjectBuilderAdditionalSelfTest method testMarshallerMappings.
/**
* Test {@link BinaryObjectBuilder#build()} adds type mapping to the binary marshaller's cache.
*/
public void testMarshallerMappings() throws IgniteCheckedException, ClassNotFoundException {
String typeName = "TestType";
int typeId = BinaryContext.defaultIdMapper().typeId(typeName);
BinaryObjectBuilder builder = newWrapper(typeName);
builder.build();
MarshallerContext marshCtx = grid(0).context().marshallerContext();
String actualTypeName = marshCtx.getClassName(MarshallerPlatformIds.JAVA_ID, typeId);
assertEquals(typeName, actualTypeName);
}
use of org.apache.ignite.binary.BinaryObjectBuilder in project ignite by apache.
the class BinarySerialiedFieldComparatorSelfTest method build.
/**
* Build object.
*
* @param parts Parts.
* @return Result.
*/
private BinaryObjectImpl build(Object... parts) {
String typeName = "Type" + TYPE_CTR.get();
BinaryObjectBuilder builder = grid().binary().builder(typeName);
if (!F.isEmpty(parts)) {
for (int i = 0; i < parts.length; ) builder.setField((String) parts[i++], parts[i++]);
}
return (BinaryObjectImpl) builder.build();
}
Aggregations