1 21 22 package org.dbunit.database; 23 24 import org.dbunit.DatabaseEnvironment; 25 import org.dbunit.dataset.*; 26 import org.dbunit.dataset.filter.DefaultColumnFilter; 27 import org.dbunit.dataset.datatype.DataType; 28 29 34 public class DatabaseDataSetTest extends AbstractDataSetTest 35 { 36 private IDatabaseConnection _connection; 37 38 public DatabaseDataSetTest(String s) 39 { 40 super(s); 41 } 42 43 46 protected void setUp() throws Exception 47 { 48 super.setUp(); 49 50 _connection = DatabaseEnvironment.getInstance().getConnection(); 51 } 52 53 protected void tearDown() throws Exception 54 { 55 super.tearDown(); 56 57 _connection = null; 58 } 59 60 63 protected IDataSet createDataSet() throws Exception 64 { 65 return _connection.createDataSet(); 66 } 67 68 protected String [] getExpectedNames() throws Exception 69 { 70 return _connection.createDataSet().getTableNames(); 71 } 72 73 protected IDataSet createDuplicateDataSet() throws Exception 74 { 75 throw new UnsupportedOperationException (); 76 } 77 78 81 public void testGetSelectStatement() throws Exception 82 { 83 String schemaName = "schema"; 84 String tableName = "table"; 85 Column[] columns = new Column[]{ 86 new Column("c1", DataType.UNKNOWN), 87 new Column("c2", DataType.UNKNOWN), 88 new Column("c3", DataType.UNKNOWN), 89 }; 90 String expected = "select c1, c2, c3 from schema.table"; 91 92 ITableMetaData metaData = new DefaultTableMetaData(tableName, columns); 93 String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null); 94 assertEquals("select statement", expected, sql); 95 } 96 97 public void testGetSelectStatementWithEscapedNames() throws Exception 98 { 99 String schemaName = "schema"; 100 String tableName = "table"; 101 Column[] columns = new Column[]{ 102 new Column("c1", DataType.UNKNOWN), 103 new Column("c2", DataType.UNKNOWN), 104 new Column("c3", DataType.UNKNOWN), 105 }; 106 String expected = "select 'c1', 'c2', 'c3' from 'schema'.'table'"; 107 108 ITableMetaData metaData = new DefaultTableMetaData(tableName, columns); 109 String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, "'?'"); 110 assertEquals("select statement", expected, sql); 111 } 112 113 public void testGetSelectStatementWithPrimaryKeys() throws Exception 114 { 115 String schemaName = "schema"; 116 String tableName = "table"; 117 Column[] columns = new Column[]{ 118 new Column("c1", DataType.UNKNOWN), 119 new Column("c2", DataType.UNKNOWN), 120 new Column("c3", DataType.UNKNOWN), 121 }; 122 String expected = "select c1, c2, c3 from schema.table order by c1, c2, c3"; 123 124 ITableMetaData metaData = new DefaultTableMetaData(tableName, columns, columns); 125 String sql = DatabaseDataSet.getSelectStatement(schemaName, metaData, null); 126 assertEquals("select statement", expected, sql); 127 } 128 129 public void testGetQualifiedTableNames() throws Exception 130 { 131 String [] expectedNames = getExpectedNames(); 132 133 IDatabaseConnection connection = new DatabaseConnection( 134 _connection.getConnection(), _connection.getSchema()); 135 connection.getConfig().setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true); 136 137 IDataSet dataSet = connection.createDataSet(); 138 String [] actualNames = dataSet.getTableNames(); 139 140 assertEquals("name count", expectedNames.length, actualNames.length); 141 for (int i = 0; i < actualNames.length; i++) 142 { 143 String expected = DataSetUtils.getQualifiedName( 144 _connection.getSchema(), expectedNames[i]); 145 String actual = actualNames[i]; 146 assertEquals("name", expected, actual); 147 } 148 } 149 150 public void testGetColumnsAndQualifiedNamesEnabled() throws Exception 151 { 152 String tableName = DataSetUtils.getQualifiedName( 153 _connection.getSchema(), "TEST_TABLE"); 154 String [] expected = {"COLUMN0", "COLUMN1", "COLUMN2", "COLUMN3"}; 155 156 IDatabaseConnection connection = new DatabaseConnection( 157 _connection.getConnection(), _connection.getSchema()); 158 connection.getConfig().setFeature( 159 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true); 160 161 ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName); 162 Column[] columns = metaData.getColumns(); 163 164 assertEquals("column count", expected.length, columns.length); 165 for (int i = 0; i < columns.length; i++) 166 { 167 assertEquals("column name", expected[i], columns[i].getColumnName()); 168 } 169 } 170 171 public void testGetPrimaryKeysAndQualifiedNamesEnabled() throws Exception 172 { 173 String tableName = DataSetUtils.getQualifiedName( 174 _connection.getSchema(), "PK_TABLE"); 175 String [] expected = {"PK0", "PK1", "PK2"}; 176 177 IDatabaseConnection connection = new DatabaseConnection( 178 _connection.getConnection(), _connection.getSchema()); 179 connection.getConfig().setFeature( 180 DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true); 181 182 ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName); 183 Column[] columns = metaData.getPrimaryKeys(); 184 185 assertEquals("column count", expected.length, columns.length); 186 for (int i = 0; i < columns.length; i++) 187 { 188 assertEquals("column name", expected[i], columns[i].getColumnName()); 189 } 190 } 191 192 public void testGetPrimaryKeysWithColumnFilters() throws Exception 193 { 194 String tableName = DataSetUtils.getQualifiedName( 195 _connection.getSchema(), "PK_TABLE"); 196 String [] expected = {"PK0", "PK2"}; 197 198 DefaultColumnFilter filter = new DefaultColumnFilter(); 199 filter.includeColumn("PK0"); 200 filter.includeColumn("PK2"); 201 202 IDatabaseConnection connection = new DatabaseConnection( 203 _connection.getConnection(), _connection.getSchema()); 204 connection.getConfig().setProperty( 205 DatabaseConfig.PROPERTY_PRIMARY_KEY_FILTER, filter); 206 207 ITableMetaData metaData = connection.createDataSet().getTableMetaData(tableName); 208 Column[] columns = metaData.getPrimaryKeys(); 209 210 assertEquals("column count", expected.length, columns.length); 211 for (int i = 0; i < columns.length; i++) 212 { 213 assertEquals("column name", expected[i], columns[i].getColumnName()); 214 } 215 } 216 217 223 public void testGetDuplicateTable() throws Exception 224 { 225 } 227 228 public void testGetDuplicateTableMetaData() throws Exception 229 { 230 } 232 233 public void testGetDuplicateTableNames() throws Exception 234 { 235 } 237 238 public void testGetDuplicateTables() throws Exception 239 { 240 } 242 243 public void testGetCaseInsensitiveDuplicateTable() throws Exception 244 { 245 } 247 248 public void testGetCaseInsensitiveDuplicateTableMetaData() throws Exception 249 { 250 } 252 253 public void testReverseIteratorAndDuplicateTable() throws Exception 254 { 255 } 257 258 public void testIteratorAndDuplicateTable() throws Exception 259 { 260 } 262 } 263 264 265 266 267 268 269 270 271 272 273 274 | Popular Tags |