You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2014/12/05 00:24:20 UTC
[28/53] [abbrv] incubator-ignite git commit: # gg-9470-rename
# gg-9470-rename
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/40cf54f6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/40cf54f6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/40cf54f6
Branch: refs/heads/master
Commit: 40cf54f6819e381aa16a30b9dfe11bc1d14ccaf2
Parents: 3edf39e
Author: sboikov <sb...@gridgain.com>
Authored: Thu Dec 4 21:59:54 2014 +0300
Committer: sboikov <sb...@gridgain.com>
Committed: Thu Dec 4 21:59:54 2014 +0300
----------------------------------------------------------------------
.../GridOptimizedMarshallerEnumSelfTest.java | 55 +
.../GridOptimizedMarshallerSelfTest.java | 437 ++++
.../optimized/GridOptimizedMarshallerTest.java | 815 +++++++
.../GridOptimizedObjectStreamSelfTest.java | 2086 ++++++++++++++++++
.../GridTestTcpDiscoveryIpFinderAdapter.java | 36 +
.../ignite/marshaller/optimized/package.html | 15 +
.../GridOptimizedMarshallerEnumSelfTest.java | 55 -
.../GridOptimizedMarshallerSelfTest.java | 437 ----
.../optimized/GridOptimizedMarshallerTest.java | 816 -------
.../GridOptimizedObjectStreamSelfTest.java | 2086 ------------------
.../GridTestTcpDiscoveryIpFinderAdapter.java | 36 -
.../grid/marshaller/optimized/package.html | 15 -
.../testsuites/GridMarshallerSelfTestSuite.java | 2 +-
13 files changed, 3445 insertions(+), 3446 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/40cf54f6/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerEnumSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerEnumSelfTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerEnumSelfTest.java
new file mode 100644
index 0000000..4bf911e
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerEnumSelfTest.java
@@ -0,0 +1,55 @@
+/* @java.file.header */
+
+/* _________ _____ __________________ _____
+ * __ ____/___________(_)______ /__ ____/______ ____(_)_______
+ * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
+ * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
+ * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
+ */
+
+package org.apache.ignite.marshaller.optimized;
+
+import junit.framework.*;
+import org.apache.ignite.marshaller.optimized.*;
+
+/**
+ *
+ */
+public class GridOptimizedMarshallerEnumSelfTest extends TestCase {
+ /**
+ * @throws Exception If failed.
+ */
+ public void testEnumSerialisation() throws Exception {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ byte[] bytes = marsh.marshal(TestEnum.Bond);
+
+ TestEnum unmarshalled = marsh.unmarshal(bytes, Thread.currentThread().getContextClassLoader());
+
+ assertEquals(TestEnum.Bond, unmarshalled);
+ assertEquals(TestEnum.Bond.desc, unmarshalled.desc);
+ }
+
+ private enum TestEnum {
+ Equity("Equity") {
+ @Override public String getTestString() {
+ return "eee";
+ }
+ },
+
+ Bond("Bond") {
+ @Override public String getTestString() {
+ return "qqq";
+ }
+ };
+
+ public final String desc;
+
+ TestEnum(String desc) {
+ this.desc = desc;
+ }
+
+ public abstract String getTestString();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/40cf54f6/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerSelfTest.java
new file mode 100644
index 0000000..aa018b4
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerSelfTest.java
@@ -0,0 +1,437 @@
+package org.apache.ignite.marshaller.optimized;
+
+import org.apache.ignite.lang.*;
+import org.apache.ignite.marshaller.*;
+import org.apache.ignite.marshaller.optimized.*;
+import org.gridgain.grid.*;
+import org.gridgain.grid.marshaller.*;
+import org.gridgain.grid.util.typedef.*;
+import org.gridgain.testframework.*;
+import org.gridgain.testframework.junits.common.*;
+
+import java.io.*;
+import java.util.concurrent.*;
+
+/**
+ * Optimized marshaller self test.
+ */
+@GridCommonTest(group = "Marshaller")
+public class GridOptimizedMarshallerSelfTest extends GridMarshallerAbstractTest {
+ /** {@inheritDoc} */
+ @Override protected GridMarshaller createMarshaller() {
+ GridOptimizedMarshaller m = new GridOptimizedMarshaller();
+
+ m.setRequireSerializable(false);
+ m.setClassNames(F.asList(GoodMarshallable.class.getName(), NoMarshallable.class.getName()));
+
+ return m;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testTestMarshalling() throws Exception {
+ final String msg = "PASSED";
+
+ assert msg != null;
+
+ byte[] buf = marshal(new IgniteRunnable() {
+ @Override public void run() {
+ c1.apply(msg);
+ c2.apply(msg);
+
+ c3.apply();
+ c4.reduce();
+
+ System.out.println("Test message: " + msg);
+ }
+ });
+
+ Runnable r = unmarshal(buf);
+
+ assertNotNull(r);
+
+ r.run();
+ }
+
+ /**
+ * Tests marshal self-linked object.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testMarshallingSelfLink() throws GridException {
+ SelfLink sl = new SelfLink("a string 1");
+
+ sl.link(sl);
+
+ SelfLink sl1 = unmarshal(marshal(sl));
+
+ assert sl1.link() == sl1;
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testInvalid() throws Exception {
+ GridTestUtils.assertThrows(
+ log,
+ new Callable<Object>() {
+ @Override public Object call() throws Exception {
+ unmarshal(new byte[10]);
+
+ return null;
+ }
+ },
+ GridException.class,
+ null
+ );
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testNested() throws Exception {
+ NestedTestObject obj = new NestedTestObject("String", 100);
+
+ NestedTestObject newObj = unmarshal(marshal(obj));
+
+ assertEquals("String", newObj.str);
+ assertEquals(100, newObj.val);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testGoodMarshallable() throws Exception {
+ GoodMarshallable obj = new GoodMarshallable("String", 100);
+
+ GoodMarshallable newObj = unmarshal(marshal(obj));
+
+ assertEquals("String", newObj.getString());
+ assertEquals(100, newObj.getInt());
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testBadMarshallable() throws Exception {
+ BadMarshallable obj = new BadMarshallable("String", 100);
+
+ try {
+ System.out.println(unmarshal(marshal(obj)));
+
+ assert false;
+ }
+ catch (GridException e) {
+ assert e.getCause() instanceof IOException;
+ assert e.getCause().getMessage().contains("must return the value of the field");
+ }
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void checkPerformance() throws Exception {
+ final int cnt = 5_000_000;
+
+ for (int j = 0; j < 5; j++) {
+ long start = System.nanoTime();
+
+ for (int i = 0; i < cnt; i++) {
+ String s = "string-" + i;
+
+ NoMarshallable obj = new NoMarshallable(s, i);
+
+ NoMarshallable newObj = unmarshal(marshal(obj));
+
+ assertEquals(s, newObj.getString());
+ }
+
+ X.println("Marshalling NoMarshallable duration: " + ((System.nanoTime() - start) / 1_000_000) + "ms");
+ }
+
+
+ for (int j = 0; j < 5; j++) {
+ long start = System.nanoTime();
+
+ for (int i = 0; i < cnt; i++) {
+ String s = "string-" + i;
+
+ GoodMarshallable obj = new GoodMarshallable(s, i);
+
+ GoodMarshallable newObj = unmarshal(marshal(obj));
+
+ assertEquals(s, newObj.getString());
+ }
+
+ X.println("Marshalling Marshallable duration: " + ((System.nanoTime() - start) / 1_000_000) + "ms");
+ }
+ }
+
+ /**
+ * Class for nested execution test.
+ */
+ private static class NestedTestObject implements Serializable {
+ /** */
+ private String str;
+
+ /** */
+ private int val;
+
+ /**
+ * @param str String.
+ * @param val Value.
+ */
+ private NestedTestObject(String str, int val) {
+ this.str = str;
+ this.val = val;
+ }
+
+ /** {@inheritDoc} */
+ private void writeObject(ObjectOutputStream out) throws IOException {
+ try {
+ byte[] arr = marshal(str);
+
+ out.writeInt(arr.length);
+ out.write(arr);
+
+ out.writeInt(val);
+ }
+ catch (GridException e) {
+ throw new IOException(e);
+ }
+ }
+
+ /** {@inheritDoc} */
+ @SuppressWarnings("UnusedParameters")
+ private void readObject(ObjectInputStream in) throws IOException {
+ try {
+ byte[] arr = new byte[in.readInt()];
+
+ in.read(arr);
+
+ str = unmarshal(arr);
+
+ val = in.readInt();
+ }
+ catch (GridException e) {
+ throw new IOException(e);
+ }
+ }
+ }
+
+ /** */
+ private static class TestObject2 {
+ /** */
+ private final int i;
+
+ /**
+ * Constructor for TestObject2 instances.
+ *
+ * @param i Integer value to hold.
+ */
+ private TestObject2(int i) {
+ this.i = i;
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object o) {
+ return i == ((TestObject2)o).i;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return i;
+ }
+ }
+
+ /**
+ * Static nested class.
+ */
+ private static class TestObject {
+ /** */
+ private final TestObject2 o2;
+
+ /** The only meaningful field in the class, used for {@link #equals(Object o)} and {@link #hashCode()}. */
+ private final String str;
+
+ /**
+ * @param str String to hold.
+ * @param i Integer.
+ */
+ TestObject(String str, int i) {
+ this.str = str;
+
+ o2 = new TestObject2(i);
+ }
+
+ /**
+ * Method for accessing value of the hold string after the object is created.
+ *
+ * @return Wrapped string.
+ */
+ public String string() {
+ return str;
+ }
+
+ /**
+ * @return Object held in this wrapped.
+ */
+ public TestObject2 obj() {
+ return o2;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return 31 * o2.hashCode() + str.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ @SuppressWarnings("RedundantIfStatement")
+ @Override public boolean equals(Object o) {
+ if (this == o)
+ return true;
+
+ if (o == null || getClass() != o.getClass())
+ return false;
+
+ TestObject obj = (TestObject)o;
+
+ if (o2 != null ? !o2.equals(obj.o2) : obj.o2 != null)
+ return false;
+
+ if (str != null ? !str.equals(obj.str) : obj.str != null)
+ return false;
+
+ return true;
+ }
+ }
+
+ /**
+ * Static nested class.
+ */
+ private static class SelfLink extends TestObject {
+ /** */
+ private SelfLink link;
+
+ /**
+ * @param str String to hold.
+ */
+ SelfLink(String str) {
+ super(str, 1);
+ }
+
+ /**
+ * @return The object this link points to,.
+ */
+ public SelfLink link() {
+ return link;
+ }
+
+ /**
+ * @param link The object this link should points to,
+ */
+ public void link(SelfLink link) {
+ this.link = link;
+ }
+ }
+
+ /**
+ *
+ */
+ public static class GoodMarshallable implements GridOptimizedMarshallable, Serializable {
+ /** Class ID required by {@link GridOptimizedMarshallable}. */
+ @SuppressWarnings({"NonConstantFieldWithUpperCaseName", "AbbreviationUsage", "UnusedDeclaration"})
+ private static Object GG_CLASS_ID;
+
+ /** */
+ private String str;
+
+ /** */
+ private int i;
+
+ /**
+ * @param str String.
+ * @param i Integer.
+ */
+ public GoodMarshallable(String str, int i) {
+ this.str = str;
+ this.i = i;
+ }
+
+ /**
+ * @return Int value.
+ */
+ private int getInt() {
+ return i;
+ }
+
+ /**
+ * @return String value
+ */
+ private String getString() {
+ return str;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Object ggClassId() {
+ return GG_CLASS_ID;
+ }
+ }
+
+ /**
+ *
+ */
+ public static class NoMarshallable implements Serializable {
+ /** */
+ private String str;
+
+ /** */
+ private int i;
+
+ /**
+ * @param str String.
+ * @param i Integer.
+ */
+ public NoMarshallable(String str, int i) {
+ this.str = str;
+ this.i = i;
+ }
+
+ /**
+ * @return Int value.
+ */
+ private int getInt() {
+ return i;
+ }
+
+ /**
+ * @return String value
+ */
+ private String getString() {
+ return str;
+ }
+ }
+
+ /**
+ *
+ */
+ private static class BadMarshallable extends TestObject implements GridOptimizedMarshallable {
+ /** Class ID required by {@link GridOptimizedMarshallable}. */
+ @SuppressWarnings({"NonConstantFieldWithUpperCaseName", "AbbreviationUsage", "UnusedDeclaration"})
+ private static Object GG_CLASS_ID;
+
+ /**
+ * @param str String.
+ * @param i Integer.
+ */
+ private BadMarshallable(String str, int i) {
+ super(str, i);
+ }
+
+ /** {@inheritDoc} */
+ @Override public Object ggClassId() {
+ return new Object();
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/40cf54f6/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java
new file mode 100644
index 0000000..d394039
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/GridOptimizedMarshallerTest.java
@@ -0,0 +1,815 @@
+package org.apache.ignite.marshaller.optimized;
+
+import org.apache.ignite.*;
+import org.apache.ignite.compute.*;
+import org.apache.ignite.marshaller.*;
+import org.gridgain.grid.*;
+import org.gridgain.grid.marshaller.*;
+import org.gridgain.grid.spi.discovery.tcp.ipfinder.*;
+import org.gridgain.grid.spi.discovery.tcp.ipfinder.vm.*;
+import org.gridgain.grid.util.typedef.internal.*;
+import org.gridgain.testframework.junits.common.*;
+import org.jetbrains.annotations.*;
+
+import java.io.*;
+import java.lang.reflect.*;
+import java.lang.reflect.Proxy;
+import java.net.*;
+import java.util.*;
+import java.util.concurrent.*;
+
+/**
+ *
+ */
+public class GridOptimizedMarshallerTest extends GridCommonAbstractTest {
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ NonSerializable outObj = marsh.unmarshal(marsh.marshal(new NonSerializable(null)), null);
+
+ outObj.checkAfterUnmarshalled();
+ }
+
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable1() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ byte[] bytes = marsh.marshal(new GridTcpDiscoveryVmIpFinder());
+
+ GridTcpDiscoveryIpFinder ipFinder = marsh.unmarshal(bytes, null);
+
+ assertFalse(ipFinder.isShared());
+
+ ipFinder = marsh.unmarshal(marsh.marshal(new GridTcpDiscoveryVmIpFinder(true)), null);
+
+ assertTrue(ipFinder.isShared());
+ }
+
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable2() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ GridTcpDiscoveryIpFinderAdapter ipFinder = new GridTcpDiscoveryIpFinderAdapter() {
+ @Override public Collection<InetSocketAddress> getRegisteredAddresses() {
+ return null;
+ }
+
+ @Override public void registerAddresses(Collection<InetSocketAddress> addrs) {
+ //No-op.
+ }
+
+ @Override public void unregisterAddresses(Collection<InetSocketAddress> addrs) {
+ //No-op.
+ }
+ };
+
+ ipFinder.setShared(false);
+
+ byte[] bytes = marsh.marshal(ipFinder);
+
+ ipFinder = marsh.unmarshal(bytes, null);
+
+ assertFalse(ipFinder.isShared());
+ }
+
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable3() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ byte[] bytes = marsh.marshal(new GridTestTcpDiscoveryIpFinderAdapter());
+
+ GridTcpDiscoveryIpFinder ipFinder = marsh.unmarshal(bytes, null);
+
+ assertFalse(ipFinder.isShared());
+ }
+
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable4() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ byte[] bytes = marsh.marshal(new GridMarshallerTestInheritedBean());
+
+ info(Arrays.toString(bytes));
+
+ GridMarshallerTestInheritedBean bean = marsh.unmarshal(bytes, null);
+
+ assertTrue(bean.isFlag());
+ }
+
+ /**
+ * Tests ability to marshal non-serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testNonSerializable5() throws GridException {
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ byte[] bytes = marsh.marshal(true);
+
+ Boolean val = marsh.unmarshal(bytes, null);
+
+ assertTrue(val);
+ }
+
+ /**
+ * Tests ability to marshal serializable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testSerializable() throws GridException {
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ SomeSerializable outObj = marsh.unmarshal(marsh.marshal(new SomeSerializable(null)), null);
+
+ outObj.checkAfterUnmarshalled();
+ }
+
+ /**
+ * @throws GridException If failed.
+ */
+ public void testSerializableAfterChangingValue() throws GridException {
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ SomeSimpleSerializable newObj = new SomeSimpleSerializable();
+
+ assert(newObj.flag);
+
+ newObj.setFlagValue(false);
+
+ assert(! newObj.flag);
+
+ SomeSimpleSerializable outObj = marsh.unmarshal(marsh.marshal(newObj), null);
+
+ assert (! outObj.flag);
+ }
+
+ /**
+ * Tests ability to marshal externalizable objects.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testExternalizable() throws GridException {
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ ExternalizableA outObj = marsh.unmarshal(marsh.marshal(new ExternalizableA(null, true)), null);
+ ExternalizableA outObj1 = marsh.unmarshal(marsh.marshal(new ExternalizableA(null, false)), null);
+
+ assertNotNull(outObj);
+ assertNotNull(outObj1);
+ }
+
+ /**
+ * Tests {@link GridOptimizedMarshaller#setRequireSerializable(boolean)}.
+ */
+ public void testRequireSerializable() {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(true);
+
+ try {
+ marsh.marshal(new NonSerializable(null));
+
+ fail();
+ }
+ catch (GridException ignore) {
+ // No-op.
+ }
+ }
+
+ /**
+ * Tests {@link GridOptimizedMarshaller#setClassNames(List)}.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testUserPreregisteredNames() throws GridException {
+ Object obj = new SomeSerializable(null);
+
+ // Clear caches.
+ ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear();
+ GridOptimizedClassResolver.userClasses(null, null);
+
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ int size1 = marsh.marshal(obj).length;
+
+ // Clear caches.
+ ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear();
+ GridOptimizedClassResolver.userClasses(null, null);
+
+ GridOptimizedMarshaller marshPreregistered = new GridOptimizedMarshaller();
+
+ marshPreregistered.setClassNames(Arrays.asList(SomeSerializable.class.getName()));
+
+ int size2 = marshPreregistered.marshal(obj).length;
+
+ assertTrue(size1 > size2);
+ }
+
+ /**
+ * Tests {@link GridOptimizedMarshaller#setClassNames(List)}.
+ *
+ * @throws GridException If marshalling failed.
+ * @throws IOException If an I/O error occurs.
+ */
+ public void testUserPreregisteredNamesPath() throws GridException, IOException {
+ Object obj = new SomeSerializable(null);
+
+ // Clear caches.
+ ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear();
+ GridOptimizedClassResolver.userClasses(null, null);
+
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ int size1 = marsh.marshal(obj).length;
+
+ // Clear caches.
+ ((Map)U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE")).clear();
+ GridOptimizedClassResolver.userClasses(null, null);
+
+ GridOptimizedMarshaller marshPreregistered = new GridOptimizedMarshaller();
+
+ File namesFile = File.createTempFile("gg-", null);
+
+ U.writeStringToFile(namesFile, SomeSerializable.class.getName(), "UTF-8");
+
+ marshPreregistered.setClassNamesPath(namesFile.getAbsolutePath());
+
+ int size2 = marshPreregistered.marshal(obj).length;
+
+ assertTrue(size1 > size2);
+ }
+
+ /**
+ * Tests {@link Proxy}.
+ *
+ * @throws GridException If marshalling failed.
+ */
+ public void testProxy() throws GridException {
+ GridOptimizedMarshaller marsh = new GridOptimizedMarshaller();
+
+ marsh.setRequireSerializable(false);
+
+ SomeItf inItf = (SomeItf)Proxy.newProxyInstance(
+ GridOptimizedMarshallerTest.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 {
+ obj.checkAfterUnmarshalled();
+
+ return 17;
+ }
+ }
+ );
+
+ SomeItf outItf = marsh.unmarshal(marsh.marshal(inItf), null);
+
+ assertEquals(outItf.checkAfterUnmarshalled(), 17);
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testDescriptorCache() throws Exception {
+ try {
+ Ignite ignite = startGridsMultiThreaded(2);
+
+ String taskClsName = "org.gridgain.grid.tests.p2p.GridSingleSplitTestTask";
+ String jobClsName = "org.gridgain.grid.tests.p2p.GridSingleSplitTestTask$GridSingleSplitTestJob";
+
+ ClassLoader ldr = getExternalClassLoader();
+
+ Class<? extends ComputeTask<?, ?>> taskCls = (Class<? extends ComputeTask<?, ?>>)ldr.loadClass(taskClsName);
+ Class<? extends ComputeTask<?, ?>> jobCls = (Class<? extends ComputeTask<?, ?>>)ldr.loadClass(jobClsName);
+
+ ignite.compute().localDeployTask(taskCls, ldr);
+
+ ignite.compute().execute(taskClsName, 2);
+
+ ConcurrentMap<Class<?>, GridOptimizedClassDescriptor> cache =
+ U.staticField(GridOptimizedMarshallerUtils.class, "CLS_DESC_CACHE");
+
+ assertTrue(cache.containsKey(jobCls));
+
+ ignite.compute().undeployTask(taskClsName);
+
+ // Wait for undeploy.
+ Thread.sleep(1000);
+
+ assertFalse(cache.containsKey(jobCls));
+ }
+ finally {
+ stopAllGrids();
+ }
+ }
+
+ /**
+ * @throws Exception If failed.
+ */
+ public void testPerformance() throws Exception {
+ System.gc();
+
+ checkPerformance(10000, 4);
+ }
+
+ /**
+ * @param cnt Number of marshalling attempts.
+ * @param tries Number of retries.
+ * @throws Exception If failed.
+ */
+ private void checkPerformance(int cnt, int tries) throws Exception {
+ GridMarshaller marsh = new GridOptimizedMarshaller();
+
+ for (int j = 0; j < tries; j++) {
+ System.gc();
+
+ long start = System.currentTimeMillis();
+
+ for (int i = 0; i < cnt; i++) {
+ TestCacheKey key = new TestCacheKey("key", "id");
+
+ TestCacheKey outKey = marsh.unmarshal(marsh.marshal(key), null);
+
+ assert key.equals(outKey);
+ assert key.hashCode() == outKey.hashCode();
+ }
+
+ info("Time non-serializable: " + (System.currentTimeMillis() - start));
+
+ System.gc();
+
+ start = System.currentTimeMillis();
+
+ for (int i = 0; i < cnt; i++) {
+ TestCacheKeySerializable key1 = new TestCacheKeySerializable("key", "id");
+
+ TestCacheKeySerializable outKey = marsh.unmarshal(marsh.marshal(key1), null);
+
+ assert key1.equals(outKey);
+ assert key1.hashCode() == outKey.hashCode();
+ }
+
+ info("Time serializable: " + (System.currentTimeMillis() - start));
+
+ System.gc();
+
+ start = System.currentTimeMillis();
+
+ for (int i = 0; i < cnt; i++) {
+ TestCacheKeyExternalizable key2 = new TestCacheKeyExternalizable("key", "id");
+
+ TestCacheKeyExternalizable outKey = marsh.unmarshal(marsh.marshal(key2), null);
+
+ assert key2.equals(outKey);
+ assert key2.hashCode() == outKey.hashCode();
+ }
+
+ info("Time externalizable: " + (System.currentTimeMillis() - start));
+
+ info(">>>");
+ }
+
+ info(">>> Finished performance check <<<");
+ }
+
+ /**
+ * Some non-serializable class.
+ */
+ @SuppressWarnings( {"PublicField","TransientFieldInNonSerializableClass","FieldMayBeStatic"})
+ private static class NonSerializableA {
+ /** */
+ private final long longVal = 0x33445566778899AAL;
+
+ /** */
+ protected Short shortVal = (short)0xAABB;
+
+ /** */
+ public String[] strArr = {"AA","BB"};
+
+ /** */
+ public boolean flag1 = true;
+
+ /** */
+ public boolean flag2;
+
+ /** */
+ public Boolean flag3;
+
+ /** */
+ public Boolean flag4 = true;
+
+ /** */
+ public Boolean flag5 = false;
+
+ /** */
+ private transient int intVal = 0xAABBCCDD;
+
+ /**
+ * @param strArr Array.
+ * @param shortVal Short value.
+ */
+ @SuppressWarnings( {"UnusedDeclaration"})
+ private NonSerializableA(@Nullable String[] strArr, @Nullable Short shortVal) {
+ // No-op.
+ }
+
+ /**
+ * Checks correctness of the state after unmarshalling.
+ */
+ void checkAfterUnmarshalled() {
+ assertEquals(longVal, 0x33445566778899AAL);
+
+ assertEquals(shortVal.shortValue(), (short)0xAABB);
+
+ assertTrue(Arrays.equals(strArr, new String[] {"AA","BB"}));
+
+ assertEquals(intVal, 0);
+
+ assertTrue(flag1);
+ assertFalse(flag2);
+ assertNull(flag3);
+ assertTrue(flag4);
+ assertFalse(flag5);
+ }
+ }
+
+ /**
+ * Some non-serializable class.
+ */
+ @SuppressWarnings( {"PublicField","TransientFieldInNonSerializableClass","PackageVisibleInnerClass"})
+ static class NonSerializableB extends NonSerializableA {
+ /** */
+ public Short shortVal = 0x1122;
+
+ /** */
+ public long longVal = 0x8877665544332211L;
+
+ /** */
+ private transient NonSerializableA[] aArr = {
+ new NonSerializableA(null, null),
+ new NonSerializableA(null, null),
+ new NonSerializableA(null, null)
+ };
+
+ /** */
+ protected Double doubleVal = 123.456;
+
+ /**
+ * Just to eliminate the default constructor.
+ */
+ private NonSerializableB() {
+ super(null, null);
+ }
+
+ /**
+ * Checks correctness of the state after unmarshalling.
+ */
+ @Override void checkAfterUnmarshalled() {
+ super.checkAfterUnmarshalled();
+
+ assertEquals(shortVal.shortValue(), 0x1122);
+
+ assertEquals(longVal, 0x8877665544332211L);
+
+ assertNull(aArr);
+
+ assertEquals(doubleVal, 123.456);
+ }
+ }
+
+ /**
+ * Some non-serializable class.
+ */
+ @SuppressWarnings( {"TransientFieldInNonSerializableClass","PublicField"})
+ private static class NonSerializable extends NonSerializableB {
+ /** */
+ private int idVal = -17;
+
+ /** */
+ private final NonSerializableA aVal = new NonSerializableB();
+
+ /** */
+ private transient NonSerializableB bVal = new NonSerializableB();
+
+ /** */
+ private NonSerializableA[] bArr = new NonSerializableA[] {
+ new NonSerializableB(),
+ new NonSerializableA(null, null)
+ };
+
+ /** */
+ public float floatVal = 567.89F;
+
+ /**
+ * Just to eliminate the default constructor.
+ *
+ * @param aVal Unused.
+ */
+ @SuppressWarnings( {"UnusedDeclaration"})
+ private NonSerializable(NonSerializableA aVal) {
+ }
+
+ /**
+ * Checks correctness of the state after unmarshalling.
+ */
+ @Override void checkAfterUnmarshalled() {
+ super.checkAfterUnmarshalled();
+
+ assertEquals(idVal, -17);
+
+ aVal.checkAfterUnmarshalled();
+
+ assertNull(bVal);
+
+ for (NonSerializableA a : bArr) {
+ a.checkAfterUnmarshalled();
+ }
+
+ assertEquals(floatVal, 567.89F);
+ }
+ }
+
+ /**
+ * Some serializable class.
+ */
+ @SuppressWarnings( {"PublicField","TransientFieldInNonSerializableClass","PackageVisibleInnerClass"})
+ static class ForSerializableB {
+ /** */
+ public Short shortVal = 0x1122;
+
+ /** */
+ public long longVal = 0x8877665544332211L;
+
+ /** */
+ private transient NonSerializableA[] aArr;
+
+ /** */
+ private transient String strVal = "abc";
+
+ /** */
+ protected Double doubleVal = 123.456;
+
+ /**
+ */
+ protected void init() {
+ shortVal = 0x1122;
+
+ longVal = 0x8877665544332211L;
+
+ aArr = new NonSerializableA[] {
+ new NonSerializableA(null, null),
+ new NonSerializableA(null, null),
+ new NonSerializableA(null, null)
+ };
+ }
+
+ /**
+ * Checks correctness of the state after unmarshalling.
+ */
+ void checkAfterUnmarshalled() {
+ assertEquals(shortVal.shortValue(), 0x1122);
+
+ assertEquals(longVal, 0x8877665544332211L);
+
+ assertNull(aArr);
+
+ assertNull(strVal);
+
+ assertEquals(doubleVal, 123.456);
+ }
+ }
+
+ /**
+ * Some serializable class.
+ */
+ private static class SomeSimpleSerializable extends ComputeJobAdapter {
+ /** */
+ private boolean flag = true;
+
+ /**
+ * @param newFlagVal - The new value of flag field.
+ */
+ public void setFlagValue(boolean newFlagVal) {
+ flag = newFlagVal;
+ }
+
+ /** {@inheritDoc} */
+ @Override public Object execute() throws GridException {
+ assert false;
+
+ return null;
+ }
+ }
+ /**
+ * Some serializable class.
+ */
+ private static class SomeSerializable extends ForSerializableB implements Serializable {
+ /**
+ * Just to eliminate the default constructor.
+ *
+ * @param id Unused.
+ */
+ @SuppressWarnings( {"UnusedDeclaration"})
+ private SomeSerializable(Long id) {
+ init();
+ }
+ }
+
+ /**
+ */
+ private static interface SomeItf {
+ /**
+ * @return Check result.
+ */
+ int checkAfterUnmarshalled();
+ }
+
+ /**
+ * Some externalizable class.
+ */
+ @SuppressWarnings( {"UnusedDeclaration", "PublicField"})
+ private static class ExternalizableA implements Externalizable {
+ /** */
+ private boolean boolVal;
+
+ /** */
+ public String[] strArr;
+
+ /** No-arg constructor is required by externalization. */
+ public ExternalizableA() {
+ // No-op.
+ }
+
+ /**
+ *
+ * @param strArr String array.
+ * @param boolVal Boolean value.
+ */
+ private ExternalizableA(String[] strArr, boolean boolVal) {
+ this.strArr = strArr;
+ this.boolVal = boolVal;
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeExternal(ObjectOutput out) throws IOException {
+ out.writeBoolean(false);
+ out.writeBoolean(false);
+ out.writeBoolean(false);
+ out.writeBoolean(false);
+ out.writeBoolean(false);
+ out.writeBoolean(false);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ boolVal = in.readBoolean();
+ in.readBoolean();
+ in.readBoolean();
+ in.readBoolean();
+ in.readBoolean();
+ in.readBoolean();
+ }
+ }
+
+ /**
+ *
+ */
+ private static class TestCacheKey implements Serializable {
+ /** */
+ private String key;
+
+ /** */
+ @SuppressWarnings({"UnusedDeclaration"})
+ private String terminalId;
+
+ /**
+ * @param key Key.
+ * @param terminalId Some ID.
+ */
+ TestCacheKey(String key, String terminalId) {
+ this.key = key;
+ this.terminalId = terminalId;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return key.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object obj) {
+ return obj instanceof TestCacheKey && key.equals(((TestCacheKey)obj).key);
+ }
+ }
+
+ /**
+ *
+ */
+ private static class TestCacheKeySerializable implements Serializable {
+ /** */
+ private String key;
+
+ /** */
+ @SuppressWarnings({"UnusedDeclaration"})
+ private String terminalId;
+
+ /**
+ * @param key Key.
+ * @param terminalId Some ID.
+ */
+ TestCacheKeySerializable(String key, String terminalId) {
+ this.key = key;
+ this.terminalId = terminalId;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return key.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object obj) {
+ return obj instanceof TestCacheKeySerializable && key.equals(((TestCacheKeySerializable)obj).key);
+ }
+ }
+
+ /**
+ *
+ */
+ private static class TestCacheKeyExternalizable implements Externalizable {
+ /** */
+ private String key;
+
+ /** */
+ private String terminalId;
+
+ /**
+ *
+ */
+ public TestCacheKeyExternalizable() {
+ // No-op.
+ }
+
+ /**
+ * @param key Key.
+ * @param terminalId Some ID.
+ */
+ TestCacheKeyExternalizable(String key, String terminalId) {
+ this.key = key;
+ this.terminalId = terminalId;
+ }
+
+ /** {@inheritDoc} */
+ @Override public int hashCode() {
+ return key.hashCode();
+ }
+
+ /** {@inheritDoc} */
+ @Override public boolean equals(Object obj) {
+ return obj instanceof TestCacheKeyExternalizable && key.equals(((TestCacheKeyExternalizable)obj).key);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void writeExternal(ObjectOutput out) throws IOException {
+ U.writeString(out, key);
+ U.writeString(out, terminalId);
+ }
+
+ /** {@inheritDoc} */
+ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
+ key = U.readString(in);
+ terminalId = U.readString(in);
+ }
+ }
+}