Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-12226 Improve BinaryObjectException message. #6903

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,13 +26,15 @@
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;
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.
Expand Down Expand Up @@ -281,9 +283,15 @@ public int fieldId() {
*/
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();
Expand Down
Expand Up @@ -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;
Expand All @@ -29,6 +31,8 @@
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
import org.junit.Test;

import static org.apache.ignite.testframework.GridTestUtils.assertThrows;

/**
*
*/
Expand Down Expand Up @@ -122,6 +126,74 @@ public void testTimeMarshalling() throws Exception {
assertEquals(field.value(binObj), field.<Time>readField(buf));
}

/**
* 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.
*/
Expand Down Expand Up @@ -247,4 +319,32 @@ private DecimalValue(BigDecimal decVal) {
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 + "]";
}
}