001    /**
002     * Copyright 2006-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.sql;
017    
018    import junit.framework.TestCase;
019    
020    public class SqlSplitterTest extends TestCase {
021    
022            public void testContainsSqlString() throws Exception {
023                    containsNot("");
024                    containsNot(" ");
025                    containsNot("  \t  ");
026    
027                    contains(";", 1);
028                    contains("SELECT * from myTable;", 22);
029    
030                    contains("SELECT * from myTable; -- with sl comment", 22);
031                    contains("SELECT * from myTable; /* with part comment */", 22);
032    
033                    contains("SELECT * from myTable /* with part comment inside*/  ; ", 54);
034    
035                    contains("SELECT * from myTable /* with ; semicolon*/  ; ", 46);
036    
037                    contains(
038                                    "INSERT INTO testTable (thevalue) VALUES ('value  !'); -- comment at the end",
039                                    53);
040    
041                    // a " inside a ' quoted text
042                    contains("INSERT INTO testTable (thevalue) VALUES ('value \"  !');", 55);
043    
044                    // a ' inside a " quoted text
045                    contains("INSERT INTO testTable (thevalue) VALUES (\"value '  !\");",
046                                    55);
047    
048                    contains("INSERT INTO testTable (thevalue) VALUES (\"value --  \");",
049                                    55);
050                    contains("INSERT INTO testTable (thevalue) VALUES ('value --  ');", 55);
051    
052                    containsNot("SELECT * from myTable where value = ';' AND -- semicolon is quoted!");
053    
054                    contains(
055                                    "INSERT INTO testTable (thevalue) VALUES (' text '' other ');",
056                                    60);
057    
058            }
059    
060            public void testMsSQLStrings() throws Exception {
061                    String del = "GO";
062    
063                    containsNot("SELECT COUNT(*) FROM Logs", del);
064                    containsNot("SELECT * FROM TPersons", del);
065                    contains("GO", del, 2);
066            }
067    
068            private void contains(String sql, int expectedIndex) throws Exception {
069                    contains(sql, ";", expectedIndex);
070            }
071    
072            private void containsNot(String sql) throws Exception {
073                    containsNot(sql, ";");
074            }
075    
076            private void contains(String sql, String delimiter, int expectedIndex)
077                            throws Exception {
078                    assertEquals(sql, SqlSplitter.containsSqlEnd(sql, delimiter),
079                                    expectedIndex);
080            }
081    
082            private void containsNot(String sql, String delimiter) throws Exception {
083                    assertTrue(
084                                    sql,
085                                    SqlSplitter.containsSqlEnd(sql, delimiter) == SqlSplitter.NO_END);
086            }
087    }