Java Code Examples for org.apache.ignite.binary.BinaryObject#deserialize()

The following examples show how to use org.apache.ignite.binary.BinaryObject#deserialize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 *
 */
@SuppressWarnings("unchecked")
@Test
public void testCopyFromInnerObjects() {
    ArrayList<Object> list = new ArrayList<>();
    list.add(new GridBinaryTestClasses.TestObjectAllTypes());
    list.add(list.get(0));

    GridBinaryTestClasses.TestObjectContainer c = new GridBinaryTestClasses.TestObjectContainer(list);

    BinaryObjectBuilderImpl builder = builder(toBinary(c));
    builder.<List>getField("foo").add("!!!");

    BinaryObject res = builder.build();

    GridBinaryTestClasses.TestObjectContainer deserialized = res.deserialize();

    List deserializedList = (List)deserialized.foo;

    assertSame(deserializedList.get(0), deserializedList.get(1));
    assertEquals("!!!", deserializedList.get(2));
    assertTrue(deserializedList.get(0) instanceof GridBinaryTestClasses.TestObjectAllTypes);
}
 
Example 2
Source File: BinaryObjectBuilderDefaultMappersSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 *
 */
@Test
public void testRemoveFromExistingObject() {
    GridBinaryTestClasses.TestObjectAllTypes obj = new GridBinaryTestClasses.TestObjectAllTypes();
    obj.setDefaultData();
    obj.enumArr = null;

    BinaryObjectBuilder builder = builder(toBinary(obj));

    builder.removeField("str");

    BinaryObject binary = builder.build();

    GridBinaryTestClasses.TestObjectAllTypes deserialzied = binary.deserialize();

    assertNull(deserialzied.str);
}
 
Example 3
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testDateAndTimestampInSingleObject() throws Exception {
    BinaryTypeConfiguration cfg1 = new BinaryTypeConfiguration(DateClass1.class.getName());

    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(cfg1));

    Date date = new Date();
    Timestamp ts = new Timestamp(System.currentTimeMillis());
    Time time = new Time(System.currentTimeMillis());
    Time[] timeArr = new Time[]{time, new Time(date.getTime()), new Time(System.currentTimeMillis())};

    DateClass1 obj1 = new DateClass1();
    obj1.date = date;
    obj1.ts = ts;
    obj1.time = time;
    obj1.timeArr = timeArr;

    BinaryObject po1 = marshal(obj1, marsh);

    assertEquals(date, po1.field("date"));
    assertEquals(Date.class, po1.field("date").getClass());
    assertEquals(ts, po1.field("ts"));
    assertEquals(Timestamp.class, po1.field("ts").getClass());
    assertEquals(time, po1.field("time"));
    assertEquals(Time.class, po1.field("time").getClass());
    assertArrayEquals(timeArr, (Object[])po1.field("timeArr"));
    assertEquals(Time[].class, po1.field("timeArr").getClass());

    obj1 = po1.deserialize();
    assertEquals(date, obj1.date);
    assertEquals(ts, obj1.ts);
    assertEquals(time, obj1.time);
    assertArrayEquals(timeArr, obj1.timeArr);
}
 
Example 4
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testWriteReplaceInheritable() throws Exception {
    ImmutableList<String> obj = ImmutableList.of("This is a test");

    BinaryMarshaller marsh = binaryMarshaller(Collections.singleton(
        new BinaryTypeConfiguration(obj.getClass().getName())
    ));

    BinaryObject po = marshal(obj, marsh);

    Object des = po.deserialize();

    assertEquals(obj, des);
}
 
Example 5
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyNonPrimitives() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(
        new BinaryTypeConfiguration(SimpleObject.class.getName())
    ));

    SimpleObject obj = simpleObject();

    BinaryObject po = marshal(obj, marsh);

    Map<String, Object> map = new HashMap<>(3, 1.0f);

    SimpleObject newObj = new SimpleObject();

    newObj.i = 12345;
    newObj.fArr = new float[] {5, 8, 0};
    newObj.str = "newStr";

    map.put("str", "str555");
    map.put("inner", newObj);
    map.put("bArr", new byte[] {6, 7, 9});

    BinaryObject copy = copy(po, map);

    assertEquals("str555", copy.<String>field("str"));
    assertEquals(newObj, copy.<BinaryObject>field("inner").deserialize());
    assertArrayEquals(new byte[] {6, 7, 9}, copy.<byte[]>field("bArr"));

    SimpleObject obj0 = copy.deserialize();

    assertEquals("str555", obj0.str);
    assertEquals(newObj, obj0.inner);
    assertArrayEquals(new byte[] {6, 7, 9}, obj0.bArr);
}
 
