effb1a5448710c8da11b62692cff7e5283d40f4e
[yangtools.git] / data / 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         byte[] value = "test".getBytes();
30         byte[] equalValue = "test".getBytes();
31
32         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
33         LeafNode<byte[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
34
35         assertTrue(leafNode.equals(leafNode));
36         assertTrue(leafNode.equals(equalLeafNode));
37         assertTrue(equalLeafNode.equals(leafNode));
38
39         Byte[] value2 = new Byte[] { new Byte("1"), new Byte("2") };
40         Byte[] equalValue2 = new Byte[] { new Byte("1"), new Byte("2") };
41
42         LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
43         LeafNode<Byte[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
44
45         assertTrue(leafNode2.equals(leafNode2));
46         assertTrue(leafNode2.equals(equalLeafNode2));
47         assertTrue(equalLeafNode2.equals(leafNode2));
48
49         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
50         byte[][] equalValue3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
51
52         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME,
53                 value3);
54         LeafNode<byte[][]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
55
56         assertTrue(leafNode3.equals(leafNode3));
57         assertTrue(leafNode3.equals(equalLeafNode3));
58         assertTrue(equalLeafNode3.equals(leafNode3));
59
60         Byte[][] value4 = new Byte[][] {
61             new Byte[] { new Byte("1"), new Byte("2") },
62             new Byte[] { new Byte("3"), new Byte("4") },
63         };
64         Byte[][] equalValue4 = new Byte[][] {
65             new Byte[] { new Byte("1"), new Byte("2") },
66             new Byte[] { new Byte("3"), new Byte("4") },
67         };
68
69         LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME,value4);
70         LeafNode<Byte[][]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
71
72         assertTrue(leafNode4.equals(leafNode4));
73         assertTrue(leafNode4.equals(equalLeafNode4));
74         assertTrue(equalLeafNode4.equals(leafNode4));
75
76         Byte value6 = new Byte("1");
77         Byte equalValue6 = new Byte("1");
78
79         LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
80         LeafNode<Byte> equalLeafNode6 = ImmutableNodes.leafNode(
81                 SAME_LEAF_QNAME, equalValue6);
82
83         assertTrue(leafNode6.equals(leafNode6));
84         assertTrue(leafNode6.equals(equalLeafNode6));
85         assertTrue(equalLeafNode6.equals(leafNode6));
86
87         String value5 = "test";
88         String equalValue5 = new String("test");
89
90         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
91         LeafNode<String> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
92
93         assertTrue(leafNode5.equals(leafNode5));
94         assertTrue(leafNode5.equals(equalLeafNode5));
95         assertTrue(equalLeafNode5.equals(leafNode5));
96     }
97
98     @Test
99     // We're testing equals()
100     @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
101     public void notEqualByteTest() {
102
103         byte[] value = "test".getBytes();
104         byte[] equalValue = "test".getBytes();
105
106         LeafNode<byte[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
107         LeafNode<byte[]> otherLeafNode = ImmutableNodes.leafNode(OTHER_LEAF_QNAME, equalValue);
108
109         assertFalse(leafNode.equals(null));
110         assertFalse(leafNode.equals(new Object()));
111         assertFalse(leafNode.equals(otherLeafNode));
112         assertFalse(otherLeafNode.equals(leafNode));
113
114         byte[] value1 = "test".getBytes();
115         byte[] otherValue1 = "test1".getBytes();
116
117         LeafNode<byte[]> leafNode1 = ImmutableNodes.leafNode(LEAF_QNAME, value1);
118         LeafNode<byte[]> otherLeafNode1 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue1);
119
120         assertFalse(leafNode1.equals(otherLeafNode1));
121         assertFalse(otherLeafNode1.equals(leafNode1));
122
123         Byte[] value2 = new Byte[] { new Byte("1"), new Byte("1") };
124         Byte[] otherValue2 = new Byte[] { new Byte("1"), new Byte("2") };
125
126         LeafNode<Byte[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
127         LeafNode<Byte[]> otherLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue2);
128
129         assertFalse(leafNode2.equals(otherLeafNode2));
130         assertFalse(otherLeafNode2.equals(leafNode2));
131
132         byte[][] value3 = new byte[][] { "test".getBytes(), "test2".getBytes() };
133         byte[][] otherValue3 = new byte[][] { "test".getBytes(), "test3".getBytes() };
134
135         LeafNode<byte[][]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
136         LeafNode<byte[][]> otherLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue3);
137
138         assertFalse(leafNode3.equals(otherLeafNode3));
139         assertFalse(otherLeafNode3.equals(leafNode3));
140
141         Byte[][] value4 = new Byte[][] {
142             new Byte[] { new Byte("1"), new Byte("2") },
143             new Byte[] { new Byte("3"), new Byte("4") },
144         };
145         Byte[][] otherValue4 = new Byte[][] {
146             new Byte[] { new Byte("1"), new Byte("2") },
147             new Byte[] { new Byte("3"), new Byte("5") },
148         };
149
150         LeafNode<Byte[][]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
151         LeafNode<Byte[][]> otherLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue4);
152
153         assertFalse(leafNode4.equals(otherLeafNode4));
154         assertFalse(otherLeafNode4.equals(leafNode4));
155
156         Byte value6 = new Byte("1");
157         Byte otherValue6 = new Byte("2");
158
159         LeafNode<Byte> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
160         LeafNode<Byte> otherLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue6);
161
162         assertFalse(leafNode6.equals(otherLeafNode6));
163         assertFalse(otherLeafNode6.equals(leafNode6));
164
165         String value5 = "test";
166         String otherValue5 = "test2";
167
168         LeafNode<String> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
169         LeafNode<String> otherLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, otherValue5);
170
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));
185
186         byte[] byteValue = new byte[] { 1, 1 };
187
188         LeafNode<byte[]> byteLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, byteValue);
189         assertFalse(byteLeafNode.equals(leafNode2));
190         assertFalse(leafNode2.equals(byteLeafNode));
191     }
192
193     @Test
194     // We're testing equals()
195     @SuppressWarnings({"EqualsWithItself", "EqualsBetweenInconvertibleTypes"})
196     public void equalsOtherTypesTest() {
197
198         char[] valueChar = "test".toCharArray();
199         char[] equalValueChar = "test".toCharArray();
200
201         LeafNode<char[]> leafNodeChar = ImmutableNodes.leafNode(LEAF_QNAME, valueChar);
202         LeafNode<char[]> equalLeafNodeChar = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValueChar);
203
204         assertTrue(leafNodeChar.equals(leafNodeChar));
205         assertTrue(leafNodeChar.equals(equalLeafNodeChar));
206         assertTrue(equalLeafNodeChar.equals(leafNodeChar));
207
208         boolean[] value = new boolean[] { true, false };
209         boolean[] equalValue = new boolean[] { true, false };
210
211         LeafNode<boolean[]> leafNode = ImmutableNodes.leafNode(LEAF_QNAME, value);
212         LeafNode<boolean[]> equalLeafNode = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue);
213
214         assertTrue(leafNode.equals(leafNode));
215         assertTrue(leafNode.equals(equalLeafNode));
216         assertTrue(equalLeafNode.equals(leafNode));
217
218         int[] value2 = new int[] { 1, 2 };
219         int[] equalValue2 = new int[] { 1, 2 };
220
221         LeafNode<int[]> leafNode2 = ImmutableNodes.leafNode(LEAF_QNAME, value2);
222         LeafNode<int[]> equalLeafNode2 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue2);
223
224         assertTrue(leafNode2.equals(leafNode2));
225         assertTrue(leafNode2.equals(equalLeafNode2));
226         assertTrue(equalLeafNode2.equals(leafNode2));
227
228         short[] value3 = new short[] { 1, 2 };
229         short[] equalValue3 = new short[] { 1, 2 };
230
231         LeafNode<short[]> leafNode3 = ImmutableNodes.leafNode(LEAF_QNAME, value3);
232         LeafNode<short[]> equalLeafNode3 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue3);
233
234         assertTrue(leafNode3.equals(leafNode3));
235         assertTrue(leafNode3.equals(equalLeafNode3));
236         assertTrue(equalLeafNode3.equals(leafNode3));
237
238         long[] value4 = new long[] { 1, 2 };
239         long[] equalValue4 = new long[] { 1, 2 };
240
241         LeafNode<long[]> leafNode4 = ImmutableNodes.leafNode(LEAF_QNAME, value4);
242         LeafNode<long[]> equalLeafNode4 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue4);
243
244         assertTrue(leafNode4.equals(leafNode4));
245         assertTrue(leafNode4.equals(equalLeafNode4));
246         assertTrue(equalLeafNode4.equals(leafNode4));
247
248         double[] value6 = new double[] { 1, 2 };
249         double[] equalValue6 = new double[] { 1, 2 };
250
251         LeafNode<double[]> leafNode6 = ImmutableNodes.leafNode(LEAF_QNAME, value6);
252         LeafNode<double[]> equalLeafNode6 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue6);
253
254         assertTrue(leafNode6.equals(leafNode6));
255         assertTrue(leafNode6.equals(equalLeafNode6));
256         assertTrue(equalLeafNode6.equals(leafNode6));
257
258         float[] value5 = new float[] { 1, 2 };
259         float[] equalValue5 = new float[] { 1, 2 };
260
261         LeafNode<float[]> leafNode5 = ImmutableNodes.leafNode(LEAF_QNAME, value5);
262         LeafNode<float[]> equalLeafNode5 = ImmutableNodes.leafNode(SAME_LEAF_QNAME, equalValue5);
263
264         assertTrue(leafNode5.equals(leafNode5));
265         assertTrue(leafNode5.equals(equalLeafNode5));
266         assertTrue(equalLeafNode5.equals(leafNode5));
267
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));
274
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));
281     }
282 }