db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r470956 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/lang/DatabaseClassLoadingTest.java junit/JDBC.java junit/SecurityManagerSetup.java junit/TestConfiguration.java
Date Fri, 03 Nov 2006 19:17:21 GMT
Author: djd
Date: Fri Nov  3 11:17:21 2006
New Revision: 470956

URL: http://svn.apache.org/viewvc?view=rev&rev=470956
Log:
DERBY-2033 (partial) Initial test cases for DatabaseClassLoadingTest but test is not ready
to
be added to a suite yet. Fix the no security manager decorator to work correctly as it is
required by this test. Add some utility methods to JDBC for testing result sets.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
  (with props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java?view=auto&rev=470956
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
Fri Nov  3 11:17:21 2006
@@ -0,0 +1,231 @@
+package org.apache.derbyTesting.functionTests.tests.lang;
+
+import java.net.URL;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+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;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.SecurityManagerSetup;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/**
+ * Test database class loading, executing routines from the
+ * installed jars including accessing resources.
+ *
+ */
+public class DatabaseClassLoadingTest extends BaseJDBCTestCase {
+    
+    public DatabaseClassLoadingTest(String name)
+    {
+        super(name);
+    }
+
+    /**
+     * Run the tests only in embedded since this is testing
+     * server side behaviour. Due to DERBY-537 and DERBY-2040
+     * most of the tests are run without a secuirty manager.
+     * Ordering is important here so the fixtures are added
+     * explicitly.
+     */
+    public static Test suite()
+    {
+        final TestSuite suite = new TestSuite("DatabaseClassLoadingTest");
+        
+        suite.addTest(new DatabaseClassLoadingTest("testWithNoInstalledJars"));
+        suite.addTest(
+                SecurityManagerSetup.noSecurityManager(
+                new DatabaseClassLoadingTest("testWithNoClasspath")));
+        suite.addTest(
+                SecurityManagerSetup.noSecurityManager(
+                        new DatabaseClassLoadingTest("testSetClasspath")));
+        
+        suite.addTest(SecurityManagerSetup.noSecurityManager(
+                new DatabaseClassLoadingTest("testAddContact")));
+        
+        suite.addTest(SecurityManagerSetup.noSecurityManager(
+                new DatabaseClassLoadingTest("testGetResource")));        
+        
+        return new CleanDatabaseTestSetup(suite) {
+            protected void decorateSQL(Statement s) throws SQLException
+            {
+                s.executeUpdate("create schema emc");
+                s.executeUpdate("create table emc.contacts " +
+                        "(id int primary key, e_mail varchar(30))");
+                s.executeUpdate(
+                  "create procedure EMC.ADDCONTACT(id INT, e_mail VARCHAR(30)) " +
+                  "MODIFIES SQL DATA " +
+                  "external name 'org.apache.derbyTesting.databaseclassloader.emc.addContact'
" +
+                  "language java parameter style java");
+
+                s.executeUpdate(
+                  "create function EMC.GETARTICLE(path VARCHAR(40)) " +
+                  "RETURNS VARCHAR(256) " +
+                  "NO SQL " +
+                  "external name 'org.apache.derbyTesting.databaseclassloader.emc.getArticle'
" +
+                  "language java parameter style java");
+                }
+        };
+    }
+    
+    /**
+     * Test the routines fail before the jars that contain their
+     * code have been installed and/or set in the classpath.
+     * @throws SQLException
+     */
+    public void testWithNoInstalledJars() throws SQLException {
+        try {
+            prepareCall("CALL EMC.ADDCONTACT(?, ?)");
+            fail("prepareCall on procedure with path to class");
+        } catch (SQLException e) {
+            assertSQLState("42X51", e);
+        }
+        try {
+            prepareStatement("VALUES EMC.GETARTICLE(?)");
+            fail("prepareCall on function with path to class");
+        } catch (SQLException e) {
+            assertSQLState("42X51", e);
+        }
+    }
+    
+    /**
+     * Install the jar, but don't set the classpath.
+     * @throws SQLException
+     */
+    public void testWithNoClasspath() throws SQLException
+    {       
+        URL jar =
+            getTestResource("org/apache/derbyTesting/functionTests/tests/lang/dcl_emc1.jar");
+        
+        assertNotNull(jar);
+        CallableStatement cs = prepareCall("CALL SQLJ.INSTALL_JAR(?, ?, 0)");
+        
+        cs.setString(1, jar.toExternalForm());
+        cs.setString(2, "EMC.MAIL_APP");
+        cs.executeUpdate();
+        cs.close();
+
+        testWithNoInstalledJars();
+    }
+    
+    /**
+     * Set the classpath to include the MAIL_APP jar.
+     * @throws SQLException
+     */
+    public void testSetClasspath() throws SQLException
+    {
+        setDBClasspath("EMC.MAIL_APP");
+        
+        // Test we don't need a re-boot to see the new classes.
+        CallableStatement cs = prepareCall("CALL EMC.ADDCONTACT(?, ?)");
+        cs.setInt(1, 0);
+        cs.setString(2, "now@classpathchange.com");
+        cs.executeUpdate();
+        cs.close();
+        
+        derby2035Workaround();
+    }
+    
+    /**
+     * Test that a new connection successfully sees the changes.
+     * @throws SQLException
+     */
+    public void testAddContact() throws SQLException
+    {
+        CallableStatement cs = prepareCall("CALL EMC.ADDCONTACT(?, ?)");
+        cs.setInt(1, 1);
+        cs.setString(2, "bill@ruletheworld.com");
+        cs.executeUpdate();
+        
+        cs.setInt(1, 2);
+        cs.setString(2, "penguin@antartic.com");
+        cs.executeUpdate();
+        
+        cs.close();
+        
+        Statement s = createStatement();
+        ResultSet rs = s.executeQuery(
+                "SELECT id, e_mail from EMC.CONTACTS ORDER BY 1");
+        
+        JDBC.assertFullResultSet(rs,
+                new String[][] {
+                {"0", "now@classpathchange.com"},
+                {"1", "bill@ruletheworld.com"},
+                {"2", "penguin@antartic.com"},
+                });
+        
+        s.close();
+    }
+    
+    public void testGetResource() throws SQLException
+    {
+        PreparedStatement ps = prepareStatement("VALUES EMC.GETARTICLE(?)");
+        
+        // Simple path should be prepended with the package name
+        // of the class executing the code to find
+        // /org/apache/derbyTesting/databaseclassloader/graduate.txt
+        ps.setString(1, "graduate.txt");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(),
+                "The Apache Foundation has released the first version of " +
+                "the open-source Derby database, which also gained support " +
+                "from Sun Microsystems.");
+        
+
+        // absolute path within the jar.
+        ps.setString(1, "/article/release.txt");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(),
+                "The Apache Derby development community is pleased to announce " +
+                "its first release after graduating from the Apache Incubator, " +
+                "Apache Derby 10.1.1.0.");
+        
+        
+        // Resources that don't exist, returns NULL.
+        ps.setString(1, "barney.txt");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), null);
+        ps.setString(1, "/article/fred.txt");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), null);
+    
+        // Accessing the class file is disallowed as well by
+        // returning a NULL
+        ps.setString(1, "emc.class");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), null);
+        ps.setString(1, "/org/apache/derbyTesting/databaseclassloader/emc.class");
+        JDBC.assertSingleValueResultSet(ps.executeQuery(), null);
+        
+        ps.close();
+    }
+    
+    private void setDBClasspath(String cp) throws SQLException
+    {
+        CallableStatement cs = prepareCall(
+          "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath', ?)");
+
+        cs.setString(1, cp);
+        cs.executeUpdate();
+        cs.close();
+    }
+    
+    private void derby2035Workaround() throws SQLException
+    {
+        // DERBY-2035 Other connections, e.g. the next fixture
+        // do not see the changes related to the new class path
+        // until the database is shutdown. However, the connection
+        // setting the change does see it!
+        // 
+        getConnection().close();
+        try {
+            TestConfiguration.getCurrent().getDefaultConnection(
+                    "shutdown=true");
+            fail("Database failed to shut down");
+        } catch (SQLException e) {
+            assertSQLState("Database shutdown", "08006", e);
+        }  
+    }
+}

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

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java?view=diff&rev=470956&r1=470955&r2=470956
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBC.java Fri Nov  3 11:17:21
2006
@@ -467,6 +467,34 @@
                 expectedColNames[i], rsmd.getColumnName(i+1));
         }
     }
