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