1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.sdbcx;
25 
26 import lib.MultiMethodTest;
27 import lib.Status;
28 import lib.StatusException;
29 
30 import com.sun.star.beans.PropertyValue;
31 import com.sun.star.sdbc.XConnection;
32 import com.sun.star.sdbc.XDriver;
33 import com.sun.star.sdbcx.XDataDefinitionSupplier;
34 import com.sun.star.sdbcx.XTablesSupplier;
35 import com.sun.star.uno.UnoRuntime;
36 
37 /**
38 * Testing <code>com.sun.star.sdbcx.XDataDefinitionSupplier</code>
39 * interface methods :
40 * <ul>
41 *  <li><code> getDataDefinitionByConnection()</code></li>
42 *  <li><code> getDataDefinitionByURL()</code></li>
43 * </ul> <p>
44 * Required object relations :
45 * <ul>
46 * <li> <code>'XDriver.URL'</code>:
47 *      is the URL of the database to which to connect</code></li>
48 * <li><code>'XDriver.UNSUITABLE_URL'</code>:
49 *      the wrong kind of URL to connect using given driver</li>
50 * <li><code>'XDriver.INFO'</code>:
51 *      a list of arbitrary string tag/value pairs as connection arguments</li>
52 * </ul> <p>
53 * @see com.sun.star.sdbcx.XDataDefinitionSupplier
54 */
55 public class _XDataDefinitionSupplier extends MultiMethodTest {
56 
57     // oObj filled by MultiMethodTest
58     public XDataDefinitionSupplier oObj = null ;
59 
60     String url = null;
61     String wrongUrl = null;
62     PropertyValue[] info = null;
63 
64     /**
65     * Retrieves relations.
66     * @throw StatusException If any relation not found.
67     */
before()68     protected void before() {
69         url = (String)tEnv.getObjRelation("XDriver.URL");
70         if (url == null) {
71             throw new StatusException(Status.failed(
72                 "Couldn't get relation 'XDriver.URL'"));
73         }
74         wrongUrl = (String)tEnv.getObjRelation("XDriver.UNSUITABLE_URL");
75         if (wrongUrl == null) {
76             throw new StatusException(Status.failed(
77                 "Couldn't get relation 'XDriver.WRONG_URL'"));
78         }
79         info = (PropertyValue[])tEnv.getObjRelation("XDriver.INFO");
80         if (info == null) {
81             throw new StatusException(Status.failed(
82                 "Couldn't get relation 'XDriver.INFO'"));
83         }
84     }
85 
86     XConnection connection = null;
87 
88     /**
89      * Obtains the connection to url(relation <code>'XDriver.URL'</code>)
90      * with info(relation <code>'XDriver.INFO'</code>).
91      * Calls the method with obtained connection and checks that returned value
92      * isn't null.
93      */
_getDataDefinitionByConnection()94     public void _getDataDefinitionByConnection() {
95         boolean bRes = true;
96         XDriver xDriver = (XDriver)
97             UnoRuntime.queryInterface(XDriver.class, oObj);
98         if (xDriver == null) {
99             log.println("The XDriver interface isn't supported");
100             tRes.tested("getDataDefinitionByConnection()",
101                         Status.skipped(false));
102             return;
103         }
104         try {
105             connection = xDriver.connect(url, info);
106         } catch(com.sun.star.sdbc.SQLException e) {
107             e.printStackTrace(log);
108             bRes = false;
109         }
110         if (connection == null) {
111             log.println("Couldn't get connection to specified url using " +
112                 "specified info");
113             tRes.tested("getDataDefinitionByConnection()",
114                         Status.skipped(false));
115             return;
116         }
117         XTablesSupplier xTS = null;
118         try {
119             log.println("getDataDefinitionByConnection(connection)");
120             xTS = oObj.getDataDefinitionByConnection(connection);
121             bRes = xTS != null;
122         } catch(com.sun.star.sdbc.SQLException e) {
123             log.println("Unexpected exception: " + e);
124             bRes = false;
125         }
126 
127         try {
128             log.println("getDataDefinitionByConnection(null)");
129             xTS = oObj.getDataDefinitionByConnection(null);
130             bRes = xTS == null;
131         } catch(com.sun.star.sdbc.SQLException e) {
132             log.println("Exception: " + e);
133             bRes = true;
134         }
135 
136         tRes.tested("getDataDefinitionByConnection()", bRes);
137     }
138 
139     /**
140      * Calls the method with url and info obtained from the relations
141      * <code>XDriver.URL</code> and <code>XDriver.INFO</code>.
142      * Checks that retuned value isn't null.
143      * Then calls the method with the unsuitable url obtained from the relation
144      * <code>XDriver.UNSUITABLE_URL</code> and checks that SQLException
145      * exception was thrown.
146      */
_getDataDefinitionByURL()147     public void _getDataDefinitionByURL() {
148         boolean bRes = false;
149 		XTablesSupplier xTS = null;
150 
151         try {
152             log.println("getDataDefinitionByURL('" + url + "')");
153             xTS = oObj.getDataDefinitionByURL(url, info);
154             bRes = xTS != null;
155         } catch (com.sun.star.sdbc.SQLException e) {
156             log.println("Unexpected exception: " + e);
157             bRes = false;
158         }
159 
160         try {
161             log.println("getDataDefinitionByURL('" + wrongUrl + "')");
162             xTS = oObj.getDataDefinitionByURL(wrongUrl, info);
163             log.println("Exception was expected");
164             bRes = false;
165         } catch (com.sun.star.sdbc.SQLException e) {
166             log.println("Expected exception");
167             bRes &= true;
168         }
169 
170         tRes.tested("getDataDefinitionByURL()", true);
171 
172     }
173 }  // finish class _XDataDefinitionSupplier
174 
175 
176