1 24 25 package org.objectweb.cjdbc.scenario.standalone.sql.request; 26 27 import java.io.File ; 28 import java.io.FileReader ; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.StringTokenizer ; 33 34 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 35 import org.objectweb.cjdbc.common.sql.RequestType; 36 import org.objectweb.cjdbc.common.sql.SelectRequest; 37 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 38 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 39 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase; 40 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase; 41 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 42 43 47 public class SelectRequestTest extends NoTemplate 48 { 49 50 private static final String RUBIS_SELECT_REQUESTS_FILE = getTextPath("RUBiS-select-requests.txt"); 51 52 53 private static final String EMPTY_VALUE = "null"; 54 55 56 private AbstractDatabase database; 57 58 59 private ArrayList results; 60 61 static boolean inited = false; 62 63 66 protected void setUp() 67 { 68 synchronized (this) 69 { 70 if (inited) 71 return; 72 database = new RUBiSDatabase(); 73 results = new ArrayList (); 74 75 String request = null, selectColumns, fromTables, whereColumns, errorMessage; 76 int requestType = 0; 77 78 try 79 { 80 File file = new File (RUBIS_SELECT_REQUESTS_FILE); 81 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 82 "requests"); 83 84 String line; 85 while ((line = in.readLine()) != null) 86 { 87 if (line.trim().equals("") || line.startsWith("//")) 88 continue; 89 90 request = null; 92 request = in.readSQLRequest(line); 93 94 if (in.readBoolean()) 96 { 97 selectColumns = in.readString("columns selected in SELECT clause"); 99 fromTables = in.readString("tables selected in FROM clause"); 100 whereColumns = in.readString("columns selected in where clause"); 101 102 if (selectColumns.equals(EMPTY_VALUE)) 103 selectColumns = ""; 104 if (fromTables.equals(EMPTY_VALUE)) 105 fail("Syntax error in requests file (tables selected in FROM clause missing for request '" 106 + request + "')"); 107 if (whereColumns.equals(EMPTY_VALUE)) 108 whereColumns = ""; 109 110 line = in.readLine(); 111 if ("CACHEABLE".equals(line)) 112 requestType = RequestType.CACHEABLE; 113 else if ("UNCACHEABLE".equals(line)) 114 requestType = RequestType.UNCACHEABLE; 115 else if ("UNIQUE_CACHEABLE".equals(line)) 116 requestType = RequestType.UNIQUE_CACHEABLE; 117 else 118 fail("Syntax error in requests file (unknow request type: '" 119 + requestType + "')"); 120 121 results.add(new ParsingResult(request, selectColumns, fromTables, 122 whereColumns, requestType)); 123 } 124 else 125 { 126 errorMessage = in.readString("error message"); 128 results.add(new ParsingResult(request, errorMessage)); 129 } 130 131 } 132 } 133 catch (IOException e) 134 { 135 String error = "An error occurs while parsing requests file: " + e; 136 if (request != null) 137 error += " (request: '" + request + "')"; 138 fail(error); 139 } 140 inited = true; 141 } 142 } 143 144 148 public void testParse() 149 { 150 Iterator it = results.iterator(); 151 int testCount = 0; 152 while (it.hasNext()) 153 { 154 testCount++; 155 parse((ParsingResult) it.next(), false); 157 158 } 159 } 160 161 private void parse(ParsingResult result, boolean isCaseSensitive) 162 { 163 String sql = result.request.toLowerCase().trim(); 164 165 SelectRequest req = null; 166 try 167 { 168 req = new SelectRequest(sql, false, 0, System 169 .getProperty("line.separator"), database.getSchema(), 170 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive); 171 172 } 174 catch (Exception e) 175 { 176 e.printStackTrace(); 177 if (result.isValid) 178 { 179 fail("Exception thrown with valid request '" + result.request + "' (" 180 + e + ")"); 181 } 182 else 183 { 184 assertEquals( 185 "Incorrect error message found while parsing this DELETE statement: '" 186 + result.request + "'", result.errorMessage, e.getMessage()); 187 return; 188 } 189 } 190 191 if (!result.isValid) 192 fail("SQLException not thrown with invalid request: '" + result.request 193 + "'"); 194 else 195 { 196 199 Iterator it; 200 TableColumn c; 201 String s; 202 203 it = result.select.iterator(); 205 while (it.hasNext()) 206 { 207 c = (TableColumn) it.next(); 208 assertTrue("'" + c.getColumnName() 209 + "' column not selected by parsing for request: '" + sql + "'", 210 req.getSelect().contains(c)); 211 } 212 213 it = result.from.iterator(); 215 while (it.hasNext()) 216 { 217 s = (String ) it.next(); 218 assertTrue("'" + s + "' table not selected by parsing for request: '" 219 + sql + "'", req.getFrom().contains(s)); 220 } 221 222 it = result.where.iterator(); 224 while (it.hasNext()) 225 { 226 c = (TableColumn) it.next(); 227 assertTrue("'" + c.getColumnName() 230 + "' column not selected by parsing for request: '" + sql + "'", 231 req.getWhere().contains(c)); 232 } 233 234 assertEquals("Incorrect request type for request '" + sql + "'", 238 result.requestType, req.getCacheAbility()); 239 } 240 } 241 242 249 protected class ParsingResult 250 { 251 252 protected String request; 253 254 255 protected boolean isValid; 256 257 261 protected ArrayList select; 262 263 267 protected ArrayList from; 268 269 273 protected ArrayList where; 274 275 278 protected int requestType; 279 280 281 protected String errorMessage; 282 283 293 protected ParsingResult(String request, String selectColumns, 294 String fromTables, String whereColumns, int requestType) 295 { 296 StringTokenizer tokenizer; 297 String s, tableName, columnName; 298 int i; 299 300 this.request = request; 301 isValid = true; 302 303 select = new ArrayList (); 305 tokenizer = new StringTokenizer (selectColumns.trim(), " "); 306 while (tokenizer.hasMoreTokens()) 307 { 308 s = tokenizer.nextToken(); 309 i = s.indexOf("."); 310 if (i == -1) 311 fail("Syntax error in sql requests file: . needed in " + s); 312 313 tableName = s.substring(0, i); 314 columnName = s.substring(i + 1, s.length()); 315 316 if (database.getSchema().getTable(tableName).getColumn(columnName) != null) 317 { 318 select.add(new TableColumn(tableName, columnName)); 319 } 320 else 321 { 322 if (database.getSchema().getTable(tableName) != null) 323 fail("Possible syntax error in sql requests file: '" + columnName 324 + "' not found in table " + tableName); 325 else 326 fail("Possible syntax error in sql requests file: '" + tableName 327 + "' not found in database schema"); 328 } 329 } 330 331 from = new ArrayList (); 333 tokenizer = new StringTokenizer (fromTables.trim(), " "); 334 while (tokenizer.hasMoreTokens()) 335 { 336 tableName = tokenizer.nextToken(); 337 338 if (database.getSchema().getTable(tableName) == null) 339 fail(tableName + " not found in database schema"); 340 else 341 from.add(tableName); 342 } 343 344 where = new ArrayList (); 346 tokenizer = new StringTokenizer (whereColumns.trim(), " "); 347 while (tokenizer.hasMoreTokens()) 348 { 349 s = tokenizer.nextToken(); 350 i = s.indexOf("."); 351 if (i == -1) 352 fail("Syntax error in sql requests file: . needed in " + s); 353 354 tableName = s.substring(0, i); 355 columnName = s.substring(i + 1, s.length()); 356 357 if (database.getSchema().getTable(tableName).getColumn(columnName) != null) 358 { 359 where.add(new TableColumn(tableName, columnName)); 360 } 361 else 362 { 363 if (database.getSchema().getTable(tableName) != null) 364 fail("Possible syntax error in sql requests file: '" + columnName 365 + "' not found in table '" + tableName + "'"); 366 else 367 fail("Possible syntax error in sql requests file: '" + tableName 368 + "' not found in database schema"); 369 } 370 } 371 372 this.requestType = requestType; 373 } 374 375 381 protected ParsingResult(String request, String errorMessage) 382 { 383 this.request = request; 384 isValid = false; 385 this.errorMessage = errorMessage; 386 } 387 } 388 } | Popular Tags |