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 DBaseDateFunctions 33 { 34 35 private final String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'"; 36 private final XMultiServiceFactory m_xORB; 37 DBaseDateFunctions(final XMultiServiceFactory _xORB)38 public DBaseDateFunctions(final XMultiServiceFactory _xORB) 39 { 40 m_xORB = _xORB; 41 } 42 testFunctions()43 public void testFunctions() throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 44 { 45 final XRowSet xRowRes = (XRowSet) UnoRuntime.queryInterface(XRowSet.class, 46 m_xORB.createInstance("com.sun.star.sdb.RowSet")); 47 48 System.out.println("starting DateTime function test!"); 49 // set the properties needed to connect to a database 50 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 51 xProp.setPropertyValue("DataSourceName", "Bibliography"); 52 53 xProp.setPropertyValue("CommandType", Integer.valueOf(com.sun.star.sdb.CommandType.COMMAND)); 54 55 curdate(xRowRes); 56 curtime(xRowRes); 57 dayname(xRowRes); 58 dayofmonth(xRowRes); 59 dayofweek(xRowRes); 60 dayofyear(xRowRes); 61 hour(xRowRes); 62 minute(xRowRes); 63 month(xRowRes); 64 monthname(xRowRes); 65 now(xRowRes); 66 quarter(xRowRes); 67 second(xRowRes); 68 week(xRowRes); 69 year(xRowRes); 70 } 71 execute(final XRowSet xRowRes, final String sql)72 private XRow execute(final XRowSet xRowRes, final String sql) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 73 { 74 final XPropertySet xProp = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xRowRes); 75 xProp.setPropertyValue("Command", "SELECT " + sql + where); 76 xRowRes.execute(); 77 final XResultSet xRes = (XResultSet) UnoRuntime.queryInterface(XResultSet.class, xRowRes); 78 assertTrue("No valid row! ", xRes.next()); 79 80 return (XRow) UnoRuntime.queryInterface(XRow.class, xRes); 81 } 82 dayofweek(final XRowSet xRowRes)83 private void dayofweek(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 84 { 85 final XRow row = execute(xRowRes, "DAYOFWEEK('1998-02-03') "); 86 assertTrue("DAYOFWEEK('1998-02-03') failed!", row.getInt(1) == 3); 87 } 88 dayofmonth(final XRowSet xRowRes)89 private void dayofmonth(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 90 { 91 final XRow row = execute(xRowRes, "DAYOFMONTH('1998-02-03') "); 92 assertTrue("DAYOFMONTH('1998-02-03') failed!", row.getInt(1) == 3); 93 } 94 dayofyear(final XRowSet xRowRes)95 private void dayofyear(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 96 { 97 final XRow row = execute(xRowRes, "DAYOFYEAR('1998-02-03') "); 98 assertTrue("DAYOFYEAR('1998-02-03') failed!", row.getInt(1) == 34); 99 } 100 month(final XRowSet xRowRes)101 private void month(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 102 { 103 final XRow row = execute(xRowRes, "month('1998-02-03') "); 104 assertTrue("month('1998-02-03') failed!", row.getInt(1) == 2); 105 } 106 dayname(final XRowSet xRowRes)107 private void dayname(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 108 { 109 final XRow row = execute(xRowRes, "DAYNAME('1998-02-05') "); 110 assertTrue("DAYNAME('1998-02-05') failed!", row.getString(1).equals("Thursday")); 111 } 112 monthname(final XRowSet xRowRes)113 private void monthname(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 114 { 115 final XRow row = execute(xRowRes, "MONTHNAME('1998-02-05') "); 116 assertTrue("MONTHNAME('1998-02-05') failed!", row.getString(1).equals("February")); 117 } 118 quarter(final XRowSet xRowRes)119 private void quarter(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 120 { 121 final XRow row = execute(xRowRes, "QUARTER('98-01-01'),QUARTER('98-04-01'),QUARTER('98-07-01'),QUARTER('98-10-01') "); 122 assertTrue("QUARTER('98-01-01') failed!", row.getInt(1) == 1); 123 assertTrue("QUARTER('98-04-01') failed!", row.getInt(2) == 2); 124 assertTrue("QUARTER('98-07-01') failed!", row.getInt(3) == 3); 125 assertTrue("QUARTER('98-10-01') failed!", row.getInt(4) == 4); 126 } 127 week(final XRowSet xRowRes)128 private void week(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 129 { 130 final XRow row = execute(xRowRes, "WEEK('1998-02-20') "); 131 assertTrue("WEEK('1998-02-20') failed!", row.getInt(1) == 7); 132 } 133 year(final XRowSet xRowRes)134 private void year(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 135 { 136 final XRow row = execute(xRowRes, "YEAR('98-02-03') "); 137 assertTrue("YEAR('98-02-03') failed!", row.getInt(1) == 98); 138 } 139 hour(final XRowSet xRowRes)140 private void hour(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 141 { 142 final XRow row = execute(xRowRes, "HOUR('10:05:03') "); 143 assertTrue("HOUR('10:05:03') failed!", row.getInt(1) == 10); 144 } 145 minute(final XRowSet xRowRes)146 private void minute(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 147 { 148 final XRow row = execute(xRowRes, "MINUTE('98-02-03 10:05:03') "); 149 assertTrue("MINUTE('98-02-03 10:05:03') failed!", row.getInt(1) == 5); 150 } 151 second(final XRowSet xRowRes)152 private void second(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 153 { 154 final XRow row = execute(xRowRes, "SECOND('10:05:03') "); 155 assertTrue("SECOND('10:05:03') failed!", row.getInt(1) == 3); 156 } 157 curdate(final XRowSet xRowRes)158 private void curdate(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 159 { 160 final XRow row = execute(xRowRes, "CURDATE() "); 161 final com.sun.star.util.Date aDate = row.getDate(1); 162 System.out.println("CURDATE() is '" + aDate.Year + "-" + aDate.Month + "-" + aDate.Day + "'"); 163 } 164 curtime(final XRowSet xRowRes)165 private void curtime(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 166 { 167 final XRow row = execute(xRowRes, "CURTIME() "); 168 final com.sun.star.util.Time aTime = row.getTime(1); 169 System.out.println("CURTIME() is '" + aTime.Hours + ":" + aTime.Minutes + ":" + aTime.Seconds + "'"); 170 } 171 now(final XRowSet xRowRes)172 private void now(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 173 { 174 final XRow row = execute(xRowRes, "NOW() "); 175 final com.sun.star.util.DateTime aTime = row.getTimestamp(1); 176 System.out.println("NOW() is '" + aTime.Year + "-" + aTime.Month + "-" + aTime.Day + "'"); 177 System.out.println("'" + aTime.Hours + ":" + aTime.Minutes + ":" + aTime.Seconds + "'"); 178 } 179 } 180