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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.memory.MultiByteClobTest
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.memory;
import java.io.IOException;
import java.io.Reader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.streams.CharAlphabet;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.DatabasePropertyTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test for small and larg clobs with multibyte characters.
*/
public class MultiByteClobTest extends BaseJDBCTestCase {
private static final int LONG_CLOB_LENGTH = 9000000;
private static final int SHORT_CLOB_LENGTH = 100;
private static final String LONG_CLOB_LENGTH_STRING = "9000000";
private static final String SHORT_CLOB_LENGTH_STRING = "100";
/**
* Create a test case with the given name.
*
* @param name of the test case.
*/
public MultiByteClobTest(String name) {
super(name);
}
public void testSmallMultiByteCharLob() throws SQLException, IOException {
setAutoCommit(false);
Statement s = createStatement();
PreparedStatement ps = prepareStatement("INSERT INTO MB_CLOBTABLE VALUES(?,?)");
// We allocate 16MB for the test so use something bigger than that.
ps.setInt(1,1);
LoopingAlphabetReader reader = new LoopingAlphabetReader(SHORT_CLOB_LENGTH, CharAlphabet.cjkSubset());
ps.setCharacterStream(2, reader, SHORT_CLOB_LENGTH);
ps.executeUpdate();
ResultSet rs = s.executeQuery("SELECT K, LENGTH(C), C FROM MB_CLOBTABLE" +
"-- DERBY-PROPERTIES constraint=pk\n ORDER BY K");
rs.next();
assertEquals(SHORT_CLOB_LENGTH_STRING, rs.getString(2));
// make sure we can still access the clob after getting length.
// It should be ok because we reset the stream
Reader rsReader = rs.getCharacterStream(3);
int len= 0;
char[] buf = new char[32672];
for (;;) {
int size = rsReader.read(buf);
if (size == -1)
break;
len += size;
int expectedValue = ((len -1) % 12) + '\u4E00';
if (size != 0)
assertEquals(expectedValue,buf[size -1]);
}
assertEquals(SHORT_CLOB_LENGTH, len);
rs.close();
// Select just length without selecting the clob.
rs = s.executeQuery("SELECT K, LENGTH(C) FROM MB_CLOBTABLE " +
"ORDER BY K");
JDBC.assertFullResultSet(rs, new String [][] {{"1",SHORT_CLOB_LENGTH_STRING}});
}
public void testLargeMultiByteCharLob() throws SQLException, IOException {
setAutoCommit(false);
Statement s = createStatement();
PreparedStatement ps = prepareStatement("INSERT INTO MB_CLOBTABLE VALUES(?,?)");
// We allocate 16MB for the test so use something bigger than that.
ps.setInt(1,1);
LoopingAlphabetReader reader = new LoopingAlphabetReader(LONG_CLOB_LENGTH, CharAlphabet.cjkSubset());
ps.setCharacterStream(2, reader, LONG_CLOB_LENGTH);
ps.executeUpdate();
ResultSet rs = s.executeQuery("SELECT K, LENGTH(C), C FROM MB_CLOBTABLE" +
"-- DERBY-PROPERTIES constraint=pk\n ORDER BY K");
rs.next();
assertEquals(LONG_CLOB_LENGTH_STRING, rs.getString(2));
// make sure we can still access the clob after getting length.
// It should be ok because we reset the stream
Reader rsReader = rs.getCharacterStream(3);
int len= 0;
char[] buf = new char[32672];
for (;;) {
int size = rsReader.read(buf);
if (size == -1)
break;
len += size;
int expectedValue = ((len -1) % 12) + '\u4E00';
if (size != 0)
assertEquals(expectedValue,buf[size -1]);
}
assertEquals(LONG_CLOB_LENGTH, len);
rs.close();
// Select just length without selecting the clob.
rs = s.executeQuery("SELECT K, LENGTH(C) FROM MB_CLOBTABLE " +
"ORDER BY K");
JDBC.assertFullResultSet(rs, new String [][] {{"1",LONG_CLOB_LENGTH_STRING}});
}
/**
* Runs the test fixtures in embedded and client.
*
* @return test suite
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("MultiByteClobTest");
suite.addTest(baseSuite("MultiByteClobTest:embedded"));
suite.addTest(TestConfiguration
.clientServerDecorator(baseSuite("MultiByteClobTest:client")));
Properties p = new Properties();
// use small pageCacheSize so we don't run out of memory on the insert.
p.setProperty("derby.storage.pageCacheSize", "100");
return new SystemPropertyTestSetup(suite,p);
}
/**
* Base suite of tests that will run in both embedded and client.
*
* @param name name for the suite.
*/
private static Test baseSuite(String name) {
BaseTestSuite suite = new BaseTestSuite(name);
suite.addTestSuite(MultiByteClobTest.class);
return new CleanDatabaseTestSetup(DatabasePropertyTestSetup
.setLockTimeouts(suite, 2, 4)) {
/**
* Creates the tables used in the test cases.
*
* @exception java.sql.SQLException
* if a database error occurs
*/
protected void decorateSQL(Statement stmt) throws SQLException {
stmt.execute("CREATE TABLE MB_CLOBTABLE " +
"(K INT CONSTRAINT PK PRIMARY KEY, C CLOB(" +
LONG_CLOB_LENGTH + "))");
}
};
}
}
|