KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
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 /**
43  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
44  */

45 public class DropRequestTest extends NoTemplate
46 {
47   /** File name containing the requests to test. */
48   private static final String JavaDoc RUBIS_DROP_REQUESTS_FILE = getTextPath("RUBiS-drop-requests.txt");
49
50   /** Database on which the requests are performed. */
51   private AbstractDatabase database;
52
53   /** List of <code>ParsingResult</code> objects. */
54   private ArrayList JavaDoc results;
55
56   static boolean inited = false;
57
58   /**
59    * @see junit.framework.TestCase#setUp()
60    */

61   protected void setUp()
62   {
63
64     synchronized (this)
65     {
66       if (inited)
67         return;
68       database = new RUBiSDatabase();
69       results = new ArrayList JavaDoc();
70       String JavaDoc request = null, tableName, errorMessage;
71
72       try
73       {
74         File JavaDoc file = new File JavaDoc(RUBIS_DROP_REQUESTS_FILE);
75         MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file),
76             "requests");
77
78         String JavaDoc line;
79         while ((line = in.readLine()) != null)
80         {
81           if (line.trim().equals("") || line.startsWith("//"))
82             continue;
83
84           // Get the request
85
request = null;
86           request = in.readSQLRequest(line);
87
88           // Get expected results for this request
89
if (in.readBoolean())
90           {
91             // Valid request
92
tableName = in.readString("table name");
93             results.add(new ParsingResult(request, true, tableName));
94           }
95           else
96           {
97             // Invalid request
98
errorMessage = in.readString("error message");
99             results.add(new ParsingResult(request, false, errorMessage));
100           }
101         }
102       }
103       catch (IOException JavaDoc e)
104       {
105         String JavaDoc 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   /**
116    * org.objectweb.cjdbc.common.sql.DropRequest#parse(DatabaseSchema, int,
117    * boolean)
118    */

119   public void testParse()
120   {
121     Iterator JavaDoc it = results.iterator();
122     while (it.hasNext())
123     {
124       parse((ParsingResult) it.next(), false);
125     }
126   }
127
128   /**
129    * Perfoms the parsing test.
130    *
131    * @param result expected result
132    * @param isCaseSensitive <code>true</code> if the parsing must be case
133    * sensitive.
134    */

135   private void parse(ParsingResult result, boolean isCaseSensitive)
136   {
137     String JavaDoc 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 JavaDoc 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       // check table
167
assertEquals("Incorrect table found", req.getTableName(), result.table
168           .getName());
169     }
170   }
171
172   /**
173    * Stores the expected result of the call to
174    * {@link org.objectweb.cjdbc.common.sql.DropRequest#parse(DatabaseSchema, int, boolean)}
175    * method.
176    *
177    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
178    */

179   protected class ParsingResult
180   {
181     /** Request to test. */
182     protected String JavaDoc request;
183
184     /** <code>true</code> if the request is valid. */
185     protected boolean isValid;
186
187     /** Database table to delete the request is valid. */
188     protected DatabaseTable table;
189
190     /** Error message if the request is invalid. */
191     protected String JavaDoc errorMessage;
192
193     /**
194      * Creates a new <code>ParsingResult</code> instance.
195      *
196      * @param request request to test.
197      * @param isValid <code>true</code> if the request is valid.
198      * @param s table name if the request is valid or error message if the
199      * request is invalid
200      */

201     protected ParsingResult(String JavaDoc request, boolean isValid, String JavaDoc 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