001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.lang.CharUTF8
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.lang;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.ResultSetMetaData;
027: import java.sql.ResultSet;
028: import java.sql.Statement;
029: import java.sql.SQLException;
030: import java.sql.Types;
031:
032: import org.apache.derby.tools.ij;
033: import org.apache.derby.tools.JDBCDisplayUtil;
034: import java.io.*;
035: import java.sql.PreparedStatement;
036:
037: /**
038: * Test all characters written through the UTF8 format.
039: */
040:
041: public class CharUTF8 {
042:
043: public static PreparedStatement psSet;
044: public static PreparedStatement psGet;
045:
046: public static void main(String[] args) {
047:
048: System.out.println("Test CharUTF8 starting");
049:
050: StringBuffer buff = new StringBuffer();
051:
052: try {
053: // use the ij utility to read the property file and
054: // make the initial connection.
055: ij.getPropertyArg(args);
056: Connection conn = ij.startJBMS();
057:
058: Statement st2 = conn.createStatement();
059: st2
060: .execute("CREATE TABLE TEST(id int not null primary key, body varchar(60))");
061: psSet = conn
062: .prepareStatement("insert into test values(?,?)");
063: psGet = conn
064: .prepareStatement("select body from test where id=?");
065:
066: int off = 0;
067: for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
068:
069: buff.append((char) i);
070:
071: if ((buff.length() == 60) || (i == Character.MAX_VALUE)) {
072:
073: String text = buff.toString();
074: System.out.println("Testing with last char value "
075: + i + " length=" + text.length());
076:
077: // set the text
078: setBody(i, text);
079:
080: // now read the text
081: String res = getBody(i);
082: if (!res.equals(text)) {
083: System.out
084: .println("FAIL -- string fetched is incorrect, length is "
085: + buff.length()
086: + ", expecting string: "
087: + text
088: + ", instead got the following: "
089: + res);
090: break;
091: }
092:
093: buff.setLength(0);
094: }
095: }
096:
097: // quick test of an empty string aswell
098: setBody(-1, "");
099: if (!getBody(-1).equals("")) {
100: System.out.println("FAIL: empty string returned as "
101: + getBody(-1));
102: }
103:
104: conn.close();
105:
106: } catch (SQLException e) {
107: dumpSQLExceptions(e);
108: } catch (Throwable e) {
109: System.out.println("FAIL -- unexpected exception:"
110: + e.toString());
111: }
112:
113: System.out.println("Test CharUTF8 finished");
114:
115: }
116:
117: private static void setBody(int key, String body) {
118:
119: try {
120: psSet.setInt(1, key);
121: psSet.setString(2, body);
122: psSet.executeUpdate();
123:
124: } catch (SQLException ex) {
125: ex.printStackTrace();
126:
127: System.out.println("FAIL -- unexpected exception");
128: System.exit(-1);
129: }
130: }
131:
132: private static String getBody(int key) {
133:
134: String result = "NO RESULT";
135:
136: try {
137: psGet.setInt(1, key);
138: ResultSet rs = psGet.executeQuery();
139:
140: if (rs.next())
141: result = rs.getString(1);
142:
143: } catch (SQLException ex) {
144: ex.printStackTrace();
145: }
146:
147: return result;
148: }
149:
150: static private void dumpSQLExceptions(SQLException se) {
151: System.out.println("FAIL -- unexpected exception: "
152: + se.toString());
153: while (se != null) {
154: System.out.print("SQLSTATE(" + se.getSQLState() + "):");
155: se = se.getNextException();
156: }
157: }
158:
159: /**
160: Utility method for dynamicLikeOptimization test. Return a single character
161: string with the highest defined Unicode character. See java.lang.Character.isDefined.
162: */
163: public static String getMaxDefinedCharAsString() {
164:
165: return "\uFA2D";
166: }
167:
168: }
|