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 DBaseStringFunctions 33 { 34 private String where = "FROM \"biblio\" \"biblio\" where \"Identifier\" = 'BOR00'"; 35 private final XMultiServiceFactory m_xORB; 36 DBaseStringFunctions(final XMultiServiceFactory _xORB)37 public DBaseStringFunctions(final XMultiServiceFactory _xORB) 38 { 39 m_xORB = _xORB; 40 } 41 testFunctions()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 String 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 upper(xRowRes); 55 lower(xRowRes); 56 ascii(xRowRes); 57 char_length(xRowRes); 58 concat(xRowRes); 59 chartest(xRowRes); 60 locate(xRowRes); 61 substring(xRowRes); 62 ltrim(xRowRes); 63 rtrim(xRowRes); 64 space(xRowRes); 65 replace(xRowRes); 66 repeat(xRowRes); 67 insert(xRowRes); 68 left(xRowRes); 69 right(xRowRes); 70 } 71 execute(final XRowSet xRowRes, String sql)72 private XRow execute(final XRowSet xRowRes, 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 upper(final XRowSet xRowRes)83 private void upper(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 84 { 85 final XRow row = execute(xRowRes, "upper('test'),UCASE('test') "); 86 assertTrue("upper('test') failed!", row.getString(1).equals("TEST")); 87 assertTrue("ucase('test') failed!", row.getString(2).equals("TEST")); 88 } 89 lower(final XRowSet xRowRes)90 private void lower(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 91 { 92 final XRow row = execute(xRowRes, "lower('TEST'),LCASE('TEST') "); 93 assertTrue("lower('TEST') failed!", row.getString(1).equals("test")); 94 assertTrue("lcase('TEST') failed!", row.getString(2).equals("test")); 95 final String temp = where; 96 where = "FROM \"biblio\" \"biblio\" where LOWER(\"Identifier\") like 'bor%'"; 97 execute(xRowRes, "lower('TEST'),LCASE('TEST') "); 98 where = temp; 99 } 100 ascii(final XRowSet xRowRes)101 private void ascii(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 102 { 103 final XRow row = execute(xRowRes, "ASCII('2') "); 104 assertTrue("ascii('2') failed!", row.getInt(1) == 50); 105 } 106 char_length(final XRowSet xRowRes)107 private void char_length(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 108 { 109 final XRow row = execute(xRowRes, "char_length('test'),character_length('test'),OCTET_LENGTH('test') "); 110 assertTrue("char_length('test') failed!", row.getInt(1) == 4); 111 assertTrue("character_length('test') failed!", row.getInt(2) == 4); 112 assertTrue("OCTET_LENGTH('test') failed!", row.getInt(3) == 4); 113 } 114 concat(final XRowSet xRowRes)115 private void concat(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 116 { 117 final XRow row = execute(xRowRes, "CONCAT('Hello',' ','World') "); 118 assertTrue("CONCAT('Hello',' ',,'World') failed!", row.getString(1).equals("Hello World")); 119 } 120 locate(final XRowSet xRowRes)121 private void locate(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 122 { 123 final XRow row = execute(xRowRes, "LOCATE('bar', 'foobarbar') "); 124 assertTrue("LOCATE('bar', 'foobarbar') failed!", row.getInt(1) == 4); 125 } 126 substring(final XRowSet xRowRes)127 private void substring(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 128 { 129 final XRow row = execute(xRowRes, "SUBSTRING('Quadratically',5) "); 130 assertTrue("SUBSTRING('Quadratically',5) failed!", row.getString(1).equals("ratically")); 131 } 132 ltrim(final XRowSet xRowRes)133 private void ltrim(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 134 { 135 final XRow row = execute(xRowRes, "LTRIM(' barbar') "); 136 assertTrue("LTRIM(' barbar') failed!", row.getString(1).equals("barbar")); 137 } 138 rtrim(final XRowSet xRowRes)139 private void rtrim(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 140 { 141 final XRow row = execute(xRowRes, "RTRIM('barbar ') "); 142 assertTrue("RTRIM('barbar ') failed!", row.getString(1).equals("barbar")); 143 } 144 space(final XRowSet xRowRes)145 private void space(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 146 { 147 final XRow row = execute(xRowRes, "space(6) "); 148 assertTrue("space(6) failed!", row.getString(1).equals(" ")); 149 } 150 replace(final XRowSet xRowRes)151 private void replace(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 152 { 153 final XRow row = execute(xRowRes, "REPLACE('www.OOo.com', 'w', 'Ww') "); 154 assertTrue("REPLACE('www.OOo.com', 'w', 'Ww') failed!", row.getString(1).equals("WwWwWw.OOo.com")); 155 } 156 repeat(final XRowSet xRowRes)157 private void repeat(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 158 { 159 final XRow row = execute(xRowRes, "REPEAT('OOo', 3) "); 160 assertTrue("REPEAT('OOo', 3) failed!", row.getString(1).equals("OOoOOoOOo")); 161 } 162 insert(final XRowSet xRowRes)163 private void insert(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 164 { 165 final XRow row = execute(xRowRes, "INSERT('Quadratic', 3, 4, 'What') "); 166 assertTrue("INSERT('Quadratic', 3, 4, 'What') failed!", row.getString(1).equals("QuWhattic")); 167 } 168 left(final XRowSet xRowRes)169 private void left(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 170 { 171 final XRow row = execute(xRowRes, "LEFT('foobarbar', 5) "); 172 assertTrue("LEFT('foobarbar', 5) failed!", row.getString(1).equals("fooba")); 173 } 174 right(final XRowSet xRowRes)175 private void right(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 176 { 177 final XRow row = execute(xRowRes, "RIGHT('foobarbar', 4) "); 178 assertTrue("RIGHT('foobarbar', 4) failed!", row.getString(1).equals("rbar")); 179 } 180 chartest(final XRowSet xRowRes)181 private void chartest(final XRowSet xRowRes) throws com.sun.star.uno.Exception, com.sun.star.beans.UnknownPropertyException 182 { 183 final XRow row = execute(xRowRes, "CHAR(ascii('t'),ascii('e'),ascii('s'),ascii('t')) "); 184 assertTrue("CHAR(ascii('t'),ascii('e'),ascii('s'),ascii('t')) failed!", row.getString(1).equals("test")); 185 } 186 } 187