View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, 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  
22  package org.dbunit.dataset;
23  
24  import java.io.File;
25  import java.io.IOException;
26  
27  import org.dbunit.dataset.datatype.DataType;
28  import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
29  import org.dbunit.testutil.TestUtils;
30  
31  /**
32   * @author Manuel Laflamme
33   * @version $Revision$
34   */
35  public class SortedTableTest extends AbstractTableTest
36  {
37  	private File sortedTableTestFile = TestUtils.getFile("xml/sortedTableTest.xml");
38  	
39      public SortedTableTest(String s)
40      {
41          super(s);
42      }
43  
44      protected ITable createTable() throws Exception
45      {
46          return createDataSet().getTable("TEST_TABLE");
47      }
48  
49      protected IDataSet createDataSet() throws Exception
50      {
51          return new SortedDataSet(createUnsortedDataSet());
52      }
53  
54      private IDataSet createUnsortedDataSet() throws DataSetException, IOException 
55      {
56      	return new FlatXmlDataSetBuilder().build(sortedTableTestFile);
57      }
58  
59      private ITable createNumericTable() throws Exception
60      {
61      	// Create a table that has numeric values in the first column
62      	Column[] columns = new Column[]{
63      			new Column("COLUMN0", DataType.NUMERIC),
64      			new Column("COLUMN1", DataType.VARCHAR)
65      	};
66      	DefaultTable table = new DefaultTable("TEST_TABLE", columns);
67      	Object[] row1 = new Object[]{new Integer(9), "row 9"};
68      	Object[] row2 = new Object[]{new Integer(10), "row 10"};
69      	Object[] row3 = new Object[]{new Integer(11), "row 11"};
70      	table.addRow(row1);
71      	table.addRow(row2);
72      	table.addRow(row3);
73      	return table;
74      }
75      
76      
77      public void testSetUseComparableTooLate() throws Exception
78      {
79      	ITable table = createTable();
80      	SortedTable sortedTable = new SortedTable(table);
81      	// access a value to initialize the array
82      	sortedTable.getValue(0, "COLUMN0");
83      	// now set the "useComparable" flag which should fail
84      	try
85      	{
86          	sortedTable.setUseComparable(true);
87          	fail("Should not be able to set 'useComparable' after table has already been in use");
88      	}
89      	catch(IllegalStateException expected)
90      	{
91      		String msgStart = "Do not use this method after the table has been used";
92      		assertTrue("Msg should start with: " + msgStart, expected.getMessage().startsWith(msgStart));
93      	}
94      }
95      
96      
97      public void testSortByComparable() throws Exception
98      {
99      	// Sort by column0 which is a numeric column
100         String columnName = "COLUMN0";
101 
102         ITable table = createNumericTable();
103         SortedTable sortedTable = new SortedTable(table, new String[]{columnName});
104         sortedTable.setUseComparable(true);
105 
106         Column[] columns = sortedTable.getTableMetaData().getColumns();
107         assertEquals("column count", 2, columns.length);
108         assertEquals("row count", 3, sortedTable.getRowCount());
109 
110         Object[] expected = {new Integer(9), new Integer(10), new Integer(11)};
111         for (int i = 0; i < sortedTable.getRowCount(); i++)
112         {
113             assertEquals("value row " + i, expected[i],
114             		sortedTable.getValue(i, columnName));
115         }
116     }
117     
118     /**
119      * Tests the sort by string which is the default behavior
120      * @throws Exception
121      */
122     public void testSortByString() throws Exception
123     {
124     	// Sort by column0 which is a numeric column
125         String columnName = "COLUMN0";
126 
127         ITable table = createNumericTable();
128         SortedTable sortedTable = new SortedTable(table, new String[]{columnName});
129 
130         Column[] columns = sortedTable.getTableMetaData().getColumns();
131         assertEquals("column count", 2, columns.length);
132         assertEquals("row count", 3, sortedTable.getRowCount());
133 
134         Object[] expected = {new Integer(10), new Integer(11), new Integer(9)};
135         for (int i = 0; i < sortedTable.getRowCount(); i++)
136         {
137             assertEquals("value row " + i, expected[i],
138             		sortedTable.getValue(i, columnName));
139         }
140     }
141 
142     
143 	public void testGetMissingValue() throws Exception
144     {
145         String columnName = "COLUMN2";
146         Object[] expected = {null, null, null, "0", "1"};
147 
148         ITable table = createDataSet().getTable("MISSING_VALUES");
149 
150         Column[] columns = table.getTableMetaData().getColumns();
151         assertEquals("column count", 3, columns.length);
152         assertEquals("row count", 5, table.getRowCount());
153         for (int i = 0; i < table.getRowCount(); i++)
154         {
155             assertEquals("value row " + i, expected[i],
156                     table.getValue(i, columnName));
157         }
158     }
159 
160     public void testCustomColumnsWithUnknownColumnName() throws Exception
161     {
162     	String[] sortColumnNames = new String[] {"COLUMN2", "COLUMNXY_UNDEFINED"};
163     	
164         ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
165         try {
166 	        new SortedTable(unsortedTable, sortColumnNames);
167 	        fail("Should not be able to create a SortedTable with unexisting columns");
168         }catch(NoSuchColumnException expected) {
169             assertTrue(expected.getMessage().startsWith("MISSING_VALUES.COLUMNXY_UNDEFINED"));
170         }
171     }
172 
173     public void testCustomColumnsWithUnknownColumn() throws Exception
174     {
175     	Column[] sortColumns = new Column[] {
176     			new Column("COLUMN2", DataType.UNKNOWN, Column.NULLABLE),
177     			new Column("COLUMNXY_UNDEFINED", DataType.UNKNOWN, Column.NULLABLE) 
178 		};
179     	
180         ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
181         try {
182 	        new SortedTable(unsortedTable, sortColumns);
183 	        fail("Should not be able to create a SortedTable with unexisting columns");
184         }catch(NoSuchColumnException expected) {
185         	assertTrue(expected.getMessage().startsWith("MISSING_VALUES.COLUMNXY_UNDEFINED"));
186         }
187     }
188 
189     public void testCustomColumnsWithDifferentColumnTypesButSameName() throws Exception
190     {
191         Column sortColumn = new Column("COLUMN2", DataType.CHAR, Column.NO_NULLS);
192         Column[] sortColumns = new Column[] { sortColumn };
193         // Use different columns (different datatype) in ITableMetaData that have valid column names
194         ITable unsortedTable = createUnsortedDataSet().getTable("MISSING_VALUES");
195         SortedTable sortedTable = new SortedTable(unsortedTable, sortColumns);
196         // Check the results
197         Column actualSortColumn = sortedTable.getSortColumns()[0];
198         // The column actually used for sorting must has some different attributes than the one passed in (dataType, nullable) 
199         assertNotSame(sortColumn, actualSortColumn);
200         assertEquals(DataType.UNKNOWN, actualSortColumn.getDataType());
201         assertEquals("COLUMN2", actualSortColumn.getColumnName());
202         assertEquals(Column.NULLABLE, actualSortColumn.getNullable());
203     }
204 
205 }
206 
207 
208 
209 
210 
211 
212 
213 
214