1 21 22 package org.dbunit.database; 23 24 import org.dbunit.DatabaseEnvironment; 25 import org.dbunit.dataset.AbstractDataSetTest; 26 import org.dbunit.dataset.IDataSet; 27 import org.dbunit.dataset.ITable; 28 import org.dbunit.dataset.NoSuchColumnException; 29 import org.dbunit.operation.DatabaseOperation; 30 31 36 public class QueryDataSetTest extends AbstractDataSetTest 37 { 38 private IDatabaseConnection _connection; 39 40 public QueryDataSetTest(String s) 41 { 42 super(s); 43 } 44 45 48 protected void setUp() throws Exception 49 { 50 super.setUp(); 51 52 DatabaseEnvironment env = DatabaseEnvironment.getInstance(); 53 _connection = env.getConnection(); 54 55 DatabaseOperation.CLEAN_INSERT.execute(_connection, env.getInitDataSet()); 56 } 57 58 protected void tearDown() throws Exception 59 { 60 super.tearDown(); 61 62 _connection = null; 63 } 64 65 68 protected String [] getExpectedNames() throws Exception 69 { 70 return getExpectedLowerNames(); 71 } 72 73 protected IDataSet createDataSet() throws Exception 74 { 75 String [] names = getExpectedNames(); 76 77 QueryDataSet dataSet = new QueryDataSet(_connection); 78 for (int i = 0; i < names.length; i++) 79 { 80 String name = names[i]; 81 String query = "select * from " + name; 82 dataSet.addTable(name, query); 83 94 } 95 return dataSet; 96 } 97 98 protected IDataSet createDuplicateDataSet() throws Exception 99 { 100 QueryDataSet dataSet = new QueryDataSet(_connection); 101 String [] names = getExpectedDuplicateNames(); 102 103 String queryOneRow = "select * from only_pk_table"; 105 dataSet.addTable(names[0], queryOneRow); 106 107 String queryNoRow = "select * from empty_table"; 109 dataSet.addTable(names[1], queryNoRow); 110 111 String queryTwoRow = "select * from pk_table where PK0=0 or PK0=1"; 113 dataSet.addTable(names[2], queryTwoRow); 114 115 return dataSet; 116 } 117 118 121 public void testGetSelectPartialData() throws Exception 122 { 123 124 QueryDataSet ptds = new QueryDataSet(_connection); 125 ptds.addTable("PK_TABLE", "SELECT PK0, PK1 FROM pk_table where PK0 = 0"); 126 127 ITable table = ptds.getTable("PK_TABLE"); 128 assertEquals("", "0", table.getValue(0, "PK0").toString()); 129 assertEquals("", "1", new String (table.getRowCount() + "")); 130 131 } 132 133 public void testGetAllColumnsWithStar() throws Exception 134 { 135 136 QueryDataSet ptds = new QueryDataSet(_connection); 137 ptds.addTable("PK_TABLE", "SELECT * FROM pk_table where PK0 = 0"); 138 139 ITable table = ptds.getTable("PK_TABLE"); 140 assertEquals("", "0", table.getValue(0, "PK0").toString()); 141 assertEquals("", "1", new String (table.getRowCount() + "")); 142 143 } 144 145 public void testGetAllRowsSingleColumn() throws Exception 146 { 147 148 QueryDataSet ptds = new QueryDataSet(_connection); 149 ptds.addTable("PK_TABLE", "SELECT PK0 FROM pk_table"); 150 151 ITable table = ptds.getTable("PK_TABLE"); 152 assertEquals("", "0", table.getValue(0, "PK0").toString()); 153 assertEquals("", "3", new String (table.getRowCount() + "")); 154 } 155 156 157 public void testOnlySpecifiedColumnsReturned() throws Exception 158 { 159 160 QueryDataSet ptds = new QueryDataSet(_connection); 161 ptds.addTable("PK_TABLE", "SELECT PK0 FROM pk_table"); 162 163 ITable table = ptds.getTable("PK_TABLE"); 164 assertEquals("", "0", table.getValue(0, "PK0").toString()); 165 166 try 167 { 168 table.getValue(0, "PK1").toString(); 169 fail("Should not have reached here, we should have thrown a NoSuchColumnException"); 170 } 171 catch (NoSuchColumnException nsce) 172 { 173 String errorMsg = "org.dbunit.dataset.NoSuchColumnException: PK_TABLE.PK1"; 174 assertTrue("Find text:" + errorMsg, nsce.toString().indexOf(errorMsg) >= 0); 175 } 176 } 177 178 public void testGetSelectPartialData2() throws Exception 179 { 180 181 QueryDataSet ptds = new QueryDataSet(_connection); 182 ptds.addTable("SECOND_TABLE", 183 "SELECT * FROM second_table where COLUMN0='row 0 col 0'"); 184 185 ITable table = ptds.getTable("SECOND_TABLE"); 186 assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString()); 187 assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString()); 188 assertEquals("", "1", new String (table.getRowCount() + "")); 189 190 } 191 192 public void testCombinedWhere() throws Exception 193 { 194 195 QueryDataSet ptds = new QueryDataSet(_connection); 196 ptds.addTable("SECOND_TABLE", 197 "SELECT COLUMN0, COLUMN3 FROM second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'"); 198 199 ITable table = ptds.getTable("SECOND_TABLE"); 200 assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString()); 201 assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString()); 202 assertEquals("", "1", new String (table.getRowCount() + "")); 203 204 } 205 206 public void testMultipleTables() throws Exception 207 { 208 ITable table = null; 209 210 QueryDataSet ptds = new QueryDataSet(_connection); 211 ptds.addTable("SECOND_TABLE", 212 "SELECT * from second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'"); 213 ptds.addTable("PK_TABLE", 214 "SELECT * FROM pk_table where PK0 = 0"); 215 216 table = ptds.getTable("SECOND_TABLE"); 217 assertEquals("", "row 0 col 0", table.getValue(0, "COLUMN0").toString()); 218 assertEquals("", "row 0 col 3", table.getValue(0, "COLUMN3").toString()); 219 assertEquals("", "1", new String (table.getRowCount() + "")); 220 221 table = ptds.getTable("PK_TABLE"); 222 assertEquals("", "0", table.getValue(0, "PK0").toString()); 223 assertEquals("", "1", new String (table.getRowCount() + "")); 224 225 } 226 227 public void testMultipleTablesWithMissingWhere() throws Exception 228 { 229 QueryDataSet ptds = new QueryDataSet(_connection); 230 ptds.addTable("SECOND_TABLE", 231 "SELECT * from second_table where COLUMN0='row 0 col 0' and COLUMN2='row 0 col 2'"); 232 ptds.addTable("PK_TABLE", null); 233 } 234 } 235 236 237 238 239 240 241 242 243 244 245 246 | Popular Tags |