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: DBaseStringFunctions.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
;
35 import com
.sun
.star
.lang
.XMultiServiceFactory
;
37 public class DBaseStringFunctions
39 private String where
= "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'";
40 private final XMultiServiceFactory m_xORB
;
41 private final DBaseDriverTest testcase
;
43 public DBaseStringFunctions(final XMultiServiceFactory _xORB
,final DBaseDriverTest _testcase
)
49 private void assure(final String s
,final boolean b
)
51 testcase
.assure2(s
, b
);
54 public void testFunctions() throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
56 final XRowSet xRowRes
= (XRowSet
) UnoRuntime
.queryInterface(XRowSet
.class,
57 m_xORB
.createInstance("com.sun.star.sdb.RowSet"));
59 testcase
.getLog().println("starting String function test");
60 // set the properties needed to connect to a database
61 final XPropertySet xProp
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, xRowRes
);
62 xProp
.setPropertyValue("DataSourceName", "Bibliography");
64 xProp
.setPropertyValue("CommandType", Integer
.valueOf(com
.sun
.star
.sdb
.CommandType
.COMMAND
));
70 catch (SQLException ex
)
72 assure("upper " + ex
.getMessage(), false);
79 catch (SQLException ex
)
81 assure("lower " + ex
.getMessage(), false);
88 catch (SQLException ex
)
90 assure("ascii " + ex
.getMessage(), false);
97 catch (SQLException ex
)
99 assure("char_len " + ex
.getMessage(), false);
106 catch (SQLException ex
)
108 assure("concat " + ex
.getMessage(), false);
115 catch (SQLException ex
)
117 assure("char " + ex
.getMessage(), false);
124 catch (SQLException ex
)
126 assure("locate " + ex
.getMessage(), false);
133 catch (SQLException ex
)
135 assure("substr " + ex
.getMessage(), false);
142 catch (SQLException ex
)
144 assure("ltrim " + ex
.getMessage(), false);
151 catch (SQLException ex
)
153 assure("rtrim " + ex
.getMessage(), false);
160 catch (SQLException ex
)
162 assure("space " + ex
.getMessage(), false);
169 catch (SQLException ex
)
171 assure("replace " + ex
.getMessage(), false);
178 catch (SQLException ex
)
180 assure("repeat " + ex
.getMessage(), false);
187 catch (SQLException ex
)
189 assure("insert " + ex
.getMessage(), false);
196 catch (SQLException ex
)
198 assure("left " + ex
.getMessage(), false);
205 catch (SQLException ex
)
207 assure("right " + ex
.getMessage(), false);
212 private XRow
execute(final XRowSet xRowRes
, String sql
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
214 final XPropertySet xProp
= (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class, xRowRes
);
215 xProp
.setPropertyValue("Command", "SELECT " + sql
+ where
);
217 final XResultSet xRes
= (XResultSet
) UnoRuntime
.queryInterface(XResultSet
.class, xRowRes
);
218 assure("No valid row! ", xRes
.next());
220 return (XRow
) UnoRuntime
.queryInterface(XRow
.class, xRes
);
223 private void upper(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
225 final XRow row
= execute(xRowRes
, "upper('test'),UCASE('test') ");
226 assure("upper('test') failed!", row
.getString(1).equals("TEST"));
227 assure("ucase('test') failed!", row
.getString(2).equals("TEST"));
230 private void lower(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
232 final XRow row
= execute(xRowRes
, "lower('TEST'),LCASE('TEST') ");
233 assure("lower('TEST') failed!", row
.getString(1).equals("test"));
234 assure("lcase('TEST') failed!", row
.getString(2).equals("test"));
235 final String temp
= where
;
236 where
= "FROM \"biblio\" \"biblio\" where LOWER(\"Identifier\") like 'bor%'";
237 execute(xRowRes
, "lower('TEST'),LCASE('TEST') ");
241 private void acsii(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
243 final XRow row
= execute(xRowRes
, "ASCII('2') ");
244 assure("acsii('2') failed!", row
.getInt(1) == 50);
247 private void char_length(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
249 final XRow row
= execute(xRowRes
, "char_length('test'),character_length('test'),OCTET_LENGTH('test') ");
250 assure("char_length('test') failed!", row
.getInt(1) == 4);
251 assure("character_length('test') failed!", row
.getInt(2) == 4);
252 assure("OCTET_LENGTH('test') failed!", row
.getInt(3) == 4);
255 private void concat(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
257 final XRow row
= execute(xRowRes
, "CONCAT('Hello',' ','World') ");
258 assure("CONCAT('Hello',' ',,'World') failed!", row
.getString(1).equals("Hello World"));
261 private void locate(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
263 final XRow row
= execute(xRowRes
, "LOCATE('bar', 'foobarbar') ");
264 assure("LOCATE('bar', 'foobarbar') failed!", row
.getInt(1) == 4);
267 private void substring(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
269 final XRow row
= execute(xRowRes
, "SUBSTRING('Quadratically',5) ");
270 assure("SUBSTRING('Quadratically',5) failed!", row
.getString(1).equals("ratically"));
273 private void ltrim(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
275 final XRow row
= execute(xRowRes
, "LTRIM(' barbar') ");
276 assure("LTRIM(' barbar') failed!", row
.getString(1).equals("barbar"));
279 private void rtrim(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
281 final XRow row
= execute(xRowRes
, "RTRIM('barbar ') ");
282 assure("RTRIM('barbar ') failed!", row
.getString(1).equals("barbar"));
285 private void space(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
287 final XRow row
= execute(xRowRes
, "space(6) ");
288 assure("space(6) failed!", row
.getString(1).equals(" "));
291 private void replace(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
293 final XRow row
= execute(xRowRes
, "REPLACE('www.OOo.com', 'w', 'Ww') ");
294 assure("REPLACE('www.OOo.com', 'w', 'Ww') failed!", row
.getString(1).equals("WwWwWw.OOo.com"));
297 private void repeat(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
299 final XRow row
= execute(xRowRes
, "REPEAT('OOo', 3) ");
300 assure("REPEAT('OOo', 3) failed!", row
.getString(1).equals("OOoOOoOOo"));
303 private void insert(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
305 final XRow row
= execute(xRowRes
, "INSERT('Quadratic', 3, 4, 'What') ");
306 assure("INSERT('Quadratic', 3, 4, 'What') failed!", row
.getString(1).equals("QuWhattic"));
309 private void left(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
311 final XRow row
= execute(xRowRes
, "LEFT('foobarbar', 5) ");
312 assure("LEFT('foobarbar', 5) failed!", row
.getString(1).equals("fooba"));
315 private void right(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
317 final XRow row
= execute(xRowRes
, "RIGHT('foobarbar', 4) ");
318 assure("RIGHT('foobarbar', 4) failed!", row
.getString(1).equals("rbar"));
321 private void chartest(final XRowSet xRowRes
) throws com
.sun
.star
.uno
.Exception
, com
.sun
.star
.beans
.UnknownPropertyException
323 final XRow row
= execute(xRowRes
, "CHAR(ascii('t'),ascii('e'),ascii('s'),ascii('t')) ");
324 assure("CHAR(ascii('t'),ascii('e'),ascii('s'),ascii('t')) failed!", row
.getString(1).equals("test"));