1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: RandomIteratorTest.java,v 1.3 2005/08/26 18:09:36 mat007 Exp $
29   */
30  
31  package jtge.util.random;
32  
33  import java.util.NoSuchElementException;
34  import java.util.Vector;
35  import jtge.util.EasyMockTestCase;
36  import org.easymock.MockControl;
37  
38  /***
39   * @author Mathieu Champlon
40   * @version $Revision: 1.3 $ $Date: 2005/08/26 18:09:36 $
41   */
42  public class RandomIteratorTest extends EasyMockTestCase
43  {
44      /***
45       * Tested object.
46       */
47      private RandomIterator iterator;
48      /***
49       * Mock objects.
50       */
51      private IRandom mockRandom;
52  
53      protected void setUp()
54      {
55          mockRandom = (IRandom)createMock( IRandom.class );
56      }
57  
58      public void testRandomizeNullIterator()
59      {
60          iterator = new RandomIterator( mockRandom, null );
61          assertFalse( iterator.hasNext() );
62          try
63          {
64              iterator.next();
65          }
66          catch( NoSuchElementException e )
67          {
68              return;
69          }
70          fail();
71      }
72  
73      public void testRemoveThrowsUnsupportedOperationException()
74      {
75          Vector vector = new Vector();
76          iterator = new RandomIterator( mockRandom, vector.iterator() );
77          try
78          {
79              iterator.remove();
80          }
81          catch( UnsupportedOperationException e )
82          {
83              return;
84          }
85          fail();
86      }
87  
88      public void testRandomizeEmptyList()
89      {
90          Vector vector = new Vector();
91          iterator = new RandomIterator( mockRandom, vector.iterator() );
92          assertFalse( iterator.hasNext() );
93          try
94          {
95              iterator.next();
96          }
97          catch( NoSuchElementException e )
98          {
99              return;
100         }
101         fail();
102     }
103 
104     public void testRandomizeListWithOneElement()
105     {
106         Vector vector = new Vector();
107         Object object = new Object();
108         vector.add( object );
109         mockRandom.nextInt( 0 );
110         control( mockRandom ).setMatcher( MockControl.ALWAYS_MATCHER );
111         control( mockRandom ).setReturnValue( 0 );
112         replay();
113         iterator = new RandomIterator( mockRandom, vector.iterator() );
114         assertTrue( iterator.hasNext() );
115         try
116         {
117             assertEquals( iterator.next(), object );
118         }
119         catch( NoSuchElementException e )
120         {
121             fail( e.getMessage() );
122         }
123         assertFalse( iterator.hasNext() );
124     }
125 
126     public void testRandomizeListWithTwoElementsReversingThem()
127     {
128         Vector vector = new Vector();
129         Object object1 = new Object();
130         Object object2 = new Object();
131         vector.add( object1 );
132         vector.add( object2 );
133         mockRandom.nextInt( 0 );
134         control( mockRandom ).setMatcher( MockControl.ALWAYS_MATCHER );
135         control( mockRandom ).setReturnValue( 0 );
136         mockRandom.nextInt( 0 );
137         control( mockRandom ).setMatcher( MockControl.ALWAYS_MATCHER );
138         control( mockRandom ).setReturnValue( 0 );
139         replay();
140         iterator = new RandomIterator( mockRandom, vector.iterator() );
141         assertTrue( iterator.hasNext() );
142         try
143         {
144             assertEquals( iterator.next(), object2 );
145         }
146         catch( NoSuchElementException e )
147         {
148             fail( e.getMessage() );
149         }
150         assertTrue( iterator.hasNext() );
151         try
152         {
153             assertEquals( iterator.next(), object1 );
154         }
155         catch( NoSuchElementException e )
156         {
157             fail( e.getMessage() );
158         }
159         assertFalse( iterator.hasNext() );
160     }
161 }