KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > standalone > sql > request > SelectRequestTest


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.standalone.sql.request;
26
27 import java.io.File JavaDoc;
28 import java.io.FileReader JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33
34 import org.objectweb.cjdbc.common.sql.ParsingGranularities;
35 import org.objectweb.cjdbc.common.sql.RequestType;
36 import org.objectweb.cjdbc.common.sql.SelectRequest;
37 import org.objectweb.cjdbc.common.sql.schema.TableColumn;
38 import org.objectweb.cjdbc.scenario.templates.NoTemplate;
39 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase;
40 import org.objectweb.cjdbc.scenario.tools.databases.RUBiSDatabase;
41 import org.objectweb.cjdbc.scenario.tools.util.MyBufferedReader;
42
43 /**
44  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
45  * org.objectweb.cjdbc.common.sql..SelectRequest
46  */

47 public class SelectRequestTest extends NoTemplate
48 {
49   /** File name containing the requests to test. */
50   private static final String JavaDoc RUBIS_SELECT_REQUESTS_FILE = getTextPath("RUBiS-select-requests.txt");
51
52   /** Null value to used in the requests file if needed. */
53   private static final String JavaDoc EMPTY_VALUE = "null";
54
55   /** Database on which the requests are performed. */
56   private AbstractDatabase database;
57
58   /** List of <code>ParsingResult</code> objects. */
59   private ArrayList JavaDoc results;
60
61   static boolean inited = false;
62
63   /**
64    * @see junit.framework.TestCase#setUp()
65    */

66   protected void setUp()
67   {
68     synchronized (this)
69     {
70       if (inited)
71         return;
72       database = new RUBiSDatabase();
73       results = new ArrayList JavaDoc();
74
75       String JavaDoc request = null, selectColumns, fromTables, whereColumns, errorMessage;
76       int requestType = 0;
77
78       try
79       {
80         File JavaDoc file = new File JavaDoc(RUBIS_SELECT_REQUESTS_FILE);
81         MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file),
82             "requests");
83
84         String JavaDoc line;
85         while ((line = in.readLine()) != null)
86         {
87           if (line.trim().equals("") || line.startsWith("//"))
88             continue;
89
90           // get the request
91
request = null;
92           request = in.readSQLRequest(line);
93
94           // get expected results for this request
95
if (in.readBoolean())
96           {
97             // valid request
98
selectColumns = in.readString("columns selected in SELECT clause");
99             fromTables = in.readString("tables selected in FROM clause");
100             whereColumns = in.readString("columns selected in where clause");
101
102             if (selectColumns.equals(EMPTY_VALUE))
103               selectColumns = "";
104             if (fromTables.equals(EMPTY_VALUE))
105               fail("Syntax error in requests file (tables selected in FROM clause missing for request '"
106                   + request + "')");
107             if (whereColumns.equals(EMPTY_VALUE))
108               whereColumns = "";
109
110             line = in.readLine();
111             if ("CACHEABLE".equals(line))
112               requestType = RequestType.CACHEABLE;
113             else if ("UNCACHEABLE".equals(line))
114               requestType = RequestType.UNCACHEABLE;
115             else if ("UNIQUE_CACHEABLE".equals(line))
116               requestType = RequestType.UNIQUE_CACHEABLE;
117             else
118               fail("Syntax error in requests file (unknow request type: '"
119                   + requestType + "')");
120
121             results.add(new ParsingResult(request, selectColumns, fromTables,
122                 whereColumns, requestType));
123           }
124           else
125           {
126             // invalid request
127
errorMessage = in.readString("error message");
128             results.add(new ParsingResult(request, errorMessage));
129           }
130
131         }
132       }
133       catch (IOException JavaDoc e)
134       {
135         String JavaDoc error = "An error occurs while parsing requests file: " + e;
136         if (request != null)
137           error += " (request: '" + request + "')";
138         fail(error);
139       }
140       inited = true;
141     }
142   }
143
144   /**
145    * org.objectweb.cjdbc.common.sql.SelectRequest#parse(DatabaseSchema, int,
146    * boolean)
147    */

