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 package complex.connectivity.dbase; 28 29 import com.sun.star.uno.UnoRuntime; 30 import com.sun.star.sdbc.*; 31 import com.sun.star.beans.XPropertySet; 32 import com.sun.star.lang.XMultiServiceFactory; 33 import complex.connectivity.SubTestCase; 34 import complex.connectivity.TestCase; 35 36 37 public class DBaseNumericFunctions extends SubTestCase 38 { 39 private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'"; 40 private final XMultiServiceFactory m_xORB; 41 42 public DBaseNumericFunctions(final XMultiServiceFactory _xORB, final TestCase i_testCase) 43 { 44 super( i_testCase ); 45 m_xORB = _xORB; 46 } 47 48 public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 49 { 50 final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, 51 m_xORB.createInstance("com.sun.star.sdb.RowSet")); 52 53 getLog().println("starting Numeric function test"); 54 // set the properties needed to connect to a database 55 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 56 xProp.setPropertyValue("DataSourceName", "Bibliography"); 57 58 xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND)); 59 60 try 61 { 62 abs(xRowRes); 63 } 64 catch (SQLException ex) 65 { 66 assure("abs " + ex.getMessage(), false); 67 throw ex; 68 } 69 try 70 { 71 acos(xRowRes); 72 } 73 catch (SQLException ex) 74 { 75 assure("acos " + ex.getMessage(), false); 76 throw ex; 77 } 78 try 79 { 80 asin(xRowRes); 81 } 82 catch (SQLException ex) 83 { 84 assure("asin " + ex.getMessage(), false); 85 throw ex; 86 } 87 try 88 { 89 atan(xRowRes); 90 } 91 catch (SQLException ex) 92 { 93 assure("atan " + ex.getMessage(), false); 94 throw ex; 95 } 96 try 97 { 98 atan2(xRowRes); 99 } 100 catch (SQLException ex) 101 { 102 assure("atan2 " + ex.getMessage(), false); 103 throw ex; 104 } 105 try 106 { 107 ceiling(xRowRes); 108 } 109 catch (SQLException ex) 110 { 111 assure("ceiling " + ex.getMessage(), false); 112 throw ex; 113 } 114 try 115 { 116 cos(xRowRes); 117 } 118 catch (SQLException ex) 119 { 120 assure("cos " + ex.getMessage(), false); 121 throw ex; 122 } 123 try 124 { 125 degrees(xRowRes); 126 } 127 catch (SQLException ex) 128 { 129 assure("degrees " + ex.getMessage(), false); 130 throw ex; 131 } 132 try 133 { 134 exp(xRowRes); 135 } 136 catch (SQLException ex) 137 { 138 assure("exp " + ex.getMessage(), false); 139 throw ex; 140 } 141 try 142 { 143 floor(xRowRes); 144 } 145 catch (SQLException ex) 146 { 147 assure("floor " + ex.getMessage(), false); 148 throw ex; 149 } 150 try 151 { 152 log(xRowRes); 153 } 154 catch (SQLException ex) 155 { 156 assure("log " + ex.getMessage(), false); 157 throw ex; 158 } 159 try 160 { 161 log10(xRowRes); 162 } 163 catch (SQLException ex) 164 { 165 assure("log10 " + ex.getMessage(), false); 166 throw ex; 167 } 168 try 169 { 170 mod(xRowRes); 171 } 172 catch (SQLException ex) 173 { 174 assure("mod " + ex.getMessage(), false); 175 throw ex; 176 } 177 try 178 { 179 pi(xRowRes); 180 } 181 catch (SQLException ex) 182 { 183 assure("pi " + ex.getMessage(), false); 184 throw ex; 185 } 186 try 187 { 188 pow(xRowRes); 189 } 190 catch (SQLException ex) 191 { 192 assure("pow " + ex.getMessage(), false); 193 throw ex; 194 } 195 try 196 { 197 radians(xRowRes); 198 } 199 catch (SQLException ex) 200 { 201 assure("radians " + ex.getMessage(), false); 202 throw ex; 203 } 204 try 205 { 206 round(xRowRes); 207 } 208 catch (SQLException ex) 209 { 210 assure("round " + ex.getMessage(), false); 211 throw ex; 212 } 213 try 214 { 215 sign(xRowRes); 216 } 217 catch (SQLException ex) 218 { 219 assure("sign " + ex.getMessage(), false); 220 throw ex; 221 } 222 try 223 { 224 sin(xRowRes); 225 } 226 catch (SQLException ex) 227 { 228 assure("sin " + ex.getMessage(), false); 229 throw ex; 230 } 231 try 232 { 233 sqrt(xRowRes); 234 } 235 catch (SQLException ex) 236 { 237 assure("sqrt " + ex.getMessage(), false); 238 throw ex; 239 } 240 try 241 { 242 tan(xRowRes); 243 } 244 catch (SQLException ex) 245 { 246 assure("tan " + ex.getMessage(), false); 247 throw ex; 248 } 249 250 } 251 252 private XRow execute(final XRowSet xRowRes,final String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 253 { 254 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 255 xProp.setPropertyValue("Command", "SELECT " + sql + where); 256 xRowRes.execute(); 257 final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes); 258 assure("No valid row! ", xRes.next()); 259 260 return (XRow) UnoRuntime.queryInterface(XRow.class, xRes); 261 } 262 263 private void abs(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 264 { 265 final XRow row = execute(xRowRes, "ABS(2),ABS(-32) "); 266 assure("ABS(2) failed!", row.getInt(1) == 2); 267 assure("ABS(-32) failed!", row.getInt(2) == 32); 268 } 269 270 private void sign(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 271 { 272 final XRow row = execute(xRowRes, "SIGN(-32),SIGN(0),SIGN(234) "); 273 assure("SIGN(-32)failed!", row.getInt(1) == -1); 274 assure("SIGN(0) failed!", row.getInt(2) == 0); 275 assure("SIGN(234) failed!", row.getInt(3) == 1); 276 } 277 278 private void mod(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 279 { 280 final XRow row = execute(xRowRes, "MOD(234, 10) "); 281 assure("MOD(234, 10) failed!", row.getInt(1) == 4); 282 } 283 284 private void floor(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 285 { 286 final XRow row = execute(xRowRes, "FLOOR(1.23),FLOOR(-1.23) "); 287 assure("FLOOR(1.23) failed!", row.getInt(1) == 1); 288 assure("FLOOR(-1.23) failed!", row.getInt(2) == -2); 289 } 290 291 private void ceiling(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 292 { 293 final XRow row = execute(xRowRes, "CEILING(1.23),CEILING(-1.23) "); 294 assure("CEILING(1.23) failed!", row.getInt(1) == 2); 295 assure("CEILING(-1.23) failed!", row.getInt(2) == -1); 296 } 297 298 private void round(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 299 { 300 final XRow row = execute(xRowRes, "ROUND(-1.23),ROUND(1.298, 1) "); 301 assure("ROUND(-1.23) failed!", row.getInt(1) == -1); 302 assure("ROUND(1.298, 1) failed!", row.getDouble(2) == 1.3); 303 } 304 305 private void exp(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 306 { 307 final XRow row = execute(xRowRes, "EXP(2),EXP(-2) "); 308 assure("EXP(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.exp(2)); 309 assure("EXP(-2) failed!", (float) row.getDouble(2) == (float) java.lang.Math.exp(-2)); 310 } 311 312 private void log(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 313 { 314 final XRow row = execute(xRowRes, "LOG(2),LOG(-2) "); 315 assure("LOG(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.log(2)); 316 row.getDouble(2); 317 assure("LOG(-2) failed!", row.wasNull()); 318 } 319 320 private void log10(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 321 { 322 final XRow row = execute(xRowRes, "LOG10(100) "); 323 assure("LOG10(100) failed!", row.getDouble(1) == 2.0); 324 } 325 326 private void pow(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 327 { 328 final XRow row = execute(xRowRes, "POWER(2,2) "); 329 assure("POWER(2,2) failed!", row.getDouble(1) == 4.0); 330 } 331 332 private void sqrt(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 333 { 334 final XRow row = execute(xRowRes, "SQRT(4) "); 335 assure("SQRT(4) failed!", row.getDouble(1) == 2.0); 336 } 337 338 private void pi(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 339 { 340 final XRow row = execute(xRowRes, "PI() "); 341 assure("PI() failed!", (float) row.getDouble(1) == (float) java.lang.Math.PI); 342 } 343 344 private void cos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 345 { 346 final XRow row = execute(xRowRes, "COS(PI()) "); 347 assure("COS(PI()) failed!", row.getDouble(1) == -1.0); 348 } 349 350 private void sin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 351 { 352 final XRow row = execute(xRowRes, "SIN(2) "); 353 assure("SIN(PI()) failed!", (float) row.getDouble(1) == (float) java.lang.Math.sin(2)); 354 } 355 356 private void tan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 357 { 358 final XRow row = execute(xRowRes, "TAN(PI()+1) "); 359 assure("TAN(PI()+1) failed!", (float) row.getDouble(1) == (float) java.lang.Math.tan(java.lang.Math.PI + 1.0)); 360 } 361 362 private void acos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 363 { 364 final XRow row = execute(xRowRes, "ACOS(1) "); 365 assure("ACOS(1) failed!", (float) row.getDouble(1) == 0.0); 366 } 367 368 private void asin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 369 { 370 final XRow row = execute(xRowRes, "ASIN(0) "); 371 assure("ASIN(0) failed!", (float) row.getDouble(1) == (float) java.lang.Math.asin(0.0)); 372 } 373 374 private void atan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 375 { 376 final XRow row = execute(xRowRes, "ATAN(0) "); 377 assure("ATAN(0) failed!", row.getDouble(1) == 0.0); 378 } 379 380 private void atan2(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 381 { 382 final XRow row = execute(xRowRes, "ATAN2(0,2) "); 383 assure("ATAN2(0,2) failed!", (float) row.getDouble(1) == 0.0); 384 } 385 386 private void degrees(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 387 { 388 final XRow row = execute(xRowRes, "DEGREES(PI()) "); 389 assure("DEGREES(PI()) failed!", row.getDouble(1) == 180.0); 390 } 391 392 private void radians(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 393 { 394 final XRow row = execute(xRowRes, "RADIANS(90) "); 395 assure("RADIANS(90) failed!", (float) row.getDouble(1) == (float) (java.lang.Math.PI / 2.0)); 396 } 397 } 398