1 30 31 32 package org.hsqldb.test; 33 34 import java.io.IOException ; 35 import java.sql.Connection ; 36 import java.sql.DriverManager ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Statement ; 40 41 import org.hsqldb.Trace; 42 43 import junit.framework.TestCase; 44 45 50 51 public class TestGroupByHaving extends TestCase { 53 54 private static final String databaseDriver = "org.hsqldb.jdbcDriver"; 58 private static final String databaseURL = "jdbc:hsqldb:mem:."; 59 private static final String databaseUser = "sa"; 60 private static final String databasePassword = ""; 61 62 private Connection conn; 66 private Statement stmt; 67 68 72 75 public TestGroupByHaving(String s) { 76 super(s); 77 } 78 79 protected static Connection getJDBCConnection() throws SQLException { 83 return DriverManager.getConnection(databaseURL, databaseUser, 84 databasePassword); 85 } 86 87 protected void setUp() throws Exception { 88 89 super.setUp(); 90 91 if (conn != null) { 92 return; 93 } 94 95 Class.forName(databaseDriver); 96 97 conn = getJDBCConnection(); 98 stmt = conn.createStatement(); 99 100 try { 103 104 stmt.execute("drop table employee if exists"); 106 } catch (Exception x) {} 107 108 stmt.execute("create table employee(id int, " 109 + "firstname VARCHAR(50), " + "lastname VARCHAR(50), " 110 + "salary decimal(10, 2), " + "superior_id int, " 111 + "CONSTRAINT PK_employee PRIMARY KEY (id), " 112 + "CONSTRAINT FK_superior FOREIGN KEY (superior_id) " 113 + "REFERENCES employee(ID))"); 114 addEmployee(1, "Mike", "Smith", 160000, -1); 115 addEmployee(2, "Mary", "Smith", 140000, -1); 116 117 addEmployee(10, "Joe", "Divis", 50000, 1); 119 addEmployee(11, "Peter", "Mason", 45000, 1); 120 addEmployee(12, "Steve", "Johnson", 40000, 1); 121 addEmployee(13, "Jim", "Hood", 35000, 1); 122 123 addEmployee(20, "Jennifer", "Divis", 60000, 2); 125 addEmployee(21, "Helen", "Mason", 50000, 2); 126 addEmployee(22, "Daisy", "Johnson", 40000, 2); 127 addEmployee(23, "Barbara", "Hood", 30000, 2); 128 } 129 130 protected void tearDown() throws Exception { 131 132 super.tearDown(); 133 134 try { 137 138 stmt.execute("drop table employee if exists"); 140 } catch (Exception x) {} 141 142 if (stmt != null) { 143 stmt.close(); 144 145 stmt = null; 146 } 147 148 if (conn != null) { 149 conn.close(); 150 151 conn = null; 152 } 153 } 154 155 private void addEmployee(int id, String firstName, String lastName, 156 double salary, int superiorId) throws Exception { 157 158 stmt.execute("insert into employee values(" + id + ", '" + firstName 159 + "', '" + lastName + "', " + salary + ", " 160 + (superiorId <= 0 ? "null" 161 : ("" + superiorId)) + ")"); 162 } 163 164 170 public void testAggregatedGroupBy() throws SQLException { 171 172 String sql = "select avg(salary), max(id) from employee " 173 + "group by superior_id " + "order by superior_id " + ""; 174 Object [][] expected = new Object [][] { 175 { 176 new Double (150000), new Integer (2) 177 }, { 178 new Double (42500), new Integer (13) 179 }, { 180 new Double (45000), new Integer (23) 181 }, 182 }; 183 184 compareResults(sql, expected, 0); 185 } 186 187 197 public void testAggregatedGroupByHaving1() throws SQLException { 198 199 String sql = "select avg(salary), max(id) from employee " 200 + "group by superior_id " + "having max(id) > 5 " 201 + "order by superior_id " + ""; 202 Object [][] expected = new Object [][] { 203 { 204 new Double (42500), new Integer (13) 205 }, { 206 new Double (45000), new Integer (23) 207 }, 208 }; 209 210 compareResults(sql, expected, 0); 211 } 212 213 223 public void testAggregatedGroupByHaving2() throws SQLException { 224 225 String sql = "select avg(salary), max(id) from employee " 226 + "group by superior_id " 227 + "having superior_id is not null " 228 + "order by superior_id " + ""; 229 Object [][] expected = new Object [][] { 230 { 231 new Double (42500), new Integer (13) 232 }, { 233 new Double (45000), new Integer (23) 234 }, 235 }; 236 237 compareResults(sql, expected, 0); 238 } 239 240 249 public void testHavingWithoutGroupBy1() throws SQLException { 250 251 String sql = "select avg(salary), max(id) from employee " 252 + "having avg(salary) > 1000 " + ""; 253 Object [][] expected = new Object [][] { 254 { 255 new Double (65000), new Integer (23) 256 }, 257 }; 258 259 compareResults(sql, expected, 0); 260 } 261 262 271 public void testHavingWithoutGroupBy2() throws SQLException { 272 273 String sql = "select avg(salary), max(id) from employee " 274 + "having avg(salary) > 1000000 " + ""; 275 Object [][] expected = new Object [][]{}; 276 277 compareResults(sql, expected, 0); 278 } 279 280 284 public void testInvalidHaving() throws SQLException { 285 286 String sql = "select avg(salary), max(id) from employee " 287 + "group by lastname " 288 + "having (max(id) > 1) and (superior_id > 1) " + ""; 289 Object [][] expected = new Object [][]{}; 290 291 compareResults(sql, expected, -Trace.NOT_IN_AGGREGATE_OR_GROUP_BY); 292 } 293 294 private void compareResults(String sql, Object [][] rows, 298 int errorCode) throws SQLException { 299 300 ResultSet rs = null; 301 302 try { 303 rs = stmt.executeQuery(sql); 304 305 assertTrue("Statement <" + sql + "> \nexpecting error code: " 306 + errorCode, (0 == errorCode)); 307 } catch (SQLException sqlx) { 308 if (sqlx.getErrorCode() != errorCode) { 309 sqlx.printStackTrace(); 310 } 311 312 assertTrue("Statement <" + sql + "> \nthrows wrong error code: " 313 + sqlx.getErrorCode() + " expecting error code: " 314 + errorCode, (sqlx.getErrorCode() == errorCode)); 315 316 return; 317 } 318 319 int rowCount = 0; 320 int colCount = rows.length > 0 ? rows[0].length 321 : 0; 322 323 while (rs.next()) { 324 assertTrue("Statement <" + sql + "> \nreturned too many rows.", 325 (rowCount < rows.length)); 326 327 Object [] columns = rows[rowCount]; 328 329 for (int col = 1, i = 0; i < colCount; i++, col++) { 330 Object result = null; 331 Object expected = columns[i]; 332 333 if (expected == null) { 334 result = rs.getString(col); 335 result = rs.wasNull() ? null 336 : result; 337 } else if (expected instanceof String ) { 338 result = rs.getString(col); 339 } else if (expected instanceof Double ) { 340 result = new Double (rs.getString(col)); 341 } else if (expected instanceof Integer ) { 342 result = new Integer (rs.getInt(col)); 343 } 344 345 assertEquals("Statement <" + sql 346 + "> \nreturned wrong value.", columns[i], 347 result); 348 } 349 350 rowCount++; 351 } 352 353 assertEquals("Statement <" + sql 354 + "> \nreturned wrong number of rows.", rows.length, 355 rowCount); 356 } 357 358 public static void main(String [] args) throws IOException { 362 363 junit.textui.TestRunner.run(TestGroupByHaving.class); 365 } 366 } 367 | Popular Tags |