KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > SQLAuthorizationPropTest


1 /*
2 *
3 * Derby - Class org.apache.derbyTesting.functionTests.lang.SQLAuthorizationPropTest
4 *
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
19 */

20
21 package org.apache.derbyTesting.functionTests.tests.lang;
22
23 import java.io.IOException JavaDoc;
24 import java.sql.CallableStatement JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 import junit.framework.Test;
32 import junit.framework.TestSuite;
33
34 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
35 import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
36 import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
37
38 public class SQLAuthorizationPropTest extends BaseJDBCTestCase {
39
40     public SQLAuthorizationPropTest(String JavaDoc name) {
41         super(name);
42     }
43
44     public static Test suite() {
45         TestSuite suite = new TestSuite();
46         suite.addTestSuite(SQLAuthorizationPropTest.class);
47         
48         // Use DatabasePropertyTestSetup decorator to set the property
49
// required by this test.
50
Properties JavaDoc props = new Properties JavaDoc();
51         props.setProperty("derby.database.sqlAuthorization", "true");
52         Test test = new SQLAuthorizationPropTest("grantRevokeAfterSettingSQLAuthProperty");
53         suite.addTest(new DatabasePropertyTestSetup (test, props));
54         
55         // This test has to be run after SQL authorization property has been
56
// set to true.
57
suite.addTest(new SQLAuthorizationPropTest("resetSQLAuthProperty"));
58         
59         return suite;
60     }
61     
62     /**
63      * Create a table to test grant/revoke statements
64      */

65     protected void setUp() throws SQLException JavaDoc {
66         Statement JavaDoc stmt = createStatement();
67         stmt.execute("create table GR_TAB (id int)");
68         stmt.close();
69     }
70
71     /**
72      * Drop the table created during setup.
73      * @throws Exception
74      */

75     protected void tearDown()
76         throws Exception JavaDoc {
77         Statement JavaDoc stmt = createStatement();
78         stmt.execute("drop table GR_TAB");
79         stmt.close();
80         super.tearDown();
81     }
82     
83     /**
84      * This method tests that grant/revoke is not available if
85      * derby.database.sqlAuthorization property is not set.
86      *
87      * @throws SQLException
88      */

89     public void testGrantRevokeWithoutSQLAuthProperty() throws SQLException JavaDoc{
90         Statement JavaDoc stmt = createStatement();
91         
92         try {
93             stmt.execute("grant select on GR_TAB to some_user");
94             fail("FAIL: Grant statement should have failed when SQL authorization is not set");
95         } catch(SQLException JavaDoc sqle) {
96             assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle);
97         }
98         
99         try {
100             stmt.execute("revoke select on GR_TAB from some_user");
101             fail("FAIL: Revoke statement should have failed when SQL authorization is not set");
102         } catch(SQLException JavaDoc sqle) {
103             assertSQLState(SQLStateConstants.LANG_GRANT_REVOKE_WITH_LEGACY_ACCESS, sqle);
104         }
105         stmt.close();
106     }
107     
108     /**
109      * This method tests that grant/revoke is available
110      * once derby.database.sqlAuthorization property is set to true.
111      *
112      * @throws SQLException
113      */

114     public void grantRevokeAfterSettingSQLAuthProperty() throws SQLException JavaDoc{
115         // Shutdown the database for derby.database.sqlAuthorization property
116
// to take effect. This was set by DatabasePropertyTestSetup decorator.
117
try{
118             getDefaultConnection("shutdown=true");
119             fail("FAIL: Failed to shutdown database");
120         } catch (SQLException JavaDoc sqle) {
121             assertSQLState(SQLStateConstants.SHUTDOWN_DATABASE, sqle);
122         }
123         
124         Statement JavaDoc stmt = createStatement();
125         stmt.execute("grant select on GR_TAB to some_user");
126         stmt.execute("revoke select on GR_TAB from some_user");
127         stmt.close();
128     }
129     
130     /**
131      * This method tests that once derby.database.sqlAuthorization property
132      * has been set to true, it cannot be reset to any other value. For the
133      * test to be valid, it must follow the test method which sets
134      * derby.database.sqlAuthorization property to true.
135      *
136      * @throws SQLException
137      */

138     public void resetSQLAuthProperty() throws SQLException JavaDoc {
139         Connection JavaDoc conn = getConnection();
140         conn.setAutoCommit(false);
141         
142         CallableStatement JavaDoc setDBP = conn.prepareCall(
143             "CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)");
144         setDBP.setString(1, "derby.database.sqlAuthorization");
145         // Resetting to any value other than true should fail
146
testPropertyReset(setDBP, "false");
147         testPropertyReset(setDBP, null);
148         testPropertyReset(setDBP, "some_value");
149         // This should work
150
testPropertyReset(setDBP, "true");
151     }
152     
153     /**
154      * This method executes a callable statement to set the database property
155      * to a given value. It checks that reset to any value other than "true"
156      * fails.
157      *
158      * @param cs CallableStatement object used to set database property
159      * @param value value of database property
160      * @throws SQLException
161      */

162     public void testPropertyReset(CallableStatement JavaDoc cs, String JavaDoc value) throws SQLException JavaDoc {
163
164         cs.setString(2, value);
165         
166         try {
167             cs.executeUpdate();
168             if(value.compareToIgnoreCase("true") != 0)
169                 fail("FAIL: Should not be possible to reset sql authorization once it has been turned on");
170         } catch (SQLException JavaDoc sqle) {
171             assertSQLState(SQLStateConstants.PROPERTY_UNSUPPORTED_CHANGE, sqle);
172         }
173         
174     }
175 }
176
Popular Tags