1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package ifc.sdbc;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import lib.MultiMethodTest;
31*cdf0e10cSrcweir import lib.Status;
32*cdf0e10cSrcweir import lib.StatusException;
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
35*cdf0e10cSrcweir import com.sun.star.sdbc.DriverPropertyInfo;
36*cdf0e10cSrcweir import com.sun.star.sdbc.SQLException;
37*cdf0e10cSrcweir import com.sun.star.sdbc.XConnection;
38*cdf0e10cSrcweir import com.sun.star.sdbc.XDriver;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /**
41*cdf0e10cSrcweir * Testing <code>com.sun.star.sdbc.XDriver</code>
42*cdf0e10cSrcweir * interface methods :
43*cdf0e10cSrcweir * <ul>
44*cdf0e10cSrcweir *  <li><code> connect()</code></li>
45*cdf0e10cSrcweir *  <li><code> acceptsURL()</code></li>
46*cdf0e10cSrcweir *  <li><code> getPropertyInfo()</code></li>
47*cdf0e10cSrcweir *  <li><code> getMajorVersion()</code></li>
48*cdf0e10cSrcweir *  <li><code> getMinorVersion()</code></li>
49*cdf0e10cSrcweir * </ul> <p>
50*cdf0e10cSrcweir * Required object relations :
51*cdf0e10cSrcweir * <ul>
52*cdf0e10cSrcweir * <li> <code>'XDriver.URL'</code>:
53*cdf0e10cSrcweir *      is the URL of the database to which to connect</code></li>
54*cdf0e10cSrcweir * <li><code>'XDriver.UNSUITABLE_URL'</code>:
55*cdf0e10cSrcweir *      the wrong kind of URL to connect using given driver</li>
56*cdf0e10cSrcweir * <li><code>'XDriver.INFO'</code>:
57*cdf0e10cSrcweir *      a list of arbitrary string tag/value pairs as connection arguments</li>
58*cdf0e10cSrcweir * </ul> <p>
59*cdf0e10cSrcweir * @see com.sun.star.sdbc.XDriver
60*cdf0e10cSrcweir */
61*cdf0e10cSrcweir public class _XDriver extends MultiMethodTest {
62*cdf0e10cSrcweir     // oObj filled by MultiMethodTest
63*cdf0e10cSrcweir     public XDriver oObj = null;
64*cdf0e10cSrcweir     String url = null;
65*cdf0e10cSrcweir     String wrongUrl = null;
66*cdf0e10cSrcweir     String nbu = null;
67*cdf0e10cSrcweir     PropertyValue[] info = null;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir     /**
70*cdf0e10cSrcweir     * Retrieves relations.
71*cdf0e10cSrcweir     * @throw StatusException If any relation not found.
72*cdf0e10cSrcweir     */
73*cdf0e10cSrcweir     protected void before() {
74*cdf0e10cSrcweir         nbu = (String) tEnv.getObjRelation("NoBadURL");
75*cdf0e10cSrcweir         url = (String)tEnv.getObjRelation("XDriver.URL");
76*cdf0e10cSrcweir         if (url == null) {
77*cdf0e10cSrcweir             throw new StatusException(Status.failed(
78*cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.URL'"));
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir         wrongUrl = (String)tEnv.getObjRelation("XDriver.UNSUITABLE_URL");
81*cdf0e10cSrcweir         if (wrongUrl == null) {
82*cdf0e10cSrcweir             throw new StatusException(Status.failed(
83*cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.WRONG_URL'"));
84*cdf0e10cSrcweir         }
85*cdf0e10cSrcweir         info = (PropertyValue[])tEnv.getObjRelation("XDriver.INFO");
86*cdf0e10cSrcweir         if (info == null) {
87*cdf0e10cSrcweir             throw new StatusException(Status.failed(
88*cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.INFO'"));
89*cdf0e10cSrcweir         }
90*cdf0e10cSrcweir     }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir     /**
93*cdf0e10cSrcweir     * Connects to <code>'XDriver.URL'</code>,
94*cdf0e10cSrcweir     * to <code>'XDriver.UNSUITABLE_URL'</code> and to wrong URL using
95*cdf0e10cSrcweir     * <code>'XDriver.INFO'</code>.
96*cdf0e10cSrcweir     * Has OK status if the method returns not null for <code>'XDriver.URL'</code>,
97*cdf0e10cSrcweir     * null for <code>'XDriver.UNSUITABLE_URL'</code> and
98*cdf0e10cSrcweir     * exception was thrown during the call with a wrong URL.
99*cdf0e10cSrcweir     */
100*cdf0e10cSrcweir     public void _connect() {
101*cdf0e10cSrcweir         boolean res = true;
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir         try {
104*cdf0e10cSrcweir             log.println("Trying to connect to " + url);
105*cdf0e10cSrcweir             XConnection connection = oObj.connect(url, info);
106*cdf0e10cSrcweir             res = (connection != null);
107*cdf0e10cSrcweir             log.println("Connected? " + res);
108*cdf0e10cSrcweir             log.println("Trying to connect to " + wrongUrl);
109*cdf0e10cSrcweir             connection = oObj.connect(wrongUrl, info);
110*cdf0e10cSrcweir             res &= (connection == null);
111*cdf0e10cSrcweir             log.println("Connected? " + !res);
112*cdf0e10cSrcweir         } catch(SQLException e) {
113*cdf0e10cSrcweir             log.println("Unexpected exception");
114*cdf0e10cSrcweir             res &= false;
115*cdf0e10cSrcweir         }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir         if (nbu==null) {
118*cdf0e10cSrcweir             try {
119*cdf0e10cSrcweir                 String badUrl = url + "bla";
120*cdf0e10cSrcweir                 log.println("Trying to connect to " + badUrl);
121*cdf0e10cSrcweir                 oObj.connect(badUrl, info);
122*cdf0e10cSrcweir                 res &= false;
123*cdf0e10cSrcweir                 log.println("Expected exception isn't thrown");
124*cdf0e10cSrcweir             } catch(SQLException e) {
125*cdf0e10cSrcweir                 log.println("Expected exception");
126*cdf0e10cSrcweir                 res &= true;
127*cdf0e10cSrcweir             }
128*cdf0e10cSrcweir         }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         tRes.tested("connect()", res);
131*cdf0e10cSrcweir     }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir     /**
134*cdf0e10cSrcweir     * Calls the method for <code>'XDriver.URL'</code> and
135*cdf0e10cSrcweir     * for <code>'XDriver.UNSUITABLE_URL'</code>.
136*cdf0e10cSrcweir     * Has OK status if the method returns true for <code>'XDriver.URL'</code>
137*cdf0e10cSrcweir     * and false for <code>'XDriver.UNSUITABLE_URL'</code>.
138*cdf0e10cSrcweir     */
139*cdf0e10cSrcweir     public void _acceptsURL() {
140*cdf0e10cSrcweir         boolean res = false;
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir         try {
143*cdf0e10cSrcweir             res = oObj.acceptsURL(url);
144*cdf0e10cSrcweir             log.println("Accepts " + url + "? " + res);
145*cdf0e10cSrcweir             res &= !oObj.acceptsURL(wrongUrl);
146*cdf0e10cSrcweir             log.println("Accepts " + wrongUrl + "? " + !res);
147*cdf0e10cSrcweir         } catch(SQLException e) {
148*cdf0e10cSrcweir             log.println("Unexpected exception");
149*cdf0e10cSrcweir             e.printStackTrace(log);
150*cdf0e10cSrcweir             res = false;
151*cdf0e10cSrcweir         }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir         tRes.tested("acceptsURL()", res);
154*cdf0e10cSrcweir     }
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir     /**
157*cdf0e10cSrcweir     * Calls the method with passed <code>'XDriver.URL'</code> and
158*cdf0e10cSrcweir     * <code>'XDriver.INFO'</code>. Prints obtained driver properties info
159*cdf0e10cSrcweir     * to log.
160*cdf0e10cSrcweir     * Has OK status if returned value isn't null.
161*cdf0e10cSrcweir     */
162*cdf0e10cSrcweir     public void _getPropertyInfo() {
163*cdf0e10cSrcweir         requiredMethod("acceptsURL()");
164*cdf0e10cSrcweir         boolean res = false;
165*cdf0e10cSrcweir         DriverPropertyInfo[] dpi = null;
166*cdf0e10cSrcweir         try {
167*cdf0e10cSrcweir             dpi = oObj.getPropertyInfo(url, info);
168*cdf0e10cSrcweir         } catch(SQLException e) {
169*cdf0e10cSrcweir             log.println("Unexpected exception");
170*cdf0e10cSrcweir             e.printStackTrace(log);
171*cdf0e10cSrcweir             res = false;
172*cdf0e10cSrcweir         }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         if (dpi != null) {
175*cdf0e10cSrcweir             res = true;
176*cdf0e10cSrcweir             log.println("Driver properties info:");
177*cdf0e10cSrcweir             for(int i = 0; i < dpi.length; i++) {
178*cdf0e10cSrcweir                 log.println("Property: " + dpi[i].Name);
179*cdf0e10cSrcweir                 log.println("Description: " + dpi[i].Description);
180*cdf0e10cSrcweir                 log.println("IsRequired? " + dpi[i].IsRequired);
181*cdf0e10cSrcweir                 log.println("Value: " + dpi[i].Value);
182*cdf0e10cSrcweir                 log.println("Choices: ");
183*cdf0e10cSrcweir                 for(int j = 0; j < dpi[i].Choices.length; j++) {
184*cdf0e10cSrcweir                     log.println("\t" + dpi[i].Choices[j]);
185*cdf0e10cSrcweir                 }
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir         }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir         tRes.tested("getPropertyInfo()", res);
190*cdf0e10cSrcweir     }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     /**
193*cdf0e10cSrcweir     * Calls the method.
194*cdf0e10cSrcweir     * Has OK status if returned value is greater than or is equal to 1.
195*cdf0e10cSrcweir     */
196*cdf0e10cSrcweir     public void _getMajorVersion() {
197*cdf0e10cSrcweir         int majorVer = oObj.getMajorVersion();
198*cdf0e10cSrcweir         boolean res = majorVer >= 1;
199*cdf0e10cSrcweir         log.println("Major version " + majorVer);
200*cdf0e10cSrcweir         tRes.tested("getMajorVersion()", res);
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     /**
204*cdf0e10cSrcweir     * Calls the method.
205*cdf0e10cSrcweir     * Has OK status if returned value is greater than or is equal to 0.
206*cdf0e10cSrcweir     */
207*cdf0e10cSrcweir     public void _getMinorVersion() {
208*cdf0e10cSrcweir         int minorVer = oObj.getMinorVersion();
209*cdf0e10cSrcweir         boolean res = minorVer >= 0;
210*cdf0e10cSrcweir         log.println("Minor version " + minorVer);
211*cdf0e10cSrcweir         tRes.tested("getMinorVersion()", res);
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir }