Search in sources :

Example 21 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.

the class BinaryMarshallerSelfTest method testSimpleObject.

/**
     * @throws Exception If failed.
     */
public void testSimpleObject() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    SimpleObject obj = simpleObject();
    BinaryObject po = marshal(obj, marsh);
    assertEquals(obj, po.deserialize());
    assertEquals(obj.b, (byte) po.field("b"));
    assertEquals(obj.s, (short) po.field("s"));
    assertEquals(obj.i, (int) po.field("i"));
    assertEquals(obj.l, (long) po.field("l"));
    assertEquals(obj.f, (float) po.field("f"), 0);
    assertEquals(obj.d, (double) po.field("d"), 0);
    assertEquals(obj.c, (char) po.field("c"));
    assertEquals(obj.bool, (boolean) po.field("bool"));
    assertEquals(obj.str, po.field("str"));
    assertEquals(obj.uuid, po.field("uuid"));
    assertEquals(obj.date, po.field("date"));
    assertEquals(Date.class, obj.date.getClass());
    assertEquals(obj.ts, po.field("ts"));
    assertArrayEquals(obj.bArr, (byte[]) po.field("bArr"));
    assertArrayEquals(obj.sArr, (short[]) po.field("sArr"));
    assertArrayEquals(obj.iArr, (int[]) po.field("iArr"));
    assertArrayEquals(obj.lArr, (long[]) po.field("lArr"));
    assertArrayEquals(obj.fArr, (float[]) po.field("fArr"), 0);
    assertArrayEquals(obj.dArr, (double[]) po.field("dArr"), 0);
    assertArrayEquals(obj.cArr, (char[]) po.field("cArr"));
    assertBooleanArrayEquals(obj.boolArr, (boolean[]) po.field("boolArr"));
    assertArrayEquals(obj.strArr, (String[]) po.field("strArr"));
    assertArrayEquals(obj.uuidArr, (UUID[]) po.field("uuidArr"));
    assertArrayEquals(obj.dateArr, (Date[]) po.field("dateArr"));
    assertArrayEquals(obj.objArr, (Object[]) po.field("objArr"));
    assertEquals(obj.col, po.field("col"));
    assertEquals(obj.map, po.field("map"));
    assertEquals(new Integer(obj.enumVal.ordinal()), new Integer(((BinaryObject) po.field("enumVal")).enumOrdinal()));
    assertArrayEquals(ordinals(obj.enumArr), ordinals((BinaryObject[]) po.field("enumArr")));
    assertNull(po.field("unknown"));
    BinaryObject innerPo = po.field("inner");
    assertEquals(obj.inner, innerPo.deserialize());
    assertEquals(obj.inner.b, (byte) innerPo.field("b"));
    assertEquals(obj.inner.s, (short) innerPo.field("s"));
    assertEquals(obj.inner.i, (int) innerPo.field("i"));
    assertEquals(obj.inner.l, (long) innerPo.field("l"));
    assertEquals(obj.inner.f, (float) innerPo.field("f"), 0);
    assertEquals(obj.inner.d, (double) innerPo.field("d"), 0);
    assertEquals(obj.inner.c, (char) innerPo.field("c"));
    assertEquals(obj.inner.bool, (boolean) innerPo.field("bool"));
    assertEquals(obj.inner.str, innerPo.field("str"));
    assertEquals(obj.inner.uuid, innerPo.field("uuid"));
    assertEquals(obj.inner.date, innerPo.field("date"));
    assertEquals(Date.class, obj.inner.date.getClass());
    assertEquals(obj.inner.ts, innerPo.field("ts"));
    assertArrayEquals(obj.inner.bArr, (byte[]) innerPo.field("bArr"));
    assertArrayEquals(obj.inner.sArr, (short[]) innerPo.field("sArr"));
    assertArrayEquals(obj.inner.iArr, (int[]) innerPo.field("iArr"));
    assertArrayEquals(obj.inner.lArr, (long[]) innerPo.field("lArr"));
    assertArrayEquals(obj.inner.fArr, (float[]) innerPo.field("fArr"), 0);
    assertArrayEquals(obj.inner.dArr, (double[]) innerPo.field("dArr"), 0);
    assertArrayEquals(obj.inner.cArr, (char[]) innerPo.field("cArr"));
    assertBooleanArrayEquals(obj.inner.boolArr, (boolean[]) innerPo.field("boolArr"));
    assertArrayEquals(obj.inner.strArr, (String[]) innerPo.field("strArr"));
    assertArrayEquals(obj.inner.uuidArr, (UUID[]) innerPo.field("uuidArr"));
    assertArrayEquals(obj.inner.dateArr, (Date[]) innerPo.field("dateArr"));
    assertArrayEquals(obj.inner.objArr, (Object[]) innerPo.field("objArr"));
    assertEquals(obj.inner.col, innerPo.field("col"));
    assertEquals(obj.inner.map, innerPo.field("map"));
    assertEquals(new Integer(obj.inner.enumVal.ordinal()), new Integer(((BinaryObject) innerPo.field("enumVal")).enumOrdinal()));
    assertArrayEquals(ordinals(obj.inner.enumArr), ordinals((BinaryObject[]) innerPo.field("enumArr")));
    assertNull(innerPo.field("inner"));
    assertNull(innerPo.field("unknown"));
}
Also used : BigInteger(java.math.BigInteger) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) BinaryObject(org.apache.ignite.binary.BinaryObject) UUID(java.util.UUID) Date(java.util.Date)

