d27baf80dc2a7a31f0e71b6552ad604774d50e20
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / schema / nodes / AbstractImmutableNormalizedValueAttrNodeTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.schema.nodes;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12
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;
17
18 public class AbstractImmutableNormalizedValueAttrNodeTest {
19
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");
24
25     @Test
26     // This test is based on using different references; we're testing equals()
27     @SuppressWarnings({"RedundantStringConstructorCall", "EqualsWithItself"})
28     public void equalsByteTest() {
29
30         LeafNode<byte[]> leafNodeNull = ImmutableNodes.leafNode(LEAF_QNAME, null);
31         LeafNode<byte[]> equalLeafNodeNull = ImmutableNodes.leafNode(SAME_LEAF_QNAME, null);
32
33         assertTrue(leafNodeNull.equals(leafNodeNull));
34         assertTrue(leafNodeNull.equals(equalLeafNodeNull));
35         assertTrue(equalLeafNodeNull.equals(leafNodeNull));
36
37         byte[] value = "test".getBytes();
38         byte[] equalValue = "test".getBytes();
39
40         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
41         LeafNode<byte[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
42
43         assertTrue(leafNode.equals(leafNode));
44         assertTrue(leafNode.equals(equalLeafNode));
45         assertTrue(equalLeafNode.equals(leafNode));
46
47         Byte[] value2 = new Byte[] { new Byte("1"), new Byte("2") };
48         Byte[] equalValue2 = new Byte[] { new Byte("1"), new Byte("2") };
49
50         LeafNode<Byte[]> leafNode2 = ImmutableNodes
51                 .leafNode(LEAF_QNAME, value2);
52         LeafNode<Byte[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
53
54         assertTrue(leafNode2.equals(leafNode2));
55         assertTrue(leafNode2.equals(equalLeafNode2));
56         assertTrue(equalLeafNode2.equals(leafNode2));
57
58         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
59         byte[][] equalValue3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
60
61         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME,
62                 value3);
63         LeafNode<byte[][]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
64
65         assertTrue(leafNode3.equals(leafNode3));
66         assertTrue(leafNode3.equals(equalLeafNode3));
67         assertTrue(equalLeafNode3.equals(leafNode3));
68
69         Byte[][] value4 = new Byte[][] {
70             new Byte[] { new Byte("1"), new Byte("2") },
71             new Byte[] { new Byte("3"), new Byte("4") },
72         };
73         Byte[][] equalValue4 = new Byte[][] {
74             new Byte[] { new Byte("1"), new Byte("2") },
75             new Byte[] { new Byte("3"), new Byte("4") },
76         };
77
78         LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME,value4);
79         LeafNode<Byte[][]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
80
81         assertTrue(leafNode4.equals(leafNode4));
82         assertTrue(leafNode4.equals(equalLeafNode4));
83         assertTrue(equalLeafNode4.equals(leafNode4));
84
85         Byte value6 = new Byte("1");
86         Byte equalValue6 = new Byte("1");
87
88         LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
89         LeafNode<Byte> equalLeafNode6 = ImmutableNodes.leafNode(
90                 SAME_LEAF_QNAME, equalValue6);
91
92         assertTrue(leafNode6.equals(leafNode6));
93         assertTrue(leafNode6.equals(equalLeafNode6));
94         assertTrue(equalLeafNode6.equals(leafNode6));
95
96         String value5 = "test";
97         String equalValue5 = new String("test");
98
99         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
100         LeafNode<String> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
101
102         assertTrue(leafNode5.equals(leafNode5));
103         assertTrue(leafNode5.equals(equalLeafNode5));
104         assertTrue(equalLeafNode5.equals(leafNode5));
105     }
106
107     @Test
108     // We're testing equals()
109     @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
110     public void notEqualByteTest() {
111
112         byte[] value = "test".getBytes();
113         byte[] equalValue = "test".getBytes();
114
115         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
116         LeafNode<byte[]> otherLeafNode = ImmutableNodes.leafNode(OTHER_LEAF_QNAME, equalValue);
117
118         assertFalse(leafNode.equals(null));
119         assertFalse(leafNode.equals(new Object()));
120         assertFalse(leafNode.equals(otherLeafNode));
121         assertFalse(otherLeafNode.equals(leafNode));
122
123         byte[] value1 = "test".getBytes();
124         byte[] otherValue1 = "test1".getBytes();
125
126         LeafNode<byte[]> leafNode1 = ImmutableNodes.leafNode(LEAF_QNAME, value1);
127         LeafNode<byte[]> otherLeafNode1 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue1);
128
129         assertFalse(leafNode1.equals(otherLeafNode1));
130         assertFalse(otherLeafNode1.equals(leafNode1));
131
132         Byte[] value2 = new Byte[] { new Byte("1"), new Byte("1") };
133         Byte[] otherValue2 = new Byte[] { new Byte("1"), new Byte("2") };
134
135         LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
136         LeafNode<Byte[]> otherLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue2);
137
138         assertFalse(leafNode2.equals(otherLeafNode2));
139         assertFalse(otherLeafNode2.equals(leafNode2));
140
141         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
142         byte[][] otherValue3 = new byte[][] { "test".getBytes(), "test3".getBytes() };
143
144         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
145         LeafNode<byte[][]> otherLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue3);
146
147         assertFalse(leafNode3.equals(otherLeafNode3));
148         assertFalse(otherLeafNode3.equals(leafNode3));
149
150         Byte[][] value4 = new Byte[][] {
151             new Byte[] { new Byte("1"), new Byte("2") },
152             new Byte[] { new Byte("3"), new Byte("4") },
153         };
154         Byte[][] otherValue4 = new Byte[][] {
155             new Byte[] { new Byte("1"), new Byte("2") },
156             new Byte[] { new Byte("3"), new Byte("5") },
157         };
158
159         LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
160         LeafNode<Byte[][]> otherLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue4);
161
162         assertFalse(leafNode4.equals(otherLeafNode4));
163         assertFalse(otherLeafNode4.equals(leafNode4));
164
165         Byte value6 = new Byte("1");
166         Byte otherValue6 = new Byte("2");
167
168         LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
169         LeafNode<Byte> otherLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue6);
170
171         assertFalse(leafNode6.equals(otherLeafNode6));
172         assertFalse(otherLeafNode6.equals(leafNode6));
173
174         String value5 = "test";
175         String otherValue5 = "test2";
176
177         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
178         LeafNode<String> otherLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue5);
179
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));
194
195         LeafNode<byte[]> leafNodeNull = ImmutableNodes.leafNode(SAME_LEAF_QNAME, null);
196         assertFalse(leafNodeNull.equals(leafNode));
197         assertFalse(leafNode.equals(leafNodeNull));
198
199         byte[] byteValue = new byte[] { 1, 1 };
200
201         LeafNode<byte[]> byteLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, byteValue);
202         assertFalse(byteLeafNode.equals(leafNode2));
203         assertFalse(leafNode2.equals(byteLeafNode));
204     }
205
206     @Test
207     // We're testing equals()
208     @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"})
209     public void equalsOtherTypesTest() {
210
211         char[] valueChar = "test".toCharArray();
212         char[] equalValueChar = "test".toCharArray();
213
214         LeafNode<char[]> leafNodeChar = ImmutableNodes.leafNode(LEAF_QNAME, valueChar);
215         LeafNode<char[]> equalLeafNodeChar = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValueChar);
216
217         assertTrue(leafNodeChar.equals(leafNodeChar));
218         assertTrue(leafNodeChar.equals(equalLeafNodeChar));
219         assertTrue(equalLeafNodeChar.equals(leafNodeChar));
220
221         boolean[] value = new boolean[] { true, false };
222         boolean[] equalValue = new boolean[] { true, false };
223
224         LeafNode<boolean[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
225         LeafNode<boolean[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
226
227         assertTrue(leafNode.equals(leafNode));
228         assertTrue(leafNode.equals(equalLeafNode));
229         assertTrue(equalLeafNode.equals(leafNode));
230
231         int[] value2 = new int[] { 1, 2 };
232         int[] equalValue2 = new int[] { 1, 2 };
233
234         LeafNode<int[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
235         LeafNode<int[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
236
237         assertTrue(leafNode2.equals(leafNode2));
238         assertTrue(leafNode2.equals(equalLeafNode2));
239         assertTrue(equalLeafNode2.equals(leafNode2));
240
241         short[] value3 = new short[] { 1, 2 };
242         short[] equalValue3 = new short[] { 1, 2 };
243
244         LeafNode<short[]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
245         LeafNode<short[]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
246
247         assertTrue(leafNode3.equals(leafNode3));
248         assertTrue(leafNode3.equals(equalLeafNode3));
249         assertTrue(equalLeafNode3.equals(leafNode3));
250
251         long[] value4 = new long[] { 1, 2 };
252         long[] equalValue4 = new long[] { 1, 2 };
253
254         LeafNode<long[]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
255         LeafNode<long[]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
256
257         assertTrue(leafNode4.equals(leafNode4));
258         assertTrue(leafNode4.equals(equalLeafNode4));
259         assertTrue(equalLeafNode4.equals(leafNode4));
260
261         double[] value6 = new double[] { 1, 2 };
262         double[] equalValue6 = new double[] { 1, 2 };
263
264         LeafNode<double[]> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
265         LeafNode<double[]> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
266
267         assertTrue(leafNode6.equals(leafNode6));
268         assertTrue(leafNode6.equals(equalLeafNode6));
269         assertTrue(equalLeafNode6.equals(leafNode6));
270
271         float[] value5 = new float[] { 1, 2 };
272         float[] equalValue5 = new float[] { 1, 2 };
273
274         LeafNode<float[]> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
275         LeafNode<float[]> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
276
277         assertTrue(leafNode5.equals(leafNode5));
278         assertTrue(leafNode5.equals(equalLeafNode5));
279         assertTrue(equalLeafNode5.equals(leafNode5));
280
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));
287
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));
294     }
295 }