summaryrefslogtreecommitdiff
path: root/connectivity/qa/drivers/dbase/DBaseNumericFunctions.java
blob: fdca0f1245acbd446f832492f8c874a7ad651b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: DBaseNumericFunctions.java,v $
 * $Revision: 1.6 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
package complex.connectivity;

import complex.connectivity.DBaseDriverTest;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.util.XCloseable;
import com.sun.star.sdbc.*;
import com.sun.star.sdb.*;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;

import com.sun.star.lang.XMultiServiceFactory;

import complexlib.ComplexTestCase;

import java.io.PrintWriter;
 
import util.utils;
import java.util.*;
import java.io.*;


public class DBaseNumericFunctions {

        private String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'";
        private XMultiServiceFactory m_xORB;
        private DBaseDriverTest testcase;
        public DBaseNumericFunctions(XMultiServiceFactory _xORB,DBaseDriverTest _testcase){
            m_xORB = _xORB;
            testcase = _testcase;
        }
        
        private void assure(String s,boolean b){
            testcase.assure2(s,b);
        }
    
    public void testFunctions() throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRowSet xRowRes = (XRowSet)UnoRuntime.queryInterface(XRowSet.class,
                            m_xORB.createInstance("com.sun.star.sdb.RowSet"));

        System.out.println("starting Numeric function test");
        // set the properties needed to connect to a database
        XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
        xProp.setPropertyValue("DataSourceName","Bibliography");
        
        xProp.setPropertyValue("CommandType",new Integer(com.sun.star.sdb.CommandType.COMMAND));
        
