Search in sources :

Example 36 with BinaryObjectBuilder

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;
                }
        }
    }
}
Also used : BinaryBuilderEnum(org.apache.ignite.internal.binary.builder.BinaryBuilderEnum) Field(java.lang.reflect.Field) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryBuilderEnum(org.apache.ignite.internal.binary.builder.BinaryBuilderEnum) GridBinaryTestClasses(org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Example 37 with BinaryObjectBuilder

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"));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) BigInteger(java.math.BigInteger) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 38 with BinaryObjectBuilder

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"));
    }
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) GridBinaryTestClasses(org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses) Date(java.util.Date) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException)

Example 39 with BinaryObjectBuilder

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);
}
Also used : BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) MarshallerContext(org.apache.ignite.marshaller.MarshallerContext)

Example 40 with BinaryObjectBuilder

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();
}
Also used : BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder)

Aggregations

BinaryObjectBuilder (org.apache.ignite.binary.BinaryObjectBuilder)83 BinaryObject (org.apache.ignite.binary.BinaryObject)63 Ignite (org.apache.ignite.Ignite)11 IgniteBinary (org.apache.ignite.IgniteBinary)8 GridBinaryTestClasses (org.apache.ignite.internal.binary.mutabletest.GridBinaryTestClasses)7 Date (java.util.Date)4 HashMap (java.util.HashMap)4 BinaryType (org.apache.ignite.binary.BinaryType)4 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 GridQueryProperty (org.apache.ignite.internal.processors.query.GridQueryProperty)3 GridQueryTypeDescriptor (org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor)3 GridH2RowDescriptor (org.apache.ignite.internal.processors.query.h2.opt.GridH2RowDescriptor)3 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)3 Column (org.h2.table.Column)3 Field (java.lang.reflect.Field)2 SQLException (java.sql.SQLException)2 LinkedHashMap (java.util.LinkedHashMap)2 UUID (java.util.UUID)2