001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.GetPropertyInfoTest
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.tests.lang;
023:
024: import java.sql.Connection;
025: import java.sql.Driver;
026: import java.sql.DriverManager;
027: import java.sql.DriverPropertyInfo;
028: import java.sql.SQLException;
029: import java.util.Properties;
030:
031: public class GetPropertyInfoTest {
032: static String protocol = "jdbc:derby:";
033: static String url = "EncryptedDB;create=true;dataEncryption=true";
034: static String driver = "org.apache.derby.jdbc.EmbeddedDriver";
035:
036: public static void main(String[] args) throws SQLException,
037: InterruptedException, Exception {
038: boolean passed = true;
039:
040: // adjust URL to compensate for other encryption providers
041: String provider = System.getProperty("testEncryptionProvider");
042: if (provider != null) {
043: url = "EncryptedDB;create=true;dataEncryption=true;encryptionProvider="
044: + provider;
045: }
046:
047: System.out.println("Test GetPropertyInfoTest starting");
048: try {
049: Properties info = new Properties();
050: Class.forName(driver).newInstance();
051: Driver cDriver = DriverManager.getDriver(protocol);
052: boolean canConnect = false;
053:
054: // Test getPropertyInfo by passing attributes in the
055: // url.
056: for (int i = 0; i < 2; i++) {
057: // In order to check for inadequate properties, we omit the
058: // bootPassword and call getPropertyInfo. (bootPassword is
059: // required because dataEncryption is true)
060: DriverPropertyInfo[] attributes = cDriver
061: .getPropertyInfo(protocol + url, info);
062:
063: // zero length means a connection attempt can be made
064: if (attributes.length == 0) {
065: canConnect = true;
066: break;
067: }
068:
069: for (int j = 0; j < attributes.length; j++) {
070: System.out.print(attributes[j].name + " - value: "
071: + attributes[j].value);
072: // Also check on the other PropertyInfo fields
073: String[] choices = attributes[j].choices;
074: System.out.print(" - description: "
075: + attributes[j].description
076: + " - required " + attributes[j].required);
077: if (choices != null) {
078: for (int k = 0; k < choices.length; k++) {
079: System.out.print(" - choices [" + k
080: + "] : " + choices[k]);
081: }
082: System.out.print("\n");
083: } else
084: System.out.print(" - choices null \n");
085: }
086:
087: // Now set bootPassword and call getPropertyInfo again.
088: // This time attribute length should be zero, sice we pass all
089: // minimum required properties.
090: url = url + ";bootPassword=db2everyplace";
091: }
092:
093: if (canConnect == false) {
094: System.out
095: .println("More attributes are required to connect to the database");
096: passed = false;
097: } else {
098: Connection conn = DriverManager.getConnection(protocol
099: + url, info);
100: conn.close();
101: }
102:
103: canConnect = false;
104:
105: // Test getPropertyInfo by passing attributes in the
106: // Properties array.
107: info.put("create", "true");
108: info.put("dataEncryption", "true");
109: info.put("bootPassword", "db2everyplace");
110: // Use alternate encryption provider if necessary.
111: if (provider != null) {
112: info.put("encryptionProvider", provider);
113: }
114:
115: for (int i = 0; i < 2; i++) {
116: // In order to check for inadequate properties, we omit the
117: // database name and call getPropertyInfo.
118: DriverPropertyInfo[] attributes = cDriver
119: .getPropertyInfo(protocol, info);
120:
121: // zero length means a connection attempt can be made
122: if (attributes.length == 0) {
123: canConnect = true;
124: break;
125: }
126:
127: for (int j = 0; j < attributes.length; j++) {
128: System.out.print(attributes[j].name + " - value: "
129: + attributes[j].value);
130: // Also check on the other PropertyInfo fields
131: String[] choices = attributes[j].choices;
132: System.out.print(" - description: "
133: + attributes[j].description
134: + " - required " + attributes[j].required);
135: if (choices != null) {
136: for (int k = 0; k < choices.length; k++) {
137: System.out.print(" - choices [" + k
138: + "] : " + choices[k]);
139: }
140: System.out.print("\n");
141: } else
142: System.out.print(" - choices null \n");
143: }
144:
145: // Now set database name and call getPropertyInfo again.
146: // This time attribute length should be zero, sice we pass all
147: // minimum required properties.
148: info.put("databaseName", "EncryptedDB1");
149: }
150:
151: if (canConnect == false) {
152: System.out
153: .println("More attributes are required to connect to the database");
154: passed = false;
155: } else {
156: Connection conn1 = DriverManager.getConnection(
157: protocol, info);
158: conn1.close();
159: }
160:
161: } catch (SQLException sqle) {
162: passed = false;
163: do {
164: System.out.println(sqle.getSQLState() + ":"
165: + sqle.getMessage());
166: sqle = sqle.getNextException();
167: } while (sqle != null);
168: } catch (Throwable e) {
169: System.out
170: .println("FAIL -- unexpected exception caught in main():\n");
171: System.out.println(e.getMessage());
172: e.printStackTrace();
173: passed = false;
174: }
175:
176: if (passed)
177: System.out.println("Test GetPropertyInfoTest finished");
178: else
179: System.out.println("Test GetPropertyInfoTest failed");
180: }
181: }
|