Update ooo320-m1
[ooovba.git] / connectivity / qa / drivers / dbase / DBaseNumericFunctions.java
blobd76456536af5758fcd1e51c85fe789eb836ed5c8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DBaseNumericFunctions.java,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package qa.drivers.dbase;
32 import com.sun.star.uno.UnoRuntime;
33 import com.sun.star.sdbc.*;
34 import com.sun.star.beans.XPropertySet;
35 import com.sun.star.lang.XMultiServiceFactory;
38 public class DBaseNumericFunctions
41 private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'";
42 private final XMultiServiceFactory m_xORB;
43 private final DBaseDriverTest testcase;
45 public DBaseNumericFunctions(final XMultiServiceFactory _xORB, final DBaseDriverTest _testcase)
47 m_xORB = _xORB;
48 testcase = _testcase;
51 private void assure(final String s, final boolean b)
53 testcase.assure2(s, b);
56 public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
58 final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class,
59 m_xORB.createInstance("com.sun.star.sdb.RowSet"));
61 testcase.getLog().println("starting Numeric function test");
62 // set the properties needed to connect to a database
63 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes);
64 xProp.setPropertyValue("DataSourceName", "Bibliography");
66 xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND));
68 try
70 abs(xRowRes);
72 catch (SQLException ex)
74 assure("abs " + ex.getMessage(), false);
75 throw ex;
77 try
79 acos(xRowRes);
81 catch (SQLException ex)
83 assure("acos " + ex.getMessage(), false);
84 throw ex;
86 try
88 asin(xRowRes);
90 catch (SQLException ex)
92 assure("asin " + ex.getMessage(), false);
93 throw ex;
95 try
97 atan(xRowRes);
99 catch (SQLException ex)
101 assure("atan " + ex.getMessage(), false);
102 throw ex;
106 atan2(xRowRes);
108 catch (SQLException ex)
110 assure("atan2 " + ex.getMessage(), false);
111 throw ex;
115 ceiling(xRowRes);
117 catch (SQLException ex)
119 assure("ceiling " + ex.getMessage(), false);
120 throw ex;
124 cos(xRowRes);
126 catch (SQLException ex)
128 assure("cos " + ex.getMessage(), false);
129 throw ex;
133 degrees(xRowRes);
135 catch (SQLException ex)
137 assure("degrees " + ex.getMessage(), false);
138 throw ex;
142 exp(xRowRes);
144 catch (SQLException ex)
146 assure("exp " + ex.getMessage(), false);
147 throw ex;
151 floor(xRowRes);
153 catch (SQLException ex)
155 assure("floor " + ex.getMessage(), false);
156 throw ex;
160 log(xRowRes);
162 catch (SQLException ex)
164 assure("log " + ex.getMessage(), false);
165 throw ex;
169 log10(xRowRes);
171 catch (SQLException ex)
173 assure("log10 " + ex.getMessage(), false);
174 throw ex;
178 mod(xRowRes);
180 catch (SQLException ex)
182 assure("mod " + ex.getMessage(), false);
183 throw ex;
187 pi(xRowRes);
189 catch (SQLException ex)
191 assure("pi " + ex.getMessage(), false);
192 throw ex;
196 pow(xRowRes);
198 catch (SQLException ex)
200 assure("pow " + ex.getMessage(), false);
201 throw ex;
205 radians(xRowRes);
207 catch (SQLException ex)
209 assure("radians " + ex.getMessage(), false);
210 throw ex;
214 round(xRowRes);
216 catch (SQLException ex)
218 assure("round " + ex.getMessage(), false);
219 throw ex;
223 sign(xRowRes);
225 catch (SQLException ex)
227 assure("sign " + ex.getMessage(), false);
228 throw ex;
232 sin(xRowRes);
234 catch (SQLException ex)
236 assure("sin " + ex.getMessage(), false);
237 throw ex;
241 sqrt(xRowRes);
243 catch (SQLException ex)
245 assure("sqrt " + ex.getMessage(), false);
246 throw ex;
250 tan(xRowRes);
252 catch (SQLException ex)
254 assure("tan " + ex.getMessage(), false);
255 throw ex;
260 private XRow execute(final XRowSet xRowRes,final String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
262 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes);
263 xProp.setPropertyValue("Command", "SELECT " + sql + where);
264 xRowRes.execute();
265 final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes);
266 assure("No valid row! ", xRes.next());
268 return (XRow) UnoRuntime.queryInterface(XRow.class, xRes);
271 private void abs(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
273 final XRow row = execute(xRowRes, "ABS(2),ABS(-32) ");
274 assure("ABS(2) failed!", row.getInt(1) == 2);
275 assure("ABS(-32) failed!", row.getInt(2) == 32);
278 private void sign(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
280 final XRow row = execute(xRowRes, "SIGN(-32),SIGN(0),SIGN(234) ");
281 assure("SIGN(-32)failed!", row.getInt(1) == -1);
282 assure("SIGN(0) failed!", row.getInt(2) == 0);
283 assure("SIGN(234) failed!", row.getInt(3) == 1);
286 private void mod(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
288 final XRow row = execute(xRowRes, "MOD(234, 10) ");
289 assure("MOD(234, 10) failed!", row.getInt(1) == 4);
292 private void floor(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
294 final XRow row = execute(xRowRes, "FLOOR(1.23),FLOOR(-1.23) ");
295 assure("FLOOR(1.23) failed!", row.getInt(1) == 1);
296 assure("FLOOR(-1.23) failed!", row.getInt(2) == -2);
299 private void ceiling(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
301 final XRow row = execute(xRowRes, "CEILING(1.23),CEILING(-1.23) ");
302 assure("CEILING(1.23) failed!", row.getInt(1) == 2);
303 assure("CEILING(-1.23) failed!", row.getInt(2) == -1);
306 private void round(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
308 final XRow row = execute(xRowRes, "ROUND(-1.23),ROUND(1.298, 1) ");
309 assure("ROUND(-1.23) failed!", row.getInt(1) == -1);
310 assure("ROUND(1.298, 1) failed!", row.getDouble(2) == 1.3);
313 private void exp(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
315 final XRow row = execute(xRowRes, "EXP(2),EXP(-2) ");
316 assure("EXP(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.exp(2));
317 assure("EXP(-2) failed!", (float) row.getDouble(2) == (float) java.lang.Math.exp(-2));
320 private void log(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
322 final XRow row = execute(xRowRes, "LOG(2),LOG(-2) ");
323 assure("LOG(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.log(2));
324 row.getDouble(2);
325 assure("LOG(-2) failed!", row.wasNull());
328 private void log10(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
330 final XRow row = execute(xRowRes, "LOG10(100) ");
331 assure("LOG10(100) failed!", row.getDouble(1) == 2.0);
334 private void pow(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
336 final XRow row = execute(xRowRes, "POWER(2,2) ");
337 assure("POWER(2,2) failed!", row.getDouble(1) == 4.0);
340 private void sqrt(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
342 final XRow row = execute(xRowRes, "SQRT(4) ");
343 assure("SQRT(4) failed!", row.getDouble(1) == 2.0);
346 private void pi(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
348 final XRow row = execute(xRowRes, "PI() ");
349 assure("PI() failed!", (float) row.getDouble(1) == (float) java.lang.Math.PI);
352 private void cos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
354 final XRow row = execute(xRowRes, "COS(PI()) ");
355 assure("COS(PI()) failed!", row.getDouble(1) == -1.0);
358 private void sin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
360 final XRow row = execute(xRowRes, "SIN(2) ");
361 assure("SIN(PI()) failed!", (float) row.getDouble(1) == (float) java.lang.Math.sin(2));
364 private void tan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
366 final XRow row = execute(xRowRes, "TAN(PI()+1) ");
367 assure("TAN(PI()+1) failed!", (float) row.getDouble(1) == (float) java.lang.Math.tan(java.lang.Math.PI + 1.0));
370 private void acos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
372 final XRow row = execute(xRowRes, "ACOS(1) ");
373 assure("ACOS(1) failed!", (float) row.getDouble(1) == 0.0);
376 private void asin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
378 final XRow row = execute(xRowRes, "ASIN(0) ");
379 assure("ASIN(0) failed!", (float) row.getDouble(1) == (float) java.lang.Math.asin(0.0));
382 private void atan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
384 final XRow row = execute(xRowRes, "ATAN(0) ");
385 assure("ATAN(0) failed!", row.getDouble(1) == 0.0);
388 private void atan2(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
390 final XRow row = execute(xRowRes, "ATAN2(0,2) ");
391 assure("ATAN2(0,2) failed!", (float) row.getDouble(1) == 0.0);
394 private void degrees(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
396 final XRow row = execute(xRowRes, "DEGREES(PI()) ");
397 assure("DEGREES(PI()) failed!", row.getDouble(1) == 180.0);
400 private void radians(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
402 final XRow row = execute(xRowRes, "RADIANS(90) ");
403 assure("RADIANS(90) failed!", (float) row.getDouble(1) == (float) (java.lang.Math.PI / 2.0));