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() {
29 byte[] value = "test".getBytes();
30 byte[] equalValue = "test".getBytes();
32 LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
33 LeafNode<byte[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
35 assertTrue(leafNode.equals(leafNode));
36 assertTrue(leafNode.equals(equalLeafNode));
37 assertTrue(equalLeafNode.equals(leafNode));
39 Byte[] value2 = new Byte[] { new Byte("1"), new Byte("2") };
40 Byte[] equalValue2 = new Byte[] { new Byte("1"), new Byte("2") };
42 LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
43 LeafNode<Byte[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
45 assertTrue(leafNode2.equals(leafNode2));
46 assertTrue(leafNode2.equals(equalLeafNode2));
47 assertTrue(equalLeafNode2.equals(leafNode2));
49 byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
50 byte[][] equalValue3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
52 LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME,
54 LeafNode<byte[][]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
56 assertTrue(leafNode3.equals(leafNode3));
57 assertTrue(leafNode3.equals(equalLeafNode3));
58 assertTrue(equalLeafNode3.equals(leafNode3));
60 Byte[][] value4 = new Byte[][] {
61 new Byte[] { new Byte("1"), new Byte("2") },
62 new Byte[] { new Byte("3"), new Byte("4") },
64 Byte[][] equalValue4 = new Byte[][] {
65 new Byte[] { new Byte("1"), new Byte("2") },
66 new Byte[] { new Byte("3"), new Byte("4") },
69 LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME,value4);
70 LeafNode<Byte[][]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
72 assertTrue(leafNode4.equals(leafNode4));
73 assertTrue(leafNode4.equals(equalLeafNode4));
74 assertTrue(equalLeafNode4.equals(leafNode4));
76 Byte value6 = new Byte("1");
77 Byte equalValue6 = new Byte("1");
79 LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
80 LeafNode<Byte> equalLeafNode6 = ImmutableNodes.leafNode(
81 SAME_LEAF_QNAME, equalValue6);
83 assertTrue(leafNode6.equals(leafNode6));
84 assertTrue(leafNode6.equals(equalLeafNode6));
85 assertTrue(equalLeafNode6.equals(leafNode6));
87 String value5 = "test";
88 String equalValue5 = new String("test");
90 LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
91 LeafNode<String> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
93 assertTrue(leafNode5.equals(leafNode5));
94 assertTrue(leafNode5.equals(equalLeafNode5));
95 assertTrue(equalLeafNode5.equals(leafNode5));
99 // We're testing equals()
100 @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
101 public void notEqualByteTest() {
103 byte[] value = "test".getBytes();
104 byte[] equalValue = "test".getBytes();
106 LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
107 LeafNode<byte[]> otherLeafNode = ImmutableNodes.leafNode(OTHER_LEAF_QNAME, equalValue);
109 assertFalse(leafNode.equals(null));
110 assertFalse(leafNode.equals(new Object()));
111 assertFalse(leafNode.equals(otherLeafNode));
112 assertFalse(otherLeafNode.equals(leafNode));
114 byte[] value1 = "test".getBytes();
115 byte[] otherValue1 = "test1".getBytes();
117 LeafNode<byte[]> leafNode1 = ImmutableNodes.leafNode(LEAF_QNAME, value1);
118 LeafNode<byte[]> otherLeafNode1 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue1);
120 assertFalse(leafNode1.equals(otherLeafNode1));
121 assertFalse(otherLeafNode1.equals(leafNode1));
123 Byte[] value2 = new Byte[] { new Byte("1"), new Byte("1") };
124 Byte[] otherValue2 = new Byte[] { new Byte("1"), new Byte("2") };
126 LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
127 LeafNode<Byte[]> otherLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue2);
129 assertFalse(leafNode2.equals(otherLeafNode2));
130 assertFalse(otherLeafNode2.equals(leafNode2));
132 byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
133 byte[][] otherValue3 = new byte[][] { "test".getBytes(), "test3".getBytes() };
135 LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
136 LeafNode<byte[][]> otherLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue3);
138 assertFalse(leafNode3.equals(otherLeafNode3));
139 assertFalse(otherLeafNode3.equals(leafNode3));
141 Byte[][] value4 = new Byte[][] {
142 new Byte[] { new Byte("1"), new Byte("2") },
143 new Byte[] { new Byte("3"), new Byte("4") },
145 Byte[][] otherValue4 = new Byte[][] {
146 new Byte[] { new Byte("1"), new Byte("2") },
147 new Byte[] { new Byte("3"), new Byte("5") },
150 LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
151 LeafNode<Byte[][]> otherLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue4);
153 assertFalse(leafNode4.equals(otherLeafNode4));
154 assertFalse(otherLeafNode4.equals(leafNode4));
156 Byte value6 = new Byte("1");
157 Byte otherValue6 = new Byte("2");
159 LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
160 LeafNode<Byte> otherLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue6);
162 assertFalse(leafNode6.equals(otherLeafNode6));
163 assertFalse(otherLeafNode6.equals(leafNode6));
165 String value5 = "test";
166 String otherValue5 = "test2";
168 LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
169 LeafNode<String> otherLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue5);
171 assertFalse(leafNode5.equals(otherLeafNode5));
172 assertFalse(otherLeafNode5.equals(leafNode5));
173 assertFalse(leafNode5.equals(leafNode));
174 assertFalse(leafNode5.equals(leafNode1));
175 assertFalse(leafNode5.equals(leafNode2));
176 assertFalse(leafNode5.equals(leafNode3));
177 assertFalse(leafNode5.equals(leafNode4));
178 assertFalse(leafNode5.equals(leafNode6));
179 assertFalse(leafNode.equals(leafNode5));
180 assertFalse(leafNode1.equals(leafNode5));
181 assertFalse(leafNode2.equals(leafNode5));
182 assertFalse(leafNode3.equals(leafNode5));
183 assertFalse(leafNode4.equals(leafNode5));
184 assertFalse(leafNode6.equals(leafNode5));
186 byte[] byteValue = new byte[] { 1, 1 };
188 LeafNode<byte[]> byteLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, byteValue);
189 assertFalse(byteLeafNode.equals(leafNode2));
190 assertFalse(leafNode2.equals(byteLeafNode));
194 // We're testing equals()
195 @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"})
196 public void equalsOtherTypesTest() {
198 char[] valueChar = "test".toCharArray();
199 char[] equalValueChar = "test".toCharArray();
201 LeafNode<char[]> leafNodeChar = ImmutableNodes.leafNode(LEAF_QNAME, valueChar);
202 LeafNode<char[]> equalLeafNodeChar = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValueChar);
204 assertTrue(leafNodeChar.equals(leafNodeChar));
205 assertTrue(leafNodeChar.equals(equalLeafNodeChar));
206 assertTrue(equalLeafNodeChar.equals(leafNodeChar));
208 boolean[] value = new boolean[] { true, false };
209 boolean[] equalValue = new boolean[] { true, false };
211 LeafNode<boolean[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
212 LeafNode<boolean[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
214 assertTrue(leafNode.equals(leafNode));
215 assertTrue(leafNode.equals(equalLeafNode));
216 assertTrue(equalLeafNode.equals(leafNode));
218 int[] value2 = new int[] { 1, 2 };
219 int[] equalValue2 = new int[] { 1, 2 };
221 LeafNode<int[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
222 LeafNode<int[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
224 assertTrue(leafNode2.equals(leafNode2));
225 assertTrue(leafNode2.equals(equalLeafNode2));
226 assertTrue(equalLeafNode2.equals(leafNode2));
228 short[] value3 = new short[] { 1, 2 };
229 short[] equalValue3 = new short[] { 1, 2 };
231 LeafNode<short[]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
232 LeafNode<short[]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
234 assertTrue(leafNode3.equals(leafNode3));
235 assertTrue(leafNode3.equals(equalLeafNode3));
236 assertTrue(equalLeafNode3.equals(leafNode3));
238 long[] value4 = new long[] { 1, 2 };
239 long[] equalValue4 = new long[] { 1, 2 };
241 LeafNode<long[]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
242 LeafNode<long[]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
244 assertTrue(leafNode4.equals(leafNode4));
245 assertTrue(leafNode4.equals(equalLeafNode4));
246 assertTrue(equalLeafNode4.equals(leafNode4));
248 double[] value6 = new double[] { 1, 2 };
249 double[] equalValue6 = new double[] { 1, 2 };
251 LeafNode<double[]> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
252 LeafNode<double[]> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
254 assertTrue(leafNode6.equals(leafNode6));
255 assertTrue(leafNode6.equals(equalLeafNode6));
256 assertTrue(equalLeafNode6.equals(leafNode6));
258 float[] value5 = new float[] { 1, 2 };
259 float[] equalValue5 = new float[] { 1, 2 };
261 LeafNode<float[]> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
262 LeafNode<float[]> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
264 assertTrue(leafNode5.equals(leafNode5));
265 assertTrue(leafNode5.equals(equalLeafNode5));
266 assertTrue(equalLeafNode5.equals(leafNode5));
268 assertFalse(leafNode.equals(leafNode5));
269 assertFalse(leafNode2.equals(leafNode5));
270 assertFalse(leafNode3.equals(leafNode5));
271 assertFalse(leafNode4.equals(leafNode5));
272 assertFalse(leafNodeChar.equals(leafNode5));
273 assertFalse(leafNode6.equals(leafNode5));
275 assertFalse(leafNode5.equals(leafNode));
276 assertFalse(leafNode5.equals(leafNode2));
277 assertFalse(leafNode5.equals(leafNode3));
278 assertFalse(leafNode5.equals(leafNode4));
279 assertFalse(leafNode5.equals(leafNodeChar));
280 assertFalse(leafNode5.equals(leafNode6));