Merge "Added XmlTreeBuilder into yang-data-impl"
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / parser / util / RefineHolderTest.java
1 package org.opendaylight.yangtools.yang.parser.util;
2
3 import static org.junit.Assert.*;
4
5 import java.net.URI;
6 import java.net.URISyntaxException;
7 import java.util.ArrayList;
8 import java.util.Date;
9 import java.util.List;
10
11 import org.junit.Before;
12 import org.junit.Test;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
15 import org.opendaylight.yangtools.yang.parser.builder.impl.UnknownSchemaNodeBuilder;
16
17 public class RefineHolderTest {
18
19     private RefineHolder rh;
20     private RefineHolder rh1;
21
22     @Before
23     public void init() {
24         rh = new RefineHolder("module", 2104, "name");
25         rh1 = new RefineHolder("module", 2104, "name");
26     }
27
28
29
30     @Test
31     public void testRefineEquality() {
32         // hashCode method test
33         assertEquals("rh should equals to itsefl", rh, rh);
34         assertFalse("rh shouldn't equal to null", rh.equals(null));
35         assertFalse("rh shouldn't equal to object of other type", rh.equals(new String("str")));
36
37         assertEquals("rh1 should equals to rh", rh, rh1);
38
39         RefineHolder rh2 = new RefineHolder("module", 2104, null);
40         assertFalse("rh shouldn't equal to rh2", rh2.equals(rh1));
41         rh2 = new RefineHolder("module", 2104, "name2");
42         assertFalse("rh shouldn't equal to rh2", rh.equals(rh2));
43
44         assertEquals("Wrong hash code", 1557537141, rh.hashCode());
45     }
46
47     @Test
48     public void testConfigurationEqualsBranch() {
49         assertEquals("rh should equal to rh1", rh, rh1);
50         rh1.setConfiguration(false);
51         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
52         rh.setConfiguration(false);
53         assertEquals("rh should equal to rh1", rh, rh1);
54         rh.setConfiguration(true);
55         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
56         rh1.setConfiguration(true);
57     }
58
59     @Test
60     public void testDefaultStrEqualsBranch() {
61         assertEquals("rh should equal to rh1", rh, rh1);
62         rh1.setDefaultStr("default string1");
63         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
64         rh.setDefaultStr("default string1");
65         assertEquals("rh should equal to rh1", rh, rh1);
66         rh.setDefaultStr("default string");
67         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
68         rh1.setDefaultStr("default string");
69     }
70
71     @Test
72     public void testDescriptionEqualsBranch() {
73         assertEquals("rh should equal to rh1", rh, rh1);
74         rh1.setDescription("description1");
75         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
76         rh.setDescription("description1");
77         assertEquals("rh should equal to rh1", rh, rh1);
78         rh.setDescription("description");
79         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
80         rh1.setDescription("description");
81     }
82
83     @Test
84     public void testMandatoryEqualsBranch() {
85         assertEquals("rh should equal to rh1", rh, rh1);
86         rh1.setMandatory(false);
87         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
88         rh.setMandatory(false);
89         assertEquals("rh should equal to rh1", rh, rh1);
90         rh.setMandatory(true);
91         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
92         rh1.setMandatory(true);
93     }
94
95     @Test
96     public void testMaxElementsEqualsBranch() {
97         assertEquals("rh should equal to rh1", rh, rh1);
98         rh1.setMaxElements(5400);
99         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
100         rh.setMaxElements(5400);
101         assertEquals("rh should equal to rh1", rh, rh1);
102         rh.setMaxElements(5435);
103         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
104         rh1.setMaxElements(5435);
105     }
106
107     @Test
108     public void testMinElementsEqualsBranch() {
109         assertEquals("rh should equal to rh1", rh, rh1);
110         rh1.setMinElements(16);
111         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
112         rh.setMinElements(16);
113         assertEquals("rh should equal to rh1", rh, rh1);
114         rh.setMinElements(159);
115         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
116         rh1.setMinElements(159);
117     }
118
119     @Test
120     public void testMustEqualsBranch() {
121         assertEquals("rh should equal to rh1", rh, rh1);
122         rh1.setMust(new MustDefinitionImpl("mustStr1", "description1", "reference1", "errorAppTag1", "errorMessage1"));
123         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
124         rh.setMust(new MustDefinitionImpl("mustStr1", "description1", "reference1", "errorAppTag1", "errorMessage1"));
125         assertEquals("rh should equal to rh1", rh, rh1);
126         rh.setMust(new MustDefinitionImpl("mustStr", "description", "reference", "errorAppTag", "errorMessage"));
127         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
128         rh1.setMust(new MustDefinitionImpl("mustStr", "description", "reference", "errorAppTag", "errorMessage"));
129     }
130
131     @Test
132     public void testPresenceEqualsBranch() {
133         assertEquals("rh should equal to rh1", rh, rh1);
134         rh1.setPresence(false);
135         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
136         rh.setPresence(false);
137         assertEquals("rh should equal to rh1", rh, rh1);
138         rh.setPresence(true);
139         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
140         rh1.setPresence(true);
141     }
142
143     @Test
144     public void testReferenceEqualsBranch() {
145         assertEquals("rh should equal to rh1", rh, rh1);
146         rh1.setReference("reference1");
147         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
148         rh.setReference("reference1");
149         assertEquals("rh should equal to rh1", rh, rh1);
150         rh.setReference("reference");
151         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
152         rh1.setReference("reference");
153     }
154
155     private URI getUri(String uri) {
156         URI simpleUri = null;
157         boolean instantionated = false;
158         try {
159             simpleUri = new URI(uri);
160             instantionated = true;
161         } catch (URISyntaxException e1) {
162             e1.printStackTrace();
163             assertTrue("Uri instance wasn't created.", instantionated);
164         }
165         return simpleUri;
166     }
167
168     @Test
169     public void testAddUnknownNodeBuilderEqualsBranch() {
170         URI simpleUri = null;
171         simpleUri = getUri("very:simple:URI");
172         assertNotNull("URI can't be null", simpleUri);
173
174         UnknownSchemaNodeBuilder usnb = new UnknownSchemaNodeBuilder("usnb", 151, new QName(simpleUri, "tst"));
175         UnknownSchemaNodeBuilder usnb1 = new UnknownSchemaNodeBuilder("usnb", 151, new QName(simpleUri, "tst"));
176
177         URI uriA = getUri("some:uriA");
178         assertNotNull("URI can't be null", simpleUri);
179         QName qnameA = new QName(uriA, new Date(5000000), "some nameA");
180         QName qnameB = new QName(uriA, new Date(6000000), "some nameB");
181         List<QName> qnamesA = new ArrayList<>();
182         List<QName> qnamesB = new ArrayList<>();
183         qnamesA.add(qnameA);
184         qnamesB.add(qnameB);
185         SchemaPath schemaPathA = new SchemaPath(qnamesA, true);
186         SchemaPath schemaPathB = new SchemaPath(qnamesB, true);
187
188         usnb.setPath(schemaPathB);
189         usnb1.setPath(schemaPathB);
190
191         assertEquals("rh should equal to rh1", rh, rh1);
192         rh1.addUnknownNodeBuilder(usnb);
193         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
194         rh.addUnknownNodeBuilder(usnb1);
195         assertEquals("rh should equal to rh1", rh, rh1);
196         usnb.setPath(schemaPathA);
197         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
198         usnb1.setPath(schemaPathA);
199     }
200
201     @Test
202     public void testParentEqualsBranch() {
203         URI simpleUriA = getUri("very:simple:URI:a");
204         URI simpleUriB = getUri("very:simple:URI:b");
205
206         UnknownSchemaNodeBuilder usnbA = new UnknownSchemaNodeBuilder("usnbA", 151, new QName(simpleUriA, "tst"));
207         UnknownSchemaNodeBuilder usnbB = new UnknownSchemaNodeBuilder("usnbB", 151, new QName(simpleUriB, "tst"));
208         UnknownSchemaNodeBuilder usnbAParent = new UnknownSchemaNodeBuilder("usnbAParent", 151, new QName(simpleUriA,
209                 "tst"));
210         usnbA.setParent(usnbAParent);
211
212         assertEquals("rh should equal to rh1", rh, rh1);
213         rh1.setParent(usnbB);
214         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
215         rh.setParent(usnbB);
216         assertEquals("rh should equal to rh1", rh, rh1);
217         rh.setParent(usnbA);
218         assertFalse("rh shouldn't equal to rh1", rh.equals(rh1));
219         rh1.setParent(usnbA);
220
221         assertEquals("rh should equal to rh1", rh, rh1);
222     }
223
224 }