KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > type > NodeTest


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of JEGG nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.type;
30
31 import java.util.Collection JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import sun.security.krb5.internal.i;
35
36 import jegg.impl.TestBase;
37
38 /**
39  *
40  */

41 public class NodeTest extends TestBase
42 {
43     static
44     {
45         setTestClass(NodeTest.class);
46     }
47     private Node nodeA, nodeA1, nodeA2;
48     private Node nodeB, nodeB1, nodeB2;
49     private Node nodeC, nodeC1, nodeC2;
50     
51     /**
52      * @param n
53      */

54     public NodeTest(String JavaDoc n)
55     {
56         super(n);
57     }
58
59     /* (non-Javadoc)
60      * @see jegg.TestBase#setup()
61      */

62     protected void setup()
63     {
64         nodeA = new Node(TypeA.class);
65         nodeA1 = new Node(TypeA1.class);
66         nodeA2 = new Node(TypeA2.class);
67         nodeB = new Node(TypeB.class);
68         nodeB1 = new Node(TypeB1.class);
69         nodeB2 = new Node(TypeB2.class);
70         nodeC = new Node(TypeC.class);
71         nodeC1 = new Node(TypeC1.class);
72         nodeC2 = new Node(TypeC2.class);
73     }
74
75     /* (non-Javadoc)
76      * @see jegg.TestBase#teardown()
77      */

78     protected void teardown()
79     {
80         // EMPTY
81
}
82
83     public void test1()
84     {
85         nodeA.insert(nodeC);
86         nodeA.insert(nodeB);
87         
88         assertNotNull(nodeA.findNearest(TypeB.class));
89         assertNotNull(nodeA.findNearest(TypeC.class));
90         
91         Collection JavaDoc a_branches = nodeA.getBranches();
92         assertNotNull(a_branches);
93         assertEquals(1,a_branches.size());
94         
95         Node b = (Node) a_branches.iterator().next();
96         assertNotNull(b);
97         assertEquals(nodeB.getType(), b.getType());
98         
99         Collection JavaDoc b_branches = b.getBranches();
100         assertNotNull(b_branches);
101         assertEquals(1,b_branches.size());
102         
103         Node c = (Node) b_branches.iterator().next();
104         assertNotNull(c);
105         assertEquals(nodeC.getType(), c.getType());
106     }
107     
108     public void test2()
109     {
110         nodeA.insert(nodeC);
111         nodeA.insert(nodeC1);
112         nodeA.insert(nodeC2);
113         
114         Collection JavaDoc a_branches = nodeA.getBranches();
115         assertNotNull(a_branches);
116         assertEquals(1,a_branches.size());
117         
118         Node c = (Node) a_branches.iterator().next();
119         assertNotNull(c);
120         assertEquals(nodeC.getType(), c.getType());
121         
122         Collection JavaDoc c_branches = c.getBranches();
123         assertNotNull(c_branches);
124         assertEquals(2,c_branches.size());
125         
126         Iterator JavaDoc it = c_branches.iterator();
127         Node c1 = (Node) it.next();
128         Node c2 = (Node) it.next();
129         assertTrue(
130            nodeC1.getType().equals(c1.getType()) ||
131            nodeC2.getType().equals(c1.getType()));
132         assertTrue(
133                 nodeC1.getType().equals(c2.getType()) ||
134                 nodeC2.getType().equals(c2.getType()));
135         assertFalse(c1.getType().equals(c2.getType()));
136     }
137     
138     public void test3()
139     {
140         nodeA.insert(nodeC1);
141         nodeA.insert(nodeC2);
142         nodeA.insert(nodeC);
143         
144         Collection JavaDoc a_branches = nodeA.getBranches();
145         assertNotNull(a_branches);
146         assertEquals(1,a_branches.size());
147         
148         Node c = (Node) a_branches.iterator().next();
149         assertNotNull(c);
150         assertEquals(nodeC.getType(), c.getType());
151         
152         Collection JavaDoc c_branches = c.getBranches();
153         assertNotNull(c_branches);
154         assertEquals(2,c_branches.size());
155         
156         Iterator JavaDoc it = c_branches.iterator();
157         Node c1 = (Node) it.next();
158         Node c2 = (Node) it.next();
159         assertTrue(
160                 nodeC1.getType().equals(c1.getType()) ||
161                 nodeC2.getType().equals(c1.getType()));
162         assertTrue(
163                 nodeC1.getType().equals(c2.getType()) ||
164                 nodeC2.getType().equals(c2.getType()));
165         assertFalse(c1.getType().equals(c2.getType()));
166     }
167     
168     public void test4()
169     {
170         nodeA.insert(nodeC1);
171         nodeA.insert(nodeC2);
172         nodeA.insert(nodeC);
173         nodeA.insert(nodeB1);
174         nodeA.insert(nodeB2);
175         nodeA.insert(nodeB);
176 // nodeA.insert(nodeA1);
177
// nodeA.insert(nodeA2)
178

179         Collection JavaDoc a_branches = nodeA.getBranches();
180         assertNotNull(a_branches);
181         assertEquals(1,a_branches.size());
182         
183         Node b = (Node) a_branches.iterator().next();
184         assertNotNull(b);
185         assertEquals(nodeB.getType(), b.getType());
186         
187         Collection JavaDoc b_branches = b.getBranches();
188         assertNotNull(b_branches);
189         assertEquals(3,b_branches.size());
190         
191         Iterator JavaDoc it = b_branches.iterator();
192         Node b1 = (Node) it.next();
193         Node b2 = (Node) it.next();
194         Node b3 = (Node) it.next();
195         assertTrue(
196                 nodeB1.getType().equals(b1.getType()) ||
197                 nodeB2.getType().equals(b1.getType()) ||
198                 nodeC.getType().equals(b1.getType()));
199         assertTrue(
200                 nodeB1.getType().equals(b2.getType()) ||
201                 nodeB2.getType().equals(b2.getType()) ||
202                 nodeC.getType().equals(b2.getType()));
203         assertTrue(
204                 nodeB1.getType().equals(b3.getType()) ||
205                 nodeB2.getType().equals(b3.getType()) ||
206                 nodeC.getType().equals(b3.getType()));
207         Node c = null;
208         if (nodeC.getType().equals(b1.getType())) c = b1;
209         if (nodeC.getType().equals(b2.getType())) c = b2;
210         if (nodeC.getType().equals(b3.getType())) c = b3;
211         
212         Collection JavaDoc c_branches = c.getBranches();
213         assertNotNull(c_branches);
214         assertEquals(2,c_branches.size());
215         it = c_branches.iterator();
216         Node c1 = (Node) it.next();
217         Node c2 = (Node) it.next();
218         assertTrue(
219                 nodeC1.getType().equals(c1.getType()) ||
220                 nodeC1.getType().equals(c2.getType()));
221         assertTrue(
222                 nodeC2.getType().equals(c1.getType()) ||
223                 nodeC2.getType().equals(c2.getType()));
224     }
225     
226     public void test5()
227     {
228         nodeB.insert(nodeB1);
229         nodeB.insert(nodeB2);
230         assertFalse(nodeB.insert(nodeA));
231         assertTrue(nodeA.insert(nodeB));
232     }
233 }
234
Popular Tags