View Javadoc

1   /**
2    * Copyright 2006-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 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  		// a " inside a ' quoted text
42  		contains("INSERT INTO testTable (thevalue) VALUES ('value \"  !');", 55);
43  
44  		// a ' inside a " quoted text
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  }