/** * @param obj Original object. * @param marsh Marshaller. * @return Result object. */ private <T> T marshalUnmarshal(Object obj, BinaryMarshaller marsh) throws IgniteCheckedException { byte[] bytes = marsh.marshal(obj); return marsh.unmarshal(bytes, null); }
@Override public TestObject applyx() throws IgniteCheckedException { return marsh.unmarshal(tuple.get(), null); } };
/** * @throws Exception If failed. */ @Test public void testUnregisteredClass() throws Exception { BinaryMarshaller m = binaryMarshaller(null, Collections.singletonList(Value.class.getName())); ClassFieldObject res = m.unmarshal(m.marshal(new ClassFieldObject(Value.class)), null); assertEquals(Value.class, res.cls); }
/** * @throws Exception If failed. */ @Test public void testMarshallingThroughJdk() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); InetSocketAddress addr = new InetSocketAddress("192.168.0.2", 4545); byte[] arr = marsh.marshal(addr); InetSocketAddress addr2 = marsh.unmarshal(arr, null); assertEquals(addr.getHostString(), addr2.getHostString()); assertEquals(addr.getPort(), addr2.getPort()); TestAddress testAddr = new TestAddress(); testAddr.addr = addr; testAddr.str1 = "Hello World"; SimpleObject simpleObj = new SimpleObject(); simpleObj.c = 'g'; simpleObj.date = new Date(); testAddr.obj = simpleObj; arr = marsh.marshal(testAddr); TestAddress testAddr2 = marsh.unmarshal(arr, null); assertEquals(testAddr.addr.getHostString(), testAddr2.addr.getHostString()); assertEquals(testAddr.addr.getPort(), testAddr2.addr.getPort()); assertEquals(testAddr.str1, testAddr2.str1); assertEquals(testAddr.obj.c, testAddr2.obj.c); assertEquals(testAddr.obj.date, testAddr2.obj.date); }
/** * @throws Exception If failed. */ @Test public void testProxy() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); SomeItf inItf = (SomeItf)Proxy.newProxyInstance( BinaryMarshallerSelfTest.class.getClassLoader(), new Class[] {SomeItf.class}, new InvocationHandler() { private NonSerializable obj = new NonSerializable(null); @Override public Object invoke(Object proxy, Method mtd, Object[] args) throws Throwable { if ("hashCode".equals(mtd.getName())) return obj.hashCode(); obj.checkAfterUnmarshalled(); return 17; } } ); SomeItf outItf = marsh.unmarshal(marsh.marshal(inItf), null); assertEquals(outItf.checkAfterUnmarshalled(), 17); }
/** * Test object with {@link Proxy} field. * * @throws Exception If fails. */ @Test public void testObjectContainingProxy() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); SomeItf inItf = (SomeItf)Proxy.newProxyInstance( BinaryMarshallerSelfTest.class.getClassLoader(), new Class[] {SomeItf.class}, new InvocationHandler() { private NonSerializable obj = new NonSerializable(null); @Override public Object invoke(Object proxy, Method mtd, Object[] args) throws Throwable { if ("hashCode".equals(mtd.getName())) return obj.hashCode(); obj.checkAfterUnmarshalled(); return 17; } } ); SomeItf outItf = marsh.unmarshal(marsh.marshal(inItf), null); assertEquals(outItf.checkAfterUnmarshalled(), 17); }
/** * @throws Exception If failed. */ @Test public void testDeclaredBodyEnum() throws Exception { final MarshallerContextTestImpl ctx = new MarshallerContextTestImpl(); ctx.registerClassName((byte)0, 1, EnumObject.class.getName()); ctx.registerClassName((byte)0, 2, DeclaredBodyEnum.class.getName()); BinaryMarshaller marsh = binaryMarshaller(); marsh.setContext(ctx); EnumObject obj = new EnumObject(1L, "test 1", DeclaredBodyEnum.TWO); final byte[] marshal = marsh.marshal(obj); final Object restored = marsh.unmarshal(marshal, null); assertTrue(restored instanceof EnumObject); obj = (EnumObject)restored; assertEquals(1, obj.id); assertEquals(DeclaredBodyEnum.TWO.ordinal(), obj.type.ordinal()); assertEquals(DeclaredBodyEnum.TWO, obj.type); assertTrue(obj.type == DeclaredBodyEnum.TWO); }
assertEquals(simpleObj, marsh.unmarshal(marsh.marshal(simpleObj), null)); simpleBinary.arr = new long[] {100, 200, 300}; assertEquals(simpleBinary, marsh.unmarshal(marsh.marshal(simpleBinary), null)); simpleExtr.arr = new long[] {20000, 300000, 400000}; assertEquals(simpleExtr, marsh.unmarshal(marsh.marshal(simpleExtr), null));
/** * @throws Exception If failed. */ @Test public void testClassFieldsMarshalling() throws Exception { BinaryMarshaller marsh = binaryMarshaller(); ObjectWithClassFields obj = new ObjectWithClassFields(); obj.cls1 = BinaryMarshallerSelfTest.class; byte[] marshal = marsh.marshal(obj); ObjectWithClassFields obj2 = marsh.unmarshal(marshal, null); assertEquals(obj.cls1, obj2.cls1); assertNull(obj2.cls2); BinaryObject portObj = marshal(obj, marsh); Class cls1 = portObj.field("cls1"); assertEquals(obj.cls1, cls1); }