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.sdbcx;
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.XDriver;
33cdf0e10cSrcweir import com.sun.star.sdbcx.XDataDefinitionSupplier;
34cdf0e10cSrcweir import com.sun.star.sdbcx.XTablesSupplier;
35cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir /**
38cdf0e10cSrcweir * Testing <code>com.sun.star.sdbcx.XDataDefinitionSupplier</code>
39cdf0e10cSrcweir * interface methods :
40cdf0e10cSrcweir * <ul>
41cdf0e10cSrcweir *  <li><code> getDataDefinitionByConnection()</code></li>
42cdf0e10cSrcweir *  <li><code> getDataDefinitionByURL()</code></li>
43cdf0e10cSrcweir * </ul> <p>
44cdf0e10cSrcweir * Required object relations :
45cdf0e10cSrcweir * <ul>
46cdf0e10cSrcweir * <li> <code>'XDriver.URL'</code>:
47cdf0e10cSrcweir *      is the URL of the database to which to connect</code></li>
48cdf0e10cSrcweir * <li><code>'XDriver.UNSUITABLE_URL'</code>:
49cdf0e10cSrcweir *      the wrong kind of URL to connect using given driver</li>
50cdf0e10cSrcweir * <li><code>'XDriver.INFO'</code>:
51cdf0e10cSrcweir *      a list of arbitrary string tag/value pairs as connection arguments</li>
52cdf0e10cSrcweir * </ul> <p>
53cdf0e10cSrcweir * @see com.sun.star.sdbcx.XDataDefinitionSupplier
54cdf0e10cSrcweir */
55cdf0e10cSrcweir public class _XDataDefinitionSupplier extends MultiMethodTest {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     // oObj filled by MultiMethodTest
58cdf0e10cSrcweir     public XDataDefinitionSupplier oObj = null ;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     String url = null;
61cdf0e10cSrcweir     String wrongUrl = null;
62cdf0e10cSrcweir     PropertyValue[] info = null;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir     /**
65cdf0e10cSrcweir     * Retrieves relations.
66cdf0e10cSrcweir     * @throw StatusException If any relation not found.
67cdf0e10cSrcweir     */
before()68cdf0e10cSrcweir     protected void before() {
69cdf0e10cSrcweir         url = (String)tEnv.getObjRelation("XDriver.URL");
70cdf0e10cSrcweir         if (url == null) {
71cdf0e10cSrcweir             throw new StatusException(Status.failed(
72cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.URL'"));
73cdf0e10cSrcweir         }
74cdf0e10cSrcweir         wrongUrl = (String)tEnv.getObjRelation("XDriver.UNSUITABLE_URL");
75cdf0e10cSrcweir         if (wrongUrl == null) {
76cdf0e10cSrcweir             throw new StatusException(Status.failed(
77cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.WRONG_URL'"));
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir         info = (PropertyValue[])tEnv.getObjRelation("XDriver.INFO");
80cdf0e10cSrcweir         if (info == null) {
81cdf0e10cSrcweir             throw new StatusException(Status.failed(
82cdf0e10cSrcweir                 "Couldn't get relation 'XDriver.INFO'"));
83cdf0e10cSrcweir         }
84cdf0e10cSrcweir     }
85cdf0e10cSrcweir 
86cdf0e10cSrcweir     XConnection connection = null;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     /**
89cdf0e10cSrcweir      * Obtains the connection to url(relation <code>'XDriver.URL'</code>)
90cdf0e10cSrcweir      * with info(relation <code>'XDriver.INFO'</code>).
91cdf0e10cSrcweir      * Calls the method with obtained connection and checks that returned value
92cdf0e10cSrcweir      * isn't null.
93cdf0e10cSrcweir      */
_getDataDefinitionByConnection()94cdf0e10cSrcweir     public void _getDataDefinitionByConnection() {
95cdf0e10cSrcweir         boolean bRes = true;
96cdf0e10cSrcweir         XDriver xDriver = (XDriver)
97cdf0e10cSrcweir             UnoRuntime.queryInterface(XDriver.class, oObj);
98cdf0e10cSrcweir         if (xDriver == null) {
99cdf0e10cSrcweir             log.println("The XDriver interface isn't supported");
100cdf0e10cSrcweir             tRes.tested("getDataDefinitionByConnection()",
101cdf0e10cSrcweir                         Status.skipped(false));
102cdf0e10cSrcweir             return;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         try {
105cdf0e10cSrcweir             connection = xDriver.connect(url, info);
106cdf0e10cSrcweir         } catch(com.sun.star.sdbc.SQLException e) {
107cdf0e10cSrcweir             e.printStackTrace(log);
108cdf0e10cSrcweir             bRes = false;
109cdf0e10cSrcweir         }
110cdf0e10cSrcweir         if (connection == null) {
111cdf0e10cSrcweir             log.println("Couldn't get connection to specified url using " +
112cdf0e10cSrcweir                 "specified info");
113cdf0e10cSrcweir             tRes.tested("getDataDefinitionByConnection()",
114cdf0e10cSrcweir                         Status.skipped(false));
115cdf0e10cSrcweir             return;
116cdf0e10cSrcweir         }
117cdf0e10cSrcweir         XTablesSupplier xTS = null;
118cdf0e10cSrcweir         try {
119cdf0e10cSrcweir             log.println("getDataDefinitionByConnection(connection)");
120cdf0e10cSrcweir             xTS = oObj.getDataDefinitionByConnection(connection);
121cdf0e10cSrcweir             bRes = xTS != null;
122cdf0e10cSrcweir         } catch(com.sun.star.sdbc.SQLException e) {
123cdf0e10cSrcweir             log.println("Unexpected exception: " + e);
124cdf0e10cSrcweir             bRes = false;
125cdf0e10cSrcweir         }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         try {
128cdf0e10cSrcweir             log.println("getDataDefinitionByConnection(null)");
129cdf0e10cSrcweir             xTS = oObj.getDataDefinitionByConnection(null);
130cdf0e10cSrcweir             bRes = xTS == null;
131cdf0e10cSrcweir         } catch(com.sun.star.sdbc.SQLException e) {
132cdf0e10cSrcweir             log.println("Exception: " + e);
133cdf0e10cSrcweir             bRes = true;
134cdf0e10cSrcweir         }
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         tRes.tested("getDataDefinitionByConnection()", bRes);
137cdf0e10cSrcweir     }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     /**
140cdf0e10cSrcweir      * Calls the method with url and info obtained from the relations
141cdf0e10cSrcweir      * <code>XDriver.URL</code> and <code>XDriver.INFO</code>.
142cdf0e10cSrcweir      * Checks that retuned value isn't null.
143cdf0e10cSrcweir      * Then calls the method with the unsuitable url obtained from the relation
144cdf0e10cSrcweir      * <code>XDriver.UNSUITABLE_URL</code> and checks that SQLException
145cdf0e10cSrcweir      * exception was thrown.
146cdf0e10cSrcweir      */
_getDataDefinitionByURL()147cdf0e10cSrcweir     public void _getDataDefinitionByURL() {
148cdf0e10cSrcweir         boolean bRes = false;
149cdf0e10cSrcweir 		XTablesSupplier xTS = null;
150cdf0e10cSrcweir 
151cdf0e10cSrcweir         try {
152cdf0e10cSrcweir             log.println("getDataDefinitionByURL('" + url + "')");
153cdf0e10cSrcweir             xTS = oObj.getDataDefinitionByURL(url, info);
154cdf0e10cSrcweir             bRes = xTS != null;
155cdf0e10cSrcweir         } catch (com.sun.star.sdbc.SQLException e) {
156cdf0e10cSrcweir             log.println("Unexpected exception: " + e);
157cdf0e10cSrcweir             bRes = false;
158cdf0e10cSrcweir         }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         try {
161cdf0e10cSrcweir             log.println("getDataDefinitionByURL('" + wrongUrl + "')");
162cdf0e10cSrcweir             xTS = oObj.getDataDefinitionByURL(wrongUrl, info);
163cdf0e10cSrcweir             log.println("Exception was expected");
164cdf0e10cSrcweir             bRes = false;
165cdf0e10cSrcweir         } catch (com.sun.star.sdbc.SQLException e) {
166cdf0e10cSrcweir             log.println("Expected exception");
167cdf0e10cSrcweir             bRes &= true;
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         tRes.tested("getDataDefinitionByURL()", true);
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir }  // finish class _XDataDefinitionSupplier
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176