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