You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2006/08/06 19:07:04 UTC

svn commit: r429155 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests: tests/lang/LangScripts.java util/ScriptTestCase.java

Author: djd
Date: Sun Aug  6 10:07:03 2006
New Revision: 429155

URL: http://svn.apache.org/viewvc?rev=429155&view=rev
Log:
Update the SQL script JUnit test running (ScriptTestCase) to use the new ij.runScript. This allows
the scripts to easily use the default connection from the super class BaseJDBCTest without having
to resort to setting system properties.
Cleanup the LangScripts test case to allow it to be called as a main method to run arbitrary
scripts from the command line.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LangScripts.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LangScripts.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LangScripts.java?rev=429155&r1=429154&r2=429155&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LangScripts.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/LangScripts.java Sun Aug  6 10:07:03 2006
@@ -24,8 +24,11 @@
 
 import org.apache.derbyTesting.functionTests.util.ScriptTestCase;
 
-public class LangScripts extends ScriptTestCase {
+public final class LangScripts extends ScriptTestCase {
 	
+	/**
+	 * All the langauge SQL scripts to be run as JUnit tests.
+	 */
 	private static final String[] SQL_LANGUAGE_TESTS = {
 		"arithmetic",
 		"bit2",
@@ -33,21 +36,42 @@
 		"union",
 		};
 	
-	LangScripts(String langTest){
-		super(langTest);
+	/**
+	 * Run a set of langauge SQL scripts passed in on the
+	 * command line.
+	 * <code>
+	 * example
+	 * java org.apache.derbyTesting.functionTests.tests.lang.LangScripts case union
+	 * </code>
+	 */
+	public static void main(String[] args)
+	{
+		junit.textui.TestRunner.run(getSuite(args));
 	}
 
 	/**
-	 * SQL scripts in the lang folder.
+	 * Return the suite that runs all the langauge SQL scripts.
+	 */
+	public static Test suite() {
+    	return getSuite(SQL_LANGUAGE_TESTS);
+    }
+    
+	/*
+	 * A single JUnit test that runs a single language SQL script.
 	 */
-	protected String getArea() {
-		return "lang";
+	private LangScripts(String langTest){
+		super(langTest);
 	}
 	
-    public static Test suite() {
+    /**
+     * Return a suite of language SQL tests from the list of
+     * script names.
+      */
+	private static Test getSuite(String[] list)
+	{
         TestSuite suite = new TestSuite();
-        for (int i = 0; i < SQL_LANGUAGE_TESTS.length; i++)
-            suite.addTest(new LangScripts(SQL_LANGUAGE_TESTS[i]));
+        for (int i = 0; i < list.length; i++)
+            suite.addTest(new LangScripts(list[i]));
 
         return getIJConfig(suite);
     }

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=429155&r1=429154&r2=429155&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 Sun Aug  6 10:07:03 2006
@@ -27,9 +27,8 @@
 import java.io.InputStreamReader;
 import java.io.PrintStream;
 import java.net.URL;
-import java.util.Properties;
+import java.sql.Connection;
 
-import junit.extensions.TestSetup;
 import junit.framework.Test;
 
 /**
@@ -38,6 +37,9 @@
  *
  */
 public abstract class ScriptTestCase extends BaseJDBCTestCase {
+	
+	private final String inputEncoding;
+	private final String outputEncoding = "US-ASCII";
 
 	/**
 	 * Create a ScriptTestCase to run a single test. 
@@ -47,6 +49,7 @@
 	public ScriptTestCase(String script)
 	{
 		super(script);
+		inputEncoding = "US-ASCII";
 	}
 	
 	/**
@@ -54,25 +57,28 @@
 	 * the .sql script lives, e.g. lang.
 	 * @return
 	 */
-	protected abstract String getArea();
-	
+	protected String getArea() {
+		
+		String name =  getClass().getName();
+		
+		int lastDot = name.lastIndexOf('.');
+		
+		name = name.substring(0, lastDot);
+		
+		lastDot = name.lastIndexOf('.');
+		
+		return name.substring(lastDot+1);
+	}
+		
 	/**
 	 * Get a decorator to setup the ij in order
 	 * to run the test. A sub-class must decorate
 	 * its suite using this call.
 	 */
-	public static TestSetup getIJConfig(Test test)
+	public static Test getIJConfig(Test test)
 	{
-		Properties ij = new Properties();
-
-		//TODO: set from configuration.
-		ij.setProperty("ij.driver", "org.apache.derby.jdbc.EmbeddedDriver");
-		ij.setProperty("ij.database", "jdbc:derby:wombat");
-
-		ij.setProperty("ij.showNoConnectionsAtStart", "true");
-		ij.setProperty("ij.showNoCountForSelect", "true");
-			
-		return new SystemPropertyTestSetup(test, ij);
+		// No decorator needed currently.
+		return test;
 	}
 	
 	/**
@@ -88,31 +94,38 @@
 	public void runTest() throws Throwable
 	{
 		String resource =
-			"/org/apache/derbyTesting/functionTests/tests/"
+			"org/apache/derbyTesting/functionTests/tests/"
 			+ getArea() + "/"
 			+ getName() + ".sql";
 		
 		String canon =
 			"org/apache/derbyTesting/functionTests/master/"
 			+ getName() + ".out";
+
+		URL sql = getTestResource(resource);
+		assertNotNull("SQL script missing: " + resource, sql);
 		
-		PrintStream originalOut = System.out;
+		InputStream sqlIn = sql.openStream();
 		
-		ByteArrayOutputStream rawBytes;
-		try {
-			
-			 rawBytes = new ByteArrayOutputStream(20 * 1024);
-			
-			PrintStream printOut = new PrintStream(rawBytes);
-			System.setOut(printOut);
+		ByteArrayOutputStream rawBytes =
+			new ByteArrayOutputStream(20 * 1024);
 		
-			org.apache.derby.tools.ij.main(new String[] {"-fr", resource});
-			
-			printOut.flush();
+		PrintStream printOut = new PrintStream(rawBytes);
+	
+		Connection conn = getConnection();
+		org.apache.derby.tools.ij.runScript(
+				conn,
+				sqlIn,
+				inputEncoding,
+				printOut,
+				outputEncoding);
+		
+		conn.close();
+		
+		printOut.flush();
+		printOut.close();
+		sqlIn.close();
 			
-		} finally {
-			System.setOut(originalOut);
-		}
 		
 		byte[] testRawBytes = rawBytes.toByteArray();
 		
@@ -123,13 +136,12 @@
 			InputStream canonStream = getTestResource(canon).openStream();
 			
 			BufferedReader cannonReader = new BufferedReader(
-					new InputStreamReader(canonStream));
+					new InputStreamReader(canonStream, outputEncoding));
 			
 			BufferedReader testOutput = new BufferedReader(
 					new InputStreamReader(
-					new ByteArrayInputStream(testRawBytes)));
-			
-			testOutput.readLine();
+					new ByteArrayInputStream(testRawBytes),
+					   outputEncoding));
 			
 			for (int lineNumber = 1; ; lineNumber++)
 			{
@@ -151,7 +163,8 @@
 					fail("Less output from test than expected, stoped at line"
 							+ lineNumber);
 										
-				assertEquals("Output at line " + lineNumber, canonLine, testLine);
+				assertEquals("Output at line " + lineNumber,
+						canonLine, testLine);
 			}
 			
 			cannonReader.close();