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.CreateRequest; 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 CreateRequestTest extends NoTemplate 51 { 52 53 private static final String RUBIS_CREATE_REQUESTS_FILE = getTextPath("RUBiS-create-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 testParseCreateRequest() 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 CreateRequest req = null; 148 try 149 { 150 req = new CreateRequest(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 e.printStackTrace(); 159 fail("Exception thrown with valid request '" + result.request + "' (" 160 + e + ")"); 161 } 162 else 163 { 164 assertEquals( 165 "Incorrect error message found while parsing this CREATE statement: '" 166 + result.request + "'", result.errorMessage, e.getMessage()); 167 return; 168 } 169 } 170 171 if (!result.isValid) 172 fail("SQLException not thrown with invalid request: '" + result.request 173 + "'"); 174 else 175 { 176 assertEquals( 177 "Incorrect table name found while parsing this CREATE statement: '" 178 + result.request + "'", result.table.getName(), req 179 .getDatabaseTable().getName()); 180 181 assertEquals( 182 "Incorrect columns found while parsing this CREATE statement: '" 183 + result.request + "'", result.table.getXml(), req 184 .getDatabaseTable().getXml()); 185 186 assertEquals( 187 "Incorrect table found while parsing this CREATE statement: '" 188 + result.request + "'", result.table, req.getDatabaseTable()); 189 } 190 } 191 192 199 protected class ParsingResult 200 { 201 202 protected String request; 203 204 205 protected boolean isValid; 206 207 208 protected DatabaseTable table; 209 210 211 protected String errorMessage; 212 213 221 protected ParsingResult(String request, String tableName, String columnList) 222 { 223 this.request = request; 224 isValid = true; 225 226 table = new DatabaseTable(tableName); 228 229 if (!columnList.equals(EMPTY_VALUE)) 231 { 232 String columnName, s; 233 StringTokenizer tokenizer = new StringTokenizer (columnList.trim(), " "); 234 boolean isUnique = false; 235 int i; 236 while (tokenizer.hasMoreTokens()) 237 { 238 s = tokenizer.nextToken(); 239 i = s.indexOf("."); 240 if (i != -1) 241 { 242 columnName = s.substring(0, i); 243 s = s.substring(i + 1, s.length()); 244 if ("unique".equals(s)) 245 isUnique = true; 246 else 247 fail("Syntax error in sql requests file ('unique' token expected instead of: '" 248 + s + "' for request '" + request + "')"); 249 } 250 else 251 { 252 isUnique = false; 253 columnName = s; 254 } 255 table.addColumn(new DatabaseColumn(columnName, isUnique)); 256 } 257 } 258 } 259 260 266 protected ParsingResult(String request, String errorMessage) 267 { 268 this.request = request; 269 isValid = false; 270 this.errorMessage = errorMessage; 271 } 272 } 273 } | Popular Tags |