001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.tools.IJRunScriptTest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: 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, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.tools;
023:
024: import java.io.UnsupportedEncodingException;
025: import java.sql.Connection;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
033: import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
034:
035: /**
036: * Test the ij.runScript api method.
037: *
038: */
039: public class IJRunScriptTest extends BaseJDBCTestCase {
040:
041: public static Test suite() {
042: TestSuite suite = new TestSuite();
043: suite.addTestSuite(IJRunScriptTest.class);
044: return new CleanDatabaseTestSetup(suite);
045: }
046:
047: public IJRunScriptTest(String name) {
048: super (name);
049: }
050:
051: /**
052: * Test execution of scripts by executing them and
053: * seeing if the object exists.
054: * @throws SQLException
055: * @throws UnsupportedEncodingException
056: */
057: public void testScriptExecution() throws SQLException,
058: UnsupportedEncodingException {
059: runTestingScript(
060: "CREATE TABLE T1(I INT);\nCREATE TABLE T2(I INT)", 0);
061:
062: // Check they exist by inserting rows.
063:
064: Statement s = createStatement();
065:
066: // Insert two rows into the first table
067: assertEquals(2, s.executeUpdate("INSERT INTO T1 VALUES 1,2"));
068:
069: // Insert three rows into the second table
070: assertEquals(3, s.executeUpdate("INSERT INTO T2 VALUES 1,2,4"));
071:
072: runTestingScript("DROP TABLE T1;DROP TABLE T2", 0);
073:
074: s.close();
075: }
076:
077: /**
078: * Test execution an empty script.
079: * @throws SQLException
080: * @throws UnsupportedEncodingException
081: */
082: public void testEmptyScript() throws SQLException,
083: UnsupportedEncodingException {
084: runTestingScript("", 0);
085: }
086:
087: /**
088: * Test execution of the IJ AUTOCOMMIT statement.
089: * @throws SQLException
090: * @throws UnsupportedEncodingException
091: */
092: public void testAutoCommitCommand() throws SQLException,
093: UnsupportedEncodingException {
094: Connection conn = getConnection();
095: assertTrue(conn.getAutoCommit());
096: runTestingScript("AUTOCOMMIT OFF;", 0);
097:
098: assertFalse(conn.isClosed());
099: assertFalse(conn.getAutoCommit());
100: }
101:
102: /**
103: * Test error counting.
104: * @throws SQLException
105: * @throws UnsupportedEncodingException
106: */
107: public void testErrorsCount() throws SQLException,
108: UnsupportedEncodingException {
109: // just a single error
110: runTestingScript("CREATE TAAABLE T (I INT);", 1);
111: runTestingScript("INSERT INTO TIJ VALUES 1;", 1);
112:
113: // two errors
114: runTestingScript(
115: "INSERT INTO TIJ VALUES 1;\nDELETE FROM SYS.SYSTABLES",
116: 2);
117: runTestingScript(
118: "INSERT INTO TIJ VALUES 1;DELETE FROM SYS.SYSTABLES", 2);
119:
120: // mixture of errors (two in all)
121: runTestingScript(
122: "CREATX TABLE TIJME(I INT);CREATE TABLE TIJME(I INT);"
123: + "INSERT INTO TIJME VALUES 1,3,4;"
124: + "INSERT INTO TIJME VALUESS 1,3,4;"
125: + "DROP TABLE TIJME", 2);
126:
127: }
128:
129: /**
130: * Run a test script using the passed in String as the source
131: * for the script. Script is run using the UTF-8 encoding and
132: * the output discarded.
133: * @param script
134: * @return error count from ij.runScript.
135: * @throws UnsupportedEncodingException
136: * @throws SQLException
137: */
138: private void runTestingScript(String script, int expectedErrorCount)
139: throws UnsupportedEncodingException, SQLException {
140: int errorCount = runSQLCommands(script);
141: assertEquals("Error count on " + script, expectedErrorCount,
142: errorCount);
143: }
144:
145: }
|