You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by am...@apache.org on 2019/09/30 12:13:02 UTC

[ignite] branch master updated: IGNITE-12226: Improve BinaryObjectException message. This closes #6903.

This is an automated email from the ASF dual-hosted git repository.

amashenkov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new e77ca58  IGNITE-12226: Improve BinaryObjectException message. This closes #6903.
e77ca58 is described below

commit e77ca584550d794f6ed9293b3a9c7ddaa48c5f16
Author: Kirill Tkalenko <tk...@yandex.ru>
AuthorDate: Mon Sep 30 15:12:50 2019 +0300

    IGNITE-12226: Improve BinaryObjectException message. This closes #6903.
---
 .../ignite/internal/binary/BinaryFieldImpl.java    |  12 ++-
 .../binary/BinaryFieldExtractionSelfTest.java      | 100 +++++++++++++++++++++
 2 files changed, 110 insertions(+), 2 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryFieldImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryFieldImpl.java
index 7713f58..f047704 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryFieldImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryFieldImpl.java
@@ -26,6 +26,7 @@ import java.sql.Timestamp;
 import java.util.Date;
 import java.util.UUID;
 import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryType;
 import org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream;
 import org.apache.ignite.internal.util.tostring.GridToStringExclude;
 import org.apache.ignite.internal.util.typedef.internal.S;
@@ -33,6 +34,7 @@ import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.binary.BinaryField;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Objects.nonNull;
 
 /**
  * Implementation of binary field descriptor.
@@ -281,9 +283,15 @@ public class BinaryFieldImpl implements BinaryFieldEx {
      */
     public int fieldOrder(BinaryObjectExImpl obj) {
         if (typeId != obj.typeId()) {
+            BinaryType expType = ctx.metadata(typeId);
+            BinaryType actualType = obj.type();
+
             throw new BinaryObjectException("Failed to get field because type ID of passed object differs" +
-                " from type ID this " + BinaryField.class.getSimpleName() + " belongs to [expected=" + typeId +
-                ", actual=" + obj.typeId() + ']');
+                " from type ID this " + BinaryField.class.getSimpleName() + " belongs to [expected=[typeId=" + typeId +
+                ", typeName=" + (nonNull(expType) ? expType.typeName() : null) + "], actual=[typeId=" +
+                actualType.typeId() + ", typeName=" + actualType.typeName() + "], fieldId=" + fieldId +
+                ", fieldName=" + fieldName + ", fieldType=" +
+                (nonNull(expType) ? expType.fieldTypeName(fieldName) : null) + ']');
         }
 
         int schemaId = obj.schemaId();
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryFieldExtractionSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryFieldExtractionSelfTest.java
index 40b05d6..9846fa6 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryFieldExtractionSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/binary/BinaryFieldExtractionSelfTest.java
@@ -17,10 +17,12 @@
 
 package org.apache.ignite.internal.binary;
 
+import java.lang.reflect.Field;
 import java.math.BigDecimal;
 import java.nio.ByteBuffer;
 import java.sql.Time;
 import java.util.concurrent.ThreadLocalRandom;
+import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.configuration.BinaryConfiguration;
 import org.apache.ignite.configuration.IgniteConfiguration;
 import org.apache.ignite.internal.util.IgniteUtils;
@@ -29,6 +31,8 @@ import org.apache.ignite.marshaller.MarshallerContextTestImpl;
 import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
 import org.junit.Test;
 
+import static org.apache.ignite.testframework.GridTestUtils.assertThrows;
+
 /**
  *
  */
@@ -123,6 +127,74 @@ public class BinaryFieldExtractionSelfTest extends GridCommonAbstractTest {
     }
 
     /**
+     * Checking the exception and its text when changing the typeId of a
+     * BinaryField.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testChangeTypeIdOfBinaryField() throws Exception {
+        BinaryMarshaller marsh = createMarshaller();
+
+        TimeValue timeVal = new TimeValue(11111L);
+        DecimalValue decimalVal = new DecimalValue(BigDecimal.ZERO);
+
+        BinaryObjectImpl timeValBinObj = toBinary(timeVal, marsh);
+        BinaryObjectImpl decimalValBinObj = toBinary(decimalVal, marsh);
+
+        BinaryFieldEx timeBinField = (BinaryFieldEx)timeValBinObj.type().field("time");
+
+        Field typeIdField = U.findField(timeBinField.getClass(), "typeId");
+        typeIdField.set(timeBinField, decimalValBinObj.typeId());
+
+        String expMsg = exceptionMessageOfDifferentTypeIdBinaryField(
+            decimalValBinObj.typeId(),
+            decimalVal.getClass().getName(),
+            timeValBinObj.typeId(),
+            timeVal.getClass().getName(),
+            U.field(timeBinField, "fieldId"),
+            timeBinField.name(),
+            null
+        );
+
+        assertThrows(log, () -> timeBinField.value(timeValBinObj), BinaryObjectException.class, expMsg);
+    }
+
+    /**
+     * Checking the exception and its text when changing the typeId of a
+     * BinaryField in case of not finding the expected BinaryType.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testChangeTypeIdOfBinaryFieldCaseNotFoundExpectedTypeId() throws Exception {
+        BinaryMarshaller marsh = createMarshaller();
+
+        TimeValue timeVal = new TimeValue(11111L);
+
+        BinaryObjectImpl timeValBinObj = toBinary(timeVal, marsh);
+
+        BinaryFieldEx timeBinField = (BinaryFieldEx)timeValBinObj.type().field("time");
+
+        int newTypeId = timeValBinObj.typeId() + 1;
+
+        Field typeIdField = U.findField(timeBinField.getClass(), "typeId");
+        typeIdField.set(timeBinField, newTypeId);
+
+        String expMsg = exceptionMessageOfDifferentTypeIdBinaryField(
+            newTypeId,
+            null,
+            timeValBinObj.typeId(),
+            timeVal.getClass().getName(),
+            U.field(timeBinField, "fieldId"),
+            timeBinField.name(),
+            null
+        );
+
+        assertThrows(log, () -> timeBinField.value(timeValBinObj), BinaryObjectException.class, expMsg);
+    }
+
+    /**
      * @throws Exception If failed.
      */
     @Test
@@ -247,4 +319,32 @@ public class BinaryFieldExtractionSelfTest extends GridCommonAbstractTest {
             this.decVal = decVal;
         }
     }
+
+    /**
+     * Creates an exception text for the case when the typeId differs in the
+     * BinaryField and the BinaryObject.
+     *
+     * @param expTypeId Expected typeId.
+     * @param expTypeName Expected typeName.
+     * @param actualTypeId Actual typeId.
+     * @param actualTypeName Actual typeName.
+     * @param fieldId FieldId.
+     * @param fieldName FieldName.
+     * @param fieldType FieldType.
+     * @return Exception message.
+     */
+    private String exceptionMessageOfDifferentTypeIdBinaryField(
+        int expTypeId,
+        String expTypeName,
+        int actualTypeId,
+        String actualTypeName,
+        int fieldId,
+        String fieldName,
+        String fieldType
+    ) {
+        return "Failed to get field because type ID of passed object differs from type ID this " +
+            "BinaryField belongs to [expected=[typeId=" + expTypeId + ", typeName=" + expTypeName +
+            "], actual=[typeId=" + actualTypeId + ", typeName=" + actualTypeName + "], fieldId=" + fieldId +
+            ", fieldName=" + fieldName + ", fieldType=" + fieldType + "]";
+    }
 }