db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mi...@apache.org
Subject svn commit: r441584 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/lang/SQLAuthorizationPropTest.java functionTests/tests/lang/_Suite.java functionTests/util/SQLStateConstants.java junit/DatabasePropertyTestSetup.java
Date Fri, 08 Sep 2006 17:39:51 GMT
Author: mikem
Date: Fri Sep  8 10:39:50 2006
New Revision: 441584

URL: http://svn.apache.org/viewvc?view=rev&rev=441584
Log:
DERBY-15222
contributed by Deepa Remesh, dremesh@gmail.com

Attaching a patch 'derby1522_v2.diff. It includes a JUnit test for testing the 
switch to SQL standard authorization. It tests following:

1. grant/revoke is not available if derby.database.sqlAuthorization property 
is not set.
2. grant/revoke is available when derby.database.sqlAuthorization is set to true.
3. Once derby.database.sqlAuthorization is set to true, it cannot be set to any other value.

This patch also modifies DatabasePropertyTestSetup.tearDown method. The tearDown method resets
the property values to old values. It will now ignore exceptions when property reset is not
supported. I am including this small change in the above patch. (I had opened DERBY-1827 for
the issue with tearDown method. ). I am using TestUtil.getConnection method to shutdown the
database. I have opened DERBY-1826 to add methods to Derby's JUnit classes for shutdown.



Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SQLAuthorizationPropTest.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/SQLStateConstants.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SQLAuthorizationPropTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SQLAuthorizationPropTest.java?view=auto&rev=441584
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SQLAuthorizationPropTest.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/SQLAuthorizationPropTest.java
Fri Sep  8 10:39:50 2006
@@ -0,0 +1,176 @@
+/*
+*
+* Derby - Class org.apache.derbyTesting.functionTests.lang.SQLAuthorizationPropTest
+*
+* 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.lang;
+
+import java.io.IOException;
+import java.sql.CallableStatement;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Properties;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
+import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
+import org.apache.derbyTesting.functionTests.util.TestUtil;
+
+public class SQLAuthorizationPropTest extends BaseJDBCTestCase {
+
+	public SQLAuthorizationPropTest(String name) {
+		super(name);
+	}
+
+	public static Test suite() {
+		TestSuite suite = new TestSuite();
+		suite.addTestSuite(SQLAuthorizationPropTest.class);
+		
+		// Use DatabasePropertyTestSetup decorator to set the property
+		// required by this test. 
+		Properties props = new Properties();
+	    props.setProperty("derby.database.sqlAuthorization", "true");
+	    Test test = new SQLAuthorizationPropTest("grantRevokeAfterSettingSQLAuthProperty");
+	    suite.addTest(new DatabasePropertyTestSetup (test, props));
+	    
+	    // This test has to be run after SQL authorization property has been 
+	    // set to true. 
+	    suite.addTest(new SQLAuthorizationPropTest("resetSQLAuthProperty"));
+	    
+	    return suite;
+	}
+	
+    /**
+     * Create a table to test grant/revoke statements
+     */
+    protected void setUp() throws SQLException {
+        Statement stmt = createStatement();
+        stmt.execute("create table GR_TAB (id int)");
+        stmt.close();
+    }
+
+    /**
+     * Drop the table created during setup.
+     * @throws Exception 
+     */
+    protected void tearDown()
+        throws Exception {
+        Statement stmt = createStatement();
+        stmt.execute("drop table GR_TAB");
+        stmt.close();
+        super.tearDown();
+    }
+    
+	/**
+	 * This method tests that grant/revoke is not available if 
+	 * derby.database.sqlAuthorization property is not set.
+	 * 
+	 * @throws SQLException
+	 */
+	public void testGrantRevokeWithoutSQLAuthProperty() throws SQLException{
+		Statement stmt = createStatement();
+		
+    	try {
+    		stmt.execute("grant select on GR_TAB to some_user");
+    		fail("FAIL: Grant statement should have failed when SQL authorization is not set");
+    	} catch(SQLException sqle) {
+    		assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle);
+    	}
+    	
+    	try {
+    		stmt.execute("revoke select on GR_TAB from some_user");
+    		fail("FAIL: Revoke statement should have failed when SQL authorization is not set");
+    	} catch(SQLException sqle) {
+    		assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle);
+    	}
+    	stmt.close();
+	}
+	
+	/**
+	 * This method tests that grant/revoke is available 
+	 * once derby.database.sqlAuthorization property is set to true.
+	 * 
+	 * @throws SQLException
+	 */
+	public void grantRevokeAfterSettingSQLAuthProperty() throws SQLException{
+		// Shutdown the database for derby.database.sqlAuthorization property 
+		// to take effect. This was set by DatabasePropertyTestSetup decorator.
+		try{ 
+			TestUtil.getConnection("wombat", "shutdown=true");
+			fail("FAIL: Failed to shutdown database");
+		} catch (SQLException sqle) {
+			assertSQLState(SQLStateConstants.SHUTDOWN_DATABASE, sqle);
+		}
+		
+		Statement stmt = createStatement();
+		stmt.execute("grant select on GR_TAB to some_user");
+    	stmt.execute("revoke select on GR_TAB from some_user");
+    	stmt.close();
+	}
+	
+	/**
+	 * This method tests that once derby.database.sqlAuthorization property 
+	 * has been set to true, it cannot be reset to any other value. For the 
+	 * test to be valid, it must follow the test method which sets 
+	 * derby.database.sqlAuthorization property to true.
+	 * 
+	 * @throws SQLException
+	 */
+	public void resetSQLAuthProperty() throws SQLException {
+		Connection conn = getConnection();
+        conn.setAutoCommit(false);
+        
+        CallableStatement setDBP =  conn.prepareCall(
+            "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
+        setDBP.setString(1, "derby.database.sqlAuthorization");
+        // Resetting to any value other than true should fail
+        testPropertyReset(setDBP, "false");
+        testPropertyReset(setDBP, null);
+        testPropertyReset(setDBP, "some_value");
+        // This should work
+        testPropertyReset(setDBP, "true");
+	}
+	
+	/**
+	 * This method executes a callable statement to set the database property
+	 * to a given value. It checks that reset to any value other than "true" 
+	 * fails.
+	 * 
+	 * @param cs CallableStatement object used to set database property
+	 * @param value value of database property
+	 * @throws SQLException
+	 */
+	public void testPropertyReset(CallableStatement cs, String value) throws SQLException {
+
+		cs.setString(2, value);
+        
+		try {
+        	cs.executeUpdate();
+        	if(value.compareToIgnoreCase("true") != 0)
+        		fail("FAIL: Should not be possible to reset sql authorization once it has been
turned on");
+        } catch (SQLException sqle) {
+        	assertSQLState(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE, sqle);
+        }
+        
+	}
+}

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

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?view=diff&rev=441584&r1=441583&r2=441584
==============================================================================
--- 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
Fri Sep  8 10:39:50 2006
@@ -62,6 +62,11 @@
         suite.addTest(GroupByExpressionTest.suite());
         suite.addTest(MathTrigFunctionsTest.suite());
         suite.addTest(TimeHandlingTest.suite());
+        
+        // This test internally sets derby.database.sqlAuthorization to true
+        // This property is not resettable and hence may affect other tests 
+        // using it and sharing the same database.
+        suite.addTest(SQLAuthorizationPropTest.suite());
 		
 		// Tests that are compiled using 1.4 target need to
 		// be added this way, otherwise creating the suite

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SQLStateConstants.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SQLStateConstants.java?view=diff&rev=441584&r1=441583&r2=441584
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SQLStateConstants.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/SQLStateConstants.java
Fri Sep  8 10:39:50 2006
@@ -343,4 +343,8 @@
     //isWrapperFor returns false is passed as a parameter to the 
     //unwrap method.
     public static final String UNABLE_TO_UNWRAP = "XJ128";
+    
+    public static final String LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS = "42Z60";
+    public static final String SHUTDOWN_DATABASE = "08006";
+    public static final String PROPERTY_UNSUPPORTED_CHANGE = "XCY02";
 }

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java?view=diff&rev=441584&r1=441583&r2=441584
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DatabasePropertyTestSetup.java
Fri Sep  8 10:39:50 2006
@@ -29,6 +29,8 @@
 
 import junit.framework.Test;
 
+import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
+
 /**
  * Test decorator to set a set of database properties on setUp
  * and restore them to the previous values on tearDown.
@@ -75,15 +77,21 @@
         CallableStatement setDBP =  conn.prepareCall(
             "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, NULL)");
     	// Clear all the system properties set by the new set
-    	// that will not be reset by the old set.
-       	for (Enumeration e = newValues.propertyNames(); e.hasMoreElements();)
-       	{
-       		String key = (String) e.nextElement();
-       		if (oldValues.getProperty(key) == null)
-            {
-                 setDBP.setString(1, key);
-                 setDBP.executeUpdate();
-             }
+    	// that will not be reset by the old set. Ignore any 
+        // invalid property values.
+        try {
+        	for (Enumeration e = newValues.propertyNames(); e.hasMoreElements();)
+        	{
+        		String key = (String) e.nextElement();
+        		if (oldValues.getProperty(key) == null)
+        		{
+        			setDBP.setString(1, key);
+        			setDBP.executeUpdate();
+        		}
+        	}
+        } catch (SQLException sqle) {
+        	if(!sqle.getSQLState().equals(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE))
+        		throw sqle;
         }
     	// and then reset nay old values which will cause the commit.
     	setProperties(oldValues);



Mime
View raw message