01: /*
02: * Copyright 2006-2007 The Scriptella Project Team.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
21: import java.util.Set;
22: import java.util.TreeSet;
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 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[] 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: final Set<String> exprSet = new TreeSet<String>();
56: exprSet.add("extra");
57: final Set<String> propSet = new TreeSet<String>();
58: propSet.add("1");
59: propSet.add("value");
60: propSet.add("VALUE");
61: propSet.add("text");
62:
63: SqlParserBase p = new SqlParserBase() {
64: int stInd;
65:
66: @Override
67: protected String handleParameter(final String name,
68: final boolean expression, boolean jdbcParam) {
69: if (expression) {
70: assertTrue(exprSet.contains(name));
71: } else {
72: assertTrue("Unexpected " + name + " property",
73: propSet.contains(name));
74: }
75: return (jdbcParam ? "?/" : "$/") + name + '/';
76: }
77:
78: protected void statementParsed(final String sql) {
79: assertEquals(removeExtraWhitespaces(expected[stInd]),
80: removeExtraWhitespaces(sql));
81: stInd++;
82: }
83: };
84: p.parse(new StringReader(s));
85:
86: }
87:
88: }
|