1   package org.apache.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  
24  /***
25   * Unit tests for the bounded queue.
26   * <p>
27   * @author Aaron Smuts
28   */
29  public class BoundedQueueUnitTest
30      extends TestCase
31  {
32      /***
33       * Verify null returned for empty.
34       */
35      public void testTakeLastEmpty()
36      {
37          // SETUP
38          int maxSize = 10;
39          BoundedQueue queue = new BoundedQueue( maxSize );
40  
41          // DO WORK
42          Object result = queue.take();
43  
44          // VERIFY
45          assertNull( "Result should be null", result );
46      }
47  
48      /***
49       * Verify that the queue returns the number of elements and the it does not exceed the max.
50       */
51      public void testSize()
52      {
53          // SETUP
54          int maxSize = 10;
55          BoundedQueue queue = new BoundedQueue( maxSize );
56  
57          // DO WORK
58          for ( int i = 0; i < maxSize * 2; i++ )
59          {
60              queue.add( "adfadsf sad " + i );
61          }
62  
63          int result = queue.size();
64  
65          // VERIFY
66          assertEquals( "Result size not as expected", maxSize, result );
67      }
68  
69      /***
70       * Verify that the items come back in the order put in.
71       */
72      public void testFIFOOrderedTake()
73      {
74          // SETUP
75          int maxSize = 10;
76          BoundedQueue queue = new BoundedQueue( maxSize );
77  
78          // DO WORK
79          for ( int i = 0; i < maxSize; i++ )
80          {
81              queue.add( String.valueOf( i ) );
82          }
83  
84  
85          // VERIFY
86  
87          for ( int i = 0; i < maxSize; i++ )
88          {
89              String result = (String)queue.take();
90              assertEquals( "Result not as expected",  String.valueOf( i ) ,  result  );
91          }
92      }
93  }