1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DBaseDateFunctions.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 package qa
.drivers
.dbase
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
33 import com
.sun
.star
.sdbc
.*;
34 import com
.sun
.star
.beans
.XPropertySet
;
36 import com
.sun
.star
.lang
.XMultiServiceFactory
;
38 public class DBaseDateFunctions
41 private final String where
= "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'";
42 private final XMultiServiceFactory m_xORB
;
43 private final DBaseDriverTest testcase
;
45 public DBaseDateFunctions(final XMultiServiceFactory _xORB
, final DBaseDriverTest _testcase
)
51 private void assure(final String s
, final boolean b
)
53 testcase
.assure2(s
, b
);
56 public void testFunctions() throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
58 final XRowSet xRowRes
= (XRowSet
) UnoRuntime
.queryInterface(XRowSet
.class,
59 m_xORB
.createInstance("com.sun.star.sdb.RowSet"));
61 testcase
.getLog().println("starting DateTime function test!");
62 // set the properties needed to connect to a database
63 final XPropertySet xProp
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, xRowRes
);
64 xProp
.setPropertyValue("DataSourceName", "Bibliography");
66 xProp
.setPropertyValue("CommandType", Integer
.valueOf(com
.sun
.star
.sdb
.CommandType
.COMMAND
));
72 catch (SQLException ex
)
74 assure("upper " + ex
.getMessage(), false);
81 catch (SQLException ex
)
83 assure("lower " + ex
.getMessage(), false);
90 catch (SQLException ex
)
92 assure("ascii " + ex
.getMessage(), false);
99 catch (SQLException ex
)
101 assure("char_len " + ex
.getMessage(), false);
108 catch (SQLException ex
)
110 assure("concat " + ex
.getMessage(), false);
117 catch (SQLException ex
)
119 assure("locate " + ex
.getMessage(), false);
126 catch (SQLException ex
)
128 assure("substr " + ex
.getMessage(), false);
135 catch (SQLException ex
)
137 assure("ltrim " + ex
.getMessage(), false);
144 catch (SQLException ex
)
146 assure("rtrim " + ex
.getMessage(), false);
153 catch (SQLException ex
)
155 assure("space " + ex
.getMessage(), false);
162 catch (SQLException ex
)
164 assure("replace " + ex
.getMessage(), false);
171 catch (SQLException ex
)
173 assure("repeat " + ex
.getMessage(), false);
180 catch (SQLException ex
)
182 assure("insert " + ex
.getMessage(), false);
189 catch (SQLException ex
)
191 assure("left " + ex
.getMessage(), false);
198 catch (SQLException ex
)
200 assure("right " + ex
.getMessage(), false);
205 private XRow
execute(final XRowSet xRowRes
, final String sql
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
207 final XPropertySet xProp
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, xRowRes
);
208 xProp
.setPropertyValue("Command", "SELECT " + sql
+ where
);
210 final XResultSet xRes
= (XResultSet
) UnoRuntime
.queryInterface(XResultSet
.class, xRowRes
);
211 assure("No valid row! ", xRes
.next());
213 return (XRow
) UnoRuntime
.queryInterface(XRow
.class, xRes
);
216 private void dayofweek(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
218 final XRow row
= execute(xRowRes
, "DAYOFWEEK('1998-02-03') ");
219 assure("DAYOFWEEK('1998-02-03') failed!", row
.getInt(1) == 3);
222 private void dayofmonth(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
224 final XRow row
= execute(xRowRes
, "DAYOFMONTH('1998-02-03') ");
225 assure("DAYOFMONTH('1998-02-03') failed!", row
.getInt(1) == 3);
228 private void dayofyear(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
230 final XRow row
= execute(xRowRes
, "DAYOFYEAR('1998-02-03') ");
231 assure("DAYOFYEAR('1998-02-03') failed!", row
.getInt(1) == 34);
234 private void month(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
236 final XRow row
= execute(xRowRes
, "month('1998-02-03') ");
237 assure("month('1998-02-03') failed!", row
.getInt(1) == 2);
240 private void dayname(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
242 final XRow row
= execute(xRowRes
, "DAYNAME('1998-02-05') ");
243 assure("DAYNAME('1998-02-05') failed!", row
.getString(1).equals("Thursday"));
246 private void monthname(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
248 final XRow row
= execute(xRowRes
, "MONTHNAME('1998-02-05') ");
249 assure("MONTHNAME('1998-02-05') failed!", row
.getString(1).equals("February"));
252 private void quarter(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
254 final XRow row
= execute(xRowRes
, "QUARTER('98-01-01'),QUARTER('98-04-01'),QUARTER('98-07-01'),QUARTER('98-10-01') ");
255 assure("QUARTER('98-01-01') failed!", row
.getInt(1) == 1);
256 assure("QUARTER('98-04-01') failed!", row
.getInt(2) == 2);
257 assure("QUARTER('98-07-01') failed!", row
.getInt(3) == 3);
258 assure("QUARTER('98-10-01') failed!", row
.getInt(4) == 4);
261 private void week(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
263 final XRow row
= execute(xRowRes
, "WEEK('1998-02-20') ");
264 assure("WEEK('1998-02-20') failed!", row
.getInt(1) == 7);
267 private void year(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
269 final XRow row
= execute(xRowRes
, "YEAR('98-02-03') ");
270 assure("YEAR('98-02-03') failed!", row
.getInt(1) == 98);
273 private void hour(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
275 final XRow row
= execute(xRowRes
, "HOUR('10:05:03') ");
276 assure("HOUR('10:05:03') failed!", row
.getInt(1) == 10);
279 private void minute(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
281 final XRow row
= execute(xRowRes
, "MINUTE('98-02-03 10:05:03') ");
282 assure("MINUTE('98-02-03 10:05:03') failed!", row
.getInt(1) == 5);
285 private void second(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
287 final XRow row
= execute(xRowRes
, "SECOND('10:05:03') ");
288 assure("SECOND('10:05:03') failed!", row
.getInt(1) == 3);
291 private void curdate(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
293 final XRow row
= execute(xRowRes
, "CURDATE() ");
294 final com
.sun
.star
.util
.Date aDate
= row
.getDate(1);
295 testcase
.getLog().println("CURDATE() is '" + aDate
.Year
+ "-" + aDate
.Month
+ "-" + aDate
.Day
+ "'");
298 private void curtime(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
300 final XRow row
= execute(xRowRes
, "CURTIME() ");
301 final com
.sun
.star
.util
.Time aTime
= row
.getTime(1);
302 testcase
.getLog().println("CURTIME() is '" + aTime
.Hours
+ ":" + aTime
.Minutes
+ ":" + aTime
.Seconds
+ "'");
305 private void now(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
307 final XRow row
= execute(xRowRes
, "NOW() ");
308 final com
.sun
.star
.util
.DateTime aTime
= row
.getTimestamp(1);
309 testcase
.getLog().println("NOW() is '" + aTime
.Year
+ "-" + aTime
.Month
+ "-" + aTime
.Day
+ "'");
310 testcase
.getLog().println("'" + aTime
.Hours
+ ":" + aTime
.Minutes
+ ":" + aTime
.Seconds
+ "'");