KickJava   Java API By Example, From Geeks To Geeks.

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


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.AlterRequest;
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 AlterRequestTest extends NoTemplate
51 {
52   /** File name containing the requests to test. */
53   private static final String JavaDoc RUBIS_CREATE_REQUESTS_FILE = getTextPath("RUBiS-alter-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 testParseAlterRequest()
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     AlterRequest req = null;
148     try
149     {
150       req = new AlterRequest(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         fail("Exception thrown with valid request '" + result.request + "' ("
159             + e + ")");
160       }
161       else
162       {
163         assertEquals(
164             "Incorrect error message found while parsing this ALTER statement: '"
165                 + result.request + "'", result.errorMessage, e.getMessage());
166         return;
167       }
168     }
169
170     if (!result.isValid)
171       fail("SQLException not thrown with invalid request: '" + result.request
172           + "'");
173     else
174     {
175       assertEquals(
176           "Incorrect table name found while parsing this ALTER statement: '"+ result.request + "'", result.table.getName(), req
177               .getDatabaseTable().getName());
178       
179       assertEquals(
180           "Incorrect table name found while parsing this ALTER statement: '"+ result.request + "'", result.table.getName(), req
181               .getTableName());
182
183       assertTrue("Incorrect Column while parsing this ALTER statement: '"+ result.request + "'", result.column.equalsIgnoreType(req.getColumn()));
184       
185     }
186   }
187
188   /**
189    * Stores the expected result of the call to
190    *
191    * @link org.objectweb.cjdbc.common.sql.CreateRequest#parse(DatabaseSchema,
192    * int, boolean) method.
193    * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
194    */

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

219     protected ParsingResult(String JavaDoc request, String JavaDoc tableName, String JavaDoc columnList)
220     {
221       this.request = request;
222       isValid = true;
223
224       // Create new table
225
table = database.getSchema().getTable(tableName);
226
227       // Parse columns to alter
228
if (!columnList.equals(EMPTY_VALUE))
229       {
230         String JavaDoc columnName, s;
231         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(columnList.trim(), " ");
232         boolean isUnique = false;
233         int i;
234         while (tokenizer.hasMoreTokens())
235         {
236           s = tokenizer.nextToken();
237           i = s.indexOf(".");
238           if (i != -1)
239           {
240             columnName = s.substring(0, i);
241             s = s.substring(i + 1, s.length());
242             if ("unique".equals(s))
243               isUnique = true;
244             else
245               fail("Syntax error in sql requests file ('unique' token expected instead of: '"
246                   + s + "' for request '" + request + "')");
247           }
248           else
249           {
250             isUnique = false;
251             columnName = s;
252           }
253           column = new DatabaseColumn(columnName, isUnique);
254         }
255       }
256     }
257
258     /**
259      * Creates a new <code>ParsingResult</code> instance for invalid request.
260      *
261      * @param request invalid request to test.
262      * @param errorMessage error message.
263      */

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