View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.util;
22  
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  import java.util.TreeSet;
27  
28  import org.apache.commons.collections.set.ListOrderedSet;
29  
30  import junit.framework.TestCase;
31  
32  /**
33   * @author Felipe Leme (dbunit@felipeal.net)
34   * @version $Revision$
35   * @since Nov 5, 2005
36   * 
37   */
38  public class CollectionsHelperTest extends TestCase {
39    
40    public static final String A = "A";
41    public static final String B = "B";
42    public static final String C = "C";
43    
44    public CollectionsHelperTest( String testName ) {
45      super( testName );
46    }
47    
48    public void testObjectsToSetNullEntry() {
49      Set output = CollectionsHelper.objectsToSet( null );
50      assertNull( "set should be null", output );
51    }
52    
53    public void testObjectsToSetEmptyEntry() {
54      Set output = CollectionsHelper.objectsToSet( new Object[0] );
55      assertNotNull( "set should not be null", output );
56      assertEquals( "set should be empty", 0, output.size() );
57    }
58  
59    public void testObjectsToSetSingleInput() {
60      Object[] input = { A };
61      Set output = CollectionsHelper.objectsToSet( input );
62      assertNotNull( "set should not be null", output );
63      Iterator i = output.iterator();
64      assertTrue( "iterator is empty", i.hasNext() );
65      assertEquals( "element 0 match", A, i.next() );
66      assertFalse( "iterator is not empty", i.hasNext() );
67    }
68    
69    public void testObjectsToSetSequence() {
70      Object[] input = { A, C, B };
71      Set output = CollectionsHelper.objectsToSet( input );
72      assertNotNull( "set should not be null", output );
73      Iterator i = output.iterator();
74      assertTrue( "iterator is empty", i.hasNext() );
75      assertEquals( "element 0 match", A, i.next() );
76      assertEquals( "element 1 match", C, i.next() );
77      assertEquals( "element 2 match", B, i.next() );
78      assertFalse( "iterator is not empty", i.hasNext() );
79    }
80    
81    public void testSetToObjectsNullEntry() {
82      Object[] output = CollectionsHelper.setToObjects( null );
83      assertNull( "array should be null", output );
84    }
85    
86    public void testSetToObjectsEmptyEntry() {
87      Set input = new HashSet();
88      Object[] output = CollectionsHelper.setToObjects( input );    
89      assertNotNull( "array should not be null", output );
90      assertEquals( "array should be empty", 0, output.length );
91    }
92  
93    public void testSetToObjectsSingle() {
94      Set input = new HashSet();
95      input.add( A );
96      Object[] output = CollectionsHelper.setToObjects( input );    
97      assertNotNull( "array should not be null", output );
98      assertEquals( "array size does not match", 1, output.length );
99      assertEquals( "element 0 does not match", A, output[0] );
100   }
101 
102   public void testSetToObjectsOrderedSet() {
103     Set input = new TreeSet();
104     input.add( A );
105     input.add( C );
106     input.add( B );
107     Object[] output = CollectionsHelper.setToObjects( input );    
108     assertNotNull( "array should not be null", output );
109     assertEquals( "array size does not match", 3, output.length );
110     assertEquals( "element 0 does not match", A, output[0] );
111     assertEquals( "element 1 does not match", B, output[1] );
112     assertEquals( "element 2 does not match", C, output[2] );
113   }
114 
115   public void testSetToObjectsSequencialSet() {
116     Set input = new ListOrderedSet();
117     input.add( A );
118     input.add( C );
119     input.add( B );
120     Object[] output = CollectionsHelper.setToObjects( input );    
121     assertNotNull( "array should not be null", output );
122     assertEquals( "array size does not match", 3, output.length );
123     assertEquals( "element 0 does not match", A, output[0] );
124     assertEquals( "element 1 does not match", C, output[1] );
125     assertEquals( "element 2 does not match", B, output[2] );
126   }
127   
128   public void testSetToStringsNullEntry() {
129     Object[] output = CollectionsHelper.setToStrings( null );
130     assertNull( "array should be null", output );
131   }
132   
133   public void testSetToStringsEmptyEntry() {
134     Set input = new HashSet();
135     Object[] output = CollectionsHelper.setToStrings( input );    
136     assertNotNull( "array should not be null", output );
137     assertEquals( "array should be empty", 0, output.length );
138   }
139 
140   public void testSetToStringsSingle() {
141     Set input = new HashSet();
142     input.add( A );
143     String[] output = CollectionsHelper.setToStrings( input );    
144     assertNotNull( "array should not be null", output );
145     assertEquals( "array size does not match", 1, output.length );
146     assertEquals( "element 0 does not match", A, output[0] );
147   }
148 
149   public void testSetToStringsOrderedSet() {
150     Set input = new TreeSet();
151     input.add( A );
152     input.add( C );
153     input.add( B );
154     String[] output = CollectionsHelper.setToStrings( input );    
155     assertNotNull( "array should not be null", output );
156     assertEquals( "array size does not match", 3, output.length );
157     assertEquals( "element 0 does not match", A, output[0] );
158     assertEquals( "element 1 does not match", B, output[1] );
159     assertEquals( "element 2 does not match", C, output[2] );
160   }
161 
162   public void testSetToStringsSequencialSet() {
163     Set input = new ListOrderedSet();
164     input.add( A );
165     input.add( C );
166     input.add( B );
167     String[] output = CollectionsHelper.setToStrings( input );    
168     assertNotNull( "array should not be null", output );
169     assertEquals( "array size does not match", 3, output.length );
170     assertEquals( "element 0 does not match", A, output[0] );
171     assertEquals( "element 1 does not match", C, output[1] );
172     assertEquals( "element 2 does not match", B, output[2] );
173   }
174 
175 
176 }