View Javadoc
1   package org.apache.commons.jcs.utils.struct;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.commons.jcs.TestLogConfigurationUtil;
24  
25  import java.io.StringWriter;
26  
27  /** Unit tests for the double linked list. */
28  public class DoubleLinkedListUnitTest
29      extends TestCase
30  {
31      /** verify that the last is added when the list is empty. */
32      public void testAddLast_Empty()
33      {
34          // SETUP
35          DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
36  
37          String payload1 = "payload1";
38          DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
39  
40          // WO WORK
41          list.addLast( node1 );
42  
43          // VERIFY
44          assertEquals( "Wrong last", node1, list.getLast() );
45      }
46  
47      /** verify that the last is added when the list is empty. */
48      public void testAddLast_NotEmpty()
49      {
50          // SETUP
51          DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
52  
53          String payload1 = "payload1";
54          DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
55  
56          String payload2 = "payload2";
57          DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 );
58  
59          // WO WORK
60          list.addLast( node1 );
61          list.addLast( node2 );
62  
63          // VERIFY
64          assertEquals( "Wrong last", node2, list.getLast() );
65      }
66  
67      /** verify that it's added last. */
68      public void testMakeLast_wasFirst()
69      {
70          // SETUP
71          DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
72  
73          String payload1 = "payload1";
74          DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
75  
76          String payload2 = "payload2";
77          DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 );
78  
79          list.addFirst( node2 );
80          list.addFirst(  node1 );
81  
82          // DO WORK
83          list.makeLast( node1 );
84  
85          // VERIFY
86          assertEquals( "Wrong size", 2, list.size() );
87          assertEquals( "Wrong last", node1, list.getLast() );
88          assertEquals( "Wrong first", node2, list.getFirst() );
89      }
90  
91      /** verify that it's added last. */
92      public void testMakeLast_wasLast()
93      {
94          // SETUP
95          DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
96  
97          String payload1 = "payload1";
98          DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
99  
100         String payload2 = "payload2";
101         DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 );
102 
103         list.addFirst( node1 );
104         list.addFirst(  node2 );
105 
106         // DO WORK
107         list.makeLast( node1 );
108 
109         // VERIFY
110         assertEquals( "Wrong size", 2, list.size() );
111         assertEquals( "Wrong last", node1, list.getLast() );
112         assertEquals( "Wrong first", node2, list.getFirst() );
113     }
114 
115     /** verify that it's added last. */
116     public void testMakeLast_wasAlone()
117     {
118         // SETUP
119         DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
120 
121         String payload1 = "payload1";
122         DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
123 
124         list.addFirst( node1 );
125 
126         // DO WORK
127         list.makeLast( node1 );
128 
129         // VERIFY
130         assertEquals( "Wrong size", 1, list.size() );
131         assertEquals( "Wrong last", node1, list.getLast() );
132         assertEquals( "Wrong first", node1, list.getFirst() );
133     }
134 
135     /** verify that it's added last. */
136     public void testMakeLast_wasInMiddle()
137     {
138         // SETUP
139         DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
140 
141         String payload1 = "payload1";
142         DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
143 
144         String payload2 = "payload2";
145         DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 );
146 
147         String payload3 = "payload3";
148         DoubleLinkedListNode<String> node3 = new DoubleLinkedListNode<String>( payload3 );
149 
150         list.addFirst( node2 );
151         list.addFirst(  node1 );
152         list.addFirst(  node3 );
153 
154         // DO WORK
155         list.makeLast( node1 );
156 
157         // VERIFY
158         assertEquals( "Wrong size", 3, list.size() );
159         assertEquals( "Wrong last", node1, list.getLast() );
160         assertEquals( "Wrong first", node3, list.getFirst() );
161     }
162 
163     /** verify that the entries are dumped. */
164     public void testDumpEntries_DebugTrue()
165     {
166         // SETUP
167         StringWriter stringWriter = new StringWriter();
168         TestLogConfigurationUtil.configureLogger( stringWriter, DoubleLinkedList.class.getName() );
169 
170         DoubleLinkedList<DoubleLinkedListNode<String>> list = new DoubleLinkedList<DoubleLinkedListNode<String>>();
171 
172         String payload1 = "payload1";
173         DoubleLinkedListNode<String> node1 = new DoubleLinkedListNode<String>( payload1 );
174 
175         String payload2 = "payload2";
176         DoubleLinkedListNode<String> node2 = new DoubleLinkedListNode<String>( payload2 );
177 
178         list.addLast( node1 );
179         list.addLast( node2 );
180         list.debugDumpEntries();
181 
182         // WO WORK
183         String result = stringWriter.toString();
184 
185         // VERIFY
186         assertTrue( "Missing node in log dump", result.indexOf( payload1 ) != -1 );
187         assertTrue( "Missing node in log dump", result.indexOf( payload2 ) != -1 );
188     }
189 }