148   public void testParse()
149   {
150     Iterator JavaDoc it = results.iterator();
151     int testCount = 0;
152     while (it.hasNext())
153     {
154       testCount++;
155       //System.out.println("Test:" + testCount);
156
parse((ParsingResult) it.next(), false);
157
158     }
159   }
160
161   private void parse(ParsingResult result, boolean isCaseSensitive)
162   {
163     String JavaDoc sql = result.request.toLowerCase().trim();
164
165     SelectRequest req = null;
166     try
167     {
168       req = new SelectRequest(sql, false, 0, System
169           .getProperty("line.separator"), database.getSchema(),
170           ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive);
171
172       //System.out.println("pkvalue="+req.getPkValue());
173
}
174     catch (Exception JavaDoc e)
175     {
176       e.printStackTrace();
177       if (result.isValid)
178       {
179         fail("Exception thrown with valid request '" + result.request + "' ("
180             + e + ")");
181       }
182       else
183       {
184         assertEquals(
185             "Incorrect error message found while parsing this DELETE statement: '"
186                 + result.request + "'", result.errorMessage, e.getMessage());
187         return;
188       }
189     }
190
191     if (!result.isValid)
192       fail("SQLException not thrown with invalid request: '" + result.request
193           + "'");
194     else
195     {
196       // the parsing does not guarantee the order so we don't compare directly
197
// the ArrayList
198

199       Iterator JavaDoc it;
200       TableColumn c;
201       String JavaDoc s;
202
203       // check select ArrayList
204
it = result.select.iterator();
205       while (it.hasNext())
206       {
207         c = (TableColumn) it.next();
208         assertTrue("'" + c.getColumnName()
209             + "' column not selected by parsing for request: '" + sql + "'",
210             req.getSelect().contains(c));
211       }
212
213       // check from ArrayList
214
it = result.from.iterator();
215       while (it.hasNext())
216       {
217         s = (String JavaDoc) it.next();
218         assertTrue("'" + s + "' table not selected by parsing for request: '"
219             + sql + "'", req.getFrom().contains(s));
220       }
221
222       // check where ArrayList
223
it = result.where.iterator();
224       while (it.hasNext())
225       {
226         c = (TableColumn) it.next();
227         //System.out.println("I should find column("+c.getColumnName()+") in
228
// result");
229
assertTrue("'" + c.getColumnName()
230             + "' column not selected by parsing for request: '" + sql + "'",
231             req.getWhere().contains(c));
232       }
233
234       // check request type
235
//System.out.println("RequestType:" + result.requestType);
236
//System.out.println("CacheAbility:" + req.getCacheAbility());
237
assertEquals("Incorrect request type for request '" + sql + "'",
238           result.requestType, req.getCacheAbility());
239     }
240   }
241
242   /**
243    * Stores the expected result of the call to
244    * {@link org.objectweb.cjdbc.common.sql.SelectRequest#parse(DatabaseSchema, int, boolean)}
245    * method.
246    *
247    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
248    */

