1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  
11  package com.sri.common.util;
12  
13  import java.util.Iterator;
14  import com.jcorporate.expresso.core.dbobj.ValidValue;
15  import junit.framework.TestCase;
16  
17  /***
18   * Test case that returns combinations of a 2 dimensional sparse array of
19   * valid values.
20   *
21   * @author Michael Rimov
22   */
23  public class TestPicklistCombinator extends TestCase {
24      private ObjectCombinator picklistPermutator = null;
25  
26      private final ValidValue[][] data = {
27          {new ValidValue("1-1", "1"), new ValidValue("1-2", "1-2"), new ValidValue("1-3", "1-3"), },
28          {new ValidValue("2-1,", "2"), new ValidValue("2-2", "2-2")}};
29  
30      protected void setUp() throws Exception {
31          super.setUp();
32          picklistPermutator = new ObjectCombinator(data, ValidValue.class);
33      }
34  
35      protected void tearDown() throws Exception {
36          picklistPermutator = null;
37          super.tearDown();
38      }
39  
40      /***
41       * Check to make sure that hasNext works, including which
42       */
43      public void testIteratorHasNext() {
44          Iterator i = picklistPermutator.iterator();
45          assertTrue(i != null);
46          assertTrue(i.hasNext());
47          i.next();
48          assertTrue(i.hasNext());
49      }
50  
51      /***
52       * Tests iterator
53       */
54      public void testIterator() {
55          Iterator i = picklistPermutator.iterator();
56          assertTrue(i != null);
57  
58          while(i.hasNext()) {
59              ValidValue[] data = (ValidValue[]) i.next();
60              assertTrue(data != null);
61          }
62  
63          i = picklistPermutator.iterator();
64          assertTrue(i.hasNext());
65          ValidValue[] returnData = (ValidValue[]) i.next();
66          assertTrue(returnData != null);
67          assertEquals(2, returnData.length);
68  
69          assertEquals(data[0][0], returnData[0]);
70          assertEquals(data[1][0], returnData[1]);
71  
72          //Next
73          returnData = (ValidValue[]) i.next();
74          assertTrue(returnData != null);
75          assertEquals(2, returnData.length);
76          assertEquals(data[0][0], returnData[0]);
77          assertEquals(data[1][1], returnData[1]);
78  
79          //Ok, now we're dealing with a rollover in the internal counters
80          returnData = (ValidValue[]) i.next();
81          assertTrue(returnData != null);
82          assertEquals(2, returnData.length);
83          assertEquals(data[0][1], returnData[0]);
84          assertEquals(data[1][0], returnData[1]);
85  
86          returnData = (ValidValue[]) i.next();
87          assertTrue(returnData != null);
88          assertEquals(2, returnData.length);
89          assertEquals(data[0][1], returnData[0]);
90          assertEquals(data[1][1], returnData[1]);
91  
92          returnData = (ValidValue[]) i.next();
93          assertTrue(returnData != null);
94          assertEquals(2, returnData.length);
95          assertEquals(data[0][2], returnData[0]);
96          assertEquals(data[1][0], returnData[1]);
97  
98      }
99  
100     /***
101      * Checks to make sure that when we go bast the end we really do throw an
102      * exception
103      */
104     public void testIteratorPastEnd() {
105         Iterator i;
106 
107         for (i = picklistPermutator.iterator(); i.hasNext(); i.next()) {
108             //Iterate through the whole stuff.
109         }
110 
111         try {
112             i.next();
113             fail("i.next() should have thrown an exception");
114         } catch (java.lang.ArrayIndexOutOfBoundsException ex) {
115         	assertNotNull(ex.getMessage());
116         }
117     }
118 
119 }