1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.sdbc;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.sdbc.DriverPropertyInfo;
32 import com.sun.star.sdbc.SQLException;
33 import com.sun.star.sdbc.XConnection;
34 import com.sun.star.sdbc.XDriver;
35 
36 /**
37 * Testing <code>com.sun.star.sdbc.XDriver</code>
38 * interface methods :
39 * <ul>
40 *  <li><code> connect()</code></li>
41 *  <li><code> acceptsURL()</code></li>
42 *  <li><code> getPropertyInfo()</code></li>
43 *  <li><code> getMajorVersion()</code></li>
44 *  <li><code> getMinorVersion()</code></li>
45 * </ul> <p>
46 * Required object relations :
47 * <ul>
48 * <li> <code>'XDriver.URL'</code>:
49 *      is the URL of the database to which to connect</code></li>
50 * <li><code>'XDriver.UNSUITABLE_URL'</code>:
51 *      the wrong kind of URL to connect using given driver</li>
52 * <li><code>'XDriver.INFO'</code>:
53 *      a list of arbitrary string tag/value pairs as connection arguments</li>
54 * </ul> <p>
55 * @see com.sun.star.sdbc.XDriver
56 */
57 public class _XDriver extends MultiMethodTest {
58     // oObj filled by MultiMethodTest
59     public XDriver oObj = null;
60     String url = null;
61     String wrongUrl = null;
62     String nbu = null;
63     PropertyValue[] info = null;
64 
65     /**
66     * Retrieves relations.
67     * @throw StatusException If any relation not found.
68     */
before()69     protected void before() {
70         nbu = (String) tEnv.getObjRelation("NoBadURL");
71         url = (String)tEnv.getObjRelation("XDriver.URL");
72         if (url == null) {
73             throw new StatusException(Status.failed(
74                 "Couldn't get relation 'XDriver.URL'"));
75         }
76         wrongUrl = (String)tEnv.getObjRelation("XDriver.UNSUITABLE_URL");
77         if (wrongUrl == null) {
78             throw new StatusException(Status.failed(
79                 "Couldn't get relation 'XDriver.WRONG_URL'"));
80         }
81         info = (PropertyValue[])tEnv.getObjRelation("XDriver.INFO");
82         if (info == null) {
83             throw new StatusException(Status.failed(
84                 "Couldn't get relation 'XDriver.INFO'"));
85         }
86     }
87 
88     /**
89     * Connects to <code>'XDriver.URL'</code>,
90     * to <code>'XDriver.UNSUITABLE_URL'</code> and to wrong URL using
91     * <code>'XDriver.INFO'</code>.
92     * Has OK status if the method returns not null for <code>'XDriver.URL'</code>,
93     * null for <code>'XDriver.UNSUITABLE_URL'</code> and
94     * exception was thrown during the call with a wrong URL.
95     */
_connect()96     public void _connect() {
97         boolean res = true;
98 
99         try {
100             log.println("Trying to connect to " + url);
101             XConnection connection = oObj.connect(url, info);
102             res = (connection != null);
103             log.println("Connected? " + res);
104             log.println("Trying to connect to " + wrongUrl);
105             connection = oObj.connect(wrongUrl, info);
106             res &= (connection == null);
107             log.println("Connected? " + !res);
108         } catch(SQLException e) {
109             log.println("Unexpected exception");
110             res &= false;
111         }
112 
113         if (nbu==null) {
114             try {
115                 String badUrl = url + "bla";
116                 log.println("Trying to connect to " + badUrl);
117                 oObj.connect(badUrl, info);
118                 res &= false;
119                 log.println("Expected exception isn't thrown");
120             } catch(SQLException e) {
121                 log.println("Expected exception");
122                 res &= true;
123             }
124         }
125 
126         tRes.tested("connect()", res);
127     }
128 
129     /**
130     * Calls the method for <code>'XDriver.URL'</code> and
131     * for <code>'XDriver.UNSUITABLE_URL'</code>.
132     * Has OK status if the method returns true for <code>'XDriver.URL'</code>
133     * and false for <code>'XDriver.UNSUITABLE_URL'</code>.
134     */
_acceptsURL()135     public void _acceptsURL() {
136         boolean res = false;
137 
138         try {
139             res = oObj.acceptsURL(url);
140             log.println("Accepts " + url + "? " + res);
141             res &= !oObj.acceptsURL(wrongUrl);
142             log.println("Accepts " + wrongUrl + "? " + !res);
143         } catch(SQLException e) {
144             log.println("Unexpected exception");
145             e.printStackTrace(log);
146             res = false;
147         }
148 
149         tRes.tested("acceptsURL()", res);
150     }
151 
152     /**
153     * Calls the method with passed <code>'XDriver.URL'</code> and
154     * <code>'XDriver.INFO'</code>. Prints obtained driver properties info
155     * to log.
156     * Has OK status if returned value isn't null.
157     */
_getPropertyInfo()158     public void _getPropertyInfo() {
159         requiredMethod("acceptsURL()");
160         boolean res = false;
161         DriverPropertyInfo[] dpi = null;
162         try {
163             dpi = oObj.getPropertyInfo(url, info);
164         } catch(SQLException e) {
165             log.println("Unexpected exception");
166             e.printStackTrace(log);
167             res = false;
168         }
169 
170         if (dpi != null) {
171             res = true;
172             log.println("Driver properties info:");
173             for(int i = 0; i < dpi.length; i++) {
174                 log.println("Property: " + dpi[i].Name);
175                 log.println("Description: " + dpi[i].Description);
176                 log.println("IsRequired? " + dpi[i].IsRequired);
177                 log.println("Value: " + dpi[i].Value);
178                 log.println("Choices: ");
179                 for(int j = 0; j < dpi[i].Choices.length; j++) {
180                     log.println("\t" + dpi[i].Choices[j]);
181                 }
182             }
183         }
184 
185         tRes.tested("getPropertyInfo()", res);
186     }
187 
188     /**
189     * Calls the method.
190     * Has OK status if returned value is greater than or is equal to 1.
191     */
_getMajorVersion()192     public void _getMajorVersion() {
193         int majorVer = oObj.getMajorVersion();
194         boolean res = majorVer >= 1;
195         log.println("Major version " + majorVer);
196         tRes.tested("getMajorVersion()", res);
197     }
198 
199     /**
200     * Calls the method.
201     * Has OK status if returned value is greater than or is equal to 0.
202     */
_getMinorVersion()203     public void _getMinorVersion() {
204         int minorVer = oObj.getMinorVersion();
205         boolean res = minorVer >= 0;
206         log.println("Minor version " + minorVer);
207         tRes.tested("getMinorVersion()", res);
208     }
209 }