Repository: ignite Updated Branches: refs/heads/master 752ea17ab -> 3ab8bbadc
IGNITE-9043 Collections cannot be assigned to fields of type Object in BinaryObject - Fixes #4447. Signed-off-by: Dmitriy Pavlov <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/3ab8bbad Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/3ab8bbad Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/3ab8bbad Branch: refs/heads/master Commit: 3ab8bbadc80446e53261da784bfbed48b6ab5322 Parents: 752ea17 Author: Denis Mekhanikov <[email protected]> Authored: Fri Aug 3 18:26:01 2018 +0300 Committer: Dmitriy Pavlov <[email protected]> Committed: Fri Aug 3 18:26:01 2018 +0300 ---------------------------------------------------------------------- .../binary/builder/BinaryObjectBuilderImpl.java | 3 +- .../BinaryObjectTypeCompatibilityTest.java | 180 +++++++++++++++++++ .../IgniteBinaryObjectsTestSuite.java | 2 + 3 files changed, 184 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/3ab8bbad/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java index 3fc5dc4..c577e02 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/builder/BinaryObjectBuilderImpl.java @@ -422,7 +422,8 @@ public class BinaryObjectBuilderImpl implements BinaryObjectBuilder { else if (!nullFieldVal) { String newFldTypeName = BinaryUtils.fieldTypeName(newFldTypeId); - if (!F.eq(newFldTypeName, oldFldTypeName)) { + if (!F.eq(newFldTypeName, oldFldTypeName) && + !oldFldTypeName.equals(BinaryUtils.fieldTypeName(GridBinaryMarshaller.OBJ))) { throw new BinaryObjectException( "Wrong value has been set [" + "typeName=" + (typeName == null ? meta.typeName() : typeName) + http://git-wip-us.apache.org/repos/asf/ignite/blob/3ab8bbad/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectTypeCompatibilityTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectTypeCompatibilityTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectTypeCompatibilityTest.java new file mode 100644 index 0000000..3ef4a83 --- /dev/null +++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryObjectTypeCompatibilityTest.java @@ -0,0 +1,180 @@ +/* + * 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.internal.binary; + +import java.math.BigDecimal; +import java.sql.Time; +import java.sql.Timestamp; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.LinkedList; +import java.util.Map; +import java.util.Random; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.binary.BinaryObject; +import org.apache.ignite.binary.BinaryObjectBuilder; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi; +import org.apache.ignite.spi.discovery.tcp.ipfinder.TcpDiscoveryIpFinder; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * + */ +public class BinaryObjectTypeCompatibilityTest extends GridCommonAbstractTest { + /** Ip finder. */ + private static final TcpDiscoveryIpFinder IP_FINDER = new TcpDiscoveryVmIpFinder(true); + + /** */ + private static final Random RANDOM = new Random(System.currentTimeMillis()); + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + IgniteConfiguration igniteCfg = super.getConfiguration(igniteInstanceName); + + ((TcpDiscoverySpi)igniteCfg.getDiscoverySpi()).setIpFinder(IP_FINDER); + + return igniteCfg; + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(); + } + + /** + * @throws Exception If failed. + */ + public void testCompatibilityWithObject() throws Exception { + Ignite ignite = startGrid(); + + BinaryObjectBuilder bldr = ignite.binary().builder("ObjectWrapper"); + bldr.setField("objField", new Object()); + bldr.build(); + + validateMap(bldr, "objField", new HashMap<>()); + validateMap(bldr, "objField", new LinkedHashMap<>()); + validateMap(bldr, "objField", new TreeMap<>()); + + validateCollection(bldr, "objField", new ArrayList<>()); + validateCollection(bldr, "objField", new LinkedList<>()); + validateCollection(bldr, "objField", new HashSet<>()); + validateCollection(bldr, "objField", new LinkedHashSet<>()); + validateCollection(bldr, "objField", new TreeSet<>()); + + validate(bldr, "objField", (byte)RANDOM.nextInt()); + validate(bldr, "objField", (short)RANDOM.nextInt()); + validate(bldr, "objField", (char)RANDOM.nextInt()); + validate(bldr, "objField", RANDOM.nextInt()); + validate(bldr, "objField", RANDOM.nextLong()); + validate(bldr, "objField", RANDOM.nextFloat()); + validate(bldr, "objField", RANDOM.nextDouble()); + + validate(bldr, "objField", Enum.DEFAULT); + validate(bldr, "objField", new BigDecimal(RANDOM.nextInt())); + validate(bldr, "objField", "Test string"); + validate(bldr, "objField", new Date()); + validate(bldr, "objField", new Timestamp(System.currentTimeMillis())); + validate(bldr, "objField", new Time(System.currentTimeMillis())); + validate(bldr, "objField", UUID.randomUUID()); + } + + /** + * @param bldr {@link BinaryObjectBuilder}, that will be used for testing. + * @param fldName Name of the field being tested. + * @param src {@link Collection} object, that should be tested. + */ + private void validateCollection(BinaryObjectBuilder bldr, String fldName, Collection<Integer> src) { + for (int i = 0; i < 1000; i++) + src.add(RANDOM.nextInt()); + + bldr.setField(fldName, src); + + BinaryObject binObj = bldr.build(); + + Collection<Integer> res = deserialize(binObj.field(fldName)); + + assertEqualsCollections(src, res); + } + + /** + * @param bldr {@link BinaryObjectBuilder}, that will be used for testing. + * @param fldName Name of the field being tested. + * @param src {@link Map} object, that should be tested. + */ + private void validateMap(BinaryObjectBuilder bldr, String fldName, Map<Integer, String> src) { + for (int i = 0; i < 1000; i++) { + int key = RANDOM.nextInt(); + src.put(key, Integer.toString(key)); + } + + bldr.setField(fldName, src); + + BinaryObject binObj = bldr.build(); + + Map<Integer, String> res = deserialize(binObj.field(fldName)); + + assertEquals(src, res); + } + + /** + * @param bldr {@code BinaryObjectBuilder}, that will be used for testing. + * @param fldName Name of the field being tested. + * @param src Value being tested. + * @param <T> Type of the value. + */ + private <T> void validate(BinaryObjectBuilder bldr, String fldName, T src) { + bldr.setField(fldName, src); + + BinaryObject binObj = bldr.build(); + + T res = deserialize(binObj.field(fldName)); + + assertEquals(src, res); + } + + /** + * @param obj Object being deserialized. + * @param <T> Result type. + * @return Deserialized value, if {@link BinaryObject} was provided, or the same object otherwise. + */ + @SuppressWarnings("unchecked") + private <T> T deserialize(Object obj) { + if (obj instanceof BinaryObject) + return ((BinaryObject)obj).deserialize(); + else + return (T)obj; + } + + /** + * Enumeration for tests. + */ + private enum Enum { + /** */ + DEFAULT + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/3ab8bbad/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java index c0211be..d37e22f 100644 --- a/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java +++ b/modules/core/src/test/java/org/apache/ignite/testsuites/IgniteBinaryObjectsTestSuite.java @@ -34,6 +34,7 @@ import org.apache.ignite.internal.binary.BinaryObjectBuilderDefaultMappersSelfTe import org.apache.ignite.internal.binary.BinaryObjectBuilderSimpleNameLowerCaseMappersSelfTest; import org.apache.ignite.internal.binary.BinaryObjectExceptionSelfTest; import org.apache.ignite.internal.binary.BinaryObjectToStringSelfTest; +import org.apache.ignite.internal.binary.BinaryObjectTypeCompatibilityTest; import org.apache.ignite.internal.binary.BinarySerialiedFieldComparatorSelfTest; import org.apache.ignite.internal.binary.BinarySimpleNameTestPropertySelfTest; import org.apache.ignite.internal.binary.BinaryTreeSelfTest; @@ -110,6 +111,7 @@ public class IgniteBinaryObjectsTestSuite extends TestSuite { suite.addTestSuite(GridBinaryAffinityKeySelfTest.class); suite.addTestSuite(GridBinaryWildcardsSelfTest.class); suite.addTestSuite(BinaryObjectToStringSelfTest.class); + suite.addTestSuite(BinaryObjectTypeCompatibilityTest.class); // Tests for objects with non-compact footers. suite.addTestSuite(BinaryMarshallerNonCompactSelfTest.class);
