KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.store.TestRoutines
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.security.AccessController JavaDoc;
25 import java.security.PrivilegedAction JavaDoc;
26 import java.security.PrivilegedActionException JavaDoc;
27 import java.security.PrivilegedExceptionAction JavaDoc;
28 import java.sql.*;
29 import java.io.*;
30
31 import org.apache.derby.iapi.reference.JDBC30Translation;
32
33
34 /**
35     Utility methods for tests routines, in order to bring some consistency to test output.
36     Any routines added here should be general purpose in nature, not specific to
37     a single test.
38
39     Add a public static method for the test and then add its creation as a procedure
40     or function in installRoutines.
41 */

42 public class TestRoutines {
43
44     /**
45         A single procedure to create all the routines in this file.
46         The script to run this is in testRoutines.sql
47     */

48     public static void installRoutines() throws SQLException {
49
50         Connection conn = DriverManager.getConnection("jdbc:default:connection");
51
52         TestRoutines.installRoutines(conn);
53
54     }
55
56     /**
57         Easy way to install all the routines from a Java test program.
58         Just call with a valid connection.
59         org.apache.derbyTesting.functionTests.util.TestRoutines.installRoutines(conn);
60     */

61     public static void installRoutines(Connection conn) throws SQLException {
62
63         Statement s = conn.createStatement();
64
65         // setSystemProperty
66
s.execute("CREATE PROCEDURE TESTROUTINE.SET_SYSTEM_PROPERTY(IN PROPERTY_KEY VARCHAR(32000), IN PROPERTY_VALUE VARCHAR(32000)) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.setSystemProperty' language java parameter style java");
67
68         // sleep
69
s.execute("CREATE PROCEDURE TESTROUTINE.SLEEP(IN SLEEP_TIME_MS BIGINT) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.sleep' language java parameter style java");
70
71         s.execute("CREATE FUNCTION TESTROUTINE.HAS_SECURITY_MANAGER() RETURNS INT NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.hasSecurityManager' language java parameter style java");
72
73         s.execute("CREATE FUNCTION TESTROUTINE.READ_FILE(FILE_NAME VARCHAR(60), ENCODING VARCHAR(60)) RETURNS VARCHAR(32000) NO SQL EXTERNAL NAME 'org.apache.derbyTesting.functionTests.util.TestRoutines.readFile' language java parameter style java");
74         s.close();
75     }
76
77
78     /**
79         TESTROUTINE.SET_SYSTEM_PROPERTY(IN PROPERTY_KEY VARCHAR(32000), IN PROPERTY_VALUE VARCHAR(32000))
80         Set a system property
81     */

82     public static void setSystemProperty(final String JavaDoc key, final String JavaDoc value) {
83         
84         // needs to run in a privileged block as it will be
85
// called through a SQL statement and thus a generated
86
// class. The generated class on the stack has no permissions
87
// granted to it.
88
AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
89             public Object JavaDoc run() {
90                 System.setProperty(key, value);
91                 return null; // nothing to return
92
}
93         });
94         
95     }
96     /**
97         TESTROUTINE.SLEEP(IN TIME_MS BIGINT)
98         Sleep for a number of milli-seconds.
99     */

100     public static void sleep(long ms) throws InterruptedException JavaDoc {
101
102         Thread.sleep(ms);
103     }
104     
105     /**
106      * TESTROUTINE.HAS_SECURITY_MANAGER()
107      * Return 0 is no security manager is installed, 1 if one is.
108      * @return
109      */

110     public static int hasSecurityManager()
111     {
112         return System.getSecurityManager() == null ? 0 : 1;
113     }
114     
115     /**
116     TESTROUTINE.READ_FILE(FILE_NAME VARCHAR(60), ENCODING VARCHAR(60)) RETURNS VARCHAR(32000)
117     Read a file using the passed in encoding display its contents
118     as ASCII with unicode esacpes..
119      * @throws PrivilegedActionException
120      * @throws IOException
121    */

122     public static String JavaDoc readFile(final String JavaDoc fileName, final String JavaDoc encoding)
123     throws PrivilegedActionException JavaDoc, IOException
124     {
125
126         // needs to run in a privileged block as it will be
127
// called through a SQL statement and thus a generated
128
// class. The generated class on the stack has no permissions
129
// granted to it.
130
FileInputStream fin = (FileInputStream)
131             AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
132             public Object JavaDoc run() throws FileNotFoundException {
133                 return new FileInputStream(fileName); // nothing to return
134
}
135         });
136         
137         InputStreamReader isr = new InputStreamReader(
138                 new BufferedInputStream(fin, 32*1024), encoding);
139                 
140         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
141         for (;;)
142         {
143             int ci = isr.read();
144             if (ci < 0)
145                 break;
146             
147             if (ci <= 0x7f)
148             {
149                 sb.append((char) ci);
150             }
151             else
152             {
153                 sb.append("\\u");
154                 String JavaDoc hex = Integer.toHexString(ci);
155                             
156                 switch (hex.length())
157                 {
158                 case 2:
159                     sb.append("00");
160                     break;
161                 case 3:
162                     sb.append("0");
163                     break;
164                 }
165                 sb.append(hex);
166             }
167         }
168
169         return sb.toString();
170     }
171 }
172
173
174
175
Popular Tags