+    
+    /**
+     * Asserts a ResultSet returns a single row with a single
+     * column equal to the passed in String value. The value can
+     * be null to indicate SQL NULL. The comparision is make
+     * using assertFullResultSet in trimmed string mode.
+     */
+    public static void assertSingleValueResultSet(ResultSet rs,
+            String value) throws SQLException
+    {
+        String[] row = new String[] {value};
+        String[][] set = new String[][] {row};
+        assertFullResultSet(rs, set);
+    }
+    
+    /**
+     * assertFullResultSet() using trimmed string comparisions.
+     * Equal to
+     * <code>
+     * assertFullResultSet(rs, expectedRows, true)
+     * </code>
+     */
+    public static void assertFullResultSet(ResultSet rs,
+            String [][] expectedRows)
+            throws SQLException
+     {
+        assertFullResultSet(rs, expectedRows, true);
+     }
 
     /**
      * Takes a result set and a two-dimensional array and asserts
@@ -619,8 +647,8 @@
             {
                 Assert.fail("Column value mismatch @ column '" +
                     rsmd.getColumnName(i+1) + "', row " + rowNum +
-                    ":\n    Expected: " + expectedRow[i] +
-                    "\n    Found:    " + obj);
+                    ":\n    Expected: >" + expectedRow[i] +
+                    "<\n    Found:    >" + obj + "<");
             }
         }
     }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java?view=diff&rev=470956&r1=470955&r2=470956
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/SecurityManagerSetup.java
Fri Nov  3 11:17:21 2006
@@ -101,6 +101,12 @@
 	protected void setUp() throws PrivilegedActionException {
 		installSecurityManager(decoratorPolicyResource);
 	}
+    
+    protected void tearDown() throws Exception
+    {
+        if ("<NONE>".equals(decoratorPolicyResource))
+            BaseTestCase.setSystemProperty("java.security.policy", "");
+    }
 	
 	/**
 	 * Install a SecurityManager with the default test policy

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?view=diff&rev=470956&r1=470955&r2=470956
==============================================================================
--- 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
Fri Nov  3 11:17:21 2006
@@ -819,6 +819,12 @@
     		SecurityManagerSetup.noSecurityManager();
     		return false;
     	} else {
+            if ("<NONE>".equals(
+                    BaseTestCase.getSystemProperty("java.security.policy")))
+            {
+                // Explict setting of no security manager
+                return false;
+            }
     		SecurityManagerSetup.installSecurityManager();
     		return true;
     	}



Mime
View raw message