KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > TestQueryProperties


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/db/TestQueryProperties.java,v $
3  * Date : $Date: 2006/03/27 14:53:05 $
4  * Version: $Revision: 1.2 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db;
33
34 import org.opencms.test.OpenCmsTestCase;
35
36 import java.io.File JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStreamReader JavaDoc;
40 import java.io.LineNumberReader JavaDoc;
41 import java.nio.charset.Charset JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44
45 import junit.framework.Test;
46 import junit.framework.TestSuite;
47
48 /**
49  * Unit tests that checks the "query.properties" files used by the various drivers for correct
50  * format.
51  * <p>
52  *
53  * This is no code functionality test but a configuration validation that was inspired by a 1 hour
54  * debug session caused by a query.properties file with a trailing tab after the "escape linebreak"
55  * backslash.
56  * <p>
57  *
58  * Currently the following checks are made:
59  * <ul>
60  * <li> <b>Invalid linebreak escape</b><br>
61  * If a linebreak is escaped by a backslash there must not follow any character in that line. </li>
62  * <li> <b>Invalid comment line</b><br>
63  * If a comment character is found it has to be the first character of the line or
64  * <ul>
65  * <li> The characters before it have to be fully "trimmable" (Unicode General Category Separator,
66  * Space [Zs]). </li>
67  * <li> The characters before it are a valid key - value pair in properties notation. </li>
68  * </ul>
69  * </li>
70  * </ul>
71  * <p>
72  *
73  * @author Achim Westermann
74  *
75  * @version $Revision: 1.2 $
76  */

