You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ro...@apache.org on 2014/09/25 18:38:08 UTC
svn commit: r1627570 - in /qpid/proton/trunk/proton-j/src:
main/java/org/apache/qpid/proton/amqp/Binary.java
test/java/org/apache/qpid/proton/amqp/BinaryTest.java
Author: robbie
Date: Thu Sep 25 16:38:08 2014
New Revision: 1627570
URL: http://svn.apache.org/r1627570
Log:
PROTON-698: return false if given object class differs, and shortcut equality of the object with itself
Added:
qpid/proton/trunk/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java
Modified:
qpid/proton/trunk/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java
Modified: qpid/proton/trunk/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java?rev=1627570&r1=1627569&r2=1627570&view=diff
==============================================================================
--- qpid/proton/trunk/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java (original)
+++ qpid/proton/trunk/proton-j/src/main/java/org/apache/qpid/proton/amqp/Binary.java Thu Sep 25 16:38:08 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/trunk/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java
URL: http://svn.apache.org/viewvc/qpid/proton/trunk/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java?rev=1627570&view=auto
==============================================================================
--- qpid/proton/trunk/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java (added)
+++ qpid/proton/trunk/proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java Thu Sep 25 16:38:08 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-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org