        try {
            abs(xRowRes);
        } catch( SQLException ex){
            assure("abs " + ex.getMessage(),false);
            throw ex;
        }
        try{
            acos(xRowRes);
        } catch( SQLException ex){
            assure("acos " + ex.getMessage(),false);
            throw ex;
        }
        try{
            asin(xRowRes);
        } catch( SQLException ex){
            assure("asin " + ex.getMessage(),false);
            throw ex;
        }
        try{
            atan(xRowRes);
        } catch( SQLException ex){
            assure("atan " + ex.getMessage(),false);
            throw ex;
        }
        try{
            atan2(xRowRes);
        } catch( SQLException ex){
            assure("atan2 " + ex.getMessage(),false);
            throw ex;
        }
        try{
            ceiling(xRowRes);
        } catch( SQLException ex){
            assure("ceiling " + ex.getMessage(),false);
            throw ex;
        }
        try{
            cos(xRowRes);
        } catch( SQLException ex){
            assure("cos " + ex.getMessage(),false);
            throw ex;
        }
        try{
            degrees(xRowRes);
        } catch( SQLException ex){
            assure("degrees " + ex.getMessage(),false);
            throw ex;
        }
        try{
            exp(xRowRes);
        } catch( SQLException ex){
            assure("exp " + ex.getMessage(),false);
            throw ex;
        }
        try{
            floor(xRowRes);
        } catch( SQLException ex){
            assure("floor " + ex.getMessage(),false);
            throw ex;
        }
        try{
            log(xRowRes);
        } catch( SQLException ex){
            assure("log " + ex.getMessage(),false);
            throw ex;
        }
        try{
            log10(xRowRes);
        } catch( SQLException ex){
            assure("log10 " + ex.getMessage(),false);
            throw ex;
        }
        try{
            mod(xRowRes);
        } catch( SQLException ex){
            assure("mod " + ex.getMessage(),false);
            throw ex;
        }
        try{
            pi(xRowRes);
        } catch( SQLException ex){
            assure("pi " + ex.getMessage(),false);
            throw ex;
        }
        try{
            pow(xRowRes);
        } catch( SQLException ex){
            assure("pow " + ex.getMessage(),false);
            throw ex;
        }
        try{
            radians(xRowRes);
        } catch( SQLException ex){
            assure("radians " + ex.getMessage(),false);
            throw ex;
        }
        try{
            round(xRowRes);
        } catch( SQLException ex){
            assure("round " + ex.getMessage(),false);
            throw ex;
        }
        try{
            sign(xRowRes);
        } catch( SQLException ex){
            assure("sign " + ex.getMessage(),false);
            throw ex;
        }
        try{
            sin(xRowRes);
        } catch( SQLException ex){
            assure("sin " + ex.getMessage(),false);
            throw ex;
        }
        try{
            sqrt(xRowRes);
        } catch( SQLException ex){
            assure("sqrt " + ex.getMessage(),false);
            throw ex;
        }
        try{
            tan(xRowRes);
        } catch( SQLException ex){
            assure("tan " + ex.getMessage(),false);
            throw ex;
        }
        
    }
    
    private XRow execute(XRowSet xRowRes,String sql) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XPropertySet xProp = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class,xRowRes);
        xProp.setPropertyValue("Command","SELECT " + sql + where);
        xRowRes.execute();
        XResultSet xRes = (XResultSet)UnoRuntime.queryInterface(XResultSet.class,xRowRes);
        assure("No valid row! ",xRes.next());
        
        return (XRow)UnoRuntime.queryInterface(XRow.class, xRes);
    }
    
    private void abs(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ABS(2),ABS(-32) ");
        assure("ABS(2) failed!",row.getInt(1) == 2);
        assure("ABS(-32) failed!",row.getInt(2) == 32);
    }
    
    private void sign(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"SIGN(-32),SIGN(0),SIGN(234) ");
        assure("SIGN(-32)failed!",row.getInt(1) == -1);
        assure("SIGN(0) failed!",row.getInt(2) == 0);
        assure("SIGN(234) failed!",row.getInt(3) == 1);
    }
    
    private void mod(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"MOD(234, 10) ");
        assure("MOD(234, 10) failed!",row.getInt(1) == 4);       
    }
    
    private void floor(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"FLOOR(1.23),FLOOR(-1.23) ");
        assure("FLOOR(1.23) failed!",row.getInt(1) == 1);       
        assure("FLOOR(-1.23) failed!",row.getInt(2) == -2);       
    }
    private void ceiling(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"CEILING(1.23),CEILING(-1.23) ");
        assure("CEILING(1.23) failed!",row.getInt(1) == 2);       
        assure("CEILING(-1.23) failed!",row.getInt(2) == -1);       
    }
    private void round(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ROUND(-1.23),ROUND(1.298, 1) ");
        assure("ROUND(-1.23) failed!",row.getInt(1) == -1);       
        assure("ROUND(1.298, 1) failed!",row.getDouble(2) == 1.3);       
    }
    private void exp(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"EXP(2),EXP(-2) ");
        assure("EXP(2) failed!",(float)row.getDouble(1) == (float)java.lang.Math.exp(2) );       
        assure("EXP(-2) failed!",(float)row.getDouble(2) == (float)java.lang.Math.exp(-2));       
    }
    private void log(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"LOG(2),LOG(-2) ");
        assure("LOG(2) failed!",(float)row.getDouble(1) == (float)java.lang.Math.log(2) );       
        row.getDouble(2);
        assure("LOG(-2) failed!",row.wasNull());       
    }
    private void log10(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"LOG10(100) ");
        assure("LOG10(100) failed!",row.getDouble(1) == 2.0 );       
    }
    private void pow(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"POWER(2,2) ");
        assure("POWER(2,2) failed!",row.getDouble(1) == 4.0 );       
    }
    private void sqrt(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"SQRT(4) ");
        assure("SQRT(4) failed!",row.getDouble(1) == 2.0 );       
    }
    private void pi(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"PI() ");
        assure("PI() failed!",(float)row.getDouble(1) == (float)java.lang.Math.PI );       
    }
    private void cos(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"COS(PI()) ");
        assure("COS(PI()) failed!",row.getDouble(1) == -1.0 );
    }
    private void sin(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"SIN(2) ");
        assure("SIN(PI()) failed!",(float)row.getDouble(1) == (float)java.lang.Math.sin( 2 ) );
    }
    private void tan(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"TAN(PI()+1) ");
        assure("TAN(PI()+1) failed!",(float)row.getDouble(1) == (float)java.lang.Math.tan(java.lang.Math.PI+1.0) );
    }
    private void acos(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ACOS(1) ");
        assure("ACOS(1) failed!",(float)row.getDouble(1) == 0.0 );
    }
    private void asin(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ASIN(0) ");
        assure("ASIN(0) failed!",(float)row.getDouble(1) == (float)java.lang.Math.asin(0.0) );
    }
    private void atan(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ATAN(0) ");
        assure("ATAN(0) failed!",row.getDouble(1) == 0.0 );
    }
    private void atan2(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"ATAN2(0,2) ");
        assure("ATAN2(0,2) failed!",(float)row.getDouble(1) == 0.0 );
    }
    private void degrees(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"DEGREES(PI()) ");
        assure("DEGREES(PI()) failed!",row.getDouble(1) == 180.0 );
    }
    private void radians(XRowSet xRowRes) throws com.sun.star.uno.Exception,com.sun.star.beans.UnknownPropertyException {
        XRow row = execute(xRowRes,"RADIANS(90) ");
        assure("RADIANS(90) failed!",(float)row.getDouble(1) == (float)(java.lang.Math.PI / 2.0) );
    }
}