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
027 public void testContainsSqlString() throws Exception
028 {
029 containsNot( "" );
030 containsNot( " " );
031 containsNot( " \t " );
032
033 contains( ";", 1 );
034 contains( "SELECT * from myTable;", 22 );
035
036 contains( "SELECT * from myTable; -- with sl comment", 22 );
037 contains( "SELECT * from myTable; /* with part comment */", 22 );
038
039 contains( "SELECT * from myTable /* with part comment inside*/ ; ", 54 );
040
041 contains( "SELECT * from myTable /* with ; semicolon*/ ; ", 46 );
042
043 contains( "INSERT INTO testTable (thevalue) VALUES ('value !'); -- comment at the end", 53 );
044
045 // a " inside a ' quoted text
046 contains( "INSERT INTO testTable (thevalue) VALUES ('value \" !');", 55 );
047
048 // a ' inside a " quoted text
049 contains( "INSERT INTO testTable (thevalue) VALUES (\"value ' !\");", 55 );
050
051 contains( "INSERT INTO testTable (thevalue) VALUES (\"value -- \");", 55 );
052 contains( "INSERT INTO testTable (thevalue) VALUES ('value -- ');", 55 );
053
054 containsNot( "SELECT * from myTable where value = ';' AND -- semicolon is quoted!" );
055
056 contains( "INSERT INTO testTable (thevalue) VALUES (' text '' other ');", 60 );
057
058 }
059
060 public void testMsSQLStrings() throws Exception
061 {
062 String del = "GO";
063
064 containsNot( "SELECT COUNT(*) FROM Logs", del );
065 containsNot( "SELECT * FROM TPersons", del );
066 contains( "GO", del, 2 );
067 }
068
069
070 private void contains( String sql, int expectedIndex ) throws Exception
071 {
072 contains( sql, ";", expectedIndex );
073 }
074
075 private void containsNot( String sql ) throws Exception
076 {
077 containsNot( sql, ";" );
078 }
079
080 private void contains( String sql, String delimiter, int expectedIndex ) throws Exception
081 {
082 assertEquals( sql, SqlSplitter.containsSqlEnd( sql, delimiter ), expectedIndex);
083 }
084
085 private void containsNot( String sql, String delimiter ) throws Exception
086 {
087 assertTrue( sql, SqlSplitter.containsSqlEnd( sql, delimiter ) == SqlSplitter.NO_END);
088 }
089 }