1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XDriver.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import lib
.MultiMethodTest
;
35 import lib
.StatusException
;
37 import com
.sun
.star
.beans
.PropertyValue
;
38 import com
.sun
.star
.sdbc
.DriverPropertyInfo
;
39 import com
.sun
.star
.sdbc
.SQLException
;
40 import com
.sun
.star
.sdbc
.XConnection
;
41 import com
.sun
.star
.sdbc
.XDriver
;
44 * Testing <code>com.sun.star.sdbc.XDriver</code>
47 * <li><code> connect()</code></li>
48 * <li><code> acceptsURL()</code></li>
49 * <li><code> getPropertyInfo()</code></li>
50 * <li><code> getMajorVersion()</code></li>
51 * <li><code> getMinorVersion()</code></li>
53 * Required object relations :
55 * <li> <code>'XDriver.URL'</code>:
56 * is the URL of the database to which to connect</code></li>
57 * <li><code>'XDriver.UNSUITABLE_URL'</code>:
58 * the wrong kind of URL to connect using given driver</li>
59 * <li><code>'XDriver.INFO'</code>:
60 * a list of arbitrary string tag/value pairs as connection arguments</li>
62 * @see com.sun.star.sdbc.XDriver
64 public class _XDriver
extends MultiMethodTest
{
65 // oObj filled by MultiMethodTest
66 public XDriver oObj
= null;
68 String wrongUrl
= null;
70 PropertyValue
[] info
= null;
73 * Retrieves relations.
74 * @throw StatusException If any relation not found.
76 protected void before() {
77 nbu
= (String
) tEnv
.getObjRelation("NoBadURL");
78 url
= (String
)tEnv
.getObjRelation("XDriver.URL");
80 throw new StatusException(Status
.failed(
81 "Couldn't get relation 'XDriver.URL'"));
83 wrongUrl
= (String
)tEnv
.getObjRelation("XDriver.UNSUITABLE_URL");
84 if (wrongUrl
== null) {
85 throw new StatusException(Status
.failed(
86 "Couldn't get relation 'XDriver.WRONG_URL'"));
88 info
= (PropertyValue
[])tEnv
.getObjRelation("XDriver.INFO");
90 throw new StatusException(Status
.failed(
91 "Couldn't get relation 'XDriver.INFO'"));
96 * Connects to <code>'XDriver.URL'</code>,
97 * to <code>'XDriver.UNSUITABLE_URL'</code> and to wrong URL using
98 * <code>'XDriver.INFO'</code>.
99 * Has OK status if the method returns not null for <code>'XDriver.URL'</code>,
100 * null for <code>'XDriver.UNSUITABLE_URL'</code> and
101 * exception was thrown during the call with a wrong URL.
103 public void _connect() {
107 log
.println("Trying to connect to " + url
);
108 XConnection connection
= oObj
.connect(url
, info
);
109 res
= (connection
!= null);
110 log
.println("Connected? " + res
);
111 log
.println("Trying to connect to " + wrongUrl
);
112 connection
= oObj
.connect(wrongUrl
, info
);
113 res
&= (connection
== null);
114 log
.println("Connected? " + !res
);
115 } catch(SQLException e
) {
116 log
.println("Unexpected exception");
122 String badUrl
= url
+ "bla";
123 log
.println("Trying to connect to " + badUrl
);
124 oObj
.connect(badUrl
, info
);
126 log
.println("Expected exception isn't thrown");
127 } catch(SQLException e
) {
128 log
.println("Expected exception");
133 tRes
.tested("connect()", res
);
137 * Calls the method for <code>'XDriver.URL'</code> and
138 * for <code>'XDriver.UNSUITABLE_URL'</code>.
139 * Has OK status if the method returns true for <code>'XDriver.URL'</code>
140 * and false for <code>'XDriver.UNSUITABLE_URL'</code>.
142 public void _acceptsURL() {
146 res
= oObj
.acceptsURL(url
);
147 log
.println("Accepts " + url
+ "? " + res
);
148 res
&= !oObj
.acceptsURL(wrongUrl
);
149 log
.println("Accepts " + wrongUrl
+ "? " + !res
);
150 } catch(SQLException e
) {
151 log
.println("Unexpected exception");
152 e
.printStackTrace(log
);
156 tRes
.tested("acceptsURL()", res
);
160 * Calls the method with passed <code>'XDriver.URL'</code> and
161 * <code>'XDriver.INFO'</code>. Prints obtained driver properties info
163 * Has OK status if returned value isn't null.
165 public void _getPropertyInfo() {
166 requiredMethod("acceptsURL()");
168 DriverPropertyInfo
[] dpi
= null;
170 dpi
= oObj
.getPropertyInfo(url
, info
);
171 } catch(SQLException e
) {
172 log
.println("Unexpected exception");
173 e
.printStackTrace(log
);
179 log
.println("Driver properties info:");
180 for(int i
= 0; i
< dpi
.length
; i
++) {
181 log
.println("Property: " + dpi
[i
].Name
);
182 log
.println("Description: " + dpi
[i
].Description
);
183 log
.println("IsRequired? " + dpi
[i
].IsRequired
);
184 log
.println("Value: " + dpi
[i
].Value
);
185 log
.println("Choices: ");
186 for(int j
= 0; j
< dpi
[i
].Choices
.length
; j
++) {
187 log
.println("\t" + dpi
[i
].Choices
[j
]);
192 tRes
.tested("getPropertyInfo()", res
);
197 * Has OK status if returned value is greater than or is equal to 1.
199 public void _getMajorVersion() {
200 int majorVer
= oObj
.getMajorVersion();
201 boolean res
= majorVer
>= 1;
202 log
.println("Major version " + majorVer
);
203 tRes
.tested("getMajorVersion()", res
);
208 * Has OK status if returned value is greater than or is equal to 0.
210 public void _getMinorVersion() {
211 int minorVer
= oObj
.getMinorVersion();
212 boolean res
= minorVer
>= 0;
213 log
.println("Minor version " + minorVer
);
214 tRes
.tested("getMinorVersion()", res
);