KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > CharUTF8


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.CharUTF8
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.sql.Types JavaDoc;
31
32 import org.apache.derby.tools.ij;
33 import org.apache.derby.tools.JDBCDisplayUtil;
34 import java.io.*;
35 import java.sql.PreparedStatement JavaDoc;
36
37 /**
38  * Test all characters written through the UTF8 format.
39  */

40
41 public class CharUTF8 {
42
43     public static PreparedStatement JavaDoc psSet;
44     public static PreparedStatement JavaDoc psGet;
45
46     public static void main(String JavaDoc[] args) {
47
48         System.out.println("Test CharUTF8 starting");
49
50         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
51
52         try {
53             // use the ij utility to read the property file and
54
// make the initial connection.
55
ij.getPropertyArg(args);
56             Connection JavaDoc conn = ij.startJBMS();
57
58             Statement JavaDoc st2 = conn.createStatement();
59             st2.execute("CREATE TABLE TEST(id int not null primary key, body varchar(60))");
60             psSet = conn.prepareStatement("insert into test values(?,?)");
61             psGet = conn.prepareStatement("select body from test where id=?");
62             
63             int off = 0;
64             for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
65
66                 buff.append((char) i);
67
68                 if ((buff.length() == 60) || (i == Character.MAX_VALUE)) {
69
70                     String JavaDoc text = buff.toString();
71                     System.out.println("Testing with last char value " + i + " length=" + text.length());
72
73                     // set the text
74
setBody(i, text);
75     
76                 // now read the text
77
String JavaDoc res = getBody(i);
78                     if (!res.equals(text)) {
79                         System.out.println("FAIL -- string fetched is incorrect, length is "
80                             + buff.length() + ", expecting string: " + text
81                             + ", instead got the following: " + res);
82                         break;
83                     }
84
85                     buff.setLength(0);
86                 }
87             }
88
89             // quick test of an empty string aswell
90
setBody(-1, "");
91             if (!getBody(-1).equals("")) {
92                 System.out.println("FAIL: empty string returned as " + getBody(-1));
93             }
94
95
96             conn.close();
97
98         } catch (SQLException JavaDoc e) {
99             dumpSQLExceptions(e);
100         } catch (Throwable JavaDoc e) {
101             System.out.println("FAIL -- unexpected exception:" + e.toString());
102         }
103
104         System.out.println("Test CharUTF8 finished");
105
106     }
107
108     private static void setBody(int key, String JavaDoc body) {
109         
110         try {
111             psSet.setInt(1, key);
112             psSet.setString(2, body);
113             psSet.executeUpdate();
114
115         } catch (SQLException JavaDoc ex) {
116             ex.printStackTrace();
117
118             System.out.println("FAIL -- unexpected exception");
119             System.exit(-1);
120         }
121     }
122         
123     private static String JavaDoc getBody(int key) {
124         
125         String JavaDoc result="NO RESULT";
126         
127         try {
128             psGet.setInt(1, key);
129             ResultSet JavaDoc rs = psGet.executeQuery();
130
131             if (rs.next())
132                 result = rs.getString(1);
133
134         } catch (SQLException JavaDoc ex) {
135               ex.printStackTrace();
136         }
137         
138         return result;
139     }
140
141     static private void dumpSQLExceptions (SQLException JavaDoc se) {
142         System.out.println("FAIL -- unexpected exception: " + se.toString());
143         while (se != null) {
144             System.out.print("SQLSTATE("+se.getSQLState()+"):");
145             se = se.getNextException();
146         }
147     }
148
149     /**
150         Utility method for dynamicLikeOptimization test. Return a single character
151         string with the highest defined Unicode character. See java.lang.Character.isDefined.
152     */

153     public static String JavaDoc getMaxDefinedCharAsString() {
154
155         return "\uFA2D";
156     }
157
158 }
159
Popular Tags