Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 25D47200B8F for ; Thu, 15 Sep 2016 08:40:03 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 24B5F160ADE; Thu, 15 Sep 2016 06:40:03 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id D4795160AEE for ; Thu, 15 Sep 2016 08:40:01 +0200 (CEST) Received: (qmail 92110 invoked by uid 500); 15 Sep 2016 06:40:01 -0000 Mailing-List: contact commits-help@ignite.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.apache.org Delivered-To: mailing list commits@ignite.apache.org Received: (qmail 91938 invoked by uid 99); 15 Sep 2016 06:40:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Sep 2016 06:40:00 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id DD3C6EEE29; Thu, 15 Sep 2016 06:40:00 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.apache.org Date: Thu, 15 Sep 2016 06:40:30 -0000 Message-Id: <81f67c94b1a947ef9f034dec70a16747@git.apache.org> In-Reply-To: <6ba44e8986c3455ea6af97ecdcf088a3@git.apache.org> References: <6ba44e8986c3455ea6af97ecdcf088a3@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [32/40] ignite git commit: IGNITE-3172 Review. archived-at: Thu, 15 Sep 2016 06:40:03 -0000 IGNITE-3172 Review. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/563c2765 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/563c2765 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/563c2765 Branch: refs/heads/ignite-comm-opts1 Commit: 563c2765d51c1afafe5fd79b28804b9760d5389f Parents: 231ead0 Author: Alexey Kuznetsov Authored: Wed Sep 14 08:38:38 2016 +0700 Committer: Alexey Kuznetsov Committed: Wed Sep 14 08:38:38 2016 +0700 ---------------------------------------------------------------------- .../cassandra/serializer/KryoSerializer.java | 8 +- .../apache/ignite/tests/KryoSerializerTest.java | 51 ++++-- .../java/org/apache/ignite/tests/MyPojo.java | 54 +++++++ .../tests/DatasourceSerializationTest.java | 158 ------------------- .../tests/DatasourceSerializationTest.java | 158 +++++++++++++++++++ 5 files changed, 257 insertions(+), 172 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/563c2765/modules/cassandra/serializers/src/main/java/org/apache/ignite/cache/store/cassandra/serializer/KryoSerializer.java ---------------------------------------------------------------------- diff --git a/modules/cassandra/serializers/src/main/java/org/apache/ignite/cache/store/cassandra/serializer/KryoSerializer.java b/modules/cassandra/serializers/src/main/java/org/apache/ignite/cache/store/cassandra/serializer/KryoSerializer.java index 50cb9a2..775e501 100644 --- a/modules/cassandra/serializers/src/main/java/org/apache/ignite/cache/store/cassandra/serializer/KryoSerializer.java +++ b/modules/cassandra/serializers/src/main/java/org/apache/ignite/cache/store/cassandra/serializer/KryoSerializer.java @@ -20,12 +20,13 @@ package org.apache.ignite.cache.store.cassandra.serializer; import com.esotericsoftware.kryo.Kryo; import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Output; -import org.apache.ignite.internal.util.typedef.internal.U; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; +import org.apache.ignite.internal.util.typedef.internal.U; + /** * Serializer based on Kryo serialization. */ @@ -36,9 +37,10 @@ public class KryoSerializer implements Serializer { /** */ private static final int DFLT_BUFFER_SIZE = 4096; - /** Thread local instance of {@link com.esotericsoftware.kryo.Kryo} */ + /** Thread local instance of {@link Kryo} */ private transient ThreadLocal kryos = new ThreadLocal() { - protected Kryo initialValue() { + /** {@inheritDoc} */ + @Override protected Kryo initialValue() { return new Kryo(); } }; http://git-wip-us.apache.org/repos/asf/ignite/blob/563c2765/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/KryoSerializerTest.java ---------------------------------------------------------------------- diff --git a/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/KryoSerializerTest.java b/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/KryoSerializerTest.java index 66f6c6a..3053c63 100644 --- a/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/KryoSerializerTest.java +++ b/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/KryoSerializerTest.java @@ -1,13 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.ignite.tests; +import java.nio.ByteBuffer; +import java.util.Date; + import org.apache.ignite.cache.store.cassandra.serializer.KryoSerializer; + import org.junit.Test; -import java.nio.ByteBuffer; -import java.util.Date; +import static org.junit.Assert.assertEquals; +/** + * Simple test for KryoSerializer. + */ public class KryoSerializerTest { - @Test public void simpleTest() { + /** + * Serialize simple object test. + */ + @Test + public void simpleTest() { MyPojo pojo1 = new MyPojo("123", 1, 123423453467L, new Date(), null); KryoSerializer ser = new KryoSerializer(); @@ -15,11 +42,14 @@ public class KryoSerializerTest { ByteBuffer buff = ser.serialize(pojo1); MyPojo pojo2 = (MyPojo)ser.deserialize(buff); - if (!pojo1.equals(pojo2)) - throw new RuntimeException("Kryo simple serialization test failed"); + assertEquals("Kryo simple serialization test failed", pojo1, pojo2); } - @Test public void cyclicStructureTest() { + /** + * Serialize object with cyclic references test. + */ + @Test + public void cyclicStructureTest() { MyPojo pojo1 = new MyPojo("123", 1, 123423453467L, new Date(), null); MyPojo pojo2 = new MyPojo("321", 2, 123456L, new Date(), pojo1); pojo1.setRef(pojo2); @@ -32,10 +62,9 @@ public class KryoSerializerTest { MyPojo pojo3 = (MyPojo)ser.deserialize(buff1); MyPojo pojo4 = (MyPojo)ser.deserialize(buff2); - if (!pojo1.equals(pojo3) || !pojo1.getRef().equals(pojo3.getRef())) - throw new RuntimeException("Kryo cyclic structure serialization test failed"); - - if (!pojo2.equals(pojo4) || !pojo2.getRef().equals(pojo4.getRef())) - throw new RuntimeException("Kryo cyclic structure serialization test failed"); + assertEquals("Kryo cyclic structure serialization test failed", pojo1, pojo3); + assertEquals("Kryo cyclic structure serialization test failed", pojo1.getRef(), pojo3.getRef()); + assertEquals("Kryo cyclic structure serialization test failed", pojo2, pojo4); + assertEquals("Kryo cyclic structure serialization test failed", pojo2.getRef(), pojo4.getRef()); } } http://git-wip-us.apache.org/repos/asf/ignite/blob/563c2765/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/MyPojo.java ---------------------------------------------------------------------- diff --git a/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/MyPojo.java b/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/MyPojo.java index 2161f25..f901db3 100644 --- a/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/MyPojo.java +++ b/modules/cassandra/serializers/src/test/java/org/apache/ignite/tests/MyPojo.java @@ -1,18 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.ignite.tests; import java.io.Serializable; import java.util.Date; +/** + * Sample POJO for tests. + */ public class MyPojo implements Serializable { + /** */ private String field1; + + /** */ private int field2; + + /** */ private long field3; + + /** */ private Date field4; + + /** */ private MyPojo ref; + /** + * Empty constructor. + */ public MyPojo() { + // No-op. } + /** + * Full constructor. + * + * @param field1 Some value. + * @param field2 Some value. + * @param field3 Some value. + * @param field4 Some value. + * @param ref Reference to other pojo. + */ public MyPojo(String field1, int field2, long field3, Date field4, MyPojo ref) { this.field1 = field1; this.field2 = field2; @@ -21,6 +63,12 @@ public class MyPojo implements Serializable { this.ref = ref; } + /** + * Compare POJOs. + * + * @param obj POJO to compare with. + * @return {@code true} if equals. + */ public boolean equals(Object obj) { if (!(obj instanceof MyPojo)) return false; @@ -38,10 +86,16 @@ public class MyPojo implements Serializable { return field2 == myObj.field2 && field3 == myObj.field3; } + /** + * @param ref New reference. + */ public void setRef(MyPojo ref) { this.ref = ref; } + /** + * @return Reference to some POJO. + */ public MyPojo getRef() { return ref; } http://git-wip-us.apache.org/repos/asf/ignite/blob/563c2765/modules/cassandra/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java ---------------------------------------------------------------------- diff --git a/modules/cassandra/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java b/modules/cassandra/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java deleted file mode 100644 index ceb90e0..0000000 --- a/modules/cassandra/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.ignite.tests; - -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.ConsistencyLevel; -import com.datastax.driver.core.Host; -import com.datastax.driver.core.HostDistance; -import com.datastax.driver.core.Statement; -import com.datastax.driver.core.policies.LoadBalancingPolicy; -import com.datastax.driver.core.policies.RoundRobinPolicy; -import com.datastax.driver.core.policies.TokenAwarePolicy; - -import java.io.Serializable; -import java.lang.reflect.Field; -import java.net.InetAddress; -import java.nio.ByteBuffer; -import java.util.Collection; -import java.util.Iterator; -import java.util.List; - -import org.apache.ignite.cache.store.cassandra.datasource.Credentials; -import org.apache.ignite.cache.store.cassandra.datasource.DataSource; -import org.apache.ignite.cache.store.cassandra.serializer.JavaSerializer; -import org.apache.ignite.tests.utils.CassandraAdminCredentials; - -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -/** - * Test for datasource serialization. - */ -public class DatasourceSerializationTest { - /** - * Sample class for serialization test. - */ - private static class MyLoadBalancingPolicy implements LoadBalancingPolicy, Serializable { - /** */ - private transient LoadBalancingPolicy plc = new TokenAwarePolicy(new RoundRobinPolicy()); - - /** {@inheritDoc} */ - @Override public void init(Cluster cluster, Collection hosts) { - plc.init(cluster, hosts); - } - - /** {@inheritDoc} */ - @Override public HostDistance distance(Host host) { - return plc.distance(host); - } - - /** {@inheritDoc} */ - @Override public Iterator newQueryPlan(String loggedKeyspace, Statement statement) { - return plc.newQueryPlan(loggedKeyspace, statement); - } - - /** {@inheritDoc} */ - @Override public void onAdd(Host host) { - plc.onAdd(host); - } - - /** {@inheritDoc} */ - @Override public void onUp(Host host) { - plc.onUp(host); - } - - /** {@inheritDoc} */ - @Override public void onDown(Host host) { - plc.onDown(host); - } - - /** {@inheritDoc} */ - @Override public void onRemove(Host host) { - plc.onRemove(host); - } - - /** {@inheritDoc} */ - @Override public void close() { - plc.close(); - } - } - - /** - * Serialization test. - */ - @Test - public void serializationTest() { - DataSource src = new DataSource(); - - Credentials cred = new CassandraAdminCredentials(); - String[] points = new String[]{"127.0.0.1", "10.0.0.2", "10.0.0.3"}; - LoadBalancingPolicy plc = new MyLoadBalancingPolicy(); - - src.setCredentials(cred); - src.setContactPoints(points); - src.setReadConsistency("ONE"); - src.setWriteConsistency("QUORUM"); - src.setLoadBalancingPolicy(plc); - - JavaSerializer serializer = new JavaSerializer(); - - ByteBuffer buff = serializer.serialize(src); - DataSource _src = (DataSource)serializer.deserialize(buff); - - Credentials _cred = (Credentials)getFieldValue(_src, "creds"); - List _points = (List)getFieldValue(_src, "contactPoints"); - ConsistencyLevel _readCons = (ConsistencyLevel)getFieldValue(_src, "readConsistency"); - ConsistencyLevel _writeCons = (ConsistencyLevel)getFieldValue(_src, "writeConsistency"); - LoadBalancingPolicy _plc = (LoadBalancingPolicy)getFieldValue(_src, "loadBalancingPlc"); - - assertTrue("Incorrectly serialized/deserialized credentials for Cassandra DataSource", - cred.getPassword().equals(_cred.getPassword()) && cred.getUser().equals(_cred.getUser())); - - assertTrue("Incorrectly serialized/deserialized contact points for Cassandra DataSource", - "/127.0.0.1".equals(_points.get(0).toString()) && - "/10.0.0.2".equals(_points.get(1).toString()) && - "/10.0.0.3".equals(_points.get(2).toString())); - - assertTrue("Incorrectly serialized/deserialized consistency levels for Cassandra DataSource", - ConsistencyLevel.ONE == _readCons && ConsistencyLevel.QUORUM == _writeCons); - - assertTrue("Incorrectly serialized/deserialized load balancing policy for Cassandra DataSource", - _plc instanceof MyLoadBalancingPolicy); - } - - /** - * @param obj Object. - * @param field Field name. - * @return Field value. - */ - private Object getFieldValue(Object obj, String field) { - try { - Field f = obj.getClass().getDeclaredField(field); - - f.setAccessible(true); - - return f.get(obj); - } - catch (Throwable e) { - throw new RuntimeException("Failed to get field '" + field + "' value", e); - } - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/563c2765/modules/cassandra/store/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java ---------------------------------------------------------------------- diff --git a/modules/cassandra/store/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java b/modules/cassandra/store/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java new file mode 100644 index 0000000..ceb90e0 --- /dev/null +++ b/modules/cassandra/store/src/test/java/org/apache/ignite/tests/DatasourceSerializationTest.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.tests; + +import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.Host; +import com.datastax.driver.core.HostDistance; +import com.datastax.driver.core.Statement; +import com.datastax.driver.core.policies.LoadBalancingPolicy; +import com.datastax.driver.core.policies.RoundRobinPolicy; +import com.datastax.driver.core.policies.TokenAwarePolicy; + +import java.io.Serializable; +import java.lang.reflect.Field; +import java.net.InetAddress; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import org.apache.ignite.cache.store.cassandra.datasource.Credentials; +import org.apache.ignite.cache.store.cassandra.datasource.DataSource; +import org.apache.ignite.cache.store.cassandra.serializer.JavaSerializer; +import org.apache.ignite.tests.utils.CassandraAdminCredentials; + +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * Test for datasource serialization. + */ +public class DatasourceSerializationTest { + /** + * Sample class for serialization test. + */ + private static class MyLoadBalancingPolicy implements LoadBalancingPolicy, Serializable { + /** */ + private transient LoadBalancingPolicy plc = new TokenAwarePolicy(new RoundRobinPolicy()); + + /** {@inheritDoc} */ + @Override public void init(Cluster cluster, Collection hosts) { + plc.init(cluster, hosts); + } + + /** {@inheritDoc} */ + @Override public HostDistance distance(Host host) { + return plc.distance(host); + } + + /** {@inheritDoc} */ + @Override public Iterator newQueryPlan(String loggedKeyspace, Statement statement) { + return plc.newQueryPlan(loggedKeyspace, statement); + } + + /** {@inheritDoc} */ + @Override public void onAdd(Host host) { + plc.onAdd(host); + } + + /** {@inheritDoc} */ + @Override public void onUp(Host host) { + plc.onUp(host); + } + + /** {@inheritDoc} */ + @Override public void onDown(Host host) { + plc.onDown(host); + } + + /** {@inheritDoc} */ + @Override public void onRemove(Host host) { + plc.onRemove(host); + } + + /** {@inheritDoc} */ + @Override public void close() { + plc.close(); + } + } + + /** + * Serialization test. + */ + @Test + public void serializationTest() { + DataSource src = new DataSource(); + + Credentials cred = new CassandraAdminCredentials(); + String[] points = new String[]{"127.0.0.1", "10.0.0.2", "10.0.0.3"}; + LoadBalancingPolicy plc = new MyLoadBalancingPolicy(); + + src.setCredentials(cred); + src.setContactPoints(points); + src.setReadConsistency("ONE"); + src.setWriteConsistency("QUORUM"); + src.setLoadBalancingPolicy(plc); + + JavaSerializer serializer = new JavaSerializer(); + + ByteBuffer buff = serializer.serialize(src); + DataSource _src = (DataSource)serializer.deserialize(buff); + + Credentials _cred = (Credentials)getFieldValue(_src, "creds"); + List _points = (List)getFieldValue(_src, "contactPoints"); + ConsistencyLevel _readCons = (ConsistencyLevel)getFieldValue(_src, "readConsistency"); + ConsistencyLevel _writeCons = (ConsistencyLevel)getFieldValue(_src, "writeConsistency"); + LoadBalancingPolicy _plc = (LoadBalancingPolicy)getFieldValue(_src, "loadBalancingPlc"); + + assertTrue("Incorrectly serialized/deserialized credentials for Cassandra DataSource", + cred.getPassword().equals(_cred.getPassword()) && cred.getUser().equals(_cred.getUser())); + + assertTrue("Incorrectly serialized/deserialized contact points for Cassandra DataSource", + "/127.0.0.1".equals(_points.get(0).toString()) && + "/10.0.0.2".equals(_points.get(1).toString()) && + "/10.0.0.3".equals(_points.get(2).toString())); + + assertTrue("Incorrectly serialized/deserialized consistency levels for Cassandra DataSource", + ConsistencyLevel.ONE == _readCons && ConsistencyLevel.QUORUM == _writeCons); + + assertTrue("Incorrectly serialized/deserialized load balancing policy for Cassandra DataSource", + _plc instanceof MyLoadBalancingPolicy); + } + + /** + * @param obj Object. + * @param field Field name. + * @return Field value. + */ + private Object getFieldValue(Object obj, String field) { + try { + Field f = obj.getClass().getDeclaredField(field); + + f.setAccessible(true); + + return f.get(obj); + } + catch (Throwable e) { + throw new RuntimeException("Failed to get field '" + field + "' value", e); + } + } +}