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/25 01:31:20 UTC
svn commit: r434562 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting:
functionTests/util/CanonTestCase.java
functionTests/util/ScriptTestCase.java junit/BaseTestCase.java
junit/TestConfiguration.java
Author: djd
Date: Thu Aug 24 16:31:20 2006
New Revision: 434562
URL: http://svn.apache.org/viewvc?rev=434562&view=rev
Log:
Add methods to get a consistent folder for JUnit tests to store any failure information in.
Split the output comparision and handling from ScriptTestCase into CanonTestCase.
Added:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.java (with props)
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/ScriptTestCase.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.java?rev=434562&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.java Thu Aug 24 16:31:20 2006
@@ -0,0 +1,142 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.CanonTestCase
+ *
+ * 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.util;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+
+/**
+ * Run a test that compares itself to a master (canon) file.
+ * This is used to support cannon based tests that ran
+ * under the old Derby test harness without having to convert
+ * them. It is not recommended for new tests. New test should
+ * use the JUnit assert mechanisms.
+ *
+ */
+abstract class CanonTestCase extends BaseJDBCTestCase {
+
+ final String outputEncoding = "US-ASCII";
+
+ private ByteArrayOutputStream rawBytes;
+
+ CanonTestCase(String name) {
+ super(name);
+ }
+
+ OutputStream getOutputStream() {
+ return rawBytes = new ByteArrayOutputStream(20 * 1024);
+ }
+
+ /**
+ * Compare the output to the canon provided.
+ *
+ * @param canon
+ * Name of canon as a resource.
+ */
+ void compareCanon(String canon) throws Throwable {
+ rawBytes.flush();
+ rawBytes.close();
+
+ byte[] testRawBytes = rawBytes.toByteArray();
+
+ try {
+ URL canonURL = getTestResource(canon);
+ assertNotNull("No master file " + canon, canonURL);
+
+ InputStream canonStream = openTestResource(canonURL);
+
+ BufferedReader cannonReader = new BufferedReader(
+ new InputStreamReader(canonStream, outputEncoding));
+
+ BufferedReader testOutput = new BufferedReader(
+ new InputStreamReader(
+ new ByteArrayInputStream(testRawBytes),
+ outputEncoding));
+
+ for (int lineNumber = 1;; lineNumber++) {
+ String testLine = testOutput.readLine();
+
+ // Skip blank lines.
+ if ("".equals(testLine))
+ continue;
+
+ String canonLine = cannonReader.readLine();
+
+ if (canonLine == null && testLine == null)
+ break;
+
+ if (canonLine == null)
+ fail("More output from test than expected");
+
+ if (testLine == null)
+ fail("Less output from test than expected, stoped at line"
+ + lineNumber);
+
+ assertEquals("Output at line " + lineNumber, canonLine,
+ testLine);
+ }
+
+ cannonReader.close();
+ testOutput.close();
+ } catch (Throwable t) {
+ dumpForFail(testRawBytes);
+ throw t;
+ }
+ }
+
+ /**
+ * Dump the output that did not compare correctly into the failure folder
+ * with the name this.getName() + ".out".
+ *
+ * @param rawOutput
+ * @throws IOException
+ * @throws PrivilegedActionException
+ */
+ private void dumpForFail(byte[] rawOutput) throws IOException,
+ PrivilegedActionException {
+
+ File folder = getFailureFolder();
+ final File outFile = new File(folder, getName() + ".out");
+
+ OutputStream outStream = (OutputStream) AccessController
+ .doPrivileged(new java.security.PrivilegedExceptionAction() {
+
+ public Object run() throws IOException {
+ return new FileOutputStream(outFile);
+ }
+ });
+
+ outStream.write(rawOutput);
+ outStream.flush();
+ outStream.close();
+ }
+}
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/CanonTestCase.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=434562&r1=434561&r2=434562&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 Thu Aug 24 16:31:20 2006
@@ -19,18 +19,10 @@
*/
package org.apache.derbyTesting.functionTests.util;
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.FileOutputStream;
import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
import java.net.URL;
import java.sql.Connection;
-import org.apache.derbyTesting.junit.BaseJDBCTestCase;
-
import junit.framework.Test;
/**
@@ -38,10 +30,9 @@
* a master output file.
*
*/
-public abstract class ScriptTestCase extends BaseJDBCTestCase {
+public abstract class ScriptTestCase extends CanonTestCase {
private final String inputEncoding;
- private final String outputEncoding = "US-ASCII";
/**
* Create a ScriptTestCase to run a single test
@@ -109,74 +100,20 @@
assertNotNull("SQL script missing: " + resource, sql);
InputStream sqlIn = openTestResource(sql);
-
- ByteArrayOutputStream rawBytes =
- new ByteArrayOutputStream(20 * 1024);
-
+
Connection conn = getConnection();
org.apache.derby.tools.ij.runScript(
conn,
sqlIn,
inputEncoding,
- rawBytes,
+ getOutputStream(),
outputEncoding);
if (!conn.isClosed() && !conn.getAutoCommit())
conn.commit();
- rawBytes.flush();
- rawBytes.close();
sqlIn.close();
-
-
- byte[] testRawBytes = rawBytes.toByteArray();
-
- try {
- URL canonURL = getTestResource(canon);
- assertNotNull("No master file " + canon, canonURL);
-
- InputStream canonStream = getTestResource(canon).openStream();
-
- BufferedReader cannonReader = new BufferedReader(
- new InputStreamReader(canonStream, outputEncoding));
-
- BufferedReader testOutput = new BufferedReader(
- new InputStreamReader(
- new ByteArrayInputStream(testRawBytes),
- outputEncoding));
-
- for (int lineNumber = 1; ; lineNumber++)
- {
- String testLine = testOutput.readLine();
-
- // Skip blank lines.
- if ("".equals(testLine))
- continue;
-
- String canonLine = cannonReader.readLine();
-
- if (canonLine == null && testLine == null)
- break;
-
- if (canonLine == null)
- fail("More output from test than expected");
-
- if (testLine == null)
- fail("Less output from test than expected, stoped at line"
- + lineNumber);
-
- assertEquals("Output at line " + lineNumber,
- canonLine, testLine);
- }
-
- cannonReader.close();
- testOutput.close();
- } catch (Throwable t) {
- FileOutputStream outFile = new FileOutputStream(getName() + ".out");
- outFile.write(testRawBytes);
- outFile.flush();
- outFile.close();
- throw t;
- }
+
+ this.compareCanon(canon);
}
}
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java?rev=434562&r1=434561&r2=434562&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseTestCase.java Thu Aug 24 16:31:20 2006
@@ -21,6 +21,7 @@
import junit.framework.TestCase;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
@@ -65,15 +66,15 @@
* <BR>
* Method is final to ensure security manager is
* enabled by default. Tests should not need to
- * ovveride runTest, instead use test methods
+ * override runTest, instead use test methods
* setUp, tearDown methods and decorators.
*/
public final void runBare() throws Throwable {
// still not ready for prime time
//if (getTestConfiguration().defaultSecurityManagerSetup())
// assertSecurityManager();
-
- super.runBare();
+
+ super.runBare();
}
/**
@@ -82,6 +83,16 @@
public final TestConfiguration getTestConfiguration()
{
return TestConfiguration.getCurrent();
+ }
+
+ /**
+ * Get the folder where a test leaves any information
+ * about its failure.
+ * @return Folder to use.
+ * @see TestConfiguration#getFailureFolder(TestCase)
+ */
+ public final File getFailureFolder() {
+ return getTestConfiguration().getFailureFolder(this);
}
/**
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?rev=434562&r1=434561&r2=434562&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java Thu Aug 24 16:31:20 2006
@@ -19,12 +19,16 @@
*/
package org.apache.derbyTesting.junit;
+import java.io.File;
import java.security.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
+import junit.framework.Test;
+import junit.framework.TestCase;
+
import org.apache.derbyTesting.functionTests.util.TestDataSourceFactory;
/**
@@ -59,6 +63,7 @@
}
/**
+ * WORK IN PROGRESS
* Set this Thread's current configuration for running tests.
* @param config Configuration to set it to.
*/
@@ -66,7 +71,34 @@
{
CURRENT_CONFIG.set(config);
}
-
+ /**
+ * WORK IN PROGRESS
+ * Return a decorator for the passed in tests that sets the
+ * configuration for the client to be Derby's JDBC client
+ * and to start the network server at setUp and shut it
+ * down at tearDown.
+ * <BR>
+ * The database configuration (name etc.) is based upon
+ * the previous configuration.
+ * <BR>
+ * The previous TestConfiguration is restored at tearDown.
+ * @param tests
+ * @return
+ */
+ public static Test derbyClientServerDecorator(Test tests)
+ {
+ TestConfiguration config = TestConfiguration.getCurrent();
+
+ // Already in the correct configuration, do nothing.
+ if (config.getJDBCClient().isDerbyNetClient())
+ return tests;
+
+ TestConfiguration derbyClientConfig = null;
+ // new TestConfiguration(config, JDBCClient.DERBYNETCLIENT);
+
+ return null;
+ }
+
/**
* This constructor creates a TestConfiguration from a Properties object.
*
@@ -111,7 +143,7 @@
/**
* Get the given system property as specified at startup.
*/
- public String getSystemStartupProperty( String key )
+ private String getSystemStartupProperty( String key )
{
return systemStartupProperties.getProperty( key );
}
@@ -342,6 +374,7 @@
//
// jvm.java sets this property to the build jar directory
// if we are using derbyTesting.jar.
+ //
//
return ( !UNUSED.equals( getSystemStartupProperty( "derbyTesting.codejar" ) ) );
@@ -353,6 +386,61 @@
*/
public boolean isSingleLegXA () {
return singleLegXA;
+ }
+
+ /**
+ * Get a folder already created where a test can
+ * write its failure information. The name of the folder,
+ * relative to ${user.dir} is:
+ * <BR>
+ * <code>
+ * fail/client/testclass/testname
+ * <code>
+ * <UL>
+ * <LI> client - value of JDBCClient.getName() for the test's configuration
+ * <LI> testclass - last element of the class name
+ * <LI> testname - value of test.getName()
+ * </UL>
+ */
+ File getFailureFolder(TestCase test){
+
+ StringBuffer sb = new StringBuffer();
+
+ sb.append("fail");
+ sb.append(File.separatorChar);
+ sb.append(getJDBCClient().getName());
+ sb.append(File.separatorChar);
+
+ String className = test.getClass().getName();
+ int lastDot = className.lastIndexOf('.');
+ if (lastDot != -1)
+ className = className.substring(lastDot+1, className.length());
+
+ sb.append(className);
+ sb.append(File.separatorChar);
+ sb.append(test.getName());
+
+ String base = sb.toString().intern();
+ final File folder = new File(base);
+
+ // Create the folder
+ // TODO: Dump this configuration in some human readable format
+ synchronized (base) {
+
+ AccessController.doPrivileged
+ (new java.security.PrivilegedAction(){
+ public Object run(){
+ if (folder.exists()) {
+ // do something
+ }
+ return new Boolean(folder.mkdirs());
+ }
+ }
+ );
+ }
+
+ return folder;
+
}
/**