Example 22 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.

the class BinaryMarshallerSelfTest method testDynamicObject.

/**
     * @throws Exception If failed.
     */
public void testDynamicObject() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(DynamicObject.class.getName())));
    BinaryObject po1 = marshal(new DynamicObject(0, 10, 20, 30), marsh);
    assertEquals(new Integer(10), po1.field("val1"));
    assertEquals(null, po1.field("val2"));
    assertEquals(null, po1.field("val3"));
    DynamicObject do1 = po1.deserialize();
    assertEquals(10, do1.val1);
    assertEquals(0, do1.val2);
    assertEquals(0, do1.val3);
    BinaryObject po2 = marshal(new DynamicObject(1, 10, 20, 30), marsh);
    assertEquals(new Integer(10), po2.field("val1"));
    assertEquals(new Integer(20), po2.field("val2"));
    assertEquals(null, po2.field("val3"));
    DynamicObject do2 = po2.deserialize();
    assertEquals(10, do2.val1);
    assertEquals(20, do2.val2);
    assertEquals(0, do2.val3);
    BinaryObject po3 = marshal(new DynamicObject(2, 10, 20, 30), marsh);
    assertEquals(new Integer(10), po3.field("val1"));
    assertEquals(new Integer(20), po3.field("val2"));
    assertEquals(new Integer(30), po3.field("val3"));
    DynamicObject do3 = po3.deserialize();
    assertEquals(10, do3.val1);
    assertEquals(20, do3.val2);
    assertEquals(30, do3.val3);
}
Also used : BigInteger(java.math.BigInteger) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 23 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.

the class BinaryMarshallerSelfTest method testBinaryCopyUuid.

/**
     * @throws Exception If failed.
     */
public void testBinaryCopyUuid() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));
    SimpleObject obj = simpleObject();
    BinaryObject po = marshal(obj, marsh);
    UUID uuid = UUID.randomUUID();
    BinaryObject copy = copy(po, F.<String, Object>asMap("uuid", uuid));
    assertEquals(uuid, copy.<UUID>field("uuid"));
    SimpleObject obj0 = copy.deserialize();
    assertEquals(uuid, obj0.uuid);
}
Also used : BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) UUID(java.util.UUID)

Example 24 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.

the class BinaryMarshallerSelfTest method testCustomIdMapperWithGlobal.

/**
     * @throws Exception If failed.
     */
public void testCustomIdMapperWithGlobal() throws Exception {
    BinaryTypeConfiguration type1 = new BinaryTypeConfiguration(CustomMappedObject1.class.getName());
    BinaryTypeConfiguration type2 = new BinaryTypeConfiguration(CustomMappedObject2.class.getName());
    type2.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(null, 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;
        }
    }, Arrays.asList(type1, type2));
    CustomMappedObject1 obj1 = new CustomMappedObject1(10, "str1");
    BinaryObjectExImpl po1 = marshal(obj1, marsh);
    assertEquals(11111, po1.type().typeId());
    assertEquals((Integer) 10, po1.field(22222));
    assertEquals("str1", po1.field(33333));
    assertEquals(10, po1.<CustomMappedObject1>deserialize().val1);
    assertEquals("str1", po1.<CustomMappedObject1>deserialize().val2);
    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);
}
Also used : BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Example 25 with BinaryTypeConfiguration

use of org.apache.ignite.binary.BinaryTypeConfiguration in project ignite by apache.

the class BinaryMarshallerSelfTest method testDuplicateTypeId.

/**
     * @throws Exception If failed.
     */
public void testDuplicateTypeId() throws Exception {
    BinaryTypeConfiguration customType1 = new BinaryTypeConfiguration("org.gridgain.Class1");
    customType1.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 100;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    BinaryTypeConfiguration customType2 = new BinaryTypeConfiguration("org.gridgain.Class2");
    customType2.setIdMapper(new BinaryIdMapper() {

        @Override
        public int typeId(String clsName) {
            return 100;
        }

        @Override
        public int fieldId(int typeId, String fieldName) {
            return 0;
        }
    });
    try {
        binaryMarshaller(Arrays.asList(customType1, customType2));
    } catch (IgniteCheckedException e) {
        assertEquals("Duplicate type ID [clsName=org.gridgain.Class2, id=100]", e.getCause().getCause().getMessage());
        return;
    }
    assert false;
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration)

Aggregations

BinaryTypeConfiguration (org.apache.ignite.binary.BinaryTypeConfiguration)67 BinaryObject (org.apache.ignite.binary.BinaryObject)25 BinaryIdMapper (org.apache.ignite.binary.BinaryIdMapper)21 BinaryConfiguration (org.apache.ignite.configuration.BinaryConfiguration)18 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)16 BinaryBasicNameMapper (org.apache.ignite.binary.BinaryBasicNameMapper)11 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)8 BinaryMarshaller (org.apache.ignite.internal.binary.BinaryMarshaller)8 TcpDiscoverySpi (org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)8 HashMap (java.util.HashMap)7 LinkedHashMap (java.util.LinkedHashMap)7 BinaryBasicIdMapper (org.apache.ignite.binary.BinaryBasicIdMapper)7 BigInteger (java.math.BigInteger)6 ArrayList (java.util.ArrayList)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 CacheKeyConfiguration (org.apache.ignite.cache.CacheKeyConfiguration)5 UUID (java.util.UUID)4 Date (java.util.Date)3 Map (java.util.Map)3 TreeMap (java.util.TreeMap)3