Author: gsim Date: Fri Oct 10 12:57:17 2014 New Revision: 1630855 URL: http://svn.apache.org/r1630855 Log: PROTON-698: return false if given object class differs, and shortcut equality of the object with itself
Added: qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java Modified: qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java Modified: qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java?rev=1630855&r1=1630854&r2=1630855&view=diff ============================================================================== --- qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java (original) +++ qpid/proton/branches/examples/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java Fri Oct 10 12:57:17 2014 @@ -50,6 +50,7 @@ public final class Binary return ByteBuffer.wrap(_data, _offset, _length); } + @Override public final int hashCode() { int hc = _hashCode; @@ -64,13 +65,20 @@ public final class Binary return hc; } + @Override public final boolean equals(Object o) { - Binary buf = (Binary) o; - if(o == null) + if (this == o) + { + return true; + } + + if (o == null || getClass() != o.getClass()) { return false; } + + Binary buf = (Binary) o; final int size = _length; if (size != buf._length) { Added: qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java URL: http://svn.apache.org/viewvc/qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java?rev=1630855&view=auto ============================================================================== --- qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java (added) +++ qpid/proton/branches/examples/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java Fri Oct 10 12:57:17 2014 @@ -0,0 +1,108 @@ +/* + * + * 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.qpid.proton.amqp; + +import static org.junit.Assert.*; + +import java.util.Arrays; + +import org.junit.Test; + +public class BinaryTest +{ + + @Test + public void testNotEqualsWithDifferentTypeObject() + { + Binary bin = createSteppedValueBinary(10); + + assertFalse("Objects should not be equal with different type", bin.equals("not-a-Binary")); + } + + @Test + public void testEqualsWithItself() + { + Binary bin = createSteppedValueBinary(10); + + assertTrue("Object should be equal to itself", bin.equals(bin)); + } + + @Test + public void testEqualsWithDifferentBinaryOfSameLengthAndContent() + { + int length = 10; + Binary bin1 = createSteppedValueBinary(length); + Binary bin2 = createSteppedValueBinary(length); + + assertTrue("Objects should be equal", bin1.equals(bin2)); + assertTrue("Objects should be equal", bin2.equals(bin1)); + } + + @Test + public void testEqualsWithDifferentLengthBinaryOfDifferentBytes() + { + int length1 = 10; + Binary bin1 = createSteppedValueBinary(length1); + Binary bin2 = createSteppedValueBinary(length1 + 1); + + assertFalse("Objects should not be equal", bin1.equals(bin2)); + assertFalse("Objects should not be equal", bin2.equals(bin1)); + } + + @Test + public void testEqualsWithDifferentLengthBinaryOfSameByte() + { + Binary bin1 = createNewRepeatedValueBinary(10, (byte) 1); + Binary bin2 = createNewRepeatedValueBinary(123, (byte) 1); + + assertFalse("Objects should not be equal", bin1.equals(bin2)); + assertFalse("Objects should not be equal", bin2.equals(bin1)); + } + + @Test + public void testEqualsWithDifferentContentBinary() + { + int length = 10; + Binary bin1 = createNewRepeatedValueBinary(length, (byte) 1); + + Binary bin2 = createNewRepeatedValueBinary(length, (byte) 1); + bin2.getArray()[5] = (byte) 0; + + assertFalse("Objects should not be equal", bin1.equals(bin2)); + assertFalse("Objects should not be equal", bin2.equals(bin1)); + } + + private Binary createSteppedValueBinary(int length) { + byte[] bytes = new byte[length]; + for (int i = 0; i < length; i++) { + bytes[i] = (byte) (length - i); + } + + return new Binary(bytes); + } + + private Binary createNewRepeatedValueBinary(int length, byte repeatedByte){ + byte[] bytes = new byte[length]; + Arrays.fill(bytes, repeatedByte); + + return new Binary(bytes); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org