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.HashMap ; 35 import java.util.Iterator ; 36 import java.util.StringTokenizer ; 37 38 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 39 import org.objectweb.cjdbc.common.sql.UpdateRequest; 40 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 41 import org.objectweb.cjdbc.common.sql.schema.TableColumn; 42 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 43 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase; 44 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase; 45 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 46 47 51 public class UpdateRequestTest extends NoTemplate 52 { 53 54 private static final String RUBIS_UPDATE_REQUESTS_FILE = getTextPath("RUBiS-update-requests.txt"); 55 56 57 private static final String EMPTY_VALUE = "null"; 58 59 60 private AbstractDatabase database; 61 62 63 private ArrayList results; 64 65 static boolean inited = false; 66 67 70 protected void setUp() 71 { 72 synchronized (this) 73 { 74 if (inited) 75 return; 76 77 database = new RUBiSDatabase(); 78 results = new ArrayList (); 79 80 String request = null, tableName, columnList, updatedValues, updatedPk, errorMessage; 81 boolean isUnique = false; 82 83 try 84 { 85 File file = new File (RUBIS_UPDATE_REQUESTS_FILE); 86 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 87 "requests"); 88 89 String line; 90 while ((line = in.readLine()) != null) 91 { 92 if (line.trim().equals("") || line.startsWith("//")) 93 continue; 94 95 request = null; 97 request = in.readSQLRequest(line); 98 99 if (in.readBoolean()) 101 { 102 tableName = in.readString("table name"); 104 columnList = in.readString("column list"); 105 106 isUnique = in.readBoolean(); 107 108 updatedValues = in.readString("updated values"); 109 if (updatedValues.equals(EMPTY_VALUE)) 110 updatedValues = null; 111 112 updatedPk = in.readString("updated pk"); 113 if (updatedPk.equals(EMPTY_VALUE)) 114 updatedPk = null; 115 116 results.add(new ParsingResult(request, tableName, columnList, 117 isUnique, updatedValues, updatedPk)); 118 } 119 else 120 { 121 errorMessage = in.readString("errorMessage"); 123 results.add(new ParsingResult(request, errorMessage)); 124 } 125 } 126 } 127 catch (IOException e) 128 { 129 String error = "An error occurs while parsing requests file: " + e; 130 if (request != null) 131 error += " (request: '" + request + "')"; 132 fail(error); 133 } 134 inited = true; 135 } 136 } 137 138 142 public void testParse() 143 { 144 Iterator it = results.iterator(); 145 while (it.hasNext()) 146 { 147 parse((ParsingResult) it.next(), false); 148 } 149 } 150 151 158 private void parse(ParsingResult result, boolean isCaseSensitive) 159 { 160 String sql = result.request.toLowerCase().trim(); 161 System.out.println("Parsing:"+sql); 162 UpdateRequest req = null; 163 try 164 { 165 req = new UpdateRequest(sql, false, 0, System 166 .getProperty("line.separator"), database.getSchema(), 167 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive); 168 } 169 catch (SQLException e) 170 { 171 if (result.isValid) 172 { 173 fail("Exception thrown with valid request '" + result.request + "' (" 174 + e + ")"); 175 } 176 else 177 { 178 assertEquals( 179 "Incorrect error message found while parsing this DELETE statement: '" 180 + result.request + "'", result.errorMessage, e.getMessage()); 181 return; 182 } 183 } 184 185 if (!result.isValid) 186 fail("SQLException not thrown with invalid request: '" + result.request 187 + "'"); 188 else 189 { 190 assertEquals("Incorrect table found", req.getTableName(), result.table 192 .getName()); 193 194 TableColumn c; 197 Iterator it = result.columns.iterator(); 198 while (it.hasNext()) 199 { 200 c = (TableColumn) it.next(); 201 assertTrue("'" + c.getColumnName() 202 + "' column not selected by parsing for request: '" + sql + "'", 203 req.getColumns().contains(c)); 204 } 205 206 assertEquals("Unicity not correct for request: '" + sql + "'", 208 result.isUnique, req.isUnique()); 209 210 if (result.updatedValues != null) 212 { 213 HashMap hash = req.getUpdatedValues(); 214 ArrayList al = new ArrayList (req.getUpdatedValues().keySet()); 215 Object [] obs = al.toArray(); 216 Arrays.sort(obs, new MyComparator()); 217 al = new ArrayList (Arrays.asList(obs)); 218 StringBuffer buf = new StringBuffer (); 219 for (int i = 0; i < al.size(); i++) 220 { 221 if (i != 0) 222 buf.append(","); 223 buf.append(al.get(i) + "=" + hash.get(al.get(i))); 224 } 225 assertEquals("Incorrect updatedValues for request: '" + sql + "'", buf 228 .toString(), result.updatedValues); 229 } 230 else 231 { 232 assertNull("Incorrect not null updatedValues for request: '" + sql 233 + "'", req.getUpdatedValues()); 234 } 235 236 assertEquals("Incorrect updated pk for request: '" + sql + "'", req 239 .getPk(), result.updatedPk); 240 } 241 } 242 243 class MyComparator implements Comparator 244 { 245 248 public int compare(Object o1, Object o2) 249 { 250 if (o1 instanceof TableColumn && o2 instanceof TableColumn) 251 return ((TableColumn) o1).getColumnName().compareTo( 252 ((TableColumn) o2).getColumnName()); 253 else 254 return (o1.toString().compareTo(o2.toString())); 255 } 256 } 257 258 265 protected class ParsingResult 266 { 267 268 protected String request; 269 270 271 protected boolean isValid; 272 273 274 protected DatabaseTable table; 275 276 277 protected ArrayList columns; 278 279 280 protected boolean isUnique; 281 282 283 protected String updatedValues; 284 285 286 protected String updatedPk; 287 288 289 protected String errorMessage; 290 291 301 protected ParsingResult(String request, String tableName, 302 String columnList, boolean isUnique, String updatedValues, 303 String updatedPk) 304 { 305 this.request = request; 306 isValid = true; 307 308 table = database.getSchema().getTable(tableName); 310 if (table == null) 311 fail("Possible syntax error in sql requests file: '" + tableName 312 + "' not found in database schema"); 313 314 columns = new ArrayList (); 316 String columnName; 317 StringTokenizer tokenizer = new StringTokenizer (columnList.trim(), " "); 318 while (tokenizer.hasMoreTokens()) 319 { 320 columnName = tokenizer.nextToken(); 321 if (table.getColumn(columnName) != null) 322 columns.add(new TableColumn(tableName, columnName)); 323 else 324 fail("Possible syntax error in sql requests file: '" + columnName 325 + "' not found in table '" + tableName + "'"); 326 } 327 328 this.isUnique = isUnique; 329 this.updatedValues = (updatedValues == null) ? null : updatedValues 330 .trim(); 331 this.updatedPk = updatedPk; 332 } 333 334 340 protected ParsingResult(String request, String errorMessage) 341 { 342 this.request = request; 343 isValid = false; 344 this.errorMessage = errorMessage; 345 } 346 } 347 } | Popular Tags |