You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.
As part of a serialization test I was running, I noticed that when UserAgent.EMPTY is serialized then deserialized, after.equals(before) returns false.
It appears it's due to this line:
if (deviceCategory != other.deviceCategory) {
returnfalse;
}
Even though DeviceCategory is final, it does not guarantee the categories are the same object instance. I believe this should fix it, though I'll submit a PR once I have a test written:
if (!deviceCategory.equals(other.deviceCategory)) {
returnfalse;
}
A test to reproduce this issue:
publicclassExampleTest {
@TestpublicequalsFailsAfterSerDeser() throwsException {
UserAgentbefore = UserAgent.EMPTY;
UserAgentafter = SerializationUtils.deserialize(SerializationUtils.serialize(item));
assertEquals(before, after);
}
/** * Lifted directly from SpringFramework 4.1.6.Release * @see org.springframework.util.SerializationUtils */staticfinalclassSerializationUtils {
privateSerializationUtils() { }
/** * Serialize the given object to a byte array. * @param object the object to serialize * @return an array of bytes representing the object in a portable fashion */publicstaticbyte[] serialize(Objectobject) {
if (object == null) {
returnnull;
}
ByteArrayOutputStreambaos = newByteArrayOutputStream(1024);
try {
ObjectOutputStreamoos = newObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
}
catch (IOExceptionex) {
thrownewIllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
}
returnbaos.toByteArray();
}
/** * Deserialize the byte array into an object. * @param bytes a serialized object * @return the result of deserializing the bytes */publicstaticObjectdeserialize(byte[] bytes) {
if (bytes == null) {
returnnull;
}
try {
ObjectInputStreamois = newObjectInputStream(newByteArrayInputStream(bytes));
returnois.readObject();
}
catch (IOExceptionex) {
thrownewIllegalArgumentException("Failed to deserialize object", ex);
}
catch (ClassNotFoundExceptionex) {
thrownewIllegalStateException("Failed to deserialize object type", ex);
}
}
}
}
The text was updated successfully, but these errors were encountered:
drobert
changed the title
bug in .equals()
bug in UserAgent.equals()
Feb 9, 2017
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
As part of a serialization test I was running, I noticed that when UserAgent.EMPTY is serialized then deserialized,
after.equals(before)
returns false.It appears it's due to this line:
Even though DeviceCategory is final, it does not guarantee the categories are the same object instance. I believe this should fix it, though I'll submit a PR once I have a test written:
A test to reproduce this issue:
The text was updated successfully, but these errors were encountered: