Debian Codegraph @

proton-j/src/test/java/org/apache/qpid/proton/amqp/BinaryTest.java

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21 package org.apache.qpid.proton.amqp;
22
23 import static org.junit.Assert.*;
24
25 import java.util.Arrays;
26
27 import org.junit.Test;
28
29 public class BinaryTest
30 {
31
32 @Test
33 public void testNotEqualsWithDifferentTypeObject()
34 {
35 Binary bin = createSteppedValueBinary(10);
36
37 assertFalse("Objects should not be equal with different type", bin.equals("not-a-Binary"));
38 }
39
40 @Test
41 public void testEqualsWithItself()
42 {
43 Binary bin = createSteppedValueBinary(10);
44
45 assertTrue("Object should be equal to itself", bin.equals(bin));
46 }
47
48 @Test
49 public void testEqualsWithDifferentBinaryOfSameLengthAndContent()
50 {
51 int length = 10;
52 Binary bin1 = createSteppedValueBinary(length);
53 Binary bin2 = createSteppedValueBinary(length);
54
55 assertTrue("Objects should be equal", bin1.equals(bin2));
56 assertTrue("Objects should be equal", bin2.equals(bin1));
57 }
58
59 @Test
60 public void testEqualsWithDifferentLengthBinaryOfDifferentBytes()
61 {
62 int length1 = 10;
63 Binary bin1 = createSteppedValueBinary(length1);
64 Binary bin2 = createSteppedValueBinary(length1 + 1);
65
66 assertFalse("Objects should not be equal", bin1.equals(bin2));
67 assertFalse("Objects should not be equal", bin2.equals(bin1));
68 }
69
70 @Test
71 public void testEqualsWithDifferentLengthBinaryOfSameByte()
72 {
73 Binary bin1 = createNewRepeatedValueBinary(10, (byte) 1);
74 Binary bin2 = createNewRepeatedValueBinary(123, (byte) 1);
75
76 assertFalse("Objects should not be equal", bin1.equals(bin2));
77 assertFalse("Objects should not be equal", bin2.equals(bin1));
78 }
79
80 @Test
81 public void testEqualsWithDifferentContentBinary()
82 {
83 int length = 10;
84 Binary bin1 = createNewRepeatedValueBinary(length, (byte) 1);
85
86 Binary bin2 = createNewRepeatedValueBinary(length, (byte) 1);
87 bin2.getArray()[5] = (byte) 0;
88
89 assertFalse("Objects should not be equal", bin1.equals(bin2));
90 assertFalse("Objects should not be equal", bin2.equals(bin1));
91 }
92
93 private Binary createSteppedValueBinary(int length) {
94 byte[] bytes = new byte[length];
95 for (int i = 0; i < length; i++) {
96 bytes[i] = (byte) (length - i);
97 }
98
99 return new Binary(bytes);
100 }
101
102 private Binary createNewRepeatedValueBinary(int length, byte repeatedByte){
103 byte[] bytes = new byte[length];
104 Arrays.fill(bytes, repeatedByte);
105
106 return new Binary(bytes);
107 }
108 }