249   protected class ParsingResult
250   {
251     /** Request to test. */
252     protected String JavaDoc request;
253
254     /** <code>true</code> if the request is valid. */
255     protected boolean isValid;
256
257     /**
258      * Columns concerned by the <code>SELECT</code> clause:
259      * <code>ArrayList</code> of <code>TableColumn</code> objects.
260      */

261     protected ArrayList JavaDoc select;
262
263     /**
264      * Columns concerned by the <code>FROM</code> clause:
265      * <code>ArrayList</code> of <code>AliasedDatabaseTable</code> objects.
266      */

267     protected ArrayList JavaDoc from;
268
269     /**
270      * Columns concerned by the <code>WHERE</code> clause:
271      * <code>ArrayList</code> of <code>TableColumn</code> objects.
272      */

273     protected ArrayList JavaDoc where;
274
275     /**
276      * Request type: either CACHEABLE, UNCACHEABLE or UNIQUE_CACHEABLE.
277      */

278     protected int requestType;
279
280     /** Error message if the request is invalid. */
281     protected String JavaDoc errorMessage;
282
283     /**
284      * Creates a new <code>ParsingResult</code> instance for valid request.
285      *
286      * @param request request to test.
287      * @param selectColumns columns concerned by the <code>SELECT</code>
288      * clause.
289      * @param fromTables columns concerned by the <code>FROM</code> clause.
290      * @param whereColumns columns concerned by the <code>WHERE</code> clause.
291      * @param requestType request type.
292      */

293     protected ParsingResult(String JavaDoc request, String JavaDoc selectColumns,
294         String JavaDoc fromTables, String JavaDoc whereColumns, int requestType)
295     {
296       StringTokenizer JavaDoc tokenizer;
297       String JavaDoc s, tableName, columnName;
298       int i;
299
300       this.request = request;
301       isValid = true;
302
303       // Parse selectColumns
304
select = new ArrayList JavaDoc();
305       tokenizer = new StringTokenizer JavaDoc(selectColumns.trim(), " ");
306       while (tokenizer.hasMoreTokens())
307       {
308         s = tokenizer.nextToken();
309         i = s.indexOf(".");
310         if (i == -1)
311           fail("Syntax error in sql requests file: . needed in " + s);
312
313         tableName = s.substring(0, i);
314         columnName = s.substring(i + 1, s.length());
315
316         if (database.getSchema().getTable(tableName).getColumn(columnName) != null)
317         {
318           select.add(new TableColumn(tableName, columnName));
319         }
320         else
321         {
322           if (database.getSchema().getTable(tableName) != null)
323             fail("Possible syntax error in sql requests file: '" + columnName
324                 + "' not found in table " + tableName);
325           else
326             fail("Possible syntax error in sql requests file: '" + tableName
327                 + "' not found in database schema");
328         }
329       }
330
331       // Parse fromTables
332
from = new ArrayList JavaDoc();
333       tokenizer = new StringTokenizer JavaDoc(fromTables.trim(), " ");
334       while (tokenizer.hasMoreTokens())
335       {
336         tableName = tokenizer.nextToken();
337
338         if (database.getSchema().getTable(tableName) == null)
339           fail(tableName + " not found in database schema");
340         else
341           from.add(tableName);
342       }
343
344       // Parse whereColumns
345
where = new ArrayList JavaDoc();
346       tokenizer = new StringTokenizer JavaDoc(whereColumns.trim(), " ");
347       while (tokenizer.hasMoreTokens())
348       {
349         s = tokenizer.nextToken();
350         i = s.indexOf(".");
351         if (i == -1)
352           fail("Syntax error in sql requests file: . needed in " + s);
353
354         tableName = s.substring(0, i);
355         columnName = s.substring(i + 1, s.length());
356
357         if (database.getSchema().getTable(tableName).getColumn(columnName) != null)
358         {
359           where.add(new TableColumn(tableName, columnName));
360         }
361         else
362         {
363           if (database.getSchema().getTable(tableName) != null)
364             fail("Possible syntax error in sql requests file: '" + columnName
365                 + "' not found in table '" + tableName + "'");
366           else
367             fail("Possible syntax error in sql requests file: '" + tableName
368                 + "' not found in database schema");
369         }
370       }
371
372       this.requestType = requestType;
373     }
374
375     /**
376      * Creates a new <code>ParsingResult</code> instance for invalid request.
377      *
378      * @param request invalid request to test.
379      * @param errorMessage error message.
380      */

381     protected ParsingResult(String JavaDoc request, String JavaDoc errorMessage)
382     {
383       this.request = request;
384       isValid = false;
385       this.errorMessage = errorMessage;
386     }
387   }
388 }
Popular Tags