1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
*
* Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ClobStoredProcedureTest
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbcapi;
import java.sql.CallableStatement;
import java.sql.SQLException;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests the stored procedures introduced as part of DERBY-208.
* These stored procedures will used by the Clob methods on the client side.
*/
public class ClobStoredProcedureTest extends BaseJDBCTestCase {
final String testStr = "I am a simple derby test case";
final long testStrLength = testStr.length();
/**
* Public constructor required for running test as standalone JUnit.
* @param name a string containing the name of the test.
*/
public ClobStoredProcedureTest(String name) {
super(name);
}
/**
* Create a suite of tests.
* @return the test suite created.
*/
public static Test suite() {
if (JDBC.vmSupportsJSR169()) {
return new BaseTestSuite(
"empty: client not supported on JSR169; procs use DriverMgr");
}
else {
return TestConfiguration.defaultSuite(
ClobStoredProcedureTest.class);
}
}
/**
* Setup the test.
* @throws a SQLException.
*/
public void setUp() throws Exception {
int locator = 0;
getConnection().setAutoCommit(false);
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBCREATELOCATOR()");
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.executeUpdate();
locator = cs.getInt(1);
cs.close();
cs = prepareCall("CALL SYSIBM.CLOBSETSTRING(?,?,?,?)");
cs.setInt(1, locator);
cs.setInt(2, 1);
cs.setLong(3, testStrLength);
cs.setString(4, testStr);
cs.execute();
cs.close();
}
/**
* Cleanup the test.
* @throws SQLException.
*/
public void tearDown() throws Exception {
commit();
super.tearDown();
}
/**
* Test the stored procedure SYSIBM.CLOBGETSUBSTRING
*
* @throws an SQLException.
*/
public void testGetSubStringSP() throws SQLException {
CallableStatement cs = prepareCall("? = CALL " +
"SYSIBM.CLOBGETSUBSTRING(?,?,?)");
cs.registerOutParameter(1, java.sql.Types.VARCHAR);
cs.setInt(2, 1);
cs.setLong(3, 1);
//get sub-string of length 10 from the clob.
cs.setInt(4, 10);
cs.executeUpdate();
String retVal = cs.getString(1);
//compare the string that is returned to the sub-string obtained directly
//from the test string. If found to be equal the stored procedure
//returns valid values.
if (testStr.substring(0, 10).compareTo(retVal) != 0) {
fail("Error SYSIBM.CLOBGETSUBSTRING returns the wrong string");
}
cs.close();
}
/**
* Tests the locator value returned by the stored procedure
* CLOBCREATELOCATOR.
*
* @throws SQLException.
*
*/
public void testClobCreateLocatorSP() throws SQLException {
//initialize the locator to a default value.
int locator = -1;
//call the stored procedure to return the created locator.
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBCREATELOCATOR()");
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.executeUpdate();
locator = cs.getInt(1);
//verify if the locator rturned and expected are equal.
//remember in setup a locator is already created
//hence expected value is 2
assertEquals("The locator values returned by " +
"SYSIBM.CLOBCREATELOCATOR() are incorrect", 2, locator);
cs.close();
}
/**
* Tests the SYSIBM.CLOBRELEASELOCATOR stored procedure.
*
* @throws SQLException
*/
public void testClobReleaseLocatorSP() throws SQLException {
CallableStatement cs = prepareCall
("CALL SYSIBM.CLOBRELEASELOCATOR(?)");
cs.setInt(1, 1);
cs.execute();
cs.close();
//once the locator has been released the CLOBGETLENGTH on that
//locator value will throw an SQLException. This assures that
//the locator has been properly released.
cs = prepareCall
("? = CALL SYSIBM.CLOBGETLENGTH(?)");
cs.registerOutParameter(1, java.sql.Types.BIGINT);
cs.setInt(2, 1);
try {
cs.executeUpdate();
} catch(SQLException sqle) {
//on expected lines. The test was successful.
return;
}
//The exception was not thrown. The test has failed here.
fail("Error the locator was not released by SYSIBM.CLOBRELEASELOCATOR");
cs.close();
}
/**
* Tests the stored procedure SYSIBM.CLOBGETLENGTH.
*
* @throws SQLException
*/
public void testClobGetLengthSP() throws SQLException {
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBGETLENGTH(?)");
cs.registerOutParameter(1, java.sql.Types.BIGINT);
cs.setInt(2, 1);
cs.executeUpdate();
//compare the actual length of the test string and the returned length.
assertEquals("Error SYSIBM.CLOBGETLENGTH returns " +
"the wrong value for the length of the Clob", testStrLength, cs.getLong(1));
cs.close();
}
/**
* Tests the stored procedure SYSIBM.CLOBGETPOSITIONFROMSTRING.
*
* @throws SQLException.
*/
public void testClobGetPositionFromStringSP() throws SQLException {
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBGETPOSITIONFROMSTRING(?,?,?)");
cs.registerOutParameter(1, java.sql.Types.BIGINT);
cs.setInt(2, 1);
cs.setString(3, new String("simple"));
cs.setLong(4, 1L);
cs.executeUpdate();
//compare the substring position returned from the stored procedure and that
//returned from the String class functions. If not found to be equal throw an
//error.
assertEquals("Error SYSIBM.CLOBGETPOSITIONFROMSTRING returns " +
"the wrong value for the position of the SUBSTRING", testStr.indexOf("simple")+1, cs.getLong(1));
cs.close();
}
/**
* Tests the stored procedure SYSIBM.CLOBSETSTRING
*
* @throws SQLException.
*/
public void testClobSetStringSP() throws SQLException {
String newString = "123456789012345";
//initialize the locator to a default value.
int locator = -1;
//call the stored procedure to return the created locator.
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBCREATELOCATOR()");
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.executeUpdate();
locator = cs.getInt(1);
cs.close();
//use this new locator to test the SETSUBSTRING function
//by inserting a new sub string and testing whether it has
//been inserted properly.
//Insert the new substring.
cs = prepareCall("CALL SYSIBM.CLOBSETSTRING(?,?,?,?)");
cs.setInt(1, locator);
cs.setInt(2, 1);
cs.setLong(3, newString.length());
cs.setString(4, newString);
cs.execute();
cs.close();
//check the new locator to see if the value has been inserted correctly.
cs = prepareCall("? = CALL " +
"SYSIBM.CLOBGETSUBSTRING(?,?,?)");
cs.registerOutParameter(1, java.sql.Types.VARCHAR);
cs.setInt(2, locator);
cs.setLong(3, 1);
//get sub-string of length 10 from the clob.
cs.setInt(4, newString.length());
cs.executeUpdate();
String retVal = cs.getString(1);
//compare the new string and the string returned by the stored
//procedure to see of they are the same.
if (newString.compareTo(retVal) != 0)
fail("SYSIBM.CLOBSETSTRING does not insert the right value");
cs.close();
}
/**
* Test the stored procedure SYSIBM.CLOBGETLENGTH
*
* @throws SQLException
*/
public void testClobTruncateSP() throws SQLException {
//----------TO BE ENABLED LATER------------------------------
//This code needs to be enabled once the set methods on the
//Clob interface are implemented. Until that time keep checking
//for a not implemented exception being thrown.
/*
CallableStatement cs = prepareCall
("CALL SYSIBM.CLOBTRUNCATE(?,?)");
cs.setInt(1, 1);
cs.setLong(2, 10L);
cs.execute();
cs.close();
cs = prepareCall
("? = CALL SYSIBM.CLOBGETLENGTH(?)");
cs.registerOutParameter(1, java.sql.Types.BIGINT);
cs.setInt(2, 1);
cs.executeUpdate();
//compare the actual length of the test string and the returned length.
assertEquals("Error SYSIBM.CLOBGETLENGTH returns " +
"the wrong value for the length of the Clob", 10
, cs.getLong(1));
cs.close();
*/
//----------TO BE ENABLED LATER------------------------------
CallableStatement cs = prepareCall
("CALL SYSIBM.CLOBTRUNCATE(?,?)");
cs.setInt(1, 1);
cs.setLong(2, 10L);
try {
cs.execute();
}
catch(SQLException sqle) {
//expected Unsupported SQLException
//The CLOBTRUNCATE is not supported but contains
//temporary code that shall be removed when
//the method is enabled.
}
cs.close();
}
/**
* Tests the SYSIBM.CLOBGETPOSITIONFROMLOCATOR stored procedure.
*
* @throws SQLException.
*/
public void testClobGetPositionFromLocatorSP() throws SQLException {
int locator = 0;
String newStr = "simple";
CallableStatement cs = prepareCall
("? = CALL SYSIBM.CLOBCREATELOCATOR()");
cs.registerOutParameter(1, java.sql.Types.INTEGER);
cs.executeUpdate();
locator = cs.getInt(1);
cs.close();
cs = prepareCall("CALL SYSIBM.CLOBSETSTRING(?,?,?,?)");
cs.setInt(1, locator);
cs.setInt(2, 1);
cs.setLong(3, newStr.length());
cs.setString(4, newStr);
cs.execute();
cs.close();
cs = prepareCall
("? = CALL SYSIBM.CLOBGETPOSITIONFROMLOCATOR(?,?,?)");
cs.registerOutParameter(1, java.sql.Types.BIGINT);
cs.setInt(2, 1);
//find the position of the bytes corresponding to
//the String simple in the test string.
cs.setInt(3, locator);
cs.setLong(4, 1L);
cs.executeUpdate();
//check to see that the returned position and the expected position
//of the substring simple in the string are matching.
assertEquals("Error SYSIBM.CLOBGETPOSITIONFROMLOCATOR returns " +
"the wrong value for the position of the Clob", 8, cs.getLong(1));
cs.close();
}
}
|