77 public class TestQueryProperties extends OpenCmsTestCase {
78
79     /**
80      * Default JUnit constructor.
81      * <p>
82      *
83      * @param arg0 JUnit parameters
84      */

85     public TestQueryProperties(String JavaDoc arg0) {
86
87         super(arg0);
88     }
89
90     /**
91      * Test suite for this test class.
92      * <p>
93      *
94      * @return the test suite
95      */

96     public static Test suite() {
97
98         TestSuite suite = new TestSuite();
99         suite.setName(TestQueryProperties.class.getName());
100
101         suite.addTest(new TestQueryProperties("testQueryPropertiesGeneric"));
102         suite.addTest(new TestQueryProperties("testQueryPropertiesMaxdb"));
103         suite.addTest(new TestQueryProperties("testQueryPropertiesMssql"));
104         suite.addTest(new TestQueryProperties("testQueryPropertiesMysql"));
105         suite.addTest(new TestQueryProperties("testQueryPropertiesMysql3"));
106         suite.addTest(new TestQueryProperties("testQueryPropertiesOracle8"));
107         suite.addTest(new TestQueryProperties("testQueryPropertiesPostgresql"));
108
109         return suite;
110     }
111
112     /**
113      * Test the generic query.properties file within the workspace for format errors.
114      * <p>
115      *
116      * @throws ParseException if the format of a tested query.properties file is invalid.
117      *
118      * @throws IOException if sth. goes wrong.
119      *
120      *
121      */

122     public void testQueryPropertiesGeneric() throws IOException JavaDoc, ParseException JavaDoc {
123
124         // generic
125
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/generic/query.properties");
126         parseQueryProperties(queries);
127
128     }
129
130     /**
131      * Test the maxdb query.properties file within the workspace for format errors.
132      * <p>
133      *
134      * @throws ParseException if the format of a tested query.properties file is invalid.
135      *
136      * @throws IOException if sth. goes wrong.
137      *
138      *
139      */

140     public void testQueryPropertiesMaxdb() throws IOException JavaDoc, ParseException JavaDoc {
141
142         // maxdb
143
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/maxdb/query.properties");
144         parseQueryProperties(queries);
145     }
146
147     /**
148      * Test the mssql query.properties file within the workspace for format errors.
149      * <p>
150      *
151      * @throws ParseException if the format of a tested query.properties file is invalid.
152      *
153      * @throws IOException if sth. goes wrong.
154      *
155      *
156      */

157     public void testQueryPropertiesMssql() throws IOException JavaDoc, ParseException JavaDoc {
158
159         // mssql
160
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/mssql/query.properties");
161         parseQueryProperties(queries);
162
163     }
164
165     /**
166      * Test the mysql query.properties file within the workspace for format errors.
167      * <p>
168      *
169      * @throws ParseException if the format of a tested query.properties file is invalid.
170      *
171      * @throws IOException if sth. goes wrong.
172      *
173      *
174      */

175     public void testQueryPropertiesMysql() throws IOException JavaDoc, ParseException JavaDoc {
176
177         // mysql
178
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/mysql/query.properties");
179         parseQueryProperties(queries);
180     }
181
182     /**
183      * Test the mysql3 query.properties file within the workspace for format errors.
184      * <p>
185      *
186      * @throws ParseException if the format of a tested query.properties file is invalid.
187      *
188      * @throws IOException if sth. goes wrong.
189      *
190      *
191      */

192     public void testQueryPropertiesMysql3() throws IOException JavaDoc, ParseException JavaDoc {
193
194         // mysql3
195
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/mysql3/query.properties");
196         parseQueryProperties(queries);
197     }
198
199     /**
200      * Test the oracle8 query.properties file within the workspace for format errors.
201      * <p>
202      *
203      * @throws ParseException if the format of a tested query.properties file is invalid.
204      *
205      * @throws IOException if sth. goes wrong.
206      *
207      *
208      */

209     public void testQueryPropertiesOracle8() throws IOException JavaDoc, ParseException JavaDoc {
210
211         // oracle8
212
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/oracle8/query.properties");
213         parseQueryProperties(queries);
214
215     }
216
217     /**
218      * Test the postgresql query.properties file within the workspace for format errors.
219      * <p>
220      *
221      * @throws ParseException if the format of a tested query.properties file is invalid.
222      *
223      * @throws IOException if sth. goes wrong.
224      *
225      *
226      */

227     public void testQueryPropertiesPostgresql() throws IOException JavaDoc, ParseException JavaDoc {
228
229         // postgresql
230
File JavaDoc queries = new File JavaDoc("src/org/opencms/db/postgresql/query.properties");
231         parseQueryProperties(queries);
232     }
233
234     /**
235      * Implementation of the checks to perform.
236      *
237      * @param f the query.properties file to parse.
238      *
239      * @throws IOException if sth. goes wrong.
240      *
241      * @throws ParseException if the format of the file was invalid.
242      */

243     private void parseQueryProperties(File JavaDoc f) throws IOException JavaDoc, ParseException JavaDoc {
244
245         LineNumberReader JavaDoc reader = new LineNumberReader JavaDoc(new InputStreamReader JavaDoc(
246             new FileInputStream JavaDoc(f),
247             Charset.forName("ISO-8859-1")));
248         String JavaDoc read;
249         int len;
250         int count = 0;
251         int lastEscape = 0;
252         while ((read = reader.readLine()) != null) {
253             len = read.length();
254             count++;
255             lastEscape = read.lastIndexOf('\\');
256
257             if (read.trim().length() > 0) {
258
259                 // filter comments
260
int firstSharp = read.indexOf('#');
261                 if (firstSharp > -1) {
262                     String JavaDoc prefix = read.substring(0, firstSharp).trim();
263                     if (prefix.length() > 0) {
264
265                         // check 1: invalid key value pair before comment char
266
try {
267                             parseKeyValue(prefix);
268                         } catch (ParseException JavaDoc pe) {
269
270                             throw new ParseException JavaDoc("Bad format in file "
271                                 + f.getAbsolutePath()
272                                 + ", line "
273                                 + count
274                                 + ": "
275                                 + pe.getMessage(), count);
276                         }
277                     } else {
278                         // valid comment - only line
279
continue;
280                     }
281                 } else {
282                     // no omment line
283
}
284             }
285
286             // check 2: invalid attempt to escape a line break, something follows
287
if (lastEscape != -1) {
288
289                 if (lastEscape != len - 1) {
290                     throw new ParseException JavaDoc("Bad format in file "
291                         + f.getAbsolutePath()
292                         + ", line "
293                         + count
294                         + ": Line termination escape '\\' is followed by further characters.", count);
295                 }
296             }
297
298             // further checks if desired
299
}
300     }
301
302     private void parseKeyValue(String JavaDoc keyValue) throws ParseException JavaDoc {
303
304         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(keyValue, "=: ", false);
305         if (tokenizer.countTokens() != 2) {
306
307             throw new ParseException JavaDoc("Illegal key value pair " + keyValue, 0);
308
309         }
310
311     }
312
313 }
314
Popular Tags