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 km...@apache.org on 2007/09/09 21:33:30 UTC

svn commit: r574053 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/suites/ functionTests/testData/ functionTests/testData/ConnectionMethods/ functionTests/tests/jdbc4/ junit/

Author: kmarsden
Date: Sun Sep  9 12:33:29 2007
New Revision: 574053

URL: http://svn.apache.org/viewvc?rev=574053&view=rev
Log:
DERBY-2855 Convert jdbc4/TestConnectionMethods.java to JUnit

Contributed by Ramin Moazeni



Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt   (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionMethodsTest.java   (with props)
Removed:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/jdbc40.runall
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestConnectionMethods_app.properties
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/short.txt
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbyall.properties
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/build.xml
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbyall.properties
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbyall.properties?rev=574053&r1=574052&r2=574053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbyall.properties (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/suites/derbyall.properties Sun Sep  9 12:33:29 2007
@@ -13,6 +13,6 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-suites=derbylang derbynetmats storeall xa derbytools jdbc20 jdbc40 jdk14 jdbcapi derbynetclientmats encryptionAll demo multi unit i18nTest
+suites=derbylang derbynetmats storeall xa derbytools jdbc20 jdk14 jdbcapi derbynetclientmats encryptionAll demo multi unit i18nTest
 derby.debug.true=enableBtreeConsistencyCheck
 derby.stream.error.logSeverityLevel=0

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt?rev=574053&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt Sun Sep  9 12:33:29 2007
@@ -0,0 +1,16 @@
+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.
+
+test data: a string column inserted as an Ascii stream

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/ConnectionMethods/short.txt
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/build.xml
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/build.xml?rev=574053&r1=574052&r2=574053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/build.xml (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/testData/build.xml Sun Sep  9 12:33:29 2007
@@ -51,6 +51,10 @@
       <fileset dir="${derby.testing.src.dir}/${derby.testing.functest.dir}/testData/ResultSetStream"
         includes="*.asc,*.del,*.txt,*.utf,*.gif"/>
     </copy>
+    <copy todir="${out.dir}/${derby.testing.functest.dir}/testData/ConnectionMethods">
+      <fileset dir="${derby.testing.src.dir}/${derby.testing.functest.dir}/testData/ConnectionMethods"
+        includes="*.txt"/>
+    </copy>
   </target>
 
 </project>

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionMethodsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionMethodsTest.java?rev=574053&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionMethodsTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ConnectionMethodsTest.java Sun Sep  9 12:33:29 2007
@@ -0,0 +1,345 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.ConnectionMethodsTest
+
+   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.jdbc4;
+
+import java.util.ArrayList;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ParameterMetaData;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.sql.SQLException;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FilePermission;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.InputStream;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.Statement;
+import java.sql.SQLException;
+import java.sql.Blob;
+import java.sql.Clob;
+import javax.sql.DataSource;
+import java.security.AccessController;
+import java.security.*;
+import org.apache.derbyTesting.junit.NetworkServerTestSetup;
+import org.apache.derby.drda.NetworkServerControl;
+import org.apache.derby.jdbc.ClientDataSource;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+import org.apache.derbyTesting.junit.JDBCDataSource;
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * This class is used to test the implementations of the JDBC 4.0 methods
+ * in the Connection interface
+ */
+public class ConnectionMethodsTest extends BaseJDBCTestCase {
+
+    FileInputStream is;
+
+    public ConnectionMethodsTest(String name) {
+        super(name);
+    }
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite("ConnectionMethodsTest");
+
+        suite.addTest(baseSuite("ConnectionMethodsTest:embedded"));
+
+        suite.addTest(
+                TestConfiguration.clientServerDecorator(
+                baseSuite("ConnectionMethodsTest:client")));
+        return suite;
+    }
+
+    public static Test baseSuite(String name) {
+        TestSuite suite = new TestSuite(ConnectionMethodsTest.class, name);
+        Test test = new SupportFilesSetup(suite, new String[] {"functionTests/testData/ConnectionMethods/short.txt"} );
+        return new CleanDatabaseTestSetup(test) {
+            protected void decorateSQL(Statement s) throws SQLException {
+                s.execute("create table clobtable2(n int,clobcol CLOB)");
+                s.execute("create table blobtable2(n int,blobcol BLOB)");
+
+            }
+        };
+    }
+    /**
+     * Test the createClob method implementation in the Connection interface
+     *
+     * @exception SQLException, FileNotFoundException, Exception if error occurs
+     */
+    public void testCreateClob() throws   SQLException,
+            FileNotFoundException, IOException,
+            Exception{
+
+        Connection conn = getConnection();
+        int b, c;
+        Clob clob;
+
+        Statement s = createStatement();
+
+        PreparedStatement ps =
+                prepareStatement("insert into clobtable2 (n, clobcol)" + " values(?,?)");
+        ps.setInt(1,1000);
+        clob = conn.createClob();
+
+        try {
+            is = (FileInputStream) AccessController.doPrivileged(
+                    new PrivilegedExceptionAction() {
+                public Object run() throws FileNotFoundException {
+                    return new FileInputStream("extin/short.txt");
+                }
+            });
+        } catch (PrivilegedActionException e) {
+            // e.getException() should be an instance of FileNotFoundException,
+            // as only "checked" exceptions will be "wrapped" in a
+            // PrivilegedActionException.
+            throw (FileNotFoundException) e.getException();
+        }
+        OutputStream os = clob.setAsciiStream(1);
+        ArrayList beforeUpdateList = new ArrayList();
+
+        c = is.read();
+        while(c>0) {
+            os.write(c);
+            beforeUpdateList.add(c);
+            c = is.read();
+        }
+        ps.setClob(2, clob);
+        ps.executeUpdate();
+
+        Statement stmt = createStatement();
+        ResultSet rs =
+                stmt.executeQuery("select clobcol from clobtable2 where n = 1000");
+        assertTrue(rs.next());
+
+        clob = rs.getClob(1);
+        assertEquals(beforeUpdateList.size(), clob.length());
+
+        //Get the InputStream from this Clob.
+        InputStream in = clob.getAsciiStream();
+        ArrayList afterUpdateList = new ArrayList();
+
+        b = in.read();
+
+        while (b > -1) {
+            afterUpdateList.add(b);
+            b = in.read();
+        }
+
+        assertEquals(beforeUpdateList.size(), afterUpdateList.size());
+
+        //Now check if the two InputStreams
+        //match
+        for (int i = 0; i < clob.length(); i++) {
+            assertEquals(beforeUpdateList.get(i), afterUpdateList.get(i));
+        }
+
+        os.close();
+        is.close();
+
+    }
+    /**
+     * Test the createBlob method implementation in the Connection interface
+     *
+     * @exception  SQLException, FileNotFoundException, Exception if error occurs
+     */
+    public void testCreateBlob() throws   SQLException,
+            FileNotFoundException,
+            IOException,
+            Exception{
+
+        Connection conn = getConnection();
+        int b, c;
+        Blob blob;
+
+        Statement s = createStatement();
+        PreparedStatement ps =
+                prepareStatement("insert into blobtable2 (n, blobcol)" + " values(?,?)");
+        ps.setInt(1,1000);
+        blob = conn.createBlob();
+
+        try {
+            is = (FileInputStream) AccessController.doPrivileged(
+                    new PrivilegedExceptionAction() {
+                public Object run() throws FileNotFoundException {
+                    return new FileInputStream("extin/short.txt");
+                }
+            });
+        } catch (PrivilegedActionException e) {
+            // e.getException() should be an instance of FileNotFoundException,
+            // as only "checked" exceptions will be "wrapped" in a
+            // PrivilegedActionException.
+            throw (FileNotFoundException) e.getException();
+        }
+
+        OutputStream os = blob.setBinaryStream(1);
+        ArrayList beforeUpdateList = new ArrayList();
+
+        int actualLength = 0;
+        c = is.read();
+        while(c>0) {
+            os.write(c);
+            beforeUpdateList.add(c);
+            c = is.read();
+            actualLength ++;
+        }
+        ps.setBlob(2, blob);
+        ps.executeUpdate();
+
+        Statement stmt = createStatement();
+        ResultSet rs =
+                stmt.executeQuery("select blobcol from blobtable2 where n = 1000");
+        assertTrue(rs.next());
+
+        blob = rs.getBlob(1);
+        assertEquals(beforeUpdateList.size(), blob.length());
+
+        //Get the InputStream from this Blob.
+        InputStream in = blob.getBinaryStream();
+        ArrayList afterUpdateList = new ArrayList();
+
+        b = in.read();
+
+        while (b > -1) {
+            afterUpdateList.add(b);
+            b = in.read();
+        }
+
+        assertEquals(beforeUpdateList.size(), afterUpdateList.size());
+
+        //Now check if the two InputStreams
+        //match
+        for (int i = 0; i < blob.length(); i++) {
+            assertEquals(beforeUpdateList.get(i), afterUpdateList.get(i));
+        }
+
+        os.close();
+        is.close();
+    }
+    /**
+     * Test the Connection.isValid method
+     *
+     * @exception SQLException, Exception if error occurs
+     */
+    public void testConnectionIsValid() throws SQLException, Exception {
+       /*
+        * Test illegal parameter values
+        */
+        Connection conn = getConnection();
+        try {
+            conn.isValid(-1);  // Negative timeout
+            fail("FAIL: isValid(-1): Invalid argument execption not thrown");
+
+        } catch (SQLException e) {
+            assertSQLState("XJ081", e);
+        }
+
+       /*
+        * Test with no timeout
+        */
+        if (!conn.isValid(0)) {
+            fail("FAIL: isValid(0): returned false");
+        }
+
+       /*
+        * Test with a valid timeout
+        */
+        if (!conn.isValid(1)) {
+            fail("FAIL: isValid(1): returned false");
+        }
+
+       /*
+        * Test on a closed connection
+        */
+        try {
+            conn.close();
+        } catch (SQLException e) {
+            assertSQLState("08003", e);
+        }
+
+        if (conn.isValid(0)) {
+            fail("FAIL: isValid(0) on closed connection: returned true");
+        }
+
+        /* Open a new connection and test it */
+        conn = getConnection();
+        if (!conn.isValid(0)) {
+            fail("FAIL: isValid(0) on open connection: returned false");
+        }
+
+       /*
+        * Test on stopped database
+        */
+        TestConfiguration.getCurrent().shutdownDatabase();
+
+        /* Test if that connection is not valid */
+        if (conn.isValid(0)) {
+            fail("FAIL: isValid(0) on stopped database: returned true");
+        }
+
+        /* Start the database by getting a new connection to it */
+        conn = getConnection();
+
+        /* Check that a new connection to the newly started database is valid */
+        if (!conn.isValid(0)) {
+            fail("FAIL: isValid(0) on new connection: " +
+                    "returned false");
+        }
+
+       /*
+        * Test on stopped Network Server client
+        */
+        if ( !usingEmbedded() ) {
+
+            TestConfiguration.getCurrent().stopNetworkServer();
+
+            /* Test that the connection is not valid */
+            if (conn.isValid(0)) {
+                fail("FAIL: isValid(0) on stopped database: returned true");
+            }
+
+           /*
+            * Start the network server and get a new connection and check that
+            * the new connection is valid.
+            */
+            TestConfiguration.getCurrent().startNetworkServer();
+
+            // Get a new connection to the database
+            conn = getConnection();
+
+            /* Check that a new connection to the newly started Derby is valid */
+            if (!conn.isValid(0)) {
+                fail("FAIL: isValid(0) on new connection: returned false");
+            }
+        }
+    }
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/_Suite.java?rev=574053&r1=574052&r2=574053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/_Suite.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/_Suite.java Sun Sep  9 12:33:29 2007
@@ -67,6 +67,7 @@
 		suite.addTest(TestJDBC40Exception.suite());
 		suite.addTest(UnsupportedVetter.suite());
 		suite.addTest(XA40Test.suite());
+		suite.addTest(ConnectionMethodsTest.suite());
         suite.addTest(VerifySignatures.suite());
         suite.addTest (LobStreamTest.suite());
         suite.addTest (BlobSetMethodsTest.suite());

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant?rev=574053&r1=574052&r2=574053&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/copyfiles.ant Sun Sep  9 12:33:29 2007
@@ -1,4 +1,2 @@
 default_app.properties
 TestRowId_app.properties
-TestConnectionMethods_app.properties
-short.txt

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=574053&r1=574052&r2=574053&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 Sun Sep  9 12:33:29 2007
@@ -20,6 +20,10 @@
 package org.apache.derbyTesting.junit;
 
 import java.io.File;
+import java.io.FileOutputStream;
+import java.io.FileNotFoundException;
+import java.io.PrintWriter;
+import java.net.InetAddress;
 import java.lang.reflect.Method;
 import java.security.*;
 import java.sql.Connection;
@@ -31,6 +35,8 @@
 import java.util.Collection;
 import java.util.Hashtable;
 
+import org.apache.derby.drda.NetworkServerControl;
+
 import junit.extensions.TestSetup;
 import junit.framework.Assert;
 import junit.framework.Test;
@@ -72,6 +78,15 @@
     public final static String DEFAULT_SSL = "off";
 
     public  final   static  String  TEST_DBO = "TEST_DBO";
+
+    private FileOutputStream serverOutput;
+		
+    /** Sleep for 500 ms before pinging the network server (again) */
+    private static final int SLEEP_TIME = 1000;
+
+    /* Network Server Control */
+    private NetworkServerControl networkServerController;
+    private NetworkServerControl networkServer;
             
     /**
      * Keys to use to look up values in properties files.
@@ -1079,6 +1094,82 @@
              BaseJDBCTestCase.assertSQLState("Engine shutdown", "XJ015", e);
         }
     }    
+
+   /**
+     * stops the Network server for this configuration.
+     *
+     */
+    public void stopNetworkServer() {
+        try {
+            NetworkServerControl networkServer = new NetworkServerControl();
+            networkServer.shutdown();
+            if (serverOutput != null) {
+                serverOutput.close();
+            }
+        } catch(Exception e) {
+            SQLException se = new SQLException("Error shutting down server");
+            se.initCause(e);
+        }
+    }
+
+   /**
+     * starts the Networs server for this configuration.
+     *
+     */
+    public void startNetworkServer() throws SQLException
+    {
+        Exception failException = null;
+        try {
+
+	    NetworkServerControl networkServer =
+                     new NetworkServerControl(InetAddress.getByName("localhost"), port);
+	    serverOutput = (FileOutputStream)
+            AccessController.doPrivileged(new PrivilegedAction() {
+                public Object run() {
+                    File logs = new File("logs");
+                    logs.mkdir();
+                    File console = new File(logs, "serverConsoleOutput.log");
+                    FileOutputStream fos = null;
+                    try {
+                        fos = new FileOutputStream(console.getPath(), true);
+                    } catch (FileNotFoundException ex) {
+                        ex.printStackTrace();
+                    }
+                    return fos;
+                }
+            });
+
+            networkServer.start(new PrintWriter(serverOutput));
+
+            // Wait for the network server to start
+            boolean started = false;
+            int retries = 10;         // Max retries = max seconds to wait
+
+            while (!started && retries > 0) {
+                try {
+                    // Sleep 1 second and then ping the network server
+                    Thread.sleep(SLEEP_TIME);
+                    networkServer.ping();
+
+                    // If ping does not throw an exception the server has started
+                    started = true;
+                } catch(Exception e) {		   
+                    retries--;
+                    failException = e;
+                 }
+                
+             }
+
+            // Check if we got a reply on ping
+            if (!started) {
+                 throw failException;
+            }
+        } catch (Exception e) {
+            SQLException se = new SQLException("Error starting network  server");
+            se.initCause(failException);
+            throw se;
+      }
+    }
     /**
      * Set the verbosity, i.e., whether debug statements print.
      */