OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / nio_char / tests / java / nio / charset / ASCIICharsetEncoderTest.java
1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.apache.harmony.nio_char.tests.java.nio.charset;
18
19 import dalvik.annotation.TestTargetClass;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestLevel;
23
24 import java.nio.ByteBuffer;
25 import java.nio.CharBuffer;
26 import java.nio.charset.CharacterCodingException;
27 import java.nio.charset.Charset;
28 import java.nio.charset.CharsetEncoder;
29 import java.nio.charset.CoderResult;
30 import java.nio.charset.CodingErrorAction;
31 import java.nio.charset.MalformedInputException;
32 import java.nio.charset.UnmappableCharacterException;
33
34 import junit.framework.TestCase;
35 @TestTargetClass(CharsetEncoder.class)
36 public class ASCIICharsetEncoderTest extends TestCase {
37
38     // charset for ascii
39     private static final Charset cs = Charset.forName("ascii");
40     private static final CharsetEncoder encoder = cs.newEncoder();
41     private static final int MAXCODEPOINT = 0x7F;
42     /*
43      * @see CharsetEncoderTest#setUp()
44      */
45     protected void setUp() throws Exception {
46     }
47
48     /*
49      * @see CharsetEncoderTest#tearDown()
50      */
51     protected void tearDown() throws Exception {
52     }
53
54     @TestTargetNew(
55         level = TestLevel.PARTIAL,
56         notes = "IllegalStateException checking missed.",
57         method = "canEncode",
58         args = {java.lang.CharSequence.class}
59     )
60     public void testCanEncodeCharSequence() {
61         // normal case for ascCS
62         assertTrue(encoder.canEncode("\u0077"));
63         assertFalse(encoder.canEncode("\uc2a3"));
64         assertFalse(encoder.canEncode("\ud800\udc00"));
65         try {
66             encoder.canEncode(null);
67         } catch (NullPointerException e) {
68         }
69         assertTrue(encoder.canEncode(""));
70     }
71
72     @TestTargets({
73         @TestTargetNew(
74             level = TestLevel.PARTIAL,
75             notes = "IllegalStateException checking missed.",
76             method = "canEncode",
77             args = {java.lang.CharSequence.class}
78         ),
79         @TestTargetNew(
80             level = TestLevel.PARTIAL,
81             notes = "IllegalStateException checking missed.",
82             method = "canEncode",
83             args = {char.class}
84         )
85     })
86     public void testCanEncodeSurrogate () {
87         assertFalse(encoder.canEncode('\ud800'));
88         assertFalse(encoder.canEncode("\udc00"));
89     }
90
91     @TestTargetNew(
92         level = TestLevel.PARTIAL,
93         notes = "IllegalStateException checking missed.",
94         method = "canEncode",
95         args = {char.class}
96     )
97     public void testCanEncodechar() throws CharacterCodingException {
98         assertTrue(encoder.canEncode('\u0077'));
99         assertFalse(encoder.canEncode('\uc2a3'));
100     }
101
102     @TestTargets({
103         @TestTargetNew(
104             level = TestLevel.COMPLETE,
105             notes = "",
106             method = "averageBytesPerChar",
107             args = {}
108         ),
109         @TestTargetNew(
110             level = TestLevel.COMPLETE,
111             notes = "",
112             method = "maxBytesPerChar",
113             args = {}
114         )
115     })
116     public void testSpecificDefaultValue() {
117         assertEquals(1.0, encoder.averageBytesPerChar(), 0.0);
118         assertEquals(1.0, encoder.maxBytesPerChar(), 0.0);
119     }
120
121     @TestTargets({
122         @TestTargetNew(
123             level = TestLevel.PARTIAL,
124             notes = "Exceptions checking missed.",
125             method = "encode",
126             args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
127         ),
128         @TestTargetNew(
129             level = TestLevel.PARTIAL,
130             notes = "Exceptions checking missed.",
131             method = "encode",
132             args = {java.nio.CharBuffer.class}
133         )
134     })
135     public void testMultiStepEncode() throws CharacterCodingException {
136         encoder.onMalformedInput(CodingErrorAction.REPORT);
137         encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
138         try {
139             encoder.encode(CharBuffer.wrap("\ud800\udc00"));
140             fail("should unmappable");
141         } catch (UnmappableCharacterException e) {
142         }
143         encoder.reset();
144         ByteBuffer out = ByteBuffer.allocate(10);
145         assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true)
146                 .isMalformed());
147         encoder.flush(out);
148         encoder.reset();
149         out = ByteBuffer.allocate(10);
150         assertSame(CoderResult.UNDERFLOW, encoder.encode(CharBuffer
151                 .wrap("\ud800"), out, false));
152         assertTrue(encoder.encode(CharBuffer.wrap("\udc00"), out, true)
153                 .isMalformed());
154     }
155
156     @TestTargetNew(
157         level = TestLevel.COMPLETE,
158         notes = "",
159         method = "encode",
160         args = {java.nio.CharBuffer.class}
161     )
162     public void testEncodeMapping() throws CharacterCodingException {
163         encoder.reset();
164
165         for (int i =0; i <= MAXCODEPOINT; i++) {
166             char[] chars = Character.toChars(i);
167             CharBuffer cb = CharBuffer.wrap(chars);
168             ByteBuffer bb = encoder.encode(cb);
169             assertEquals(i, bb.get(0));
170         }
171
172         CharBuffer cb = CharBuffer.wrap("\u0080");
173         try {
174             encoder.encode(cb);
175         } catch (UnmappableCharacterException e) {
176             //expected
177         }
178
179         cb = CharBuffer.wrap("\ud800");
180         try {
181             encoder.encode(cb);
182         } catch (MalformedInputException e) {
183             //expected
184         }
185
186         ByteBuffer bb = ByteBuffer.allocate(0x10);
187         cb = CharBuffer.wrap("A");
188         encoder.reset();
189         encoder.encode(cb, bb, false);
190         try {
191         encoder.encode(cb);
192         } catch (IllegalStateException e) {
193             //expected
194         }
195     }
196
197     @TestTargets({
198         @TestTargetNew(
199             level = TestLevel.PARTIAL,
200             notes = "Checks functionality. Exceptions checking missed.",
201             method = "encode",
202             args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
203         ),
204         @TestTargetNew(
205             level = TestLevel.PARTIAL,
206             notes = "Checks functionality. Exceptions checking missed.",
207             method = "flush",
208             args = {java.nio.ByteBuffer.class}
209         ),
210         @TestTargetNew(
211             level = TestLevel.PARTIAL,
212             notes = "Checks functionality. Exceptions checking missed.",
213             method = "reset",
214             args = {}
215         )
216     })
217     public void testInternalState() {
218         CharBuffer in = CharBuffer.wrap("A");
219         ByteBuffer out = ByteBuffer.allocate(0x10);
220
221         //normal encoding process
222         encoder.reset();
223         encoder.encode(in, out, false);
224         in = CharBuffer.wrap("B");
225         encoder.encode(in, out, true);
226         encoder.flush(out);
227     }
228
229     //reset could be called at any time
230     @TestTargets({
231         @TestTargetNew(
232             level = TestLevel.PARTIAL,
233             notes = "Checks functionality. Exceptions checking missed.",
234             method = "reset",
235             args = {}
236         ),
237         @TestTargetNew(
238             level = TestLevel.PARTIAL,
239             notes = "Checks functionality. Exceptions checking missed.",
240             method = "encode",
241             args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
242         ),
243         @TestTargetNew(
244             level = TestLevel.PARTIAL,
245             notes = "Checks functionality. Exceptions checking missed.",
246             method = "flush",
247             args = {java.nio.ByteBuffer.class}
248         )
249     })
250     public void testInternalState_Reset() {
251         CharsetEncoder newEncoder = cs.newEncoder();
252         //Init - > reset
253         newEncoder.reset();
254
255         //reset - > reset
256         newEncoder.reset();
257
258         //encoding - >reset
259         {
260             CharBuffer in = CharBuffer.wrap("A");
261             ByteBuffer out = ByteBuffer.allocate(0x10);
262             newEncoder.encode(in, out, false);
263             newEncoder.reset();
264         }
265
266         //encoding end -> reset
267         {
268             CharBuffer in = CharBuffer.wrap("A");
269             ByteBuffer out = ByteBuffer.allocate(0x10);
270             newEncoder.encode(in, out, true);
271             newEncoder.reset();
272         }
273         //flused -> reset
274         {
275             CharBuffer in = CharBuffer.wrap("A");
276             ByteBuffer out = ByteBuffer.allocate(0x10);
277             newEncoder.encode(in, out, true);
278             newEncoder.flush(out);
279             newEncoder.reset();
280         }
281     }
282
283     @TestTargetNew(
284         level = TestLevel.PARTIAL,
285         notes = "CoderMalfunctionError checking missed.",
286         method = "encode",
287         args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
288     )
289     public void testInternalState_Encoding() {
290         CharsetEncoder newEncoder = cs.newEncoder();
291         //Init - > encoding
292         {
293             CharBuffer in = CharBuffer.wrap("A");
294             ByteBuffer out = ByteBuffer.allocate(0x10);
295             newEncoder.encode(in, out, false);
296         }
297
298         //reset - > encoding
299         {
300             CharBuffer in = CharBuffer.wrap("A");
301             ByteBuffer out = ByteBuffer.allocate(0x10);
302             newEncoder.reset();
303             newEncoder.encode(in, out, false);
304         }
305         //reset - > encoding - > encoding
306         {
307             newEncoder.reset();
308             CharBuffer in = CharBuffer.wrap("A");
309             ByteBuffer out = ByteBuffer.allocate(0x10);
310             newEncoder.encode(in, out, false);
311             in = CharBuffer.wrap("BC");
312             newEncoder.encode(in, out, false);
313         }
314
315         //encoding_end - > encoding
316         {
317             newEncoder.reset();
318             CharBuffer in = CharBuffer.wrap("A");
319             ByteBuffer out = ByteBuffer.allocate(0x10);
320             newEncoder.encode(in, out, true);
321             in = CharBuffer.wrap("BC");
322             try {
323                 newEncoder.encode(in, out, false);
324                 fail("Should throw IllegalStateException");
325             } catch (IllegalStateException e) {
326                 //expected
327             }
328         }
329         //flushed - > encoding
330         {
331             newEncoder.reset();
332             CharBuffer in = CharBuffer.wrap("A");
333             ByteBuffer out = ByteBuffer.allocate(0x10);
334             newEncoder.encode(in, out, true);
335             newEncoder.flush(out);
336             in = CharBuffer.wrap("BC");
337             try {
338                 newEncoder.encode(in, out, false);
339                 fail("Should throw IllegalStateException");
340             } catch (IllegalStateException e) {
341                 //expected
342             }
343         }
344     }
345
346     @TestTargetNew(
347         level = TestLevel.PARTIAL,
348         notes = "CoderMalfunctionError checking missed.",
349         method = "encode",
350         args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
351     )
352     public void testInternalState_Encoding_END() {
353         CharsetEncoder newEncoder = cs.newEncoder();
354
355         //Init - >encoding_end
356         {
357             CharBuffer in = CharBuffer.wrap("A");
358             ByteBuffer out = ByteBuffer.allocate(0x10);
359             newEncoder.encode(in, out, true);
360         }
361
362         //Reset -> encoding_end
363         {
364             CharBuffer in = CharBuffer.wrap("A");
365             ByteBuffer out = ByteBuffer.allocate(0x10);
366             newEncoder.reset();
367             newEncoder.encode(in, out, true);
368         }
369
370         //encoding -> encoding_end
371         {
372             newEncoder.reset();
373             CharBuffer in = CharBuffer.wrap("A");
374             ByteBuffer out = ByteBuffer.allocate(0x10);
375             newEncoder.encode(in, out, false);
376             in = CharBuffer.wrap("BC");
377             newEncoder.encode(in, out, true);
378         }
379
380         //Reset -> encoding_end
381         {
382             newEncoder.reset();
383             CharBuffer in = CharBuffer.wrap("A");
384             ByteBuffer out = ByteBuffer.allocate(0x10);
385             newEncoder.encode(in, out, true);
386             in = CharBuffer.wrap("BC");
387             newEncoder.encode(in, out, true);
388         }
389
390         //Flushed -> encoding_end
391         {
392             newEncoder.reset();
393             CharBuffer in = CharBuffer.wrap("A");
394             ByteBuffer out = ByteBuffer.allocate(0x10);
395             newEncoder.encode(in, out, true);
396             newEncoder.flush(out);
397             in = CharBuffer.wrap("BC");
398             try {
399                 newEncoder.encode(in, out, true);
400                 fail("Should throw IllegalStateException");
401             } catch (IllegalStateException e) {
402                 //expected
403             }
404         }
405     }
406
407     @TestTargetNew(
408         level = TestLevel.PARTIAL,
409         notes = "CoderMalfunctionError checking missed.",
410         method = "encode",
411         args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
412     )
413     public void testInternalState_Flushed() {
414         CharsetEncoder newEncoder = cs.newEncoder();
415
416         //init -> flushed
417         {
418             ByteBuffer out = ByteBuffer.allocate(0x10);
419             newEncoder.flush(out);
420         }
421
422         //reset - > flushed
423         {
424             newEncoder.reset();
425             CharBuffer in = CharBuffer.wrap("A");
426             ByteBuffer out = ByteBuffer.allocate(0x10);
427             newEncoder.encode(in, out, true);
428             newEncoder.reset();
429             newEncoder.flush(out);
430         }
431
432         //encoding - > flushed
433         {
434             newEncoder.reset();
435             CharBuffer in = CharBuffer.wrap("A");
436             ByteBuffer out = ByteBuffer.allocate(0x10);
437             newEncoder.encode(in, out, false);
438             try {
439
440                 newEncoder.flush(out);
441                 fail("Should throw IllegalStateException");
442             } catch (IllegalStateException e) {
443                 // expected
444             }
445         }
446
447         //encoding_end -> flushed
448         {
449             newEncoder.reset();
450             CharBuffer in = CharBuffer.wrap("A");
451             ByteBuffer out = ByteBuffer.allocate(0x10);
452             newEncoder.encode(in, out, true);
453             newEncoder.flush(out);
454         }
455
456         //flushd - > flushed
457         {
458             newEncoder.reset();
459             CharBuffer in = CharBuffer.wrap("A");
460             ByteBuffer out = ByteBuffer.allocate(0x10);
461             newEncoder.encode(in, out, true);
462             newEncoder.flush(out);
463             try {
464                 newEncoder.flush(out);
465                 fail("Should throw IllegalStateException");
466             } catch (IllegalStateException e) {
467                 // expected
468             }
469         }
470     }
471
472     @TestTargets({
473         @TestTargetNew(
474             level = TestLevel.PARTIAL,
475             notes = "Functional test.",
476             method = "encode",
477             args = {java.nio.CharBuffer.class}
478         ),
479         @TestTargetNew(
480             level = TestLevel.PARTIAL,
481             notes = "Functional test.",
482             method = "flush",
483             args = {java.nio.ByteBuffer.class}
484         ),
485         @TestTargetNew(
486             level = TestLevel.PARTIAL,
487             notes = "Functional test.",
488             method = "reset",
489             args = {}
490         )
491     })
492     public void testInternalState_Encode() throws CharacterCodingException {
493         CharsetEncoder newEncoder = cs.newEncoder();
494         //Init - > encode
495         {
496             CharBuffer in = CharBuffer.wrap("A");
497             newEncoder.encode(in);
498         }
499
500         //Reset - > encode
501         {
502             newEncoder.reset();
503             CharBuffer in = CharBuffer.wrap("A");
504             newEncoder.encode(in);
505         }
506
507         //Encoding -> encode
508         {
509             newEncoder.reset();
510             CharBuffer in = CharBuffer.wrap("A");
511             ByteBuffer out = ByteBuffer.allocate(0x10);
512             newEncoder.encode(in, out, false);
513             in = CharBuffer.wrap("BC");
514             newEncoder.encode(in);
515         }
516
517         //Encoding_end -> encode
518         {
519             newEncoder.reset();
520             CharBuffer in = CharBuffer.wrap("A");
521             ByteBuffer out = ByteBuffer.allocate(0x10);
522             newEncoder.encode(in, out, true);
523             in = CharBuffer.wrap("BC");
524             newEncoder.encode(in);
525         }
526
527         //Flushed -> reset
528         {
529             newEncoder.reset();
530             CharBuffer in = CharBuffer.wrap("A");
531             ByteBuffer out = ByteBuffer.allocate(0x10);
532             newEncoder.encode(in, out, true);
533             in = CharBuffer.wrap("BC");
534             newEncoder.flush(out);
535             out = newEncoder.encode(in);
536         }
537     }
538
539     @TestTargets({
540         @TestTargetNew(
541             level = TestLevel.PARTIAL,
542             notes = "CoderMalfunctionError checking missed.",
543             method = "encode",
544             args = {java.nio.CharBuffer.class}
545         ),
546         @TestTargetNew(
547             level = TestLevel.PARTIAL,
548             notes = "CoderMalfunctionError checking missed.",
549             method = "encode",
550             args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
551         ),
552         @TestTargetNew(
553             level = TestLevel.PARTIAL,
554             notes = "CoderMalfunctionError checking missed.",
555             method = "flush",
556             args = {java.nio.ByteBuffer.class}
557         ),
558         @TestTargetNew(
559             level = TestLevel.PARTIAL,
560             notes = "CoderMalfunctionError checking missed.",
561             method = "reset",
562             args = {}
563         )
564     })
565     public void testInternalState_from_Encode() throws CharacterCodingException {
566         CharsetEncoder newEncoder = cs.newEncoder();
567
568         //Encode -> Reset
569         {
570             CharBuffer in = CharBuffer.wrap("A");
571             newEncoder.encode(in);
572             newEncoder.reset();
573         }
574
575         // Encode -> encoding
576         {
577             CharBuffer in = CharBuffer.wrap("A");
578             newEncoder.encode(in);
579             ByteBuffer out = ByteBuffer.allocate(0x10);
580             try {
581                 newEncoder.encode(in, out, false);
582                 fail("Should throw IllegalStateException");
583             } catch (IllegalStateException e) {
584                 // expected
585             }
586         }
587
588         //Encode -> Encoding_end
589         {
590             CharBuffer in = CharBuffer.wrap("A");
591             newEncoder.encode(in);
592             ByteBuffer out = ByteBuffer.allocate(0x10);
593             try {
594                 newEncoder.encode(in, out, true);
595                 fail("Should throw IllegalStateException");
596             } catch (IllegalStateException e) {
597                 // expected
598             }
599         }
600         //Encode -> Flushed
601         {
602             CharBuffer in = CharBuffer.wrap("A");
603             ByteBuffer out = newEncoder.encode(in);
604             try {
605                 newEncoder.flush(out);
606                 fail("Should throw IllegalStateException");
607             } catch (IllegalStateException e) {
608                 // expected
609             }
610         }
611
612         //Encode - > encode
613         {
614             CharBuffer in = CharBuffer.wrap("A");
615             newEncoder.encode(in);
616             in = CharBuffer.wrap("BC");
617             newEncoder.encode(in);
618         }
619     }
620 }