KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.GetPropertyInfoTest
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.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.Driver JavaDoc;
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.DriverPropertyInfo JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.util.Properties JavaDoc;
30
31
32 public class GetPropertyInfoTest
33 {
34     static String JavaDoc protocol = "jdbc:derby:";
35     static String JavaDoc url = "EncryptedDB;create=true;dataEncryption=true";
36     static String JavaDoc driver = "org.apache.derby.jdbc.EmbeddedDriver";
37
38     public static void main(String JavaDoc[] args) throws SQLException JavaDoc,
39         InterruptedException JavaDoc, Exception JavaDoc
40     {
41         boolean passed = true;
42
43         // adjust URL to compensate for other encryption providers
44
String JavaDoc provider = System.getProperty("testEncryptionProvider");
45         if (provider != null)
46         {
47             url = "EncryptedDB;create=true;dataEncryption=true;encryptionProvider=" + provider;
48         }
49
50         System.out.println("Test GetPropertyInfoTest starting");
51         try
52         {
53             Properties JavaDoc info = new Properties JavaDoc();
54             Class.forName(driver).newInstance();
55             Driver JavaDoc cDriver = DriverManager.getDriver(protocol);
56             boolean canConnect = false;
57
58             // Test getPropertyInfo by passing attributes in the
59
// url.
60
for(int i = 0; i < 2; i++)
61             {
62                 // In order to check for inadequate properties, we omit the
63
// bootPassword and call getPropertyInfo. (bootPassword is
64
// required because dataEncryption is true)
65
DriverPropertyInfo JavaDoc[] attributes = cDriver.getPropertyInfo(protocol+url,info);
66                 
67                 // zero length means a connection attempt can be made
68
if (attributes.length == 0)
69                 {
70                     canConnect = true;
71                     break;
72                 }
73
74                 for (int j = 0; j < attributes.length; j++)
75                 {
76                     System.out.print(attributes[j].name + " - value: " + attributes[j].value);
77                     // Also check on the other PropertyInfo fields
78
String JavaDoc[] choices = attributes[j].choices;
79                     System.out.print(" - description: "
80                         + attributes[j].description +
81                         " - required " + attributes[j].required);
82                     if (choices != null)
83                     {
84                         for (int k = 0; k < choices.length; k++)
85                         {
86                             System.out.print(" - choices [" + k + "] : " + choices[k]);
87                         }
88                         System.out.print("\n");
89                     }
90                     else
91                         System.out.print(" - choices null \n");
92                 }
93
94                 // Now set bootPassword and call getPropertyInfo again.
95
// This time attribute length should be zero, sice we pass all
96
// minimum required properties.
97
url = url + ";bootPassword=db2everyplace";
98             }
99
100             if(canConnect == false)
101             {
102                 System.out.println("More attributes are required to connect to the database");
103                 passed = false;
104             }
105             else
106             {
107                 Connection JavaDoc conn = DriverManager.getConnection(protocol + url, info);
108                 conn.close();
109             }
110         
111             canConnect = false;
112
113             // Test getPropertyInfo by passing attributes in the
114
// Properties array.
115
info.put("create", "true");
116             info.put("dataEncryption", "true");
117             info.put("bootPassword", "db2everyplace");
118             // Use alternate encryption provider if necessary.
119
if (provider != null)
120             {
121                 info.put("encryptionProvider", provider);
122             }
123
124             for(int i = 0; i < 2; i++)
125             {
126                 // In order to check for inadequate properties, we omit the
127
// database name and call getPropertyInfo.
128
DriverPropertyInfo JavaDoc[] attributes = cDriver.getPropertyInfo(protocol,info);
129             
130                 // zero length means a connection attempt can be made
131
if (attributes.length == 0)
132                 {
133                     canConnect = true;
134                     break;
135                 }
136
137                 for (int j = 0; j < attributes.length; j++)
138                 {
139                     System.out.print(attributes[j].name + " - value: " + attributes[j].value);
140                     // Also check on the other PropertyInfo fields
141
String JavaDoc[] choices = attributes[j].choices;
142                     System.out.print(" - description: "
143                         + attributes[j].description +
144                         " - required " + attributes[j].required);
145                     if (choices != null)
146                     {
147                         for (int k = 0; k < choices.length; k++)
148                         {
149                             System.out.print(" - choices [" + k + "] : " + choices[k]);
150                         }
151                         System.out.print("\n");
152                     }
153                     else
154                         System.out.print(" - choices null \n");
155                 }
156
157                 // Now set database name and call getPropertyInfo again.
158
// This time attribute length should be zero, sice we pass all
159
// minimum required properties.
160
info.put("databaseName", "EncryptedDB1");
161             }
162
163             if(canConnect == false)
164             {
165                 System.out.println("More attributes are required to connect to the database");
166                 passed = false;
167             }
168             else
169             {
170                 Connection JavaDoc conn1 = DriverManager.getConnection(protocol, info);
171                 conn1.close();
172             }
173
174         }
175         catch(SQLException JavaDoc sqle)
176         {
177             passed = false;
178             do {
179                 System.out.println(sqle.getSQLState() + ":" + sqle.getMessage());
180                 sqle = sqle.getNextException();
181             } while (sqle != null);
182         }
183         catch (Throwable JavaDoc e)
184         {
185             System.out.println("FAIL -- unexpected exception caught in main():\n");
186             System.out.println(e.getMessage());
187             e.printStackTrace();
188             passed = false;
189         }
190
191         if(passed)
192             System.out.println("Test GetPropertyInfoTest finished");
193         else
194             System.out.println("Test GetPropertyInfoTest failed");
195     }
196 }
197
198     
199
200
201
202
203
204
205
206
207
208
Popular Tags