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