KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > util > LinkedNodeTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.util;
19
20 import junit.framework.TestCase;
21
22 /**
23  *
24  * @author chirino
25  */

26 public class LinkedNodeTest extends TestCase {
27     
28     
29     static class IntLinkedNode extends LinkedNode {
30         public final int v;
31
32         public IntLinkedNode(int v){
33             this.v = v;
34         };
35         
36         @Override JavaDoc
37         public String JavaDoc toString() {
38             return ""+v;
39         }
40     }
41
42     IntLinkedNode i1 = new IntLinkedNode(1);
43     IntLinkedNode i2 = new IntLinkedNode(2);
44     IntLinkedNode i3 = new IntLinkedNode(3);
45     IntLinkedNode i4 = new IntLinkedNode(4);
46     IntLinkedNode i5 = new IntLinkedNode(5);
47     IntLinkedNode i6 = new IntLinkedNode(6);
48
49     public void testLinkAfter() {
50         
51         i1.linkAfter(i2.linkAfter(i3));
52         // Order should be 1,2,3
53

54         assertTrue( i1.getNext() == i2 );
55         assertTrue( i1.getNext().getNext() == i3 );
56         assertNull( i1.getNext().getNext().getNext() );
57
58         assertTrue( i3.getPrevious() == i2 );
59         assertTrue( i3.getPrevious().getPrevious() == i1 );
60         assertNull( i3.getPrevious().getPrevious().getPrevious() );
61
62         assertTrue( i1.isHeadNode() );
63         assertFalse(i1.isTailNode() );
64         assertFalse(i2.isHeadNode() );
65         assertFalse(i2.isTailNode() );
66         assertTrue( i3.isTailNode() );
67         assertFalse(i3.isHeadNode() );
68         
69         i1.linkAfter(i4.linkAfter(i5));
70         
71         // Order should be 1,4,5,2,3
72

73         assertTrue( i1.getNext() == i4 );
74         assertTrue( i1.getNext().getNext() == i5 );
75         assertTrue( i1.getNext().getNext().getNext() == i2 );
76         assertTrue( i1.getNext().getNext().getNext().getNext() == i3 );
77         assertNull( i1.getNext().getNext().getNext().getNext().getNext() );
78
79         assertTrue( i3.getPrevious() == i2 );
80         assertTrue( i3.getPrevious().getPrevious() == i5 );
81         assertTrue( i3.getPrevious().getPrevious().getPrevious() == i4 );
82         assertTrue( i3.getPrevious().getPrevious().getPrevious().getPrevious() == i1 );
83         assertNull( i3.getPrevious().getPrevious().getPrevious().getPrevious().getPrevious() );
84         
85         assertTrue( i1.isHeadNode() );
86         assertFalse(i1.isTailNode() );
87         assertFalse(i4.isHeadNode() );
88         assertFalse(i4.isTailNode() );
89         assertFalse(i5.isHeadNode() );
90         assertFalse(i5.isTailNode() );
91         assertFalse(i2.isHeadNode() );
92         assertFalse(i2.isTailNode() );
93         assertTrue( i3.isTailNode() );
94         assertFalse(i3.isHeadNode() );
95                 
96     }
97     
98     public void testLinkBefore() {
99         
100         i3.linkBefore(i2.linkBefore(i1));
101         
102         assertTrue( i1.getNext() == i2 );
103         assertTrue( i1.getNext().getNext() == i3 );
104         assertNull( i1.getNext().getNext().getNext() );
105
106         assertTrue( i3.getPrevious() == i2 );
107         assertTrue( i3.getPrevious().getPrevious() == i1 );
108         assertNull( i3.getPrevious().getPrevious().getPrevious() );
109
110         assertTrue( i1.isHeadNode() );
111         assertFalse(i1.isTailNode() );
112         assertFalse(i2.isHeadNode() );
113         assertFalse(i2.isTailNode() );
114         assertTrue( i3.isTailNode() );
115         assertFalse(i3.isHeadNode() );
116         
117         i2.linkBefore(i5.linkBefore(i4));
118         
119         // Order should be 1,4,5,2,3
120

121         assertTrue( i1.getNext() == i4 );
122         assertTrue( i1.getNext().getNext() == i5 );
123         assertTrue( i1.getNext().getNext().getNext() == i2 );
124         assertTrue( i1.getNext().getNext().getNext().getNext() == i3 );
125         assertNull( i1.getNext().getNext().getNext().getNext().getNext() );
126
127         assertTrue( i3.getPrevious() == i2 );
128         assertTrue( i3.getPrevious().getPrevious() == i5 );
129         assertTrue( i3.getPrevious().getPrevious().getPrevious() == i4 );
130         assertTrue( i3.getPrevious().getPrevious().getPrevious().getPrevious() == i1 );
131         assertNull( i3.getPrevious().getPrevious().getPrevious().getPrevious().getPrevious() );
132         
133         assertTrue( i1.isHeadNode() );
134         assertFalse(i1.isTailNode() );
135         assertFalse(i4.isHeadNode() );
136         assertFalse(i4.isTailNode() );
137         assertFalse(i5.isHeadNode() );
138         assertFalse(i5.isTailNode() );
139         assertFalse(i2.isHeadNode() );
140         assertFalse(i2.isTailNode() );
141         assertTrue( i3.isTailNode() );
142         assertFalse(i3.isHeadNode() );
143                 
144     }
145     
146     public void testUnlink() {
147         
148         i1.linkAfter(i2.linkAfter(i3));
149         i3.linkAfter(i4);
150         i1.linkBefore(i5);
151         i1.linkAfter(i6);
152
153         // Order should be 5,1,6,2,3,4
154
i4.unlink();
155         i5.unlink();
156         i6.unlink();
157         
158         // Order should be 1,2,3
159

160         assertTrue( i1.getNext() == i2 );
161         assertTrue( i1.getNext().getNext() == i3 );
162         assertNull( i1.getNext().getNext().getNext() );
163
164         assertTrue( i3.getPrevious() == i2 );
165         assertTrue( i3.getPrevious().getPrevious() == i1 );
166         assertNull( i3.getPrevious().getPrevious().getPrevious() );
167
168         assertTrue( i1.isHeadNode() );
169         assertFalse(i1.isTailNode() );
170         assertFalse(i2.isHeadNode() );
171         assertFalse(i2.isTailNode() );
172         assertTrue( i3.isTailNode() );
173         assertFalse(i3.isHeadNode() );
174     }
175     
176 }
177
Popular Tags