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.XConnection;
32cdf0e10cSrcweir import com.sun.star.sdbc.XDriverManager;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir /**
35cdf0e10cSrcweir * Testing <code>com.sun.star.sdbc.XDriverManager</code>
36cdf0e10cSrcweir * interface methods :
37cdf0e10cSrcweir * <ul>
38cdf0e10cSrcweir *  <li><code> getConnection()</code></li>
39cdf0e10cSrcweir *  <li><code> getConnectionWithInfo()</code></li>
40cdf0e10cSrcweir *  <li><code> setLoginTimeout()</code></li>
41cdf0e10cSrcweir *  <li><code> getLoginTimeout()</code></li>
42cdf0e10cSrcweir * </ul> <p>
43cdf0e10cSrcweir * Required object relations :
44cdf0e10cSrcweir * <ul>
45cdf0e10cSrcweir * <li> <code>'SDBC.URL'</code>:
46cdf0e10cSrcweir *      is the URL of the database to which to connect using sdbc-driver
47cdf0e10cSrcweir * </code></li>
48cdf0e10cSrcweir * <li> <code>'JDBC.URL'</code>:
49cdf0e10cSrcweir *      is the URL of the database to which to connect using jdbc-driver
50cdf0e10cSrcweir * </code></li>
51cdf0e10cSrcweir * <li> <code>'JDBC.INFO'</code> of type <code>PropertyValue[]</code>:
52cdf0e10cSrcweir *      a list of arbitrary string tag/value pairs as connection arguments;
53cdf0e10cSrcweir *      normally at least a "user" and "password" property should be included
54cdf0e10cSrcweir * </code></li>
55cdf0e10cSrcweir * </ul> <p>
56cdf0e10cSrcweir * @see com.sun.star.sdbc.XDriverManager
57cdf0e10cSrcweir */
58cdf0e10cSrcweir public class _XDriverManager extends MultiMethodTest {
59cdf0e10cSrcweir     // oObj filled by MultiMethodTest
60cdf0e10cSrcweir     public XDriverManager oObj = null;
61cdf0e10cSrcweir     String sdbcURL = null;
62cdf0e10cSrcweir     String jdbcURL = null;
63cdf0e10cSrcweir     PropertyValue[] jdbcINFO = null;
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     /**
66cdf0e10cSrcweir      * Retrieves the required object relations.
67cdf0e10cSrcweir      */
before()68cdf0e10cSrcweir     protected void before() {
69cdf0e10cSrcweir         sdbcURL = (String)tEnv.getObjRelation("SDBC.URL");
70cdf0e10cSrcweir         if (sdbcURL == null) {
71cdf0e10cSrcweir             throw new StatusException(
72cdf0e10cSrcweir                 Status.failed("Couldn't get relation 'SDBC.URL'"));
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         jdbcURL = (String)tEnv.getObjRelation("JDBC.URL");
75cdf0e10cSrcweir         if (jdbcURL == null) {
76cdf0e10cSrcweir             throw new StatusException(
77cdf0e10cSrcweir                 Status.failed("Couldn't get relation 'JDBC.URL'"));
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir         jdbcINFO = (PropertyValue[])tEnv.getObjRelation("JDBC.INFO");
80cdf0e10cSrcweir         if (jdbcINFO == null) {
81cdf0e10cSrcweir             throw new StatusException(
82cdf0e10cSrcweir                 Status.failed("Couldn't get relation 'JDBC.INFO'"));
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     /**
87cdf0e10cSrcweir      * Calls the method with the url received from the relation
88cdf0e10cSrcweir      * <code>SDBC.URL</code>.
89cdf0e10cSrcweir      * Has OK status if exception wasn't thrown and
90cdf0e10cSrcweir      * if returned value isn't null.
91cdf0e10cSrcweir      */
_getConnection()92cdf0e10cSrcweir     public void _getConnection() {
93cdf0e10cSrcweir         boolean res = true;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         try {
96cdf0e10cSrcweir             log.println("getConnection(" + sdbcURL + ")");
97cdf0e10cSrcweir             XConnection connection = oObj.getConnection(sdbcURL);
98cdf0e10cSrcweir             res = connection != null;
99cdf0e10cSrcweir         } catch(com.sun.star.sdbc.SQLException e) {
100cdf0e10cSrcweir             log.println("Unexpected exception");
101cdf0e10cSrcweir             e.printStackTrace(log);
102cdf0e10cSrcweir             res = false;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         tRes.tested("getConnection()", res);
106cdf0e10cSrcweir     }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     /**
109cdf0e10cSrcweir      * Calls the method with the url received from the relation
110cdf0e10cSrcweir      * <code>JDBC.URL</code> and with info received from the relation
111cdf0e10cSrcweir      * <code>JDBC.INFO</code>.
112cdf0e10cSrcweir      * Has OK status if exception wasn't thrown and
113cdf0e10cSrcweir      * if returned value isn't null.
114cdf0e10cSrcweir      */
_getConnectionWithInfo()115cdf0e10cSrcweir     public void _getConnectionWithInfo() {
116cdf0e10cSrcweir         boolean res = true;
117cdf0e10cSrcweir 
118cdf0e10cSrcweir         try {
119cdf0e10cSrcweir             log.println("getConnectionWithInfo(" + jdbcURL + ")");
120cdf0e10cSrcweir             XConnection connection =
121cdf0e10cSrcweir                 oObj.getConnectionWithInfo(jdbcURL, jdbcINFO);
122cdf0e10cSrcweir             res = connection != null;
123cdf0e10cSrcweir         } catch(com.sun.star.sdbc.SQLException e) {
124cdf0e10cSrcweir             log.println("Unexpected exception");
125cdf0e10cSrcweir             e.printStackTrace(log);
126cdf0e10cSrcweir             res = false;
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         tRes.tested("getConnectionWithInfo()", res);
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir     /**
133cdf0e10cSrcweir      * Calls the method and checks returned value.
134cdf0e10cSrcweir      * Has OK status if timeout that was set and timeout that was returned by
135cdf0e10cSrcweir      * the method <code>getLoginTimeout()</code> are equal.
136cdf0e10cSrcweir      */
_setLoginTimeout()137cdf0e10cSrcweir     public void _setLoginTimeout() {
138cdf0e10cSrcweir         requiredMethod("getLoginTimeout()");
139cdf0e10cSrcweir         final int TO = 111;
140cdf0e10cSrcweir         log.println("setLoginTimeout(" + TO + ")");
141cdf0e10cSrcweir         oObj.setLoginTimeout(TO);
142cdf0e10cSrcweir         int timeout = oObj.getLoginTimeout();
143cdf0e10cSrcweir         log.println("getLoginTimeout(): " + timeout);
144cdf0e10cSrcweir         tRes.tested("setLoginTimeout()", timeout == TO);
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
147cdf0e10cSrcweir     /**
148cdf0e10cSrcweir      * Calls the method.
149cdf0e10cSrcweir      */
_getLoginTimeout()150cdf0e10cSrcweir     public void _getLoginTimeout() {
151cdf0e10cSrcweir         int timeout = oObj.getLoginTimeout();
152cdf0e10cSrcweir         log.println("getLoginTimeout(): " + timeout);
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         tRes.tested("getLoginTimeout()", true);
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir }