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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.apache.harmony.nio_char.tests.java.nio.charset;
19 import dalvik.annotation.TestTargetClass;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestTargetNew;
22 import dalvik.annotation.TestLevel;
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;
34 import junit.framework.TestCase;
35 @TestTargetClass(CharsetEncoder.class)
36 public class ASCIICharsetEncoderTest extends TestCase {
39 private static final Charset cs = Charset.forName("ascii");
40 private static final CharsetEncoder encoder = cs.newEncoder();
41 private static final int MAXCODEPOINT = 0x7F;
43 * @see CharsetEncoderTest#setUp()
45 protected void setUp() throws Exception {
49 * @see CharsetEncoderTest#tearDown()
51 protected void tearDown() throws Exception {
55 level = TestLevel.PARTIAL,
56 notes = "IllegalStateException checking missed.",
58 args = {java.lang.CharSequence.class}
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"));
66 encoder.canEncode(null);
67 } catch (NullPointerException e) {
69 assertTrue(encoder.canEncode(""));
74 level = TestLevel.PARTIAL,
75 notes = "IllegalStateException checking missed.",
77 args = {java.lang.CharSequence.class}
80 level = TestLevel.PARTIAL,
81 notes = "IllegalStateException checking missed.",
86 public void testCanEncodeSurrogate () {
87 assertFalse(encoder.canEncode('\ud800'));
88 assertFalse(encoder.canEncode("\udc00"));
92 level = TestLevel.PARTIAL,
93 notes = "IllegalStateException checking missed.",
97 public void testCanEncodechar() throws CharacterCodingException {
98 assertTrue(encoder.canEncode('\u0077'));
99 assertFalse(encoder.canEncode('\uc2a3'));
104 level = TestLevel.COMPLETE,
106 method = "averageBytesPerChar",
110 level = TestLevel.COMPLETE,
112 method = "maxBytesPerChar",
116 public void testSpecificDefaultValue() {
117 assertEquals(1.0, encoder.averageBytesPerChar(), 0.0);
118 assertEquals(1.0, encoder.maxBytesPerChar(), 0.0);
123 level = TestLevel.PARTIAL,
124 notes = "Exceptions checking missed.",
126 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
129 level = TestLevel.PARTIAL,
130 notes = "Exceptions checking missed.",
132 args = {java.nio.CharBuffer.class}
135 public void testMultiStepEncode() throws CharacterCodingException {
136 encoder.onMalformedInput(CodingErrorAction.REPORT);
137 encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
139 encoder.encode(CharBuffer.wrap("\ud800\udc00"));
140 fail("should unmappable");
141 } catch (UnmappableCharacterException e) {
144 ByteBuffer out = ByteBuffer.allocate(10);
145 assertTrue(encoder.encode(CharBuffer.wrap("\ud800"), out, true)
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)
157 level = TestLevel.COMPLETE,
160 args = {java.nio.CharBuffer.class}
162 public void testEncodeMapping() throws CharacterCodingException {
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));
172 CharBuffer cb = CharBuffer.wrap("\u0080");
175 } catch (UnmappableCharacterException e) {
179 cb = CharBuffer.wrap("\ud800");
182 } catch (MalformedInputException e) {
186 ByteBuffer bb = ByteBuffer.allocate(0x10);
187 cb = CharBuffer.wrap("A");
189 encoder.encode(cb, bb, false);
192 } catch (IllegalStateException e) {
199 level = TestLevel.PARTIAL,
200 notes = "Checks functionality. Exceptions checking missed.",
202 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
205 level = TestLevel.PARTIAL,
206 notes = "Checks functionality. Exceptions checking missed.",
208 args = {java.nio.ByteBuffer.class}
211 level = TestLevel.PARTIAL,
212 notes = "Checks functionality. Exceptions checking missed.",
217 public void testInternalState() {
218 CharBuffer in = CharBuffer.wrap("A");
219 ByteBuffer out = ByteBuffer.allocate(0x10);
221 //normal encoding process
223 encoder.encode(in, out, false);
224 in = CharBuffer.wrap("B");
225 encoder.encode(in, out, true);
229 //reset could be called at any time
232 level = TestLevel.PARTIAL,
233 notes = "Checks functionality. Exceptions checking missed.",
238 level = TestLevel.PARTIAL,
239 notes = "Checks functionality. Exceptions checking missed.",
241 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
244 level = TestLevel.PARTIAL,
245 notes = "Checks functionality. Exceptions checking missed.",
247 args = {java.nio.ByteBuffer.class}
250 public void testInternalState_Reset() {
251 CharsetEncoder newEncoder = cs.newEncoder();
260 CharBuffer in = CharBuffer.wrap("A");
261 ByteBuffer out = ByteBuffer.allocate(0x10);
262 newEncoder.encode(in, out, false);
266 //encoding end -> reset
268 CharBuffer in = CharBuffer.wrap("A");
269 ByteBuffer out = ByteBuffer.allocate(0x10);
270 newEncoder.encode(in, out, true);
275 CharBuffer in = CharBuffer.wrap("A");
276 ByteBuffer out = ByteBuffer.allocate(0x10);
277 newEncoder.encode(in, out, true);
278 newEncoder.flush(out);
284 level = TestLevel.PARTIAL,
285 notes = "CoderMalfunctionError checking missed.",
287 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
289 public void testInternalState_Encoding() {
290 CharsetEncoder newEncoder = cs.newEncoder();
293 CharBuffer in = CharBuffer.wrap("A");
294 ByteBuffer out = ByteBuffer.allocate(0x10);
295 newEncoder.encode(in, out, false);
300 CharBuffer in = CharBuffer.wrap("A");
301 ByteBuffer out = ByteBuffer.allocate(0x10);
303 newEncoder.encode(in, out, false);
305 //reset - > encoding - > encoding
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);
315 //encoding_end - > encoding
318 CharBuffer in = CharBuffer.wrap("A");
319 ByteBuffer out = ByteBuffer.allocate(0x10);
320 newEncoder.encode(in, out, true);
321 in = CharBuffer.wrap("BC");
323 newEncoder.encode(in, out, false);
324 fail("Should throw IllegalStateException");
325 } catch (IllegalStateException e) {
329 //flushed - > encoding
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");
338 newEncoder.encode(in, out, false);
339 fail("Should throw IllegalStateException");
340 } catch (IllegalStateException e) {
347 level = TestLevel.PARTIAL,
348 notes = "CoderMalfunctionError checking missed.",
350 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
352 public void testInternalState_Encoding_END() {
353 CharsetEncoder newEncoder = cs.newEncoder();
355 //Init - >encoding_end
357 CharBuffer in = CharBuffer.wrap("A");
358 ByteBuffer out = ByteBuffer.allocate(0x10);
359 newEncoder.encode(in, out, true);
362 //Reset -> encoding_end
364 CharBuffer in = CharBuffer.wrap("A");
365 ByteBuffer out = ByteBuffer.allocate(0x10);
367 newEncoder.encode(in, out, true);
370 //encoding -> encoding_end
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);
380 //Reset -> encoding_end
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);
390 //Flushed -> encoding_end
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");
399 newEncoder.encode(in, out, true);
400 fail("Should throw IllegalStateException");
401 } catch (IllegalStateException e) {
408 level = TestLevel.PARTIAL,
409 notes = "CoderMalfunctionError checking missed.",
411 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
413 public void testInternalState_Flushed() {
414 CharsetEncoder newEncoder = cs.newEncoder();
418 ByteBuffer out = ByteBuffer.allocate(0x10);
419 newEncoder.flush(out);
425 CharBuffer in = CharBuffer.wrap("A");
426 ByteBuffer out = ByteBuffer.allocate(0x10);
427 newEncoder.encode(in, out, true);
429 newEncoder.flush(out);
432 //encoding - > flushed
435 CharBuffer in = CharBuffer.wrap("A");
436 ByteBuffer out = ByteBuffer.allocate(0x10);
437 newEncoder.encode(in, out, false);
440 newEncoder.flush(out);
441 fail("Should throw IllegalStateException");
442 } catch (IllegalStateException e) {
447 //encoding_end -> flushed
450 CharBuffer in = CharBuffer.wrap("A");
451 ByteBuffer out = ByteBuffer.allocate(0x10);
452 newEncoder.encode(in, out, true);
453 newEncoder.flush(out);
459 CharBuffer in = CharBuffer.wrap("A");
460 ByteBuffer out = ByteBuffer.allocate(0x10);
461 newEncoder.encode(in, out, true);
462 newEncoder.flush(out);
464 newEncoder.flush(out);
465 fail("Should throw IllegalStateException");
466 } catch (IllegalStateException e) {
474 level = TestLevel.PARTIAL,
475 notes = "Functional test.",
477 args = {java.nio.CharBuffer.class}
480 level = TestLevel.PARTIAL,
481 notes = "Functional test.",
483 args = {java.nio.ByteBuffer.class}
486 level = TestLevel.PARTIAL,
487 notes = "Functional test.",
492 public void testInternalState_Encode() throws CharacterCodingException {
493 CharsetEncoder newEncoder = cs.newEncoder();
496 CharBuffer in = CharBuffer.wrap("A");
497 newEncoder.encode(in);
503 CharBuffer in = CharBuffer.wrap("A");
504 newEncoder.encode(in);
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);
517 //Encoding_end -> encode
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);
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);
541 level = TestLevel.PARTIAL,
542 notes = "CoderMalfunctionError checking missed.",
544 args = {java.nio.CharBuffer.class}
547 level = TestLevel.PARTIAL,
548 notes = "CoderMalfunctionError checking missed.",
550 args = {java.nio.CharBuffer.class, java.nio.ByteBuffer.class, boolean.class}
553 level = TestLevel.PARTIAL,
554 notes = "CoderMalfunctionError checking missed.",
556 args = {java.nio.ByteBuffer.class}
559 level = TestLevel.PARTIAL,
560 notes = "CoderMalfunctionError checking missed.",
565 public void testInternalState_from_Encode() throws CharacterCodingException {
566 CharsetEncoder newEncoder = cs.newEncoder();
570 CharBuffer in = CharBuffer.wrap("A");
571 newEncoder.encode(in);
575 // Encode -> encoding
577 CharBuffer in = CharBuffer.wrap("A");
578 newEncoder.encode(in);
579 ByteBuffer out = ByteBuffer.allocate(0x10);
581 newEncoder.encode(in, out, false);
582 fail("Should throw IllegalStateException");
583 } catch (IllegalStateException e) {
588 //Encode -> Encoding_end
590 CharBuffer in = CharBuffer.wrap("A");
591 newEncoder.encode(in);
592 ByteBuffer out = ByteBuffer.allocate(0x10);
594 newEncoder.encode(in, out, true);
595 fail("Should throw IllegalStateException");
596 } catch (IllegalStateException e) {
602 CharBuffer in = CharBuffer.wrap("A");
603 ByteBuffer out = newEncoder.encode(in);
605 newEncoder.flush(out);
606 fail("Should throw IllegalStateException");
607 } catch (IllegalStateException e) {
614 CharBuffer in = CharBuffer.wrap("A");
615 newEncoder.encode(in);
616 in = CharBuffer.wrap("BC");
617 newEncoder.encode(in);