2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
18 public class AbstractImmutableNormalizedValueAttrNodeTest {
20 private static final QName ROOT_QNAME = QName.create("urn:test", "2014-03-13", "root");
21 private static final QName LEAF_QNAME = QName.create(ROOT_QNAME, "my-leaf");
22 private static final QName SAME_LEAF_QNAME = QName.create(ROOT_QNAME, "my-leaf");
23 private static final QName OTHER_LEAF_QNAME = QName.create(ROOT_QNAME, "my-other-leaf");
26 // This test is based on using different references; we're testing equals()
27 @SuppressWarnings({"RedundantStringConstructorCall", "EqualsWithItself"})
28 public void equalsByteTest() {
30 LeafNode<byte[]> leafNodeNull = ImmutableNodes.leafNode(LEAF_QNAME, null);
31 LeafNode<byte[]> equalLeafNodeNull = ImmutableNodes.leafNode(SAME_LEAF_QNAME, null);
33 assertTrue(leafNodeNull.equals(leafNodeNull));
34 assertTrue(leafNodeNull.equals(equalLeafNodeNull));
35 assertTrue(equalLeafNodeNull.equals(leafNodeNull));
37 byte[] value = "test".getBytes();
38 byte[] equalValue = "test".getBytes();
40 LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
41 LeafNode<byte[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
43 assertTrue(leafNode.equals(leafNode));
44 assertTrue(leafNode.equals(equalLeafNode));
45 assertTrue(equalLeafNode.equals(leafNode));
47 Byte[] value2 = new Byte[] { new Byte("1"), new Byte("2") };
48 Byte[] equalValue2 = new Byte[] { new Byte("1"), new Byte("2") };
50 LeafNode<Byte[]> leafNode2 = ImmutableNodes
51 .leafNode(LEAF_QNAME, value2);
52 LeafNode<Byte[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
54 assertTrue(leafNode2.equals(leafNode2));
55 assertTrue(leafNode2.equals(equalLeafNode2));
56 assertTrue(equalLeafNode2.equals(leafNode2));
58 byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
59 byte[][] equalValue3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
61 LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME,
63 LeafNode<byte[][]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
65 assertTrue(leafNode3.equals(leafNode3));
66 assertTrue(leafNode3.equals(equalLeafNode3));
67 assertTrue(equalLeafNode3.equals(leafNode3));
69 Byte[][] value4 = new Byte[][] {
70 new Byte[] { new Byte("1"), new Byte("2") },
71 new Byte[] { new Byte("3"), new Byte("4") },
73 Byte[][] equalValue4 = new Byte[][] {
74 new Byte[] { new Byte("1"), new Byte("2") },
75 new Byte[] { new Byte("3"), new Byte("4") },
78 LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME,value4);
79 LeafNode<Byte[][]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
81 assertTrue(leafNode4.equals(leafNode4));
82 assertTrue(leafNode4.equals(equalLeafNode4));
83 assertTrue(equalLeafNode4.equals(leafNode4));
85 Byte value6 = new Byte("1");
86 Byte equalValue6 = new Byte("1");
88 LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
89 LeafNode<Byte> equalLeafNode6 = ImmutableNodes.leafNode(
90 SAME_LEAF_QNAME, equalValue6);
92 assertTrue(leafNode6.equals(leafNode6));
93 assertTrue(leafNode6.equals(equalLeafNode6));
94 assertTrue(equalLeafNode6.equals(leafNode6));
96 String value5 = "test";
97 String equalValue5 = new String("test");
99 LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
100 LeafNode<String> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
102 assertTrue(leafNode5.equals(leafNode5));
103 assertTrue(leafNode5.equals(equalLeafNode5));
104 assertTrue(equalLeafNode5.equals(leafNode5));
108 // We're testing equals()
109 @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
110 public void notEqualByteTest() {
112 byte[] value = "test".getBytes();
113 byte[] equalValue = "test".getBytes();
115 LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
116 LeafNode<byte[]> otherLeafNode = ImmutableNodes.leafNode(OTHER_LEAF_QNAME, equalValue);
118 assertFalse(leafNode.equals(null));
119 assertFalse(leafNode.equals(new Object()));
120 assertFalse(leafNode.equals(otherLeafNode));
121 assertFalse(otherLeafNode.equals(leafNode));
123 byte[] value1 = "test".getBytes();
124 byte[] otherValue1 = "test1".getBytes();
126 LeafNode<byte[]> leafNode1 = ImmutableNodes.leafNode(LEAF_QNAME, value1);
127 LeafNode<byte[]> otherLeafNode1 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue1);
129 assertFalse(leafNode1.equals(otherLeafNode1));
130 assertFalse(otherLeafNode1.equals(leafNode1));
132 Byte[] value2 = new Byte[] { new Byte("1"), new Byte("1") };
133 Byte[] otherValue2 = new Byte[] { new Byte("1"), new Byte("2") };
135 LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
136 LeafNode<Byte[]> otherLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue2);
138 assertFalse(leafNode2.equals(otherLeafNode2));
139 assertFalse(otherLeafNode2.equals(leafNode2));
141 byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
142 byte[][] otherValue3 = new byte[][] { "test".getBytes(), "test3".getBytes() };
144 LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
145 LeafNode<byte[][]> otherLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue3);
147 assertFalse(leafNode3.equals(otherLeafNode3));
148 assertFalse(otherLeafNode3.equals(leafNode3));
150 Byte[][] value4 = new Byte[][] {
151 new Byte[] { new Byte("1"), new Byte("2") },
152 new Byte[] { new Byte("3"), new Byte("4") },
154 Byte[][] otherValue4 = new Byte[][] {
155 new Byte[] { new Byte("1"), new Byte("2") },
156 new Byte[] { new Byte("3"), new Byte("5") },
159 LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
160 LeafNode<Byte[][]> otherLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue4);
162 assertFalse(leafNode4.equals(otherLeafNode4));
163 assertFalse(otherLeafNode4.equals(leafNode4));
165 Byte value6 = new Byte("1");
166 Byte otherValue6 = new Byte("2");
168 LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
169 LeafNode<Byte> otherLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue6);
171 assertFalse(leafNode6.equals(otherLeafNode6));
172 assertFalse(otherLeafNode6.equals(leafNode6));
174 String value5 = "test";
175 String otherValue5 = "test2";
177 LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
178 LeafNode<String> otherLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue5);
180 assertFalse(leafNode5.equals(otherLeafNode5));
181 assertFalse(otherLeafNode5.equals(leafNode5));
182 assertFalse(leafNode5.equals(leafNode));
183 assertFalse(leafNode5.equals(leafNode1));
184 assertFalse(leafNode5.equals(leafNode2));
185 assertFalse(leafNode5.equals(leafNode3));
186 assertFalse(leafNode5.equals(leafNode4));
187 assertFalse(leafNode5.equals(leafNode6));
188 assertFalse(leafNode.equals(leafNode5));
189 assertFalse(leafNode1.equals(leafNode5));
190 assertFalse(leafNode2.equals(leafNode5));
191 assertFalse(leafNode3.equals(leafNode5));
192 assertFalse(leafNode4.equals(leafNode5));
193 assertFalse(leafNode6.equals(leafNode5));
195 LeafNode<byte[]> leafNodeNull = ImmutableNodes.leafNode(SAME_LEAF_QNAME, null);
196 assertFalse(leafNodeNull.equals(leafNode));
197 assertFalse(leafNode.equals(leafNodeNull));
199 byte[] byteValue = new byte[] { 1, 1 };
201 LeafNode<byte[]> byteLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, byteValue);
202 assertFalse(byteLeafNode.equals(leafNode2));
203 assertFalse(leafNode2.equals(byteLeafNode));
207 // We're testing equals()
208 @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"})
209 public void equalsOtherTypesTest() {
211 char[] valueChar = "test".toCharArray();
212 char[] equalValueChar = "test".toCharArray();
214 LeafNode<char[]> leafNodeChar = ImmutableNodes.leafNode(LEAF_QNAME, valueChar);
215 LeafNode<char[]> equalLeafNodeChar = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValueChar);
217 assertTrue(leafNodeChar.equals(leafNodeChar));
218 assertTrue(leafNodeChar.equals(equalLeafNodeChar));
219 assertTrue(equalLeafNodeChar.equals(leafNodeChar));
221 boolean[] value = new boolean[] { true, false };
222 boolean[] equalValue = new boolean[] { true, false };
224 LeafNode<boolean[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
225 LeafNode<boolean[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
227 assertTrue(leafNode.equals(leafNode));
228 assertTrue(leafNode.equals(equalLeafNode));
229 assertTrue(equalLeafNode.equals(leafNode));
231 int[] value2 = new int[] { 1, 2 };
232 int[] equalValue2 = new int[] { 1, 2 };
234 LeafNode<int[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
235 LeafNode<int[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
237 assertTrue(leafNode2.equals(leafNode2));
238 assertTrue(leafNode2.equals(equalLeafNode2));
239 assertTrue(equalLeafNode2.equals(leafNode2));
241 short[] value3 = new short[] { 1, 2 };
242 short[] equalValue3 = new short[] { 1, 2 };
244 LeafNode<short[]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
245 LeafNode<short[]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
247 assertTrue(leafNode3.equals(leafNode3));
248 assertTrue(leafNode3.equals(equalLeafNode3));
249 assertTrue(equalLeafNode3.equals(leafNode3));
251 long[] value4 = new long[] { 1, 2 };
252 long[] equalValue4 = new long[] { 1, 2 };
254 LeafNode<long[]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
255 LeafNode<long[]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
257 assertTrue(leafNode4.equals(leafNode4));
258 assertTrue(leafNode4.equals(equalLeafNode4));
259 assertTrue(equalLeafNode4.equals(leafNode4));
261 double[] value6 = new double[] { 1, 2 };
262 double[] equalValue6 = new double[] { 1, 2 };
264 LeafNode<double[]> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
265 LeafNode<double[]> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
267 assertTrue(leafNode6.equals(leafNode6));
268 assertTrue(leafNode6.equals(equalLeafNode6));
269 assertTrue(equalLeafNode6.equals(leafNode6));
271 float[] value5 = new float[] { 1, 2 };
272 float[] equalValue5 = new float[] { 1, 2 };
274 LeafNode<float[]> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
275 LeafNode<float[]> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
277 assertTrue(leafNode5.equals(leafNode5));
278 assertTrue(leafNode5.equals(equalLeafNode5));
279 assertTrue(equalLeafNode5.equals(leafNode5));
281 assertFalse(leafNode.equals(leafNode5));
282 assertFalse(leafNode2.equals(leafNode5));
283 assertFalse(leafNode3.equals(leafNode5));
284 assertFalse(leafNode4.equals(leafNode5));
285 assertFalse(leafNodeChar.equals(leafNode5));
286 assertFalse(leafNode6.equals(leafNode5));
288 assertFalse(leafNode5.equals(leafNode));
289 assertFalse(leafNode5.equals(leafNode2));
290 assertFalse(leafNode5.equals(leafNode3));
291 assertFalse(leafNode5.equals(leafNode4));
292 assertFalse(leafNode5.equals(leafNodeChar));
293 assertFalse(leafNode5.equals(leafNode6));