1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.sql;
17
18 import junit.framework.TestCase;
19
20 public class SqlSplitterTest extends TestCase {
21
22 public void testContainsSqlString() throws Exception {
23 containsNot("");
24 containsNot(" ");
25 containsNot(" \t ");
26
27 contains(";", 1);
28 contains("SELECT * from myTable;", 22);
29
30 contains("SELECT * from myTable; -- with sl comment", 22);
31 contains("SELECT * from myTable; /* with part comment */", 22);
32
33 contains("SELECT * from myTable /* with part comment inside*/ ; ", 54);
34
35 contains("SELECT * from myTable /* with ; semicolon*/ ; ", 46);
36
37 contains(
38 "INSERT INTO testTable (thevalue) VALUES ('value !'); -- comment at the end",
39 53);
40
41
42 contains("INSERT INTO testTable (thevalue) VALUES ('value \" !');", 55);
43
44
45 contains("INSERT INTO testTable (thevalue) VALUES (\"value ' !\");",
46 55);
47
48 contains("INSERT INTO testTable (thevalue) VALUES (\"value -- \");",
49 55);
50 contains("INSERT INTO testTable (thevalue) VALUES ('value -- ');", 55);
51
52 containsNot("SELECT * from myTable where value = ';' AND -- semicolon is quoted!");
53
54 contains(
55 "INSERT INTO testTable (thevalue) VALUES (' text '' other ');",
56 60);
57
58 }
59
60 public void testMsSQLStrings() throws Exception {
61 String del = "GO";
62
63 containsNot("SELECT COUNT(*) FROM Logs", del);
64 containsNot("SELECT * FROM TPersons", del);
65 contains("GO", del, 2);
66 }
67
68 private void contains(String sql, int expectedIndex) throws Exception {
69 contains(sql, ";", expectedIndex);
70 }
71
72 private void containsNot(String sql) throws Exception {
73 containsNot(sql, ";");
74 }
75
76 private void contains(String sql, String delimiter, int expectedIndex)
77 throws Exception {
78 assertEquals(sql, SqlSplitter.containsSqlEnd(sql, delimiter),
79 expectedIndex);
80 }
81
82 private void containsNot(String sql, String delimiter) throws Exception {
83 assertTrue(
84 sql,
85 SqlSplitter.containsSqlEnd(sql, delimiter) == SqlSplitter.NO_END);
86 }
87 }