Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

bug in UserAgent.equals() #136

Open
drobert opened this issue Feb 9, 2017 · 0 comments
Open

bug in UserAgent.equals() #136

drobert opened this issue Feb 9, 2017 · 0 comments

Comments

@drobert
Copy link

drobert commented Feb 9, 2017

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) {
    return false;
}

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)) {
    return false;
}

A test to reproduce this issue:

public class ExampleTest {

    @Test
    public equalsFailsAfterSerDeser() throws Exception {
        UserAgent before = UserAgent.EMPTY;
        UserAgent after = SerializationUtils.deserialize(SerializationUtils.serialize(item));
 
        assertEquals(before, after);
    }

    /**
     * Lifted directly from SpringFramework 4.1.6.Release
     * @see org.springframework.util.SerializationUtils
     */
    static final class SerializationUtils {
        private SerializationUtils() { }

        /**
         * 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
         */
        public static byte[] serialize(Object object) {
            if (object == null) {
                return null;
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
            try {
                ObjectOutputStream oos = new ObjectOutputStream(baos);
                oos.writeObject(object);
                oos.flush();
            }
            catch (IOException ex) {
                throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
            }
            return baos.toByteArray();
        }

        /**
         * Deserialize the byte array into an object.
         * @param bytes a serialized object
         * @return the result of deserializing the bytes
         */
        public static Object deserialize(byte[] bytes) {
            if (bytes == null) {
                return null;
            }
            try {
                ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));
                return ois.readObject();
            }
            catch (IOException ex) {
                throw new IllegalArgumentException("Failed to deserialize object", ex);
            }
            catch (ClassNotFoundException ex) {
                throw new IllegalStateException("Failed to deserialize object type", ex);
            }
        }
    }
}
@drobert drobert changed the title bug in .equals() bug in UserAgent.equals() Feb 9, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant