KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > SQLParserBaseTest


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.AbstractTestCase;
19
20 import java.io.StringReader JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.TreeSet JavaDoc;
23
24 /**
25  * Tests {@link scriptella.jdbc.SqlParserBase}.
26  *
27  * @author Fyodor Kupolov
28  * @version 1.0
29  */

30 public class SQLParserBaseTest extends AbstractTestCase {
31     public void test() {
32         String JavaDoc s = "-not skipped\n/*+ HintOrComment*/\n" +
33                 " \n" +
34                 " CREATE TABLE Test (\n" +
35                 " ID INT,\n" +
36                 " $VALUE VARCHAR(255)\n" +
37                 " );\n" +
38                 " ${extra}\n" +
39                 " insert into test(id, value) values (?1, '?justatext');\n" +
40                 " insert into test(id, value) values (?value,'A test?{justatext}');\n" +
41                 " insert into test(id, value) values (3,?text);\n" +
42                 " //comment$justatext ?{justatext} ?justatext\n";
43         //comments are ignored and quoted values are not parsed
44
final String JavaDoc[] expected = {
45                 "-not skipped\n/*+ HintOrComment*/\nCREATE TABLE Test (\n" +
46                         " ID INT,\n" +
47                         " $/VALUE/ VARCHAR(255)\n" +
48                         " )",
49                 " $/extra/" +
50                         " insert into test(id, value) values (?/1/, '?justatext')",
51                 " insert into test(id, value) values (?/value/,'A test?{justatext}')",
52                 " insert into test(id, value) values (3,?/text/)",
53                 " //comment$justatext ?{justatext} ?justatext\n"};
54
55
56         final Set JavaDoc<String JavaDoc> exprSet = new TreeSet JavaDoc<String JavaDoc>();
57         exprSet.add("extra");
58         final Set JavaDoc<String JavaDoc> propSet = new TreeSet JavaDoc<String JavaDoc>();
59         propSet.add("1");
60         propSet.add("value");
61         propSet.add("VALUE");
62         propSet.add("text");
63
64
65         SqlParserBase p = new SqlParserBase() {
66             int stInd;
67
68             @Override JavaDoc
69             protected String JavaDoc handleParameter(final String JavaDoc name, final boolean expression, boolean jdbcParam) {
70                 if (expression) {
71                     assertTrue(exprSet.contains(name));
72                 } else {
73                     assertTrue("Unexpected "+name+" property", propSet.contains(name));
74                 }
75                 return (jdbcParam ? "?/" : "$/") + name + '/';
76             }
77
78             protected void statementParsed(final String JavaDoc sql) {
79                 assertEquals(removeExtraWhitespaces(expected[stInd]), removeExtraWhitespaces(sql));
80                 stInd++;
81             }
82         };
83         p.parse(new StringReader JavaDoc(s));
84
85     }
86
87
88
89 }
90
Popular Tags