xref: /trunk/main/connectivity/qa/complex/connectivity/dbase/DBaseNumericFunctions.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1c3ab0d6aSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3c3ab0d6aSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4c3ab0d6aSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5c3ab0d6aSAndrew Rist  * distributed with this work for additional information
6c3ab0d6aSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7c3ab0d6aSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8c3ab0d6aSAndrew Rist  * "License"); you may not use this file except in compliance
9c3ab0d6aSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11c3ab0d6aSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13c3ab0d6aSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14c3ab0d6aSAndrew Rist  * software distributed under the License is distributed on an
15c3ab0d6aSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16c3ab0d6aSAndrew Rist  * KIND, either express or implied.  See the License for the
17c3ab0d6aSAndrew Rist  * specific language governing permissions and limitations
18c3ab0d6aSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20c3ab0d6aSAndrew Rist  *************************************************************/
21c3ab0d6aSAndrew Rist 
22c3ab0d6aSAndrew Rist 
23cdf0e10cSrcweir package complex.connectivity.dbase;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
26cdf0e10cSrcweir import com.sun.star.sdbc.*;
27cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir 
30*fcc38b58SDamjan Jovanovic import static org.junit.Assert.*;
31cdf0e10cSrcweir 
32*fcc38b58SDamjan Jovanovic public class DBaseNumericFunctions
33cdf0e10cSrcweir {
34cdf0e10cSrcweir     private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'";
35cdf0e10cSrcweir     private final XMultiServiceFactory m_xORB;
36cdf0e10cSrcweir 
DBaseNumericFunctions(final XMultiServiceFactory _xORB)37*fcc38b58SDamjan Jovanovic     public DBaseNumericFunctions(final XMultiServiceFactory _xORB)
38cdf0e10cSrcweir     {
39cdf0e10cSrcweir         m_xORB = _xORB;
40cdf0e10cSrcweir     }
41cdf0e10cSrcweir 
testFunctions()42cdf0e10cSrcweir     public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
43cdf0e10cSrcweir     {
44cdf0e10cSrcweir         final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class,
45cdf0e10cSrcweir                 m_xORB.createInstance("com.sun.star.sdb.RowSet"));
46cdf0e10cSrcweir 
47*fcc38b58SDamjan Jovanovic         System.out.println("starting Numeric function test");
48cdf0e10cSrcweir         // set the properties needed to connect to a database
49cdf0e10cSrcweir         final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes);
50cdf0e10cSrcweir         xProp.setPropertyValue("DataSourceName", "Bibliography");
51cdf0e10cSrcweir 
52cdf0e10cSrcweir         xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND));
53cdf0e10cSrcweir 
54cdf0e10cSrcweir         abs(xRowRes);
55cdf0e10cSrcweir         acos(xRowRes);
56cdf0e10cSrcweir         asin(xRowRes);
57cdf0e10cSrcweir         atan(xRowRes);
58cdf0e10cSrcweir         atan2(xRowRes);
59cdf0e10cSrcweir         ceiling(xRowRes);
60cdf0e10cSrcweir         cos(xRowRes);
61cdf0e10cSrcweir         degrees(xRowRes);
62cdf0e10cSrcweir         exp(xRowRes);
63cdf0e10cSrcweir         floor(xRowRes);
64cdf0e10cSrcweir         log(xRowRes);
65cdf0e10cSrcweir         log10(xRowRes);
66cdf0e10cSrcweir         mod(xRowRes);
67cdf0e10cSrcweir         pi(xRowRes);
68cdf0e10cSrcweir         pow(xRowRes);
69cdf0e10cSrcweir         radians(xRowRes);
70cdf0e10cSrcweir         round(xRowRes);
71cdf0e10cSrcweir         sign(xRowRes);
72cdf0e10cSrcweir         sin(xRowRes);
73cdf0e10cSrcweir         sqrt(xRowRes);
74cdf0e10cSrcweir         tan(xRowRes);
75cdf0e10cSrcweir     }
76cdf0e10cSrcweir 
execute(final XRowSet xRowRes,final String sql)77cdf0e10cSrcweir     private XRow execute(final XRowSet xRowRes,final  String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
78cdf0e10cSrcweir     {
79cdf0e10cSrcweir         final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes);
80cdf0e10cSrcweir         xProp.setPropertyValue("Command", "SELECT " + sql + where);
81cdf0e10cSrcweir         xRowRes.execute();
82cdf0e10cSrcweir         final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes);
83*fcc38b58SDamjan Jovanovic         assertTrue("No valid row! ", xRes.next());
84cdf0e10cSrcweir 
85cdf0e10cSrcweir         return (XRow) UnoRuntime.queryInterface(XRow.class, xRes);
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
abs(final XRowSet xRowRes)88cdf0e10cSrcweir     private void abs(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ABS(2),ABS(-32) ");
91*fcc38b58SDamjan Jovanovic         assertTrue("ABS(2) failed!", row.getInt(1) == 2);
92*fcc38b58SDamjan Jovanovic         assertTrue("ABS(-32) failed!", row.getInt(2) == 32);
93cdf0e10cSrcweir     }
94cdf0e10cSrcweir 
sign(final XRowSet xRowRes)95cdf0e10cSrcweir     private void sign(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         final XRow row = execute(xRowRes, "SIGN(-32),SIGN(0),SIGN(234) ");
98*fcc38b58SDamjan Jovanovic         assertTrue("SIGN(-32)failed!", row.getInt(1) == -1);
99*fcc38b58SDamjan Jovanovic         assertTrue("SIGN(0) failed!", row.getInt(2) == 0);
100*fcc38b58SDamjan Jovanovic         assertTrue("SIGN(234) failed!", row.getInt(3) == 1);
101cdf0e10cSrcweir     }
102cdf0e10cSrcweir 
mod(final XRowSet xRowRes)103cdf0e10cSrcweir     private void mod(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
104cdf0e10cSrcweir     {
105cdf0e10cSrcweir         final XRow row = execute(xRowRes, "MOD(234, 10) ");
106*fcc38b58SDamjan Jovanovic         assertTrue("MOD(234, 10) failed!", row.getInt(1) == 4);
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir 
floor(final XRowSet xRowRes)109cdf0e10cSrcweir     private void floor(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
110cdf0e10cSrcweir     {
111cdf0e10cSrcweir         final XRow row = execute(xRowRes, "FLOOR(1.23),FLOOR(-1.23) ");
112*fcc38b58SDamjan Jovanovic         assertTrue("FLOOR(1.23) failed!", row.getInt(1) == 1);
113*fcc38b58SDamjan Jovanovic         assertTrue("FLOOR(-1.23) failed!", row.getInt(2) == -2);
114cdf0e10cSrcweir     }
115cdf0e10cSrcweir 
ceiling(final XRowSet xRowRes)116cdf0e10cSrcweir     private void ceiling(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         final XRow row = execute(xRowRes, "CEILING(1.23),CEILING(-1.23) ");
119*fcc38b58SDamjan Jovanovic         assertTrue("CEILING(1.23) failed!", row.getInt(1) == 2);
120*fcc38b58SDamjan Jovanovic         assertTrue("CEILING(-1.23) failed!", row.getInt(2) == -1);
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
round(final XRowSet xRowRes)123cdf0e10cSrcweir     private void round(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ROUND(-1.23),ROUND(1.298, 1) ");
126*fcc38b58SDamjan Jovanovic         assertTrue("ROUND(-1.23) failed!", row.getInt(1) == -1);
127*fcc38b58SDamjan Jovanovic         assertTrue("ROUND(1.298, 1) failed!", row.getDouble(2) == 1.3);
128cdf0e10cSrcweir     }
129cdf0e10cSrcweir 
exp(final XRowSet xRowRes)130cdf0e10cSrcweir     private void exp(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
131cdf0e10cSrcweir     {
132cdf0e10cSrcweir         final XRow row = execute(xRowRes, "EXP(2),EXP(-2) ");
133*fcc38b58SDamjan Jovanovic         assertTrue("EXP(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.exp(2));
134*fcc38b58SDamjan Jovanovic         assertTrue("EXP(-2) failed!", (float) row.getDouble(2) == (float) java.lang.Math.exp(-2));
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
log(final XRowSet xRowRes)137cdf0e10cSrcweir     private void log(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
138cdf0e10cSrcweir     {
139cdf0e10cSrcweir         final XRow row = execute(xRowRes, "LOG(2),LOG(-2) ");
140*fcc38b58SDamjan Jovanovic         assertTrue("LOG(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.log(2));
141cdf0e10cSrcweir         row.getDouble(2);
142*fcc38b58SDamjan Jovanovic         assertTrue("LOG(-2) failed!", row.wasNull());
143cdf0e10cSrcweir     }
144cdf0e10cSrcweir 
log10(final XRowSet xRowRes)145cdf0e10cSrcweir     private void log10(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
146cdf0e10cSrcweir     {
147cdf0e10cSrcweir         final XRow row = execute(xRowRes, "LOG10(100) ");
148*fcc38b58SDamjan Jovanovic         assertTrue("LOG10(100) failed!", row.getDouble(1) == 2.0);
149cdf0e10cSrcweir     }
150cdf0e10cSrcweir 
pow(final XRowSet xRowRes)151cdf0e10cSrcweir     private void pow(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         final XRow row = execute(xRowRes, "POWER(2,2) ");
154*fcc38b58SDamjan Jovanovic         assertTrue("POWER(2,2) failed!", row.getDouble(1) == 4.0);
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
sqrt(final XRowSet xRowRes)157cdf0e10cSrcweir     private void sqrt(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
158cdf0e10cSrcweir     {
159cdf0e10cSrcweir         final XRow row = execute(xRowRes, "SQRT(4) ");
160*fcc38b58SDamjan Jovanovic         assertTrue("SQRT(4) failed!", row.getDouble(1) == 2.0);
161cdf0e10cSrcweir     }
162cdf0e10cSrcweir 
pi(final XRowSet xRowRes)163cdf0e10cSrcweir     private void pi(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         final XRow row = execute(xRowRes, "PI() ");
166*fcc38b58SDamjan Jovanovic         assertTrue("PI() failed!", (float) row.getDouble(1) == (float) java.lang.Math.PI);
167cdf0e10cSrcweir     }
168cdf0e10cSrcweir 
cos(final XRowSet xRowRes)169cdf0e10cSrcweir     private void cos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
170cdf0e10cSrcweir     {
171cdf0e10cSrcweir         final XRow row = execute(xRowRes, "COS(PI()) ");
172*fcc38b58SDamjan Jovanovic         assertTrue("COS(PI()) failed!", row.getDouble(1) == -1.0);
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir 
sin(final XRowSet xRowRes)175cdf0e10cSrcweir     private void sin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
176cdf0e10cSrcweir     {
177cdf0e10cSrcweir         final XRow row = execute(xRowRes, "SIN(2) ");
178*fcc38b58SDamjan Jovanovic         assertTrue("SIN(PI()) failed!", (float) row.getDouble(1) == (float) java.lang.Math.sin(2));
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
tan(final XRowSet xRowRes)181cdf0e10cSrcweir     private void tan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
182cdf0e10cSrcweir     {
183cdf0e10cSrcweir         final XRow row = execute(xRowRes, "TAN(PI()+1) ");
184*fcc38b58SDamjan Jovanovic         assertTrue("TAN(PI()+1) failed!", (float) row.getDouble(1) == (float) java.lang.Math.tan(java.lang.Math.PI + 1.0));
185cdf0e10cSrcweir     }
186cdf0e10cSrcweir 
acos(final XRowSet xRowRes)187cdf0e10cSrcweir     private void acos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
188cdf0e10cSrcweir     {
189cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ACOS(1) ");
190*fcc38b58SDamjan Jovanovic         assertTrue("ACOS(1) failed!", (float) row.getDouble(1) == 0.0);
191cdf0e10cSrcweir     }
192cdf0e10cSrcweir 
asin(final XRowSet xRowRes)193cdf0e10cSrcweir     private void asin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
194cdf0e10cSrcweir     {
195cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ASIN(0) ");
196*fcc38b58SDamjan Jovanovic         assertTrue("ASIN(0) failed!", (float) row.getDouble(1) == (float) java.lang.Math.asin(0.0));
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
atan(final XRowSet xRowRes)199cdf0e10cSrcweir     private void atan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ATAN(0) ");
202*fcc38b58SDamjan Jovanovic         assertTrue("ATAN(0) failed!", row.getDouble(1) == 0.0);
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
atan2(final XRowSet xRowRes)205cdf0e10cSrcweir     private void atan2(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
206cdf0e10cSrcweir     {
207cdf0e10cSrcweir         final XRow row = execute(xRowRes, "ATAN2(0,2) ");
208*fcc38b58SDamjan Jovanovic         assertTrue("ATAN2(0,2) failed!", (float) row.getDouble(1) == 0.0);
209cdf0e10cSrcweir     }
210cdf0e10cSrcweir 
degrees(final XRowSet xRowRes)211cdf0e10cSrcweir     private void degrees(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         final XRow row = execute(xRowRes, "DEGREES(PI()) ");
214*fcc38b58SDamjan Jovanovic         assertTrue("DEGREES(PI()) failed!", row.getDouble(1) == 180.0);
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
radians(final XRowSet xRowRes)217cdf0e10cSrcweir     private void radians(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         final XRow row = execute(xRowRes, "RADIANS(90) ");
220*fcc38b58SDamjan Jovanovic         assertTrue("RADIANS(90) failed!", (float) row.getDouble(1) == (float) (java.lang.Math.PI / 2.0));
221cdf0e10cSrcweir     }
222cdf0e10cSrcweir }
223