db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r433331 - in /db/derby/code/trunk/java: testing/org/apache/derbyTesting/functionTests/tests/lang/ testing/org/apache/derbyTesting/functionTests/tests/tools/ testing/org/apache/derbyTesting/functionTests/util/ testing/org/apache/derbyTesting...
Date Mon, 21 Aug 2006 19:19:08 GMT
Author: djd
Date: Mon Aug 21 12:19:07 2006
New Revision: 433331

URL: http://svn.apache.org/viewvc?rev=433331&view=rev
Log:
DERBY-1609 Add IJRunScriptTest and _Suite JUnit tests for the testing of ij.runScript.
Modify runScript to take an OutputStream instead of a PrintStream, fix it to
correctly return the number of errors seen while running the script.
Add utility methods to BaseJDBCTestCase to run setup scripts from an InputStream or a String.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java
    db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?rev=433331&r1=433330&r2=433331&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Mon Aug 21 12:19:07 2006
@@ -1,6 +1,6 @@
 /*
 
-   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi._Suite
+   Derby - Class org.apache.derbyTesting.functionTests.tests.lang._Suite
 
        Licensed to the Apache Software Foundation (ASF) under one
        or more contributor license agreements.  See the NOTICE file

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java?rev=433331&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
Mon Aug 21 12:19:07 2006
@@ -0,0 +1,149 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.tools.IJRunScriptTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+ */
+
+package org.apache.derbyTesting.functionTests.tests.tools;
+
+import java.io.UnsupportedEncodingException;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
+
+/**
+ * Test the ij.runScript api method.
+ *
+ */
+public class IJRunScriptTest extends BaseJDBCTestCase {
+    
+    public static Test suite()
+    {
+        TestSuite suite = new TestSuite();
+        suite.addTestSuite(IJRunScriptTest.class);
+        return new CleanDatabaseTestSetup(suite);
+    }
+    
+    public IJRunScriptTest(String name)
+    {
+        super(name);
+    }
+    
+    /**
+     * Test execution of scripts by executing them and
+     * seeing if the object exists.
+     * @throws SQLException
+     * @throws UnsupportedEncodingException 
+     */
+    public void testScriptExecution()
+        throws SQLException, UnsupportedEncodingException
+    {       
+        runTestingScript("CREATE TABLE T1(I INT);\nCREATE TABLE T2(I INT)", 0);
+        
+        // Check they exist by inserting rows.
+        
+        Statement s = createStatement();
+        
+        // Insert two rows into the first table
+        assertEquals(2, s.executeUpdate("INSERT INTO T1 VALUES 1,2"));
+
+        // Insert three rows into the second table
+        assertEquals(3, s.executeUpdate("INSERT INTO T2 VALUES 1,2,4"));
+        
+        runTestingScript("DROP TABLE T1;DROP TABLE T2", 0);
+               
+        s.close();
+    }
+
+    /**
+     * Test execution an empty script.
+     * @throws SQLException
+     * @throws UnsupportedEncodingException 
+     */
+    public void testEmptyScript()
+        throws SQLException, UnsupportedEncodingException
+    {       
+        runTestingScript("", 0);
+    }   
+
+    /**
+     * Test execution of the IJ AUTOCOMMIT statement.
+     * @throws SQLException
+     * @throws UnsupportedEncodingException 
+     */
+    public void testAutoCommitCommand()
+        throws SQLException, UnsupportedEncodingException
+    {      
+        Connection conn = getXConnection();
+        assertTrue(conn.getAutoCommit());
+        runTestingScript("AUTOCOMMIT OFF;", 0);
+        
+        assertFalse(conn.isClosed());
+        assertFalse(conn.getAutoCommit());
+    }
+    
+    /**
+     * Test error counting.
+     * @throws SQLException
+     * @throws UnsupportedEncodingException 
+     */
+    public void testErrorsCount()
+        throws SQLException, UnsupportedEncodingException
+    {      
+       // just a single error
+       runTestingScript("CREATE TAAABLE T (I INT);", 1);
+       runTestingScript("INSERT INTO TIJ VALUES 1;", 1);
+
+       // two errors
+       runTestingScript("INSERT INTO TIJ VALUES 1;\nDELETE FROM SYS.SYSTABLES", 2);
+       runTestingScript("INSERT INTO TIJ VALUES 1;DELETE FROM SYS.SYSTABLES", 2);
+       
+       // mixture of errors (two in all)
+       runTestingScript("CREATX TABLE TIJME(I INT);CREATE TABLE TIJME(I INT);" +
+               "INSERT INTO TIJME VALUES 1,3,4;" +
+               "INSERT INTO TIJME VALUESS 1,3,4;" +
+               "DROP TABLE TIJME"
+               , 2);
+       
+   }
+        
+
+    /**
+     * Run a test script using the passed in String as the source
+     * for the script. Script is run using the UTF-8 encoding and
+     * the output discarded.
+     * @param script
+     * @return error count from ij.runScript.
+     * @throws UnsupportedEncodingException
+     * @throws SQLException
+     */
+    private void runTestingScript(String script, int expectedErrorCount)
+        throws UnsupportedEncodingException, SQLException
+    {       
+        int errorCount = runSQLCommands(script);
+        assertEquals("Error count on " + script,
+                expectedErrorCount, errorCount );
+    }
+    
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/IJRunScriptTest.java
------------------------------------------------------------------------------
    svn:executable = *

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java?rev=433331&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
Mon Aug 21 12:19:07 2006
@@ -0,0 +1,58 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.tools._Suite
+
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       specific language governing permissions and limitations
+       under the License
+*/
+package org.apache.derbyTesting.functionTests.tests.tools;
+
+import org.apache.derbyTesting.junit.BaseTestCase;
+import org.apache.derbyTesting.junit.JDBC;
+
+import junit.framework.Test; 
+import junit.framework.TestSuite;
+
+/**
+ * Suite to run all JUnit tests in this package:
+ * org.apache.derbyTesting.functionTests.tests.tools
+ *
+ */
+public class _Suite extends BaseTestCase {
+
+    /**
+     * Use suite method instead.
+     */
+    private _Suite(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+
+        TestSuite suite = new TestSuite("tools");
+
+        suite.addTest(IJRunScriptTest.suite());
+
+        // Tests that are compiled using 1.4 target need to
+        // be added this way, otherwise creating the suite
+        // will throw an invalid class version error
+        if (JDBC.vmSupportsJDBC3() || JDBC.vmSupportsJSR169()) {
+        }
+
+        return suite;
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/tools/_Suite.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java?rev=433331&r1=433330&r2=433331&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java
Mon Aug 21 12:19:07 2006
@@ -112,22 +112,20 @@
 		
 		ByteArrayOutputStream rawBytes =
 			new ByteArrayOutputStream(20 * 1024);
-		
-		PrintStream printOut = new PrintStream(rawBytes);
-	
+			
 		Connection conn = getXConnection();
 		org.apache.derby.tools.ij.runScript(
 				conn,
 				sqlIn,
 				inputEncoding,
-				printOut,
+                rawBytes,
 				outputEncoding);
 		
 		if (!conn.isClosed() && !conn.getAutoCommit())
 		    conn.commit();
 		
-		printOut.flush();
-		printOut.close();
+        rawBytes.flush();
+        rawBytes.close();
 		sqlIn.close();
 			
 		

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?rev=433331&r1=433330&r2=433331&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java Mon
Aug 21 12:19:07 2006
@@ -19,11 +19,17 @@
  */
 package org.apache.derbyTesting.junit;
 
+import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.io.Reader;
+import java.io.UnsupportedEncodingException;
 import java.sql.*;
 
+import org.apache.derby.tools.ij;
+
 
 /**
  * Base class for JDBC JUnit tests.
@@ -177,6 +183,45 @@
     public Connection openConnection(String databaseName) throws SQLException
     {
         return getTestConfiguration().openConnection(databaseName);
+    }
+    
+    /**
+     * Run a SQL script through ij discarding the output
+     * using this object's default connection. Intended for
+     * setup scripts.
+     * @throws UnsupportedEncodingException 
+     * @throws SQLException 
+     */
+    public int runScript(InputStream script, String encoding)
+        throws UnsupportedEncodingException, SQLException
+    {
+        // Sink output.
+        OutputStream sink = new OutputStream() {
+            public void write(byte[] b, int off, int len) {}
+            public void write(int b) {}
+        };
+        
+        // Use the same encoding as the input for the output.    
+        return ij.runScript(getXConnection(), script, encoding,
+                sink, encoding);       
+    }
+    
+    /**
+     * Run a set of SQL commands from a String discarding the output.
+     * Commands are separated by a semi-colon. Connection used
+     * is this objects default connection.
+     * @param sqlCommands
+     * @return Number of errors executing the script.
+     * @throws UnsupportedEncodingException
+     * @throws SQLException
+     */
+    public int runSQLCommands(String sqlCommands)
+        throws UnsupportedEncodingException, SQLException
+    {
+        byte[] raw = sqlCommands.getBytes("UTF-8");
+        ByteArrayInputStream in = new ByteArrayInputStream(raw);
+        
+        return runScript(in, "UTF-8");
     }
     
     /**

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java?rev=433331&r1=433330&r2=433331&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/utilMain.java Mon Aug 21
12:19:07 2006
@@ -83,6 +83,11 @@
 	 * displaying a SQLException.
 	 */
 	private final boolean showErrorCode;
+    
+    /**
+     * Value of the system property ij.execptionTrace
+     */
+    private final String ijExceptionTrace;
 
 	protected boolean isJCC;	//The driver being used is JCC
 
@@ -146,6 +151,8 @@
 			Boolean.valueOf(
 					util.getSystemProperty("ij.showErrorCode")
 					).booleanValue();
+        
+        ijExceptionTrace = util.getSystemProperty("ij.exceptionTrace");
 
 		this.numConnections = numConnections;
 		/* 1 StatementFinder and ConnectionEnv per connection/user. */
@@ -359,11 +366,11 @@
 				}
 
     			} catch (ParseException e) {
-                    scriptErrorCount++;
-					if (command != null) doCatch(command);
+ 					if (command != null)
+                        scriptErrorCount += doCatch(command) ? 0 : 1;
 				} catch (TokenMgrError e) {
-                    scriptErrorCount++;
-					if (command != null) doCatch(command);
+ 					if (command != null)
+                        scriptErrorCount += doCatch(command) ? 0 : 1;
     			} catch (SQLException e) {
                     scriptErrorCount++;
 					// SQL exception occurred in ij's actions; print and continue
@@ -491,12 +498,13 @@
 	 * catch processing on failed commands. This really ought to
 	 * be in ij somehow, but it was easier to catch in Main.
 	 */
-	private void doCatch(String command) {
+	private boolean doCatch(String command) {
 		// this retries the failed statement
 		// as a JSQL statement; it uses the
 		// ijParser since that maintains our
 		// connection and state.
 
+        
 	    try {
 			boolean	elapsedTimeOn = ijParser.getElapsedTimeState();
 			long	beginTime = 0;
@@ -515,6 +523,7 @@
 				out.println(langUtil.getTextMessage("IJ_ElapTime0Mil_4", 
 				langUtil.getNumberAsString(endTime - beginTime)));
 			}
+            return true;
 
 	    } catch (SQLException e) {
 			// SQL exception occurred in ij's actions; print and continue
@@ -530,6 +539,7 @@
 	  		out.println(langUtil.getTextMessage("IJ_JavaErro0_7", t.toString()));
 			doTrace(t);
 	    }
+        return false;
 	}
 
 	/**
@@ -578,7 +588,7 @@
 			String st1 = JDBCDisplayUtil.mapNull(e.getSQLState(),langUtil.getTextMessage("IJ_NoSqls"));
 			String st2 = JDBCDisplayUtil.mapNull(e.getMessage(),langUtil.getTextMessage("IJ_NoMess"));
 			out.println(langUtil.getTextMessage("IJ_Erro012",  st1, st2, errorCode));
-			JDBCDisplayUtil.doTrace(out, e);
+			doTrace(e);
 		}
 		if (fatalException != null)
 		{
@@ -590,7 +600,7 @@
 	 * stack trace dumper
 	 */
 	private void doTrace(Throwable t) {
-		if (util.getSystemProperty("ij.exceptionTrace") != null) {
+		if (ijExceptionTrace != null) {
 			t.printStackTrace(out);
 		}
 		out.flush();

Modified: db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java?rev=433331&r1=433330&r2=433331&view=diff
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/tools/ij.java Mon Aug 21 12:19:07 2006
@@ -31,6 +31,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.io.PrintStream;
 import java.io.UnsupportedEncodingException;
 import java.sql.Connection;
@@ -74,11 +75,12 @@
   /**
    * Run a SQL script from an InputStream and write
    * the resulting output to the provided PrintStream.
+   * SQL commands are separated by a semi-colon ';' character.
    * 
    * @param conn Connection to be used as the script's default connection. 
    * @param sqlIn InputStream for the script.
    * @param inputEncoding Encoding of the script.
-   * @param sqlOut PrintStream for the script's output
+   * @param sqlOut OutputStream for the script's output
    * @param outputEncoding Output encoding to use.
    * @return Number of SQLExceptions thrown during the execution, -1 if not known.
    * @throws UnsupportedEncodingException
@@ -87,7 +89,7 @@
 		  Connection conn,
 		  InputStream sqlIn,
 		  String inputEncoding,
-		  PrintStream sqlOut,
+		  OutputStream sqlOut,
 		  String outputEncoding)
 		  throws UnsupportedEncodingException
   {
@@ -119,9 +121,7 @@
 	  
 	  utilMain um = ijE.getutilMain(1, lo);
 
-	  um.goScript(conn, li);
-	  
-	  return -1;
+	  return um.goScript(conn, li);
   }
 
   private ij() { // no instances allowed



Mime
View raw message