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