KickJava   Java API By Example, From Geeks To Geeks.

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


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.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Arrays JavaDoc;
33 import java.util.Comparator JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import org.objectweb.cjdbc.common.sql.DeleteRequest;
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 /**
47  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
48  * org.objectweb.cjdbc.common.sql..DeleteRequest
49  */

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

72   protected void setUp()
73   {
74     synchronized (this)
75     {
76       if (inited)
77         return;
78       database = new RUBiSDatabase();
79       results = new ArrayList JavaDoc();
80
81       String JavaDoc request = null, tableName, columnList, errorMessage;
82       boolean isUnique = false;
83
84       try
85       {
86         File JavaDoc file = new File JavaDoc(RUBIS_DELETE_REQUESTS_FILE);
87
88         MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file),
89             "requests");
90
91         String JavaDoc line;
92         while ((line = in.readLine()) != null)
93         {
94           if (line.trim().equals("") || line.startsWith("//"))
95             continue;
96
97           // get the request
98
request = null;
99           request = in.readSQLRequest(line);
100
101           // get expected results for this request
102
if (in.readBoolean())
103           {
104             // valid request
105
tableName = in.readString("table name");
106             columnList = in.readString("column list");
107             isUnique = in.readBoolean();
108             updatedPk = in.readString("updated pk");
109             if (updatedPk.equals(EMPTY_VALUE))
110               updatedPk = null;
111
112             results.add(new ParsingResult(request, tableName, columnList,
113                 isUnique, updatedPk));
114           }
115           else
116           {
117             // invalid request
118
errorMessage = in.readString("error message");
119             results.add(new ParsingResult(request, errorMessage));
120           }
121         }
122       }
123       catch (IOException JavaDoc e)
124       {
125         String JavaDoc error = "An error occurs while parsing requests file: " + e;
126         if (request != null)
127           error += " (request: '" + request + "')";
128         fail(error);
129       }
130       inited = true;
131     }
132
133   }
134
135   /**
136    * org.objectweb.cjdbc.common.sql.DeleteRequest#parse(DatabaseSchema, int,
137    * boolean)
138    */

139   public void testParse()
140   {
141     Iterator JavaDoc it = results.iterator();
142     while (it.hasNext())
143     {
144       parse((ParsingResult) it.next(), false);
145     }
146   }
147
148   /**
149    * Perfoms the parsing test.
150    *
151    * @param result expected result
152    * @param isCaseSensitive <code>true</code> if the parsing must be case
153    * sensitive.
154    */

155   private void parse(ParsingResult result, boolean isCaseSensitive)
156   {
157     String JavaDoc sql = result.request.toLowerCase().trim();
158     DeleteRequest req = null;
159     try
160     {
161       req = new DeleteRequest(sql, false, 0, System
162           .getProperty("line.separator"), database.getSchema(),
163           ParsingGranularities.COLUMN_UNIQUE, isCaseSensitive);
164     }
165     catch (SQLException JavaDoc e)
166     {
167       if (result.isValid)
168       {
169         fail("Exception thrown with valid request '" + result.request + "' ("
170             + e + ")");
171       }
172       else
173       {
174         assertEquals(
175             "Incorrect error message found while parsing this DELETE statement: '"
176                 + result.request + "'", result.errorMessage, e.getMessage());
177         return;
178       }
179     }
180
181     if (!result.isValid)
182       fail("SQLException not thrown with invalid request: '" + result.request
183           + "'");
184     else
185     {
186       // check table
187
assertEquals("Incorrect table found", result.table.getName(), req
188           .getTableName());
189
190       //System.out.println("UpdatedPk:" + req.getPk());
191
assertEquals(result.updatedPk, req.getPk());
192
193       // TODO: test the extraction of the selected columns and the unicity
194
// (does not work currently)
195
// // check columns ArrayList (the parsing does not guaranty the order so
196
// // we don't compare directly the ArrayList)
197
// TableColumn c;
198
// Iterator it = result.columns.iterator();
199
// while (it.hasNext())
200
// {
201
// c = (TableColumn) it.next();
202
// assertTrue(
203
// "'"
204
// + c.getColumn()
205
// + "' column not selected by parsing for request: '"
206
// + sql
207
// + "'",
208
// req.getColumns().contains(c));
209
// }
210
//
211
// // check unicity
212
// assertEquals(
213
// "Unicity not correct for request: '" + sql + "'",
214
// result.isUnique,
215
// req.isUnique());
216
}
217   }
218
219   /**
220    * Sort the given ArrayList
221    *
222    * @param al the ArrayList to sort
223    * @return the sorted ArrayList
224    */

