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.sdbc; 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.DriverPropertyInfo; 36 import com.sun.star.sdbc.SQLException; 37 import com.sun.star.sdbc.XConnection; 38 import com.sun.star.sdbc.XDriver; 39 40 /** 41 * Testing <code>com.sun.star.sdbc.XDriver</code> 42 * interface methods : 43 * <ul> 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> 49 * </ul> <p> 50 * Required object relations : 51 * <ul> 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> 58 * </ul> <p> 59 * @see com.sun.star.sdbc.XDriver 60 */ 61 public class _XDriver extends MultiMethodTest { 62 // oObj filled by MultiMethodTest 63 public XDriver oObj = null; 64 String url = null; 65 String wrongUrl = null; 66 String nbu = null; 67 PropertyValue[] info = null; 68 69 /** 70 * Retrieves relations. 71 * @throw StatusException If any relation not found. 72 */ 73 protected void before() { 74 nbu = (String) tEnv.getObjRelation("NoBadURL"); 75 url = (String)tEnv.getObjRelation("XDriver.URL"); 76 if (url == null) { 77 throw new StatusException(Status.failed( 78 "Couldn't get relation 'XDriver.URL'")); 79 } 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'")); 84 } 85 info = (PropertyValue[])tEnv.getObjRelation("XDriver.INFO"); 86 if (info == null) { 87 throw new StatusException(Status.failed( 88 "Couldn't get relation 'XDriver.INFO'")); 89 } 90 } 91 92 /** 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. 99 */ 100 public void _connect() { 101 boolean res = true; 102 103 try { 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"); 114 res &= false; 115 } 116 117 if (nbu==null) { 118 try { 119 String badUrl = url + "bla"; 120 log.println("Trying to connect to " + badUrl); 121 oObj.connect(badUrl, info); 122 res &= false; 123 log.println("Expected exception isn't thrown"); 124 } catch(SQLException e) { 125 log.println("Expected exception"); 126 res &= true; 127 } 128 } 129 130 tRes.tested("connect()", res); 131 } 132 133 /** 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>. 138 */ 139 public void _acceptsURL() { 140 boolean res = false; 141 142 try { 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); 150 res = false; 151 } 152 153 tRes.tested("acceptsURL()", res); 154 } 155 156 /** 157 * Calls the method with passed <code>'XDriver.URL'</code> and 158 * <code>'XDriver.INFO'</code>. Prints obtained driver properties info 159 * to log. 160 * Has OK status if returned value isn't null. 161 */ 162 public void _getPropertyInfo() { 163 requiredMethod("acceptsURL()"); 164 boolean res = false; 165 DriverPropertyInfo[] dpi = null; 166 try { 167 dpi = oObj.getPropertyInfo(url, info); 168 } catch(SQLException e) { 169 log.println("Unexpected exception"); 170 e.printStackTrace(log); 171 res = false; 172 } 173 174 if (dpi != null) { 175 res = true; 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]); 185 } 186 } 187 } 188 189 tRes.tested("getPropertyInfo()", res); 190 } 191 192 /** 193 * Calls the method. 194 * Has OK status if returned value is greater than or is equal to 1. 195 */ 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); 201 } 202 203 /** 204 * Calls the method. 205 * Has OK status if returned value is greater than or is equal to 0. 206 */ 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); 212 } 213 }