001: package org.drools.util;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import junit.framework.Assert;
020: import junit.framework.TestCase;
021:
022: public class LinkedListTest extends TestCase {
023:
024: LinkedList list = null;
025: LinkedListNode node1 = null;
026: LinkedListNode node2 = null;
027: LinkedListNode node3 = null;
028:
029: protected void setUp() throws Exception {
030: super .setUp();
031: this .list = new LinkedList();
032: this .node1 = new AbstractBaseLinkedListNode();
033: this .node2 = new AbstractBaseLinkedListNode();
034: this .node3 = new AbstractBaseLinkedListNode();
035: }
036:
037: protected void tearDown() throws Exception {
038: super .tearDown();
039: }
040:
041: /*
042: * Test method for 'org.drools.util.LinkedList.add(LinkedListNode)'
043: */
044: public void testAdd() {
045: this .list.add(this .node1);
046: Assert.assertNull("Node1 previous should be null", this .node1
047: .getPrevious());
048: Assert.assertNull("Node1 next should be null", this .node1
049: .getNext());
050: Assert.assertSame("First node should be node1", this .list
051: .getFirst(), this .node1);
052: Assert.assertSame("Last node should be node1", this .list
053: .getLast(), this .node1);
054:
055: this .list.add(this .node2);
056: Assert.assertSame("node1 next should be node2", this .node1
057: .getNext(), this .node2);
058: Assert.assertSame("node2 previous should be node1", this .node2
059: .getPrevious(), this .node1);
060: Assert.assertSame("First node should be node1", this .list
061: .getFirst(), this .node1);
062: Assert.assertSame("Last node should be node2", this .list
063: .getLast(), this .node2);
064:
065: this .list.add(this .node3);
066: Assert.assertSame("node2 next should be node3", this .node2
067: .getNext(), this .node3);
068: Assert.assertSame("node3 previous should be node2", this .node3
069: .getPrevious(), this .node2);
070: Assert.assertEquals("LinkedList should have 3 nodes", this .list
071: .size(), 3);
072: Assert.assertSame("First node should be node1", this .list
073: .getFirst(), this .node1);
074: Assert.assertSame("Last node should be node3", this .list
075: .getLast(), this .node3);
076: }
077:
078: /*
079: * Test method for 'org.drools.util.LinkedList.remove(LinkedListNode)'
080: */
081: public void testRemove() {
082: this .list.add(this .node1);
083: this .list.add(this .node2);
084: this .list.add(this .node3);
085:
086: Assert.assertSame("Node2 previous should be node1", this .node2
087: .getPrevious(), this .node1);
088: Assert.assertSame("Node2 next should be node3", this .node2
089: .getNext(), this .node3);
090: this .list.remove(this .node2);
091: Assert.assertNull("Node2 previous should be null", this .node2
092: .getPrevious());
093: Assert.assertNull("Node2 next should be null", this .node2
094: .getNext());
095:
096: Assert.assertNull("Node1 previous should be null", this .node1
097: .getPrevious());
098: Assert.assertSame("Node1 next should be node3", this .node1
099: .getNext(), this .node3);
100: this .list.remove(this .node1);
101: Assert.assertNull("Node1 previous should be null", this .node1
102: .getPrevious());
103: Assert.assertNull("Node1 next should be null", this .node1
104: .getNext());
105:
106: Assert.assertNull("Node3 previous should be null", this .node3
107: .getPrevious());
108: Assert.assertNull("Node3 next should be null", this .node3
109: .getNext());
110: this .list.remove(this .node3);
111: Assert.assertNull("Node3 previous should be null", this .node3
112: .getPrevious());
113: Assert.assertNull("Node3 next should be null", this .node3
114: .getNext());
115: }
116:
117: /*
118: * Test method for 'org.drools.util.LinkedList.getFirst()'
119: */
120: public void testGetFirst() {
121: Assert.assertNull(
122: "Empty list should return null on getFirst()",
123: this .list.getFirst());
124: this .list.add(this .node1);
125: Assert.assertSame("List should return node1 on getFirst()",
126: this .list.getFirst(), this .node1);
127: this .list.add(this .node2);
128: Assert.assertSame("List should return node1 on getFirst()",
129: this .list.getFirst(), this .node1);
130: this .list.add(this .node3);
131: Assert.assertSame("List should return node1 on getFirst()",
132: this .list.getFirst(), this .node1);
133: }
134:
135: /*
136: * Test method for 'org.drools.util.LinkedList.getLast()'
137: */
138: public void testGetLast() {
139: Assert.assertNull("Empty list should return null on getLast()",
140: this .list.getLast());
141: this .list.add(this .node1);
142: Assert.assertSame("List should return node1 on getLast()",
143: this .list.getLast(), this .node1);
144: this .list.add(this .node2);
145: Assert.assertSame("List should return node2 on getLast()",
146: this .list.getLast(), this .node2);
147: this .list.add(this .node3);
148: Assert.assertSame("List should return node3 on getLast()",
149: this .list.getLast(), this .node3);
150: }
151:
152: /*
153: * Test method for 'org.drools.util.LinkedList.removeFirst()'
154: */
155: public void testRemoveFirst() {
156: this .list.add(this .node1);
157: this .list.add(this .node2);
158: this .list.add(this .node3);
159:
160: Assert.assertSame("List should return node1 on getFirst()",
161: this .list.getFirst(), this .node1);
162: this .list.removeFirst();
163: Assert.assertSame("List should return node2 on getFirst()",
164: this .list.getFirst(), this .node2);
165: this .list.removeFirst();
166: Assert.assertSame("List should return node3 on getFirst()",
167: this .list.getFirst(), this .node3);
168: this .list.removeFirst();
169: Assert.assertNull(
170: "Empty list should return null on getFirst()",
171: this .list.getFirst());
172: }
173:
174: /*
175: * Test method for 'org.drools.util.LinkedList.removeLast()'
176: */
177: public void testRemoveLast() {
178: this .list.add(this .node1);
179: this .list.add(this .node2);
180: this .list.add(this .node3);
181:
182: Assert.assertSame("List should return node1 on getLast()",
183: this .list.getLast(), this .node3);
184: this .list.removeLast();
185: Assert.assertSame("List should return node2 on getLast()",
186: this .list.getLast(), this .node2);
187: this .list.removeLast();
188: Assert.assertSame("List should return node3 on getLast()",
189: this .list.getLast(), this .node1);
190: this .list.removeLast();
191: Assert.assertNull("Empty list should return null on getLast()",
192: this .list.getLast());
193: }
194:
195: /*
196: * Test method for 'org.drools.util.LinkedList.isEmpty()'
197: */
198: public void testIsEmpty() {
199: Assert.assertTrue("Empty list should return true on isEmpty()",
200: this .list.isEmpty());
201: this .list.add(this .node1);
202: Assert.assertFalse(
203: "Not empty list should return false on isEmpty()",
204: this .list.isEmpty());
205: }
206:
207: /*
208: * Test method for 'org.drools.util.LinkedList.clear()'
209: */
210: public void testClear() {
211: this .list.add(this .node1);
212: this .list.add(this .node2);
213: this .list.add(this .node3);
214:
215: Assert.assertEquals("List size should be 3", this .list.size(),
216: 3);
217: this .list.clear();
218: Assert.assertEquals("Empty list should have size 0", this .list
219: .size(), 0);
220: }
221:
222: /*
223: * Test method for 'org.drools.util.LinkedList.size()'
224: */
225: public void testSize() {
226: this .list.add(this .node1);
227: Assert.assertEquals("LinkedList should have 1 node", this .list
228: .size(), 1);
229:
230: this .list.add(this .node2);
231: Assert.assertEquals("LinkedList should have 2 nodes", this .list
232: .size(), 2);
233:
234: this .list.add(this .node3);
235: Assert.assertEquals("LinkedList should have 3 nodes", this .list
236: .size(), 3);
237: }
238:
239: public void testInsertAfter() {
240: try {
241: this .list.insertAfter(null, this .node1);
242: } catch (NullPointerException e) {
243: e.printStackTrace();
244: fail("Should NOT raise NPE!");
245: }
246: }
247:
248: }
|