1 21 22 package org.dbunit.database; 23 24 import org.dbunit.AbstractDatabaseTest; 25 import org.dbunit.dataset.*; 26 import org.dbunit.dataset.datatype.DataType; 27 28 33 public class DatabaseTableMetaDataTest extends AbstractDatabaseTest 34 { 35 public DatabaseTableMetaDataTest(String s) 36 { 37 super(s); 38 } 39 40 protected IDataSet createDataSet() throws Exception 41 { 42 return _connection.createDataSet(); 43 } 44 45 public void testGetPrimaryKeys() throws Exception 46 { 47 String tableName = "PK_TABLE"; 48 String [] expected = {"PK0", "PK1", "PK2"}; 50 51 ITableMetaData metaData = createDataSet().getTableMetaData(tableName); 52 Column[] columns = metaData.getPrimaryKeys(); 53 54 assertEquals("pk count", expected.length, columns.length); 55 for (int i = 0; i < columns.length; i++) 56 { 57 Column column = columns[i]; 58 assertEquals("name", expected[i], column.getColumnName()); 59 } 60 } 61 62 public void testGetNoPrimaryKeys() throws Exception 63 { 64 String tableName = "TEST_TABLE"; 65 66 ITableMetaData metaData = createDataSet().getTableMetaData(tableName); 67 Column[] columns = metaData.getPrimaryKeys(); 68 69 assertEquals("pk count", 0, columns.length); 70 } 71 72 public void testGetNoColumns() throws Exception 73 { 74 String tableName = "UNKNOWN_TABLE"; 75 76 ITableMetaData metaData = new DatabaseTableMetaData(tableName, 77 getConnection()); 78 try 79 { 80 metaData.getColumns(); 81 fail("Should not be here!"); 82 } 83 catch (NoColumnsFoundException e) 84 { 85 } 86 87 try 89 { 90 metaData.getColumns(); 91 fail("Should not be here!"); 92 } 93 catch (NoColumnsFoundException e) 94 { 95 } 96 } 97 98 public void testColumnIsNullable() throws Exception 99 { 100 String tableName = "PK_TABLE"; 101 String [] notNullable = {"PK0", "PK1", "PK2"}; 102 String [] nullable = {"NORMAL0", "NORMAL1"}; 103 104 ITableMetaData metaData = createDataSet().getTableMetaData(tableName); 105 Column[] columns = metaData.getColumns(); 106 107 assertEquals("column count", nullable.length + notNullable.length, 108 columns.length); 109 110 for (int i = 0; i < notNullable.length; i++) 112 { 113 Column column = DataSetUtils.getColumn(notNullable[i], columns); 114 assertEquals(notNullable[i], Column.NO_NULLS, column.getNullable()); 115 } 116 117 for (int i = 0; i < nullable.length; i++) 119 { 120 Column column = DataSetUtils.getColumn(nullable[i], columns); 121 assertEquals(nullable[i], Column.NULLABLE, column.getNullable()); 122 } 123 } 124 125 147 public void testColumnDataType() throws Exception 148 { 149 String tableName = "EMPTY_MULTITYPE_TABLE"; 150 String [] expectedNames = { 151 "VARCHAR_COL", 152 "NUMERIC_COL", 153 "TIMESTAMP_COL", 154 "VARBINARY_COL", 155 }; 156 DataType[] expectedTypes = { 157 DataType.VARCHAR, 158 DataType.NUMERIC, 159 DataType.TIMESTAMP, 160 DataType.VARBINARY, 161 }; 162 163 ITableMetaData metaData = createDataSet().getTableMetaData(tableName); 164 Column[] columns = metaData.getColumns(); 165 166 assertEquals("expected columns", expectedNames.length, expectedTypes.length); 167 assertEquals("column count", expectedNames.length, columns.length); 168 169 for (int i = 0; i < columns.length; i++) 170 { 171 Column column = columns[i]; 172 assertEquals("name", expectedNames[i], column.getColumnName()); 173 assertEquals("datatype", expectedTypes[i], column.getDataType()); 174 } 175 } 176 } 177 178 179 180 181 182 183 184 | Popular Tags |