1 // Tags: JDK1.4 |
|
2 |
|
3 // Copyright (C) 2004 Free Software Foundation, Inc. |
|
4 // Written by Julian Scheid (julian@sektor37.de) and |
|
5 // Mark J. Wielaard (mark@klomp.org) |
|
6 |
|
7 // This file is part of Mauve. |
|
8 |
|
9 // Mauve is free software; you can redistribute it and/or modify |
|
10 // it under the terms of the GNU General Public License as published by |
|
11 // the Free Software Foundation; either version 2, or (at your option) |
|
12 // any later version. |
|
13 |
|
14 // Mauve is distributed in the hope that it will be useful, |
|
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 // GNU General Public License for more details. |
|
18 |
|
19 // You should have received a copy of the GNU General Public License |
|
20 // along with Mauve; see the file COPYING. If not, write to |
|
21 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
22 // Boston, MA 02111-1307, USA. */ |
|
23 |
|
24 package gnu.testlet.java.nio.charset.Charset; |
|
25 |
|
26 import gnu.testlet.Testlet; |
|
27 import gnu.testlet.TestHarness; |
|
28 |
|
29 import java.nio.*; |
|
30 import java.nio.charset.*; |
|
31 |
|
32 /** |
|
33 * Test for some under/overflow situations exposing a bug in GNU |
|
34 * Classpath UTF_8 Charset implementation found by Julian Scheid |
|
35 * (julian@sektor37.de). |
|
36 */ |
|
37 public class UTF8Charset implements Testlet |
|
38 { |
|
39 public void test(TestHarness h) |
|
40 { |
|
41 final int first_chunk_size = 4; |
|
42 final int second_chunk_size = 3; |
|
43 |
|
44 byte[] inBytes = new byte[first_chunk_size + second_chunk_size]; |
|
45 |
|
46 // fill with some harmless ASCII7 char |
|
47 for (int i = 0; i < inBytes.length; ++i) |
|
48 inBytes[i] = 'X'; |
|
49 |
|
50 ByteBuffer inBuffer = ByteBuffer.wrap(inBytes); |
|
51 |
|
52 CharBuffer outBuffer1 = CharBuffer.allocate(first_chunk_size); |
|
53 CharBuffer outBuffer2 = CharBuffer.allocate(second_chunk_size); |
|
54 |
|
55 Charset utf8Charset = Charset.forName("UTF-8"); |
|
56 CharsetDecoder decoder = utf8Charset.newDecoder(); |
|
57 |
|
58 CoderResult coderResult1 |
|
59 = decoder.decode(inBuffer, outBuffer1, false); |
|
60 |
|
61 h.check(coderResult1.isOverflow(), |
|
62 "Expected decoder to return overflow status"); |
|
63 h.check(first_chunk_size == inBuffer.position(), |
|
64 "Expected input buffer position to be " + first_chunk_size |
|
65 + ", but it is " + inBuffer.position()); |
|
66 |
|
67 CoderResult coderResult2 |
|
68 = decoder.decode(inBuffer, outBuffer2, false); |
|
69 |
|
70 h.check(coderResult2.isUnderflow(), |
|
71 "Expected decoder to return underflow status"); |
|
72 h.check((first_chunk_size + second_chunk_size) == inBuffer.position(), |
|
73 "Expected input buffer position to be " |
|
74 + (first_chunk_size + second_chunk_size) |
|
75 + ", but it is " + inBuffer.position()); |
|
76 } |
|
77 } |
|