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 30 import static org.junit.Assert.*; 31 32 public class DBaseNumericFunctions 33 { 34 private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'"; 35 private final XMultiServiceFactory m_xORB; 36 37 public DBaseNumericFunctions(final XMultiServiceFactory _xORB) 38 { 39 m_xORB = _xORB; 40 } 41 42 public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 43 { 44 final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, 45 m_xORB.createInstance("com.sun.star.sdb.RowSet")); 46 47 System.out.println("starting Numeric function test"); 48 // set the properties needed to connect to a database 49 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 50 xProp.setPropertyValue("DataSourceName", "Bibliography"); 51 52 xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND)); 53 54 abs(xRowRes); 55 acos(xRowRes); 56 asin(xRowRes); 57 atan(xRowRes); 58 atan2(xRowRes); 59 ceiling(xRowRes); 60 cos(xRowRes); 61 degrees(xRowRes); 62 exp(xRowRes); 63 floor(xRowRes); 64 log(xRowRes); 65 log10(xRowRes); 66 mod(xRowRes); 67 pi(xRowRes); 68 pow(xRowRes); 69 radians(xRowRes); 70 round(xRowRes); 71 sign(xRowRes); 72 sin(xRowRes); 73 sqrt(xRowRes); 74 tan(xRowRes); 75 } 76 77 private XRow execute(final XRowSet xRowRes,final String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 78 { 79 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 80 xProp.setPropertyValue("Command", "SELECT " + sql + where); 81 xRowRes.execute(); 82 final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes); 83 assertTrue("No valid row! ", xRes.next()); 84 85 return (XRow) UnoRuntime.queryInterface(XRow.class, xRes); 86 } 87 88 private void abs(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 89 { 90 final XRow row = execute(xRowRes, "ABS(2),ABS(-32) "); 91 assertTrue("ABS(2) failed!", row.getInt(1) == 2); 92 assertTrue("ABS(-32) failed!", row.getInt(2) == 32); 93 } 94 95 private void sign(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 96 { 97 final XRow row = execute(xRowRes, "SIGN(-32),SIGN(0),SIGN(234) "); 98 assertTrue("SIGN(-32)failed!", row.getInt(1) == -1); 99 assertTrue("SIGN(0) failed!", row.getInt(2) == 0); 100 assertTrue("SIGN(234) failed!", row.getInt(3) == 1); 101 } 102 103 private void mod(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 104 { 105 final XRow row = execute(xRowRes, "MOD(234, 10) "); 106 assertTrue("MOD(234, 10) failed!", row.getInt(1) == 4); 107 } 108 109 private void floor(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 110 { 111 final XRow row = execute(xRowRes, "FLOOR(1.23),FLOOR(-1.23) "); 112 assertTrue("FLOOR(1.23) failed!", row.getInt(1) == 1); 113 assertTrue("FLOOR(-1.23) failed!", row.getInt(2) == -2); 114 } 115 116 private void ceiling(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 117 { 118 final XRow row = execute(xRowRes, "CEILING(1.23),CEILING(-1.23) "); 119 assertTrue("CEILING(1.23) failed!", row.getInt(1) == 2); 120 assertTrue("CEILING(-1.23) failed!", row.getInt(2) == -1); 121 } 122 123 private void round(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 124 { 125 final XRow row = execute(xRowRes, "ROUND(-1.23),ROUND(1.298, 1) "); 126 assertTrue("ROUND(-1.23) failed!", row.getInt(1) == -1); 127 assertTrue("ROUND(1.298, 1) failed!", row.getDouble(2) == 1.3); 128 } 129 130 private void exp(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 131 { 132 final XRow row = execute(xRowRes, "EXP(2),EXP(-2) "); 133 assertTrue("EXP(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.exp(2)); 134 assertTrue("EXP(-2) failed!", (float) row.getDouble(2) == (float) java.lang.Math.exp(-2)); 135 } 136 137 private void log(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 138 { 139 final XRow row = execute(xRowRes, "LOG(2),LOG(-2) "); 140 assertTrue("LOG(2) failed!", (float) row.getDouble(1) == (float) java.lang.Math.log(2)); 141 row.getDouble(2); 142 assertTrue("LOG(-2) failed!", row.wasNull()); 143 } 144 145 private void log10(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 146 { 147 final XRow row = execute(xRowRes, "LOG10(100) "); 148 assertTrue("LOG10(100) failed!", row.getDouble(1) == 2.0); 149 } 150 151 private void pow(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 152 { 153 final XRow row = execute(xRowRes, "POWER(2,2) "); 154 assertTrue("POWER(2,2) failed!", row.getDouble(1) == 4.0); 155 } 156 157 private void sqrt(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 158 { 159 final XRow row = execute(xRowRes, "SQRT(4) "); 160 assertTrue("SQRT(4) failed!", row.getDouble(1) == 2.0); 161 } 162 163 private void pi(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 164 { 165 final XRow row = execute(xRowRes, "PI() "); 166 assertTrue("PI() failed!", (float) row.getDouble(1) == (float) java.lang.Math.PI); 167 } 168 169 private void cos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 170 { 171 final XRow row = execute(xRowRes, "COS(PI()) "); 172 assertTrue("COS(PI()) failed!", row.getDouble(1) == -1.0); 173 } 174 175 private void sin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 176 { 177 final XRow row = execute(xRowRes, "SIN(2) "); 178 assertTrue("SIN(PI()) failed!", (float) row.getDouble(1) == (float) java.lang.Math.sin(2)); 179 } 180 181 private void tan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 182 { 183 final XRow row = execute(xRowRes, "TAN(PI()+1) "); 184 assertTrue("TAN(PI()+1) failed!", (float) row.getDouble(1) == (float) java.lang.Math.tan(java.lang.Math.PI + 1.0)); 185 } 186 187 private void acos(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 188 { 189 final XRow row = execute(xRowRes, "ACOS(1) "); 190 assertTrue("ACOS(1) failed!", (float) row.getDouble(1) == 0.0); 191 } 192 193 private void asin(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 194 { 195 final XRow row = execute(xRowRes, "ASIN(0) "); 196 assertTrue("ASIN(0) failed!", (float) row.getDouble(1) == (float) java.lang.Math.asin(0.0)); 197 } 198 199 private void atan(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 200 { 201 final XRow row = execute(xRowRes, "ATAN(0) "); 202 assertTrue("ATAN(0) failed!", row.getDouble(1) == 0.0); 203 } 204 205 private void atan2(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 206 { 207 final XRow row = execute(xRowRes, "ATAN2(0,2) "); 208 assertTrue("ATAN2(0,2) failed!", (float) row.getDouble(1) == 0.0); 209 } 210 211 private void degrees(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 212 { 213 final XRow row = execute(xRowRes, "DEGREES(PI()) "); 214 assertTrue("DEGREES(PI()) failed!", row.getDouble(1) == 180.0); 215 } 216 217 private void radians(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 218 { 219 final XRow row = execute(xRowRes, "RADIANS(90) "); 220 assertTrue("RADIANS(90) failed!", (float) row.getDouble(1) == (float) (java.lang.Math.PI / 2.0)); 221 } 222 } 223