1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with 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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.directory.server.core.cursor;
20  
21  
22  import junit.framework.TestCase;
23  
24  import java.util.ArrayList;
25  import java.util.Collections;
26  import java.util.List;
27  
28  
29  /**
30   * Tests the ListCursor class.  The assertXxxx() methods defined in this class
31   * can be collected in an abstract test case class that can be used to test
32   * the behavior of any Cursor implementation down the line.
33   *
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   * @version $Rev$, $Date$
36   */
37  public class ListCursorTest extends TestCase
38  {
39      public void testEmptyList() throws Exception
40      {
41          ListCursor<String> cursor = new ListCursor<String>();
42  
43          // close test
44          cursor.close();
45          assertClosed( cursor, "cursor.isCloased() should return true after closing the cursor", true );
46      }
47  
48  
49      public void testSingleElementList() throws Exception
50      {
51          ListCursor<String> cursor = new ListCursor<String>( Collections.singletonList( "singleton" ) );
52          cursor.close();
53  
54          // close test
55          cursor.close();
56          assertClosed( cursor, "cursor.isCloased() should return true after closing the cursor", true );
57  
58          // bad bounds: start = end is senseless
59          try
60          {
61              cursor = new ListCursor<String>( Collections.singletonList( "singleton" ), 0 );
62              cursor.close();
63              fail( "when the start = end bounds this is senseless and should complain" );
64          }
65          catch ( IllegalArgumentException e )
66          {
67              assertNotNull( e );
68          }
69  
70          // bad bounds: start = end is senseless
71          try
72          {
73              cursor = new ListCursor<String>( 1, Collections.singletonList( "singleton" ) );
74              cursor.close();
75              fail( "when the start = end bounds this is senseless and should complain" );
76          }
77          catch ( IllegalArgumentException e )
78          {
79              assertNotNull( e );
80          }
81  
82          // bad bounds: start > end is senseless
83          try
84          {
85              cursor = new ListCursor<String>( 5, Collections.singletonList( "singleton" ) );
86              cursor.close();
87              fail( "when the start = end bounds this is senseless and should complain" );
88          }
89          catch ( IllegalArgumentException e )
90          {
91              assertNotNull( e );
92          }
93  
94          // bad bounds: end < start is senseless too in another way :)
95          try
96          {
97              cursor = new ListCursor<String>( Collections.singletonList( "singleton" ), -5 );
98              cursor.close();
99              fail( "when the start = end bounds this is senseless and should complain" );
100         }
101         catch ( IllegalArgumentException e )
102         {
103             assertNotNull( e );
104         }
105 
106         // bad bounds: start out of range
107         try
108         {
109             cursor = new ListCursor<String>( -5, Collections.singletonList( "singleton" ) );
110             cursor.close();
111             fail( "when the start = end bounds this is senseless and should complain" );
112         }
113         catch ( IllegalArgumentException e )
114         {
115             assertNotNull( e );
116         }
117 
118         // bad bounds: end out of range
119         try
120         {
121             cursor = new ListCursor<String>( Collections.singletonList( "singleton" ), 5 );
122             cursor.close();
123             fail( "when the start = end bounds this is senseless and should complain" );
124         }
125         catch ( IllegalArgumentException e )
126         {
127             assertNotNull( e );
128         }
129     }
130 
131 
132     public void testManyElementList() throws Exception
133     {
134         List<String> list = new ArrayList<String>();
135         list.add( "item 1" );
136         list.add( "item 2" );
137         list.add( "item 3" );
138         list.add( "item 4" );
139         list.add( "item 5" );
140 
141         // test with bounds of the list itself
142         ListCursor<String> cursor = new ListCursor<String>( list );
143         cursor.close();
144 
145         // test with nonzero lower bound
146         cursor = new ListCursor<String>( 1, list );
147         cursor.close();
148 
149         // test with nonzero lower bound and upper bound
150         cursor = new ListCursor<String>( 1, list, 4 );
151 
152         // close test
153         cursor.close();
154         assertClosed( cursor, "cursor.isCloased() should return true after closing the cursor", true );
155 
156         // bad bounds: start = end is senseless
157         try
158         {
159             cursor = new ListCursor<String>( list, 0 );
160             cursor.close();
161             fail( "when the start = end bounds this is senseless and should complain" );
162         }
163         catch ( IllegalArgumentException e )
164         {
165             assertNotNull( e );
166         }
167 
168         // bad bounds: start = end is senseless
169         try
170         {
171             cursor = new ListCursor<String>( 5, list );
172             cursor.close();
173             fail( "when the start = end bounds this is senseless and should complain" );
174         }
175         catch ( IllegalArgumentException e )
176         {
177             assertNotNull( e );
178         }
179 
180         // bad bounds: start > end is senseless
181         try
182         {
183             cursor = new ListCursor<String>( 10, list );
184             cursor.close();
185             fail( "when the start = end bounds this is senseless and should complain" );
186         }
187         catch ( IllegalArgumentException e )
188         {
189             assertNotNull( e );
190         }
191 
192         // bad bounds: end < start is senseless too in another way :)
193         try
194         {
195             cursor = new ListCursor<String>( list, -5 );
196             cursor.close();
197             fail( "when the start = end bounds this is senseless and should complain" );
198         }
199         catch ( IllegalArgumentException e )
200         {
201             assertNotNull( e );
202         }
203 
204         // bad bounds: start out of range
205         try
206         {
207             cursor = new ListCursor<String>( -5, list );
208             cursor.close();
209             fail( "when the start = end bounds this is senseless and should complain" );
210         }
211         catch ( IllegalArgumentException e )
212         {
213             assertNotNull( e );
214         }
215 
216         // bad bounds: end out of range
217         try
218         {
219             cursor = new ListCursor<String>( list, 10 );
220             cursor.close();
221             fail( "when the start = end bounds this is senseless and should complain" );
222         }
223         catch ( IllegalArgumentException e )
224         {
225             assertNotNull( e );
226         }
227     }
228 
229 
230 
231 
232     protected void assertClosed( Cursor cursor, String msg, boolean expected )
233     {
234         try
235         {
236             assertEquals( msg, expected, cursor.isClosed() );
237         }
238         catch ( Exception e )
239         {
240             fail( "cursor.isClosed() test should not fail after closing the cursor" );
241         }
242 
243         try
244         {
245             cursor.close();
246         }
247         catch ( Exception e )
248         {
249             fail( "cursor.close() after closing the cursor should not fail with exceptions" );
250         }
251 
252 
253         try
254         {
255             cursor.afterLast();
256             fail( "cursor.afterLast() after closing the cursor should fail with an IOException" );
257         }
258         catch ( Exception e )
259         {
260             assertNotNull( e );
261         }
262 
263         try
264         {
265             cursor.beforeFirst();
266             fail( "cursor.beforeFirst() after closing the cursor should fail with an IOException" );
267         }
268         catch ( Exception e )
269         {
270             assertNotNull( e );
271         }
272 
273         try
274         {
275             cursor.first();
276             fail( "cursor.first() after closing the cursor should fail with an IOException" );
277         }
278         catch ( Exception e )
279         {
280             assertNotNull( e );
281         }
282 
283         try
284         {
285             cursor.get();
286             fail( "cursor.get() after closing the cursor should fail with an IOException" );
287         }
288         catch ( Exception e )
289         {
290             assertNotNull( e );
291         }
292 
293         try
294         {
295             cursor.last();
296             fail( "cursor.last() after closing the cursor should fail with an IOException" );
297         }
298         catch ( Exception e )
299         {
300             assertNotNull( e );
301         }
302 
303         try
304         {
305             cursor.next();
306             fail( "cursor.next() after closing the cursor should fail with an IOException" );
307         }
308         catch ( Exception e )
309         {
310             assertNotNull( e );
311         }
312 
313         try
314         {
315             cursor.previous();
316             fail( "cursor.previous() after closing the cursor should fail with an IOException" );
317         }
318         catch ( Exception e )
319         {
320             assertNotNull( e );
321         }
322     }
323 }