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.InsertRequest; 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 InsertRequestTest extends NoTemplate 51 { 52 53 private static final String RUBIS_INSERT_REQUESTS_FILE = getTextPath("RUBiS-insert-requests.txt"); 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 73 database = new RUBiSDatabase(); 74 results = new ArrayList (); 75 String request = null, tableName, columnList, errorMessage; 76 77 try 78 { 79 File file = new File (RUBIS_INSERT_REQUESTS_FILE); 80 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 81 "requests"); 82 83 String line; 84 while ((line = in.readLine()) != null) 85 { 86 if (line.trim().equals("") || line.startsWith("//")) 87 continue; 88 89 request = null; 91 request = in.readSQLRequest(line); 92 93 if (in.readBoolean()) 95 { 96 tableName = in.readString("table name"); 98 columnList = in.readString("column list"); 99 results.add(new ParsingResult(request, tableName, columnList)); 100 } 101 else 102 { 103 errorMessage = in.readString("error message"); 105 results.add(new ParsingResult(request, errorMessage)); 106 } 107 } 108 } 109 catch (IOException e) 110 { 111 String error = "An error occurs while parsing requests file: " + e; 112 if (request != null) 113 error += " (request: '" + request + "')"; 114 fail(error); 115 } 116 inited = true; 117 } 118 } 119 120 124 public void testParse() 125 { 126 Iterator it = results.iterator(); 127 while (it.hasNext()) 128 { 129 parse((ParsingResult) it.next(), false); 130 } 131 } 132 133 140 private void parse(ParsingResult result, boolean isCaseSensitive) 141 { 142 String sql = result.request.toLowerCase().trim(); 143 InsertRequest req = null; 144 try 145 { 146 req = new InsertRequest(sql, false, 0, System 147 .getProperty("line.separator"), database.getSchema(), 148 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive, false ); 149 } 150 catch (SQLException e) 151 { 152 if (result.isValid) 153 { 154 fail("Exception thrown with valid request '" + result.request + "' (" 155 + e + ")"); 156 } 157 else 158 { 159 assertEquals( 160 "Incorrect error message found while parsing this DELETE statement: '" 161 + result.request + "'", result.errorMessage, e.getMessage()); 162 return; 163 } 164 } 165 166 if (!result.isValid) 167 fail("SQLException not thrown with invalid request: '" + result.request 168 + "'"); 169 else 170 { 171 assertEquals("Incorrect table found", req.getTableName(), result.table 173 .getName()); 174 175 ArrayList al = req.getColumns(); 176 Object [] obs = al.toArray(); 177 Arrays.sort(obs, new MyComparator()); 178 al = new ArrayList (Arrays.asList(obs)); 179 180 req.getColumns(); 181 ArrayList al2 = result.columns; 182 Object [] obs2 = al2.toArray(); 183 Arrays.sort(obs2, new MyComparator()); 184 al2 = new ArrayList (Arrays.asList(obs2)); 185 186 assertEquals(al.toString() + System.getProperty("line.separator") 189 + al2.toString(), al, al2); 190 } 191 } 192 193 199 public ArrayList sortArrayList(ArrayList al) 200 { 201 Object [] obs = al.toArray(); 202 Arrays.sort(obs, new MyComparator()); 203 return new ArrayList (Arrays.asList(obs)); 204 } 205 206 213 public void debugArrayList(String header, ArrayList al) 214 { 215 StringBuffer buf = new StringBuffer (); 216 for (int i = 0; i < al.size(); i++) 217 { 218 if (i != 0) 219 buf.append(","); 220 Object o = al.get(i); 221 if (o instanceof TableColumn) 222 buf.append(((TableColumn) o).getColumnName()); 223 else 224 buf.append(o.toString()); 225 } 226 System.out.println(header + ":" + buf.toString()); 227 } 228 229 class MyComparator implements Comparator 230 { 231 234 public int compare(Object o1, Object o2) 235 { 236 if (o1 instanceof TableColumn && o2 instanceof TableColumn) 237 return ((TableColumn) o1).getColumnName().compareTo( 238 ((TableColumn) o2).getColumnName()); 239 else 240 return (o1.toString().compareTo(o2.toString())); 241 } 242 } 243 244 251 protected class ParsingResult 252 { 253 254 protected String request; 255 256 257 protected boolean isValid; 258 259 260 protected DatabaseTable table; 261 262 266 protected ArrayList columns; 267 268 269 protected String errorMessage; 270 271 278 protected ParsingResult(String request, String tableName, String columnList) 279 { 280 this.request = request; 281 isValid = true; 282 283 table = database.getSchema().getTable(tableName); 285 if (table == null) 286 fail("Possible syntax error in sql requests file: '" + tableName 287 + "' not found in database schema"); 288 289 columns = new ArrayList (); 291 String columnName; 292 StringTokenizer tokenizer = new StringTokenizer (columnList.trim(), " "); 293 while (tokenizer.hasMoreTokens()) 294 { 295 columnName = tokenizer.nextToken(); 296 if (table.getColumn(columnName) != null) 297 columns.add(new TableColumn(tableName, columnName)); 298 else 299 fail("Possible syntax error in sql requests file: '" + columnName 300 + "' not found in table '" + tableName + "'"); 301 } 302 } 303 304 310 protected ParsingResult(String request, String errorMessage) 311 { 312 this.request = request; 313 isValid = false; 314 this.errorMessage = errorMessage; 315 } 316 } 317 } | Popular Tags |