23 import java.nio.ByteBuffer;
24 import java.nio.CharBuffer;
25 import java.nio.charset.Charset;
26 import java.util.Arrays;
28 import com.mysql.ndbjtie.mysql.CharsetMap;
29 import com.mysql.ndbjtie.mysql.CharsetMapConst;
37 static public ByteBuffer char2bb(
char[] c) {
39 ByteBuffer bb = ByteBuffer.allocateDirect(len);
40 for(
int i = 0 ;
i < len ;
i++)
46 String bbdump (ByteBuffer sbb) {
47 ByteBuffer bb = sbb.asReadOnlyBuffer();
48 byte[] bytes =
new byte[bb.capacity()];
51 return Arrays.toString(bytes);
55 int bbcmp(ByteBuffer sbb1, ByteBuffer sbb2) {
56 ByteBuffer bb1 = sbb1.asReadOnlyBuffer();
57 ByteBuffer bb2 = sbb2.asReadOnlyBuffer();
63 if(b1 < b2)
return -1;
64 }
while((b1 != 0) && (b2 != 0));
69 int bbncmp(ByteBuffer sbb1, ByteBuffer sbb2,
int n) {
70 ByteBuffer bb1 = sbb1.asReadOnlyBuffer();
71 ByteBuffer bb2 = sbb2.asReadOnlyBuffer();
78 if(b1 < b2)
return -1;
84 public void printRecodeResult(
int rcode,
int lengths[], ByteBuffer b1,
87 out.println(
" Return code: " + rcode +
" Len0: "
88 + lengths[0] +
" Len1: " + lengths[1] +
"\n"
89 +
" " + bbdump(b1) +
" => " + bbdump(b2)
95 int latin1_num, utf8_num, utf16_num;
96 out.println(
"--> MySqlUtilsCharsetMapTest.test()");
102 out.println(
" --> Test that mysql includes UTF-8 and 16-bit Unicode");
105 out.println(
" UTF-8 charset num: " + utf8_num +
106 " UTF-16 or UCS-2 charset num: " + utf16_num);
107 assert( ! ((utf8_num == 0) || (utf16_num == 0)));
108 out.println(
" <-- Test that mysql includes UTF-8 and 16-bit Unicode");
111 out.println(
" --> Test CharsetMap::getName()");
114 assert(utf8_name.compareTo(
"UTF-8") == 0);
117 out.println(
" Using mysql \"" + utf16 +
"\" for UTF-16.");
118 out.println(
" <-- Test CharsetMap::getName()");
127 final char[] cmy_word_latin1 =
new char[] { 0xFC,
'l',
'k',
'e',
'r', 0 };
128 final char[] cmy_word_utf8 =
new char[] { 0xC3, 0xBC,
'l',
'k',
'e',
'r', 0 };
129 final char[] cmy_word_truncated =
new char[] { 0xC3, 0xBC,
'l',
'k', 0 };
130 final char[] cmy_bad_utf8 =
new char[] {
'l' , 0xBC,
'a',
'd', 0 };
133 out.println(
" --> CharsetMap::recode() Tests");
136 ByteBuffer my_word_latin1 = char2bb(cmy_word_latin1);
137 ByteBuffer my_word_utf8 = char2bb(cmy_word_utf8);
138 out.println(
" --> Test that latin1 is available.");
140 out.println(
" latin1 charset number: " + latin1_num +
141 " standard name: " + csmap.
getName(latin1_num));
142 assert(latin1_num != 0);
143 assert(csmap.
getName(latin1_num).compareTo(
"windows-1252") == 0);
144 out.println(
" Latin1 source string: " + bbdump(my_word_latin1) +
"\n" +
145 " UTF8 source string: " + bbdump(my_word_utf8));
146 out.println(
" <-- Test that latin1 is available.");
150 out.println(
" --> RECODE TEST 1: recode from UTF-8 to Latin 1");
151 ByteBuffer my_word_utf8 = char2bb(cmy_word_utf8);
152 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
153 int[] lengths =
new int[] { 7 , 16 };
155 int rr1 = csmap.
recode(lengths, utf8_num, latin1_num,
156 my_word_utf8, result_buff);
157 printRecodeResult(rr1, lengths, my_word_utf8, result_buff);
158 assert(rr1 == CharsetMapConst.RecodeStatus.RECODE_OK);
159 assert(lengths[0] == 7);
160 assert(lengths[1] == 6);
161 assert(bbcmp(char2bb(cmy_word_latin1), result_buff) == 0);
162 out.println(
" <-- RECODE TEST 1");
166 out.println(
" --> RECODE TEST 2: recode from Latin1 to to UTF-8");
167 ByteBuffer my_word_latin1 = char2bb(cmy_word_latin1);
168 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
169 int[] lengths =
new int[] { 6 , 16 };
171 int rr2 = csmap.
recode(lengths, latin1_num, utf8_num,
172 my_word_latin1, result_buff);
173 printRecodeResult(rr2, lengths, my_word_latin1, result_buff);
174 assert(rr2 == CharsetMapConst.RecodeStatus.RECODE_OK);
175 assert(lengths[0] == 6);
176 assert(lengths[1] == 7);
177 assert(bbcmp(result_buff, char2bb(cmy_word_utf8)) == 0);
178 out.println(
" <-- RECODE TEST 2");
182 out.println(
" --> RECODE TEST 3: too-small result buffer");
183 ByteBuffer my_word_latin1 = char2bb(cmy_word_latin1);
184 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
185 ByteBuffer my_word_truncated = char2bb(cmy_word_truncated);
186 int[] lengths =
new int[] { 6 , 4 };
188 int rr3 = csmap.
recode(lengths, latin1_num, utf8_num,
189 my_word_latin1, result_buff);
190 printRecodeResult(rr3, lengths, my_word_latin1, result_buff);
191 assert(rr3 == CharsetMapConst.RecodeStatus.RECODE_BUFF_TOO_SMALL);
192 assert(lengths[0] == 3);
193 assert(lengths[1] == 4);
195 assert(bbncmp(result_buff, char2bb(cmy_word_truncated), 4) == 0);
196 out.println(
" <-- RECODE TEST 3");
200 out.println(
" --> RECODE TEST 4: invalid character set");
201 ByteBuffer my_word_latin1 = char2bb(cmy_word_latin1);
202 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
203 int[] lengths =
new int[] { 6 , 16 };
204 int rr4 = csmap.
recode(lengths, 0, 999, my_word_latin1, result_buff);
205 out.println(
" Return code: " + rr4);
206 assert(rr4 == CharsetMapConst.RecodeStatus.RECODE_BAD_CHARSET);
207 out.println(
" <-- RECODE TEST 4");
211 out.println(
" --> RECODE TEST 5: source string is ill-formed UTF-8");
212 ByteBuffer my_bad_utf8 = char2bb(cmy_bad_utf8);
213 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
214 int[] lengths =
new int[] { 5 , 16 };
215 int rr5 = csmap.
recode(lengths, utf8_num, latin1_num,
216 my_bad_utf8, result_buff);
217 out.println(
" Return code: " + rr5);
218 assert(rr5 == CharsetMapConst.RecodeStatus.RECODE_BAD_SRC);
219 out.println(
" <-- RECODE TEST 5");
223 out.println(
" --> RECODE TEST 6: convert an actual java string to UTF-8");
225 ByteBuffer str_bb = ByteBuffer.allocateDirect(16);
226 CharBuffer cb = str_bb.asCharBuffer();
227 cb.append(
"\u00FClker");
229 ByteBuffer result_buff = ByteBuffer.allocateDirect(16);
230 int[] lengths =
new int[] { 12 , 16 };
232 int rr6 = csmap.
recode(lengths, utf16_num, utf8_num,
233 str_bb, result_buff);
234 printRecodeResult(rr6, lengths, str_bb, result_buff);
235 assert(lengths[0]) == 12;
236 assert(lengths[1]) == 7;
237 assert(bbncmp(result_buff, char2bb(cmy_word_utf8), 6) == 0);
238 out.println(
" <-- RECODE TEST 6");
244 out.println(
" --> IS MULTIBYTE TEST");
251 int nNull = 0, nSingle = 0, nMulti = 0;
252 for(
int i = 0;
i < 256 ;
i++) {
254 if(result == null) nNull++;
256 if(result[0]) nMulti++;
260 out.println(
" Unused: " + nNull +
261 " single-byte: " +nSingle +
" multi-byte: " + nMulti );
266 out.println(
" <-- IS MULTIBYTE TEST");
268 out.println(
"<-- MySqlUtilsCharsetMapTest.test()");
272 static public void main(
String[] args)
throws Exception {
273 out.println(
"--> MySqlUtilsCharsetMapTest.main()");
280 out.println(
"<-- MySqlUtilsCharsetMapTest.main()");