KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > tools > IJRunScriptTest


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.tools.IJRunScriptTest
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.tools;
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
33 import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
34
35 /**
36  * Test the ij.runScript api method.
37  *
38  */

39 public class IJRunScriptTest extends BaseJDBCTestCase {
40     
41     public static Test suite()
42     {
43         TestSuite suite = new TestSuite();
44         suite.addTestSuite(IJRunScriptTest.class);
45         return new CleanDatabaseTestSetup(suite);
46     }
47     
48     public IJRunScriptTest(String JavaDoc name)
49     {
50         super(name);
51     }
52     
53     /**
54      * Test execution of scripts by executing them and
55      * seeing if the object exists.
56      * @throws SQLException
57      * @throws UnsupportedEncodingException
58      */

59     public void testScriptExecution()
60         throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc
61     {
62         runTestingScript("CREATE TABLE T1(I INT);\nCREATE TABLE T2(I INT)", 0);
63         
64         // Check they exist by inserting rows.
65

66         Statement JavaDoc s = createStatement();
67         
68         // Insert two rows into the first table
69
assertEquals(2, s.executeUpdate("INSERT INTO T1 VALUES 1,2"));
70
71         // Insert three rows into the second table
72
assertEquals(3, s.executeUpdate("INSERT INTO T2 VALUES 1,2,4"));
73         
74         runTestingScript("DROP TABLE T1;DROP TABLE T2", 0);
75                
76         s.close();
77     }
78
79     /**
80      * Test execution an empty script.
81      * @throws SQLException
82      * @throws UnsupportedEncodingException
83      */

84     public void testEmptyScript()
85         throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc
86     {
87         runTestingScript("", 0);
88     }
89
90     /**
91      * Test execution of the IJ AUTOCOMMIT statement.
92      * @throws SQLException
93      * @throws UnsupportedEncodingException
94      */

95     public void testAutoCommitCommand()
96         throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc
97     {
98         Connection JavaDoc conn = getConnection();
99         assertTrue(conn.getAutoCommit());
100         runTestingScript("AUTOCOMMIT OFF;", 0);
101         
102         assertFalse(conn.isClosed());
103         assertFalse(conn.getAutoCommit());
104     }
105     
106     /**
107      * Test error counting.
108      * @throws SQLException
109      * @throws UnsupportedEncodingException
110      */

111     public void testErrorsCount()
112         throws SQLException JavaDoc, UnsupportedEncodingException JavaDoc
113     {
114        // just a single error
115
runTestingScript("CREATE TAAABLE T (I INT);", 1);
116        runTestingScript("INSERT INTO TIJ VALUES 1;", 1);
117
118        // two errors
119
runTestingScript("INSERT INTO TIJ VALUES 1;\nDELETE FROM SYS.SYSTABLES", 2);
120        runTestingScript("INSERT INTO TIJ VALUES 1;DELETE FROM SYS.SYSTABLES", 2);
121        
122        // mixture of errors (two in all)
123
runTestingScript("CREATX TABLE TIJME(I INT);CREATE TABLE TIJME(I INT);" +
124                "INSERT INTO TIJME VALUES 1,3,4;" +
125                "INSERT INTO TIJME VALUESS 1,3,4;" +
126                "DROP TABLE TIJME"
127                , 2);
128        
129    }
130         
131
132     /**
133      * Run a test script using the passed in String as the source
134      * for the script. Script is run using the UTF-8 encoding and
135      * the output discarded.
136      * @param script
137      * @return error count from ij.runScript.
138      * @throws UnsupportedEncodingException
139      * @throws SQLException
140      */

141     private void runTestingScript(String JavaDoc script, int expectedErrorCount)
142         throws UnsupportedEncodingException JavaDoc, SQLException JavaDoc
143     {
144         int errorCount = runSQLCommands(script);
145         assertEquals("Error count on " + script,
146                 expectedErrorCount, errorCount );
147     }
148     
149 }
150
Popular Tags