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