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.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Arrays ; 33 import java.util.Comparator ; 34 import java.util.Iterator ; 35 import java.util.StringTokenizer ; 36 37 import org.objectweb.cjdbc.common.sql.DeleteRequest; 38 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 39 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 40 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 41 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 42 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase; 43 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase; 44 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 45 46 50 public class DeleteRequestTest extends NoTemplate 51 { 52 53 public static final String RUBIS_DELETE_REQUESTS_FILE = getTextPath("RUBiS-delete-requests.txt"); 54 55 56 public static final String EMPTY_VALUE = "null"; 57 58 59 private AbstractDatabase database; 60 61 62 private ArrayList results; 63 64 65 private String updatedPk; 66 67 static boolean inited = false; 68 69 72 protected void setUp() 73 { 74 synchronized (this) 75 { 76 if (inited) 77 return; 78 database = new RUBiSDatabase(); 79 results = new ArrayList (); 80 81 String request = null, tableName, columnList, errorMessage; 82 boolean isUnique = false; 83 84 try 85 { 86 File file = new File (RUBIS_DELETE_REQUESTS_FILE); 87 88 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 89 "requests"); 90 91 String line; 92 while ((line = in.readLine()) != null) 93 { 94 if (line.trim().equals("") || line.startsWith("//")) 95 continue; 96 97 request = null; 99 request = in.readSQLRequest(line); 100 101 if (in.readBoolean()) 103 { 104 tableName = in.readString("table name"); 106 columnList = in.readString("column list"); 107 isUnique = in.readBoolean(); 108 updatedPk = in.readString("updated pk"); 109 if (updatedPk.equals(EMPTY_VALUE)) 110 updatedPk = null; 111 112 results.add(new ParsingResult(request, tableName, columnList, 113 isUnique, updatedPk)); 114 } 115 else 116 { 117 errorMessage = in.readString("error message"); 119 results.add(new ParsingResult(request, errorMessage)); 120 } 121 } 122 } 123 catch (IOException e) 124 { 125 String error = "An error occurs while parsing requests file: " + e; 126 if (request != null) 127 error += " (request: '" + request + "')"; 128 fail(error); 129 } 130 inited = true; 131 } 132 133 } 134 135 139 public void testParse() 140 { 141 Iterator it = results.iterator(); 142 while (it.hasNext()) 143 { 144 parse((ParsingResult) it.next(), false); 145 } 146 } 147 148 155 private void parse(ParsingResult result, boolean isCaseSensitive) 156 { 157 String sql = result.request.toLowerCase().trim(); 158 DeleteRequest req = null; 159 try 160 { 161 req = new DeleteRequest(sql, false, 0, System 162 .getProperty("line.separator"), database.getSchema(), 163 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive); 164 } 165 catch (SQLException e) 166 { 167 if (result.isValid) 168 { 169 fail("Exception thrown with valid request '" + result.request + "' (" 170 + e + ")"); 171 } 172 else 173 { 174 assertEquals( 175 "Incorrect error message found while parsing this DELETE statement: '" 176 + result.request + "'", result.errorMessage, e.getMessage()); 177 return; 178 } 179 } 180 181 if (!result.isValid) 182 fail("SQLException not thrown with invalid request: '" + result.request 183 + "'"); 184 else 185 { 186 assertEquals("Incorrect table found", result.table.getName(), req 188 .getTableName()); 189 190 assertEquals(result.updatedPk, req.getPk()); 192 193 } 217 } 218 219 225 public ArrayList sortArrayList(ArrayList al) 226 { 227 Object [] obs = al.toArray(); 228 Arrays.sort(obs, new MyComparator()); 229 return new ArrayList (Arrays.asList(obs)); 230 } 231 232 239 public void debugArrayList(String header, ArrayList al) 240 { 241 StringBuffer buf = new StringBuffer (); 242 for (int i = 0; i < al.size(); i++) 243 { 244 if (i != 0) 245 buf.append(","); 246 Object o = al.get(i); 247 if (o instanceof TableColumn) 248 buf.append(((TableColumn) o).getColumnName()); 249 else 250 buf.append(o.toString()); 251 } 252 } 254 255 class MyComparator implements Comparator 256 { 257 260 public int compare(Object o1, Object o2) 261 { 262 if (o1 instanceof TableColumn && o2 instanceof TableColumn) 263 return ((TableColumn) o1).getColumnName().compareTo( 264 ((TableColumn) o2).getColumnName()); 265 else 266 return (o1.toString().compareTo(o2.toString())); 267 } 268 } 269 270 277 protected class ParsingResult 278 { 279 280 protected String request; 281 282 283 protected boolean isValid; 284 285 286 protected DatabaseTable table; 287 288 289 protected String errorMessage; 290 291 292 protected ArrayList columns; 293 294 295 protected boolean isUnique; 296 297 298 protected String updatedPk; 299 300 309 protected ParsingResult(String request, String tableName, 310 String columnList, boolean isUnique, String updatedPk) 311 { 312 this.request = request; 313 isValid = true; 314 315 table = database.getSchema().getTable(tableName); 317 if (table == null) 318 fail("Possible syntax error in sql requests file: '" + tableName 319 + "' not found in database schema"); 320 321 columns = new ArrayList (); 323 String columnName; 324 StringTokenizer tokenizer = new StringTokenizer (columnList.trim(), " "); 325 while (tokenizer.hasMoreTokens()) 326 { 327 columnName = tokenizer.nextToken(); 328 if (table.getColumn(columnName) != null) 329 columns.add(new TableColumn(tableName, columnName)); 330 else 331 fail("Possible syntax error in sql requests file: '" + columnName 332 + "' not found in table '" + tableName + "'"); 333 } 334 335 this.isUnique = isUnique; 336 this.updatedPk = updatedPk; 337 } 338 339 345 protected ParsingResult(String request, String errorMessage) 346 { 347 this.request = request; 348 isValid = false; 349 this.errorMessage = errorMessage; 350 } 351 } 352 } | Popular Tags |