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