Example 6
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
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);
}
 
Example 7
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyMixed() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(new BinaryTypeConfiguration(SimpleObject.class.getName())));

    SimpleObject obj = simpleObject();

    BinaryObject po = marshal(obj, marsh);

    Map<String, Object> map = new HashMap<>(3, 1.0f);

    SimpleObject newObj = new SimpleObject();

    newObj.i = 12345;
    newObj.fArr = new float[] {5, 8, 0};
    newObj.str = "newStr";

    map.put("i", 1234);
    map.put("str", "str555");
    map.put("inner", newObj);
    map.put("s", (short)2323);
    map.put("bArr", new byte[] {6, 7, 9});
    map.put("b", (byte)111);

    BinaryObject copy = copy(po, map);

    assertEquals(1234, copy.<Integer>field("i").intValue());
    assertEquals("str555", copy.<String>field("str"));
    assertEquals(newObj, copy.<BinaryObject>field("inner").deserialize());
    assertEquals((short)2323, copy.<Short>field("s").shortValue());
    assertArrayEquals(new byte[] {6, 7, 9}, copy.<byte[]>field("bArr"));
    assertEquals((byte)111, copy.<Byte>field("b").byteValue());

    SimpleObject obj0 = copy.deserialize();

    assertEquals(1234, obj0.i);
    assertEquals("str555", obj0.str);
    assertEquals(newObj, obj0.inner);
    assertEquals((short)2323, obj0.s);
    assertArrayEquals(new byte[] {6, 7, 9}, obj0.bArr);
    assertEquals((byte)111, obj0.b);
}
 
Example 8
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyObject() throws Exception {
    BinaryMarshaller marsh = binaryMarshaller(Arrays.asList(
        new BinaryTypeConfiguration(SimpleObject.class.getName())
    ));

    SimpleObject obj = simpleObject();

    BinaryObject po = marshal(obj, marsh);

    SimpleObject newObj = new SimpleObject();

    newObj.i = 12345;
    newObj.fArr = new float[] {5, 8, 0};
    newObj.str = "newStr";

    BinaryObject copy = copy(po, F.<String, Object>asMap("inner", newObj));

    assertEquals(newObj, copy.<BinaryObject>field("inner").deserialize());

    SimpleObject obj0 = copy.deserialize();

    assertEquals(newObj, obj0.inner);
}
 
Example 9
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
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);
}
 
Example 10
Source File: BinaryObjectBuilderAdditionalSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 *
 */
@Test
public void testFloatArrayModification() {
    GridBinaryTestClasses.TestObjectAllTypes obj = new GridBinaryTestClasses.TestObjectAllTypes();

    obj.fArr = new float[] {1.0f, 1.0f, 1.0f};

    BinaryObjectBuilderImpl mutObj = wrap(obj);

    float[] arr = mutObj.getField("fArr");
    arr[0] = 2.0f;

    BinaryObject resBinary = mutObj.build();

    GridBinaryTestClasses.TestObjectAllTypes res = resBinary.deserialize();

    Assert.assertArrayEquals(new float[] {2.0f, 1.0f, 1.0f}, res.fArr, 0);
}
 
Example 11
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyFloatArray() 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("fArr", new float[] {1, 2, 3}));

    assertArrayEquals(new float[] {1, 2, 3}, copy.<float[]>field("fArr"), 0);

    SimpleObject obj0 = copy.deserialize();

    assertArrayEquals(new float[] {1, 2, 3}, obj0.fArr, 0);
}
 
Example 12
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyLongArray() 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("lArr", new long[] {1, 2, 3}));

    assertArrayEquals(new long[] {1, 2, 3}, copy.<long[]>field("lArr"));

    SimpleObject obj0 = copy.deserialize();

    assertArrayEquals(new long[] {1, 2, 3}, obj0.lArr);
}
 
Example 13
Source File: IgnitePdsBinaryMetadataOnClusterRestartTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @param nodesCnt Number of nodes in grid.
 */
