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 34 import org.objectweb.cjdbc.common.sql.DropRequest; 35 import org.objectweb.cjdbc.common.sql.ParsingGranularities; 36 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable; 37 import org.objectweb.cjdbc.scenario.templates.NoTemplate; 38 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase; 39 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase; 40 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader; 41 42 45 public class DropRequestTest extends NoTemplate 46 { 47 48 private static final String RUBIS_DROP_REQUESTS_FILE = getTextPath("RUBiS-drop-requests.txt"); 49 50 51 private AbstractDatabase database; 52 53 54 private ArrayList results; 55 56 static boolean inited = false; 57 58 61 protected void setUp() 62 { 63 64 synchronized (this) 65 { 66 if (inited) 67 return; 68 database = new RUBiSDatabase(); 69 results = new ArrayList (); 70 String request = null, tableName, errorMessage; 71 72 try 73 { 74 File file = new File (RUBIS_DROP_REQUESTS_FILE); 75 MyBufferedReader in = new MyBufferedReader(new FileReader (file), 76 "requests"); 77 78 String line; 79 while ((line = in.readLine()) != null) 80 { 81 if (line.trim().equals("") || line.startsWith("//")) 82 continue; 83 84 request = null; 86 request = in.readSQLRequest(line); 87 88 if (in.readBoolean()) 90 { 91 tableName = in.readString("table name"); 93 results.add(new ParsingResult(request, true, tableName)); 94 } 95 else 96 { 97 errorMessage = in.readString("error message"); 99 results.add(new ParsingResult(request, false, errorMessage)); 100 } 101 } 102 } 103 catch (IOException e) 104 { 105 String error = "An error occurs while parsing requests file: " + e; 106 if (request != null) 107 error += " (request: '" + request + "')"; 108 fail(error); 109 } 110 inited = true; 111 } 112 113 } 114 115 119 public void testParse() 120 { 121 Iterator it = results.iterator(); 122 while (it.hasNext()) 123 { 124 parse((ParsingResult) it.next(), false); 125 } 126 } 127 128 135 private void parse(ParsingResult result, boolean isCaseSensitive) 136 { 137 String sql = result.request.toLowerCase().trim(); 138 DropRequest req = null; 139 try 140 { 141 req = new DropRequest(sql, false, 0, 142 System.getProperty("line.separator"), database.getSchema(), 143 ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive); 144 } 145 catch (SQLException e) 146 { 147 if (result.isValid) 148 { 149 fail("Exception thrown with valid request '" + result.request + "' (" 150 + e + ")"); 151 } 152 else 153 { 154 assertEquals( 155 "Incorrect error message found while parsing this DELETE statement: '" 156 + result.request + "'", result.errorMessage, e.getMessage()); 157 return; 158 } 159 } 160 161 if (!result.isValid) 162 fail("SQLException not thrown with invalid request: '" + result.request 163 + "'"); 164 else 165 { 166 assertEquals("Incorrect table found", req.getTableName(), result.table 168 .getName()); 169 } 170 } 171 172 179 protected class ParsingResult 180 { 181 182 protected String request; 183 184 185 protected boolean isValid; 186 187 188 protected DatabaseTable table; 189 190 191 protected String errorMessage; 192 193 201 protected ParsingResult(String request, boolean isValid, String s) 202 { 203 this.request = request; 204 this.isValid = isValid; 205 206 if (isValid) 207 { 208 table = database.getSchema().getTable(s); 209 if (table == null) 210 fail("Possible syntax error in sql requests file: '" + s 211 + "' not found in database schema"); 212 } 213 else 214 { 215 errorMessage = s; 216 } 217 } 218 } 219 }
| Popular Tags
|