KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.StringTokenizer JavaDoc;
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 /**
45  * <code>CreateRequest</code> test class.
46  *
47  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
48  * org.objectweb.cjdbc.common.sql..CreateRequest
49  */

50 public class CreateRequestTest extends NoTemplate
51 {
52   /** File name containing the requests to test. */
53   private static final String JavaDoc RUBIS_CREATE_REQUESTS_FILE = getTextPath("RUBiS-create-requests.txt");
54
55   /** Null value to used in the requests file if needed. */
56   private 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   private static boolean inited = false;
65
66   /**
67    * @see junit.framework.TestCase#setUp()
68    */

69   protected void setUp()
70   {
71
72     synchronized (this)
73     {
74       if (inited)
75         return;
76       database = new RUBiSDatabase();
77       results = new ArrayList JavaDoc();
78       String JavaDoc request = null, tableName, columnList, errorMessage;
79
80       try
81       {
82         File JavaDoc file = new File JavaDoc(RUBIS_CREATE_REQUESTS_FILE);
83         MyBufferedReader in = new MyBufferedReader(new FileReader JavaDoc(file),
84             "requests");
85
86         String JavaDoc line;
87         while ((line = in.readLine()) != null)
88         {
89           if (line.trim().equals("") || line.startsWith("//"))
90             continue;
91
92           // Get the request
93
request = null;
94           request = in.readSQLRequest(line);
95
96           // Get expected results for this request
97
if (in.readBoolean())
98           {
99             // Valid request
100
tableName = in.readString("table name");
101             columnList = in.readString("column list");
102             results.add(new ParsingResult(request, tableName, columnList));
103           }
104           else
105           {
106             // Invalid request
107
errorMessage = in.readString("errorMessage");
108             results.add(new ParsingResult(request, errorMessage));
109           }
110         }
111       }
112       catch (IOException JavaDoc e)
113       {
114         String JavaDoc 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   /**
125    * org.objectweb.cjdbc.common.sql.CreateRequest#parse(DatabaseSchema, int,
126    * boolean)
127    */

128   public void testParseCreateRequest()
129   {
130     Iterator JavaDoc it = results.iterator();
131     while (it.hasNext())
132     {
133       parse((ParsingResult) it.next(), false);
134     }
135   }
136
137   /**
138    * Perfoms the parsing test.
139    *
140    * @param result expected result
141    * @param isCaseSensitive <code>true</code> if the parsing must be case
142    * sensitive.
143    */

144   private void parse(ParsingResult result, boolean isCaseSensitive)
145   {
146     String JavaDoc 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 JavaDoc 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   /**
193    * Stores the expected result of the call to
194    *
195    * @link org.objectweb.cjdbc.common.sql.CreateRequest#parse(DatabaseSchema,
196    * int, boolean) method.
197    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
198    */

199   protected class ParsingResult
200   {
201     /** Request to test. */
202     protected String JavaDoc request;
203
204     /** <code>true</code> if the request is valid. */
205     protected boolean isValid;
206
207     /** Database table to create if the request is valid. */
208     protected DatabaseTable table;
209
210     /** Error message if the request is invalid. */
211     protected String JavaDoc errorMessage;
212
213     /**
214      * Creates a new <code>ParsingResult</code> instance for valid request.
215      *
216      * @param request valid request to test.
217      * @param tableName database name to test.
218      * @param columnList column list (eg: col1.unique col2 col3). '.unique'
219      * means that the column is unique.
220      */

221     protected ParsingResult(String JavaDoc request, String JavaDoc tableName, String JavaDoc columnList)
222     {
223       this.request = request;
224       isValid = true;
225
226       // Create new table
227
table = new DatabaseTable(tableName);
228
229       // Parse columns to create
230
if (!columnList.equals(EMPTY_VALUE))
231       {
232         String JavaDoc columnName, s;
233         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(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     /**
261      * Creates a new <code>ParsingResult</code> instance for invalid request.
262      *
263      * @param request invalid request to test.
264      * @param errorMessage error message.
265      */

266     protected ParsingResult(String JavaDoc request, String JavaDoc errorMessage)
267     {
268       this.request = request;
269       isValid = false;
270       this.errorMessage = errorMessage;
271     }
272   }
273 }
Popular Tags