private void examineStaticMetadata(int nodesCnt) {
    for (int i = 0; i < nodesCnt; i++) {
        IgniteCache cache = grid(i).cache(CACHE_NAME).withKeepBinary();

        BinaryObject o1 = (BinaryObject) cache.get(0);

        TestValue1 t1 = o1.deserialize();

        assertEquals(0, t1.getValue());

        BinaryObject o2 = (BinaryObject) cache.get(1);

        TestValue2 t2 = o2.deserialize();

        assertEquals("value", t2.getValue());

        assertEquals(TestValue1.class.getName(), o1.type().typeName());
        assertEquals(TestValue2.class.getName(), o2.type().typeName());

        assertEquals("val", o1.type().affinityKeyFieldName());
    }
}
 
Example 14
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyShortArray() 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("sArr", new short[] {1, 2, 3}));

    assertArrayEquals(new short[] {1, 2, 3}, copy.<short[]>field("sArr"));

    SimpleObject obj0 = copy.deserialize();

    assertArrayEquals(new short[] {1, 2, 3}, obj0.sArr);
}
 
Example 15
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyByteArray() 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("bArr", new byte[] {1, 2, 3}));

    assertArrayEquals(new byte[] {1, 2, 3}, copy.<byte[]>field("bArr"));

    SimpleObject obj0 = copy.deserialize();

    assertArrayEquals(new byte[] {1, 2, 3}, obj0.bArr);
}
 
Example 16
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
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);
}
 
Example 17
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testBinaryCopyString() 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("str", "str3"));

    assertEquals("str3", copy.<String>field("str"));

    SimpleObject obj0 = copy.deserialize();

    assertEquals("str3", obj0.str);
}
 
Example 18
Source File: BinaryMarshallerSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
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);
}
 
Example 19
Source File: GridCacheBinaryAtomicEntryProcessorDeploymentSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception Exception.
 */
private void doTestGet(boolean withKeepBinary) throws Exception {
    try {
        startGrid(0);

        startClientGrid(1);

        Class valCls = grid(1).configuration().getClassLoader().loadClass(TEST_VALUE);

        assertTrue(grid(1).configuration().isClientMode());
        assertFalse(grid(0).configuration().isClientMode());

        IgniteCache cache1 = grid(1).cache(DEFAULT_CACHE_NAME);
        IgniteCache cache0 = grid(0).cache(DEFAULT_CACHE_NAME);

        if (withKeepBinary) {
            cache1 = cache1.withKeepBinary();
            cache0 = cache0.withKeepBinary();
        }

        cache1.put("key", valCls.newInstance());

        if (withKeepBinary) {
            BinaryObject obj = (BinaryObject)(cache0.get("key"));

            try {
                obj.deserialize();

                fail("Exception did not happened.");
            }
            catch (BinaryInvalidTypeException ignored) {
                // No-op.
            }
        }
        else
            try {
                cache0.get("key");

                fail("Exception did not happened.");
            }
            catch (CacheException ex) {
                assertTrue(X.hasCause(ex, BinaryInvalidTypeException.class));
            }
    }
    finally {
        stopAllGrids();
    }
}
 
Example 20
Source File: GridCacheBinaryObjectsAbstractSelfTest.java    From ignite with Apache License 2.0 2 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@SuppressWarnings("unchecked")
@Test
public void testCircularReference() throws Exception {
    IgniteCache c = keepBinaryCache();

    TestReferenceObject obj1 = new TestReferenceObject();

    obj1.obj = new TestReferenceObject(obj1);

    c.put(1, obj1);

    BinaryObject po = (BinaryObject)c.get(1);

    String str = po.toString();

    log.info("toString: " + str);

    assertNotNull(str);

    BinaryNameMapper nameMapper = BinaryContext.defaultNameMapper();

    if (cfg.getBinaryConfiguration() != null && cfg.getBinaryConfiguration().getNameMapper() != null)
        nameMapper = cfg.getBinaryConfiguration().getNameMapper();

    String typeName = nameMapper.typeName(TestReferenceObject.class.getName());

    assertTrue("Unexpected toString: " + str,
        S.includeSensitive() ?
        str.startsWith(typeName) && str.contains("obj=" + typeName + " [") :
        str.startsWith("BinaryObject") && str.contains("idHash=") && str.contains("hash=")
    );

    TestReferenceObject obj1_r = po.deserialize();

    assertNotNull(obj1_r);

    TestReferenceObject obj2_r = obj1_r.obj;

    assertNotNull(obj2_r);

    assertSame(obj1_r, obj2_r.obj);
}