225   public ArrayList JavaDoc sortArrayList(ArrayList JavaDoc al)
226   {
227     Object JavaDoc[] obs = al.toArray();
228     Arrays.sort(obs, new MyComparator());
229     return new ArrayList JavaDoc(Arrays.asList(obs));
230   }
231
232   /**
233    * Print the given ArrayList prepended by a given header. Expects an ArrayList
234    * of TableColumn but will not fail if it is not.
235    *
236    * @param header header to display
237    * @param al ArrayList to display
238    */

239   public void debugArrayList(String JavaDoc header, ArrayList JavaDoc al)
240   {
241     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
242     for (int i = 0; i < al.size(); i++)
243     {
244       if (i != 0)
245         buf.append(",");
246       Object JavaDoc o = al.get(i);
247       if (o instanceof TableColumn)
248         buf.append(((TableColumn) o).getColumnName());
249       else
250         buf.append(o.toString());
251     }
252     //System.out.println(header + ":" + buf.toString());
253
}
254
255   class MyComparator implements Comparator JavaDoc
256   {
257     /**
258      * Compare two <code>TableColumn</code> objects
259      */

260     public int compare(Object JavaDoc o1, Object JavaDoc o2)
261     {
262       if (o1 instanceof TableColumn && o2 instanceof TableColumn)
263         return ((TableColumn) o1).getColumnName().compareTo(
264             ((TableColumn) o2).getColumnName());
265       else
266         return (o1.toString().compareTo(o2.toString()));
267     }
268   }
269
270   /**
271    * Stores the expected result of the call to
272    * {@link org.objectweb.cjdbc.common.sql.DeleteRequest#parse(DatabaseSchema, int, boolean)}
273    * method.
274    *
275    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
276    */

277   protected class ParsingResult
278   {
279     /** Request to test. */
280     protected String JavaDoc request;
281
282     /** <code>true</code> if the request is valid. */
283     protected boolean isValid;
284
285     /** Database table to delete the request is valid. */
286     protected DatabaseTable table;
287
288     /** Error message if the request is invalid. */
289     protected String JavaDoc errorMessage;
290
291     /** <code>ArrayList</code> of <code>TableColumn</code> objects. */
292     protected ArrayList JavaDoc columns;
293
294     /** <code>true</code> this query only deletes a single row. */
295     protected boolean isUnique;
296
297     /** Updated pk is is unique is true */
298     protected String JavaDoc updatedPk;
299
300     /**
301      * Creates a new <code>ParsingResult</code> instance for valid request.
302      *
303      * @param request valid request to test.
304      * @param tableName database name to test.
305      * @param columnList column list (eg: col1.unique col2 col3). '.unique'
306      * means that the column is unique.
307      * @param isUnique <code>true</code> this query only deletes a single row.
308      */

309     protected ParsingResult(String JavaDoc request, String JavaDoc tableName,
310         String JavaDoc columnList, boolean isUnique, String JavaDoc updatedPk)
311     {
312       this.request = request;
313       isValid = true;
314
315       // Get the table to delete
316
table = database.getSchema().getTable(tableName);
317       if (table == null)
318         fail("Possible syntax error in sql requests file: '" + tableName
319             + "' not found in database schema");
320
321       // parse columns
322
columns = new ArrayList JavaDoc();
323       String JavaDoc columnName;
324       StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(columnList.trim(), " ");
325       while (tokenizer.hasMoreTokens())
326       {
327         columnName = tokenizer.nextToken();
328         if (table.getColumn(columnName) != null)
329           columns.add(new TableColumn(tableName, columnName));
330         else
331           fail("Possible syntax error in sql requests file: '" + columnName
332               + "' not found in table '" + tableName + "'");
333       }
334
335       this.isUnique = isUnique;
336       this.updatedPk = updatedPk;
337     }
338
339     /**
340      * Creates a new <code>ParsingResult</code> instance for invalid request.
341      *
342      * @param request invalid request to test.
343      * @param errorMessage error message.
344      */

345     protected ParsingResult(String JavaDoc request, String JavaDoc errorMessage)
346     {
347       this.request = request;
348       isValid = false;
349       this.errorMessage = errorMessage;
350     }
351   }
352 }
Popular Tags