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.Iterator ; 33 import java.util.StringTokenizer ; 34 35 import org.objectweb.cjdbc.common.sql.AlterRequest; 36 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 37 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn; 38 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 39 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 40 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase; 41 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase; 42 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 43 44 50 public class AlterRequestTest extends NoTemplate 51 { 52 53 private static final String RUBIS_CREATE_REQUESTS_FILE = getTextPath("RUBiS-alter-requests.txt"); 54 55 56 private static final String EMPTY_VALUE = "null"; 57 58 59 private AbstractDatabase database; 60 61 62 private ArrayList results; 63 64 private static boolean inited = false; 65 66 69 protected void setUp() 70 { 71 72 synchronized (this) 73 { 74 if (inited) 75 return; 76 database = new RUBiSDatabase(); 77 results = new ArrayList (); 78 String request = null, tableName, columnList, errorMessage; 79 80 try 81 { 82 File file = new File (RUBIS_CREATE_REQUESTS_FILE); 83 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 84 "requests"); 85 86 String line; 87 while ((line = in.readLine()) != null) 88 { 89 if (line.trim().equals("") || line.startsWith("//")) 90 continue; 91 92 request = null; 94 request = in.readSQLRequest(line); 95 96 if (in.readBoolean()) 98 { 99 tableName = in.readString("table name"); 101 columnList = in.readString("column list"); 102 results.add(new ParsingResult(request, tableName, columnList)); 103 } 104 else 105 { 106 errorMessage = in.readString("errorMessage"); 108 results.add(new ParsingResult(request, errorMessage)); 109 } 110 } 111 } 112 catch (IOException e) 113 { 114 String error = "An error occurs while parsing requests file: " + e; 115 if (request != null) 116 error += " (request: '" + request + "')"; 117 fail(error); 118 } 119 inited = true; 120 } 121 122 } 123 124 128 public void testParseAlterRequest() 129 { 130 Iterator it = results.iterator(); 131 while (it.hasNext()) 132 { 133 parse((ParsingResult) it.next(), false); 134 } 135 } 136 137 144 private void parse(ParsingResult result, boolean isCaseSensitive) 145 { 146 String sql = result.request.toLowerCase().trim(); 147 AlterRequest req = null; 148 try 149 { 150 req = new AlterRequest(sql, false, 0, System 151 .getProperty("line.separator"), database.getSchema(), 152 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive); 153 } 154 catch (SQLException e) 155 { 156 if (result.isValid) 157 { 158 fail("Exception thrown with valid request '" + result.request + "' (" 159 + e + ")"); 160 } 161 else 162 { 163 assertEquals( 164 "Incorrect error message found while parsing this ALTER statement: '" 165 + result.request + "'", result.errorMessage, e.getMessage()); 166 return; 167 } 168 } 169 170 if (!result.isValid) 171 fail("SQLException not thrown with invalid request: '" + result.request 172 + "'"); 173 else 174 { 175 assertEquals( 176 "Incorrect table name found while parsing this ALTER statement: '"+ result.request + "'", result.table.getName(), req 177 .getDatabaseTable().getName()); 178 179 assertEquals( 180 "Incorrect table name found while parsing this ALTER statement: '"+ result.request + "'", result.table.getName(), req 181 .getTableName()); 182 183 assertTrue("Incorrect Column while parsing this ALTER statement: '"+ result.request + "'", result.column.equalsIgnoreType(req.getColumn())); 184 185 } 186 } 187 188 195 protected class ParsingResult 196 { 197 198 protected String request; 199 200 201 protected boolean isValid; 202 203 204 protected DatabaseTable table; 205 206 protected DatabaseColumn column; 207 208 209 protected String errorMessage; 210 211 219 protected ParsingResult(String request, String tableName, String columnList) 220 { 221 this.request = request; 222 isValid = true; 223 224 table = database.getSchema().getTable(tableName); 226 227 if (!columnList.equals(EMPTY_VALUE)) 229 { 230 String columnName, s; 231 StringTokenizer tokenizer = new StringTokenizer (columnList.trim(), " "); 232 boolean isUnique = false; 233 int i; 234 while (tokenizer.hasMoreTokens()) 235 { 236 s = tokenizer.nextToken(); 237 i = s.indexOf("."); 238 if (i != -1) 239 { 240 columnName = s.substring(0, i); 241 s = s.substring(i + 1, s.length()); 242 if ("unique".equals(s)) 243 isUnique = true; 244 else 245 fail("Syntax error in sql requests file ('unique' token expected instead of: '" 246 + s + "' for request '" + request + "')"); 247 } 248 else 249 { 250 isUnique = false; 251 columnName = s; 252 } 253 column = new DatabaseColumn(columnName, isUnique); 254 } 255 } 256 } 257 258 264 protected ParsingResult(String request, String errorMessage) 265 { 266 this.request = request; 267 isValid = false; 268 this.errorMessage = errorMessage; 269 } 270 } 271 } | Popular Tags |