KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > T_Authorize


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.T_Authorize
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, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.ResultSetMetaData JavaDoc;
29 import java.sql.SQLException JavaDoc;
30
31 /** Utility functions for testing authorization. */
32 public class T_Authorize
33 {
34
35     public static void verifyAccessRW(int k)
36          throws Exception JavaDoc
37     {
38         verifyAccess(k, false);
39     }
40     public static void verifyAccessRO(int k)
41          throws Exception JavaDoc
42     {
43         verifyAccess(k, true);
44     }
45
46     /**
47       Verify that the database enforces the expected access mode appropriatly.
48       This function depends on DDL performed by the authorize.jsql test.
49       
50       @param k A key for adding/deleting rows in table t.
51       @param shortReadOnly true -> the connection should be ReadOnly
52       */

53     private static void verifyAccess(int k, boolean shouldBeReadOnly)
54          throws Exception JavaDoc
55     {
56         String JavaDoc qText,sText;
57         int[] args = new int[2];
58         int[] qArgs = new int[2];
59         
60         Connection JavaDoc c =
61             DriverManager.getConnection("jdbc:default:connection");
62
63         if (c.isReadOnly() != shouldBeReadOnly)
64             throw new Exception JavaDoc("Connection read-only mode does not match " + shouldBeReadOnly);
65
66         sText = "create table t2 (a int)";
67         verifyExecute(c,sText,0,args,shouldBeReadOnly,0);
68
69         if (!shouldBeReadOnly)
70         {
71             sText = "drop table t2";
72             verifyExecute(c,sText,0,args,shouldBeReadOnly,0);
73         }
74         
75         args[0] = k;
76         sText = "insert into AUTH_TEST.t1 values ?";
77         verifyExecute(c,sText,1,args,shouldBeReadOnly,1);
78         qText = "select a from AUTH_TEST.t1 where a = ?";
79         qArgs[0] = k;
80         verifyResult(c,qText,1,qArgs,!shouldBeReadOnly,Integer.toString(k));
81
82         args[0] = -k;
83         args[1] = k;
84         sText = "update AUTH_TEST.t1 set a=? where a=?";
85         verifyExecute(c,sText,2,args,shouldBeReadOnly,1);
86         qArgs[0] = -k;
87         verifyResult(c,qText,1,qArgs,!shouldBeReadOnly,Integer.toString(-k));
88
89         sText = "delete from AUTH_TEST.t1 where a=?";
90         verifyExecute(c,sText,1,args,shouldBeReadOnly,1);
91         verifyResult(c,qText,1,qArgs,false,null);
92
93         sText = "call sqlj.install_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v1','j1v1.jar', 'extinout/j1v1.jar'), 'APP.J1', 0)";
94         verifyExecute(c,sText,0,args,shouldBeReadOnly,0);
95         qText = "select filename from sys.sysfiles where filename = 'J1'";
96         verifyResult(c,qText,0,qArgs,!shouldBeReadOnly,"J1");
97
98         if (shouldBeReadOnly)
99             sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.IMMUTABLE')";
100         else
101             sText = "call sqlj.replace_jar(AUTH_TEST.resourcefile('org.apache.derbyTesting.functionTests.testData.v2','j1v2.jar', 'extinout/j1v2.jar'), 'APP.J1')";
102         verifyExecute(c,sText,0,args,shouldBeReadOnly,0);
103         verifyResult(c,qText,0,qArgs,!shouldBeReadOnly,"J1"); //RESOLVE: verify jar content
104

105         if (shouldBeReadOnly)
106             sText = "call sqlj.remove_jar('APP.IMMUTABLE', 0)";
107         else
108             sText = "call sqlj.remove_jar('APP.J1', 0)";
109         verifyExecute(c,sText,0,args,shouldBeReadOnly,0);
110         verifyResult(c,qText,0,qArgs,false,null);
111
112         c.close();
113     }
114
115     private static void verifyExecute(Connection JavaDoc c,
116                                       String JavaDoc sText,
117                                       int paramCount,
118                                       int[] args,
119                                       boolean shouldBeReadOnly,
120                                       int expectRowCount)
121          throws Exception JavaDoc
122     {
123
124         PreparedStatement JavaDoc ps = null;
125         try {
126             ps = c.prepareStatement(sText);
127             for (int ix=0;ix<paramCount; ix++)
128                 ps.setInt(ix+1,args[ix]);
129             int rc = ps.executeUpdate();
130             if (shouldBeReadOnly)
131                 throw new Exception JavaDoc("operation incorrectly allowed for read only connection "+sText);
132             if (rc != expectRowCount)
133             {
134                 StringBuffer JavaDoc argSb = new StringBuffer JavaDoc();
135                 for (int ix=0;ix<paramCount;ix++)
136                 {
137                     if (ix!=0) argSb.append(",");
138                     argSb.append(args[ix]);
139                 }
140                 throw new Exception JavaDoc("Incorrect row count "+rc+
141                                     " for "+sText+
142                                     " with args "+argSb);
143                 
144             }
145         }
146
147         catch (SQLException JavaDoc sqle) {
148             String JavaDoc sqlState = sqle.getSQLState();
149             boolean authorizeError = sqlState.equals("25502") ||
150                                      sqlState.equals("25503") ||
151                                      sqlState.equals("25505");
152             if (!(shouldBeReadOnly && authorizeError))
153                 throw new Exception JavaDoc("Unexpected exception for "+sText+
154                                     " ("+sqle+")");
155         }
156
157         finally {
158             if (ps != null)
159                 ps.close();
160         }
161     }
162
163     private static void verifyResult(Connection JavaDoc c,
164                                      String JavaDoc qText,
165                                      int paramCount,
166                                      int[] args,
167                                      boolean expectResult,
168                                      String JavaDoc expect)
169         throws Exception JavaDoc
170     {
171         PreparedStatement JavaDoc ps = c.prepareStatement(qText);
172         for (int ix=0;ix<paramCount; ix++)
173             ps.setInt(ix+1,args[ix]);
174         ResultSet JavaDoc rs = ps.executeQuery();
175         boolean isRow = rs.next();
176         if (expectResult)
177         {
178             if (!isRow) throw new Exception JavaDoc("incorrect row count");
179             ResultSetMetaData JavaDoc rmd = rs.getMetaData();
180             if (rmd.getColumnCount() != 1) new Exception JavaDoc("bad column count");
181             String JavaDoc colVal = rs.getString(1);
182             if (!expect.equals(colVal))
183                 throw new Exception JavaDoc("bad return column "+colVal);
184             isRow = rs.next();
185             if (isRow) throw new Exception JavaDoc("incorrect row count");
186         }
187         else
188         {
189             if (isRow) throw new Exception JavaDoc("incorrect row count");
190         }
191     }
192 }
193
Popular Tags