001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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.unitils.dbmaintainer.script;
017:
018: import org.junit.Before;
019: import org.junit.Test;
020: import org.unitils.UnitilsJUnit4;
021: import org.unitils.dbmaintainer.script.impl.SQLCodeScriptRunner;
022: import static org.unitils.reflectionassert.ReflectionAssert.assertLenEquals;
023:
024: import java.util.Arrays;
025: import java.util.List;
026:
027: /**
028: * todo javadoc
029: *
030: * @author Filip Neven
031: * @author Tim Ducheyne
032: */
033: public class DefaultCodeScriptRunnerTest extends UnitilsJUnit4 {
034:
035: /* Tested instance */
036: private SQLCodeScriptRunner sqlCodeScriptRunner;
037:
038: /* Normal script, containing 2 statements and a blank line */
039: private static final String NORMAL_SCRIPT = "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\n"
040: + "BEGIN\n"
041: + "statement1;\n"
042: + "statement2;\n"
043: + "END\n"
044: + "/ \n"
045: + "PROCEDURE TEST2 (param1 VARCHAR, param2 BLOB) IS\n"
046: + "BEGIN\n"
047: + "statement1;\n"
048: + "statement2;\n"
049: + "END;\n"
050: + "/";
051:
052: private static final String SCRIPT_WITH_MULTILINE_COMMENT = "/* multiline\n"
053: + " comment */"
054: + "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\n"
055: + "BEGIN\n"
056: + "statement1;\n"
057: + "statement2;\n"
058: + "END;\n"
059: + "/";
060:
061: private static final String SCRIPT_WITH_LINE_COMMENT = "-- line comment /\n"
062: + "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\n"
063: + "BEGIN\n"
064: + "statement1;\n"
065: + "statement2;\n"
066: + "END;\n"
067: + "/";
068:
069: private static final String SCRIPT_WITH_SLASH_IN_CODE = "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\n"
070: + "BEGIN\n"
071: + "/statement1/;\n"
072: + "statement2;\n"
073: + "END;\n"
074: + "/\n"
075: + "PROCEDURE TEST2 (param1 VARCHAR, param2 BLOB) IS\n"
076: + "BEGIN\n"
077: + "/statement1/\n"
078: + "statement2;\n"
079: + "END\n"
080: + "/";
081:
082: /**
083: * Initialize test fixture
084: */
085: @Before
086: public void setUp() throws Exception {
087: sqlCodeScriptRunner = new SQLCodeScriptRunner();
088: }
089:
090: @Test
091: public void testExecute() throws Exception {
092: List<String> result = sqlCodeScriptRunner
093: .parseStatements(NORMAL_SCRIPT);
094: assertLenEquals(
095: Arrays
096: .asList(
097: "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\nstatement1;\nstatement2;\nEND",
098: "PROCEDURE TEST2 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\nstatement1;\nstatement2;\nEND;"),
099: result);
100: }
101:
102: @Test
103: public void testExecute_multilineComment() throws Exception {
104: List<String> result = sqlCodeScriptRunner
105: .parseStatements(SCRIPT_WITH_MULTILINE_COMMENT);
106: assertLenEquals(
107: Arrays
108: .asList("/* multiline\n comment */PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\nstatement1;\nstatement2;\nEND;"),
109: result);
110: }
111:
112: @Test
113: public void testExecute_lineComment() throws Exception {
114: List<String> result = sqlCodeScriptRunner
115: .parseStatements(SCRIPT_WITH_LINE_COMMENT);
116: assertLenEquals(
117: Arrays
118: .asList("-- line comment /\nPROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\nstatement1;\nstatement2;\nEND;"),
119: result);
120: }
121:
122: @Test
123: public void testExecute_scriptWithSlashInCode() throws Exception {
124: List<String> result = sqlCodeScriptRunner
125: .parseStatements(SCRIPT_WITH_SLASH_IN_CODE);
126: assertLenEquals(
127: Arrays
128: .asList(
129: "PROCEDURE TEST1 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\n/statement1/;\nstatement2;\nEND;",
130: "PROCEDURE TEST2 (param1 VARCHAR, param2 BLOB) IS\nBEGIN\n/statement1/\nstatement2;\nEND"),
131: result);
132: }
133:
134: }
|