Update ooo320-m1
[ooovba.git] / connectivity / qa / drivers / dbase / DBaseStringFunctions.java
blobf6a35dafc7bb56778be058b71a7aafd90c8edb84
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DBaseStringFunctions.java,v $
10 * $Revision: 1.6 $
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)
45 m_xORB = _xORB;
46 testcase = _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));
66 try
68 upper(xRowRes);
70 catch (SQLException ex)
72 assure("upper " + ex.getMessage(), false);
73 throw ex;
75 try
77 lower(xRowRes);
79 catch (SQLException ex)
81 assure("lower " + ex.getMessage(), false);
82 throw ex;
84 try
86 acsii(xRowRes);
88 catch (SQLException ex)
90 assure("ascii " + ex.getMessage(), false);
91 throw ex;
93 try
95 char_length(xRowRes);
97 catch (SQLException ex)
99 assure("char_len " + ex.getMessage(), false);
100 throw ex;
104 concat(xRowRes);
106 catch (SQLException ex)
108 assure("concat " + ex.getMessage(), false);
109 throw ex;
113 chartest(xRowRes);
115 catch (SQLException ex)
117 assure("char " + ex.getMessage(), false);
118 throw ex;
122 locate(xRowRes);
124 catch (SQLException ex)
126 assure("locate " + ex.getMessage(), false);
127 throw ex;
131 substring(xRowRes);
133 catch (SQLException ex)
135 assure("substr " + ex.getMessage(), false);
136 throw ex;
140 ltrim(xRowRes);
142 catch (SQLException ex)
144 assure("ltrim " + ex.getMessage(), false);
145 throw ex;
149 rtrim(xRowRes);
151 catch (SQLException ex)
153 assure("rtrim " + ex.getMessage(), false);
154 throw ex;
158 space(xRowRes);
160 catch (SQLException ex)
162 assure("space " + ex.getMessage(), false);
163 throw ex;
167 replace(xRowRes);
169 catch (SQLException ex)
171 assure("replace " + ex.getMessage(), false);
172 throw ex;
176 repeat(xRowRes);
178 catch (SQLException ex)
180 assure("repeat " + ex.getMessage(), false);
181 throw ex;
185 insert(xRowRes);
187 catch (SQLException ex)
189 assure("insert " + ex.getMessage(), false);
190 throw ex;
194 left(xRowRes);
196 catch (SQLException ex)
198 assure("left " + ex.getMessage(), false);
199 throw ex;
203 right(xRowRes);
205 catch (SQLException ex)
207 assure("right " + ex.getMessage(), false);
208 throw ex;
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);
216 xRowRes.execute();
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') ");
238 where = temp;
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"));