001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.store.TestRoutines
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.util;
023:
024: import java.security.AccessController;
025: import java.security.PrivilegedAction;
026: import java.security.PrivilegedActionException;
027: import java.security.PrivilegedExceptionAction;
028: import java.sql.*;
029: import java.io.*;
030:
031: import org.apache.derby.iapi.reference.JDBC30Translation;
032:
033: /**
034: Utility methods for tests routines, in order to bring some consistency to test output.
035: Any routines added here should be general purpose in nature, not specific to
036: a single test.
037:
038: Add a public static method for the test and then add its creation as a procedure
039: or function in installRoutines.
040: */
041: public class TestRoutines {
042:
043: /**
044: A single procedure to create all the routines in this file.
045: The script to run this is in testRoutines.sql
046: */
047: public static void installRoutines() throws SQLException {
048:
049: Connection conn = DriverManager
050: .getConnection("jdbc:default:connection");
051:
052: TestRoutines.installRoutines(conn);
053:
054: }
055:
056: /**
057: Easy way to install all the routines from a Java test program.
058: Just call with a valid connection.
059: org.apache.derbyTesting.functionTests.util.TestRoutines.installRoutines(conn);
060: */
061: public static void installRoutines(Connection conn)
062: throws SQLException {
063:
064: Statement s = conn.createStatement();
065:
066: // setSystemProperty
067: s
068: .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");
069:
070: // sleep
071: s
072: .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");
073:
074: s
075: .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");
076:
077: s
078: .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");
079: s.close();
080: }
081:
082: /**
083: TESTROUTINE.SET_SYSTEM_PROPERTY(IN PROPERTY_KEY VARCHAR(32000), IN PROPERTY_VALUE VARCHAR(32000))
084: Set a system property
085: */
086: public static void setSystemProperty(final String key,
087: final String value) {
088:
089: // needs to run in a privileged block as it will be
090: // called through a SQL statement and thus a generated
091: // class. The generated class on the stack has no permissions
092: // granted to it.
093: AccessController.doPrivileged(new PrivilegedAction() {
094: public Object run() {
095: System.setProperty(key, value);
096: return null; // nothing to return
097: }
098: });
099:
100: }
101:
102: /**
103: TESTROUTINE.SLEEP(IN TIME_MS BIGINT)
104: Sleep for a number of milli-seconds.
105: */
106: public static void sleep(long ms) throws InterruptedException {
107:
108: Thread.sleep(ms);
109: }
110:
111: /**
112: * TESTROUTINE.HAS_SECURITY_MANAGER()
113: * Return 0 is no security manager is installed, 1 if one is.
114: * @return
115: */
116: public static int hasSecurityManager() {
117: return System.getSecurityManager() == null ? 0 : 1;
118: }
119:
120: /**
121: TESTROUTINE.READ_FILE(FILE_NAME VARCHAR(60), ENCODING VARCHAR(60)) RETURNS VARCHAR(32000)
122: Read a file using the passed in encoding display its contents
123: as ASCII with unicode esacpes..
124: * @throws PrivilegedActionException
125: * @throws IOException
126: */
127: public static String readFile(final String fileName,
128: final String encoding) throws PrivilegedActionException,
129: IOException {
130:
131: // needs to run in a privileged block as it will be
132: // called through a SQL statement and thus a generated
133: // class. The generated class on the stack has no permissions
134: // granted to it.
135: FileInputStream fin = (FileInputStream) AccessController
136: .doPrivileged(new PrivilegedExceptionAction() {
137: public Object run() throws FileNotFoundException {
138: return new FileInputStream(fileName); // nothing to return
139: }
140: });
141:
142: InputStreamReader isr = new InputStreamReader(
143: new BufferedInputStream(fin, 32 * 1024), encoding);
144:
145: StringBuffer sb = new StringBuffer();
146: for (;;) {
147: int ci = isr.read();
148: if (ci < 0)
149: break;
150:
151: if (ci <= 0x7f) {
152: sb.append((char) ci);
153: } else {
154: sb.append("\\u");
155: String hex = Integer.toHexString(ci);
156:
157: switch (hex.length()) {
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: }
|