• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 tests.api.java.nio.charset;
18 
19 import java.nio.ByteBuffer;
20 import java.nio.CharBuffer;
21 import java.nio.charset.Charset;
22 import java.nio.charset.CharsetDecoder;
23 import java.nio.charset.CharsetEncoder;
24 import java.nio.charset.CoderResult;
25 import java.nio.charset.IllegalCharsetNameException;
26 import java.nio.charset.UnsupportedCharsetException;
27 import java.nio.charset.spi.CharsetProvider;
28 import java.security.Permission;
29 import java.util.Iterator;
30 import java.util.Locale;
31 import java.util.SortedMap;
32 import java.util.Vector;
33 
34 import junit.framework.TestCase;
35 
36 /**
37  * Test class java.nio.Charset.
38  */
39 public class CharsetTest extends TestCase {
40 
41 	static MockCharset charset1 = new MockCharset("mockCharset00",
42 			new String[] { "mockCharset01", "mockCharset02" });
43 
44 	static MockCharset charset2 = new MockCharset("mockCharset10",
45 			new String[] { "mockCharset11", "mockCharset12" });
46 
47 	/*
48 	 * @see TestCase#setUp()
49 	 */
setUp()50 	protected void setUp() throws Exception {
51 		super.setUp();
52 	}
53 
54 	/*
55 	 * @see TestCase#tearDown()
56 	 */
tearDown()57 	protected void tearDown() throws Exception {
58 		super.tearDown();
59 	}
60 
61 	/*
62 	 * Test the required 6 charsets are supported.
63 	 */
testRequiredCharsetSupported()64 	public void testRequiredCharsetSupported() {
65 		assertTrue(Charset.isSupported("US-ASCII"));
66 		assertTrue(Charset.isSupported("ASCII"));
67 		assertTrue(Charset.isSupported("ISO-8859-1"));
68 		assertTrue(Charset.isSupported("ISO8859_1"));
69 		assertTrue(Charset.isSupported("UTF-8"));
70 		assertTrue(Charset.isSupported("UTF8"));
71 		assertTrue(Charset.isSupported("UTF-16"));
72 		assertTrue(Charset.isSupported("UTF-16BE"));
73 		assertTrue(Charset.isSupported("UTF-16LE"));
74 
75 		Charset c1 = Charset.forName("US-ASCII");
76 		assertEquals("US-ASCII", Charset.forName("US-ASCII").name());
77 		assertEquals("US-ASCII", Charset.forName("ASCII").name());
78 		assertEquals("ISO-8859-1", Charset.forName("ISO-8859-1").name());
79 		assertEquals("ISO-8859-1", Charset.forName("ISO8859_1").name());
80 		assertEquals("UTF-8", Charset.forName("UTF-8").name());
81 		assertEquals("UTF-8", Charset.forName("UTF8").name());
82 		assertEquals("UTF-16", Charset.forName("UTF-16").name());
83 		assertEquals("UTF-16BE", Charset.forName("UTF-16BE").name());
84 		assertEquals("UTF-16LE", Charset.forName("UTF-16LE").name());
85 
86 		assertNotSame(Charset.availableCharsets(), Charset.availableCharsets());
87 		// assertSame(Charset.forName("US-ASCII"), Charset.availableCharsets()
88 		// .get("US-ASCII"));
89 		// assertSame(Charset.forName("US-ASCII"), c1);
90 		assertTrue(Charset.availableCharsets().containsKey("US-ASCII"));
91 		assertTrue(Charset.availableCharsets().containsKey("ISO-8859-1"));
92 		assertTrue(Charset.availableCharsets().containsKey("UTF-8"));
93 		assertTrue(Charset.availableCharsets().containsKey("UTF-16"));
94 		assertTrue(Charset.availableCharsets().containsKey("UTF-16BE"));
95 		assertTrue(Charset.availableCharsets().containsKey("UTF-16LE"));
96 	}
97 
98 	/*
99 	 * Test the method isSupported(String) with null.
100 	 */
testIsSupported_Null()101 	public void testIsSupported_Null() {
102 		try {
103 			Charset.isSupported(null);
104 			fail("Should throw IllegalArgumentException!");
105 		} catch (IllegalArgumentException e) {
106 			// expected
107 		}
108 	}
109 
110 	/*
111 	 * Test the method isSupported(String) with empty string.
112 	 *
113 	 */
testIsSupported_EmptyString()114 	public void testIsSupported_EmptyString() {
115 		try {
116 			Charset.isSupported("");
117 		} catch (IllegalArgumentException e) {
118                         // FIXME: Commented out since RI does throw IAE
119                         // fail("Should not throw IllegalArgumentException!");
120 		}
121 	}
122 
123 	/*
124 	 * Test the method isSupported(String) with a string starting with ".".
125 	 *
126 	 */
testIsSupported_InvalidInitialCharacter()127 	public void testIsSupported_InvalidInitialCharacter() {
128 		try {
129 			Charset.isSupported(".char");
130 		} catch (IllegalArgumentException e) {
131 			fail("Should not throw IllegalArgumentException!");
132 		}
133 	}
134 
135 	/*
136 	 * Test the method isSupported(String) with illegal charset name.
137 	 */
testIsSupported_IllegalName()138 	public void testIsSupported_IllegalName() {
139 		try {
140 			Charset.isSupported(" ///#$$");
141 			fail("Should throw IllegalCharsetNameException!");
142 		} catch (IllegalCharsetNameException e) {
143 			// expected
144 		}
145 	}
146 
147 	/*
148 	 * Test the method isSupported(String) with not supported charset name.
149 	 */
testIsSupported_NotSupported()150 	public void testIsSupported_NotSupported() {
151 		assertFalse(Charset.isSupported("impossible"));
152 	}
153 
154 	/*
155 	 * Test the method forName(String) with null.
156 	 */
testForName_Null()157 	public void testForName_Null() {
158 		try {
159 			Charset.forName(null);
160 			fail("Should throw IllegalArgumentException!");
161 		} catch (IllegalArgumentException e) {
162 			// expected
163 		}
164 	}
165 
166 	/*
167 	 * Test the method forName(String) with empty string.
168 	 */
testForName_EmptyString()169 	public void testForName_EmptyString() {
170 		try {
171 			Charset.forName("");
172 			fail("Should throw IllegalArgumentException!");
173 		} catch (IllegalArgumentException e) {
174 			// expected
175 		}
176 	}
177 
178 	/*
179 	 * Test the method forName(String) with a string starting with ".".
180 	 */
testForName_InvalidInitialCharacter()181 	public void testForName_InvalidInitialCharacter() {
182 		try {
183 			Charset.forName(".char");
184 			fail("Should throw IllegalArgumentException!");
185 		} catch (IllegalArgumentException e) {
186 			// expected
187 		}
188 	}
189 
190 	/*
191 	 * Test the method forName(String) with illegal charset name.
192 	 */
testForName_IllegalName()193 	public void testForName_IllegalName() {
194 		try {
195 			Charset.forName(" ///#$$");
196 			fail("Should throw IllegalCharsetNameException!");
197 		} catch (IllegalCharsetNameException e) {
198 			// expected
199 		}
200 	}
201 
202 	/*
203 	 * Test the method forName(String) with not supported charset name.
204 	 */
testForName_NotSupported()205 	public void testForName_NotSupported() {
206 		try {
207 			Charset.forName("impossible");
208 			fail("Should throw UnsupportedCharsetException!");
209 		} catch (UnsupportedCharsetException e) {
210 			// expected
211 		}
212 	}
213 
214 	/*
215 	 * Test the constructor with normal parameter values.
216 	 */
testConstructor_Normal()217 	public void testConstructor_Normal() {
218 		final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_";
219 		MockCharset c = new MockCharset(mockName, new String[] { "mock" });
220 		assertEquals(mockName, c.name());
221 		assertEquals(mockName, c.displayName());
222 		assertEquals(mockName, c.displayName(Locale.getDefault()));
223 		assertEquals("mock", c.aliases().toArray()[0]);
224 		assertEquals(1, c.aliases().toArray().length);
225 	}
226 
227 	/*
228 	 * Test the constructor with empty canonical name.
229 	 *
230 	 */
testConstructor_EmptyCanonicalName()231 	public void testConstructor_EmptyCanonicalName() {
232 		try {
233 			new MockCharset("", new String[0]);
234 		} catch (IllegalCharsetNameException e) {
235                         // FIXME: Commented out since RI does throw IAE
236                         // fail("Should not throw IllegalArgumentException!");
237 		}
238 	}
239 
240 	/*
241 	 * Test the constructor with illegal canonical name: starting with neither a
242 	 * digit nor a letter.
243 	 *
244 	 */
testConstructor_IllegalCanonicalName_Initial()245 	public void testConstructor_IllegalCanonicalName_Initial() {
246 		try {
247 			new MockCharset("-123", new String[] { "mock" });
248 		} catch (IllegalCharsetNameException e) {
249 			fail("Should not throw IllegalArgumentException!");
250 		}
251 	}
252 
253 	/*
254 	 * Test the constructor with illegal canonical name, illegal character in
255 	 * the middle.
256 	 */
testConstructor_IllegalCanonicalName_Middle()257 	public void testConstructor_IllegalCanonicalName_Middle() {
258 		try {
259 			new MockCharset("1%%23", new String[] { "mock" });
260 			fail("Should throw IllegalCharsetNameException!");
261 		} catch (IllegalCharsetNameException e) {
262 			// expected
263 		}
264 		try {
265 			new MockCharset("1//23", new String[] { "mock" });
266 			fail("Should throw IllegalCharsetNameException!");
267 		} catch (IllegalCharsetNameException e) {
268 			// expected
269 		}
270 	}
271 
272 	/*
273 	 * Test the constructor with null canonical name.
274 	 */
testConstructor_NullCanonicalName()275 	public void testConstructor_NullCanonicalName() {
276 		try {
277 			MockCharset c = new MockCharset(null, new String[] { "mock" });
278 			fail("Should throw NullPointerException!");
279 		} catch (NullPointerException e) {
280 			// expected
281 		}
282 	}
283 
284 	/*
285 	 * Test the constructor with null aliases.
286 	 */
testConstructor_NullAliases()287 	public void testConstructor_NullAliases() {
288 		MockCharset c = new MockCharset("mockChar", null);
289 		assertEquals("mockChar", c.name());
290 		assertEquals("mockChar", c.displayName());
291 		assertEquals("mockChar", c.displayName(Locale.getDefault()));
292 		assertEquals(0, c.aliases().toArray().length);
293 	}
294 
295 	/*
296 	 * Test the constructor with a null aliases.
297 	 */
testConstructor_NullAliase()298 	public void testConstructor_NullAliase() {
299 		try {
300 			new MockCharset("mockChar", new String[] { "mock", null });
301 			fail("Should throw NullPointerException!");
302 		} catch (NullPointerException e) {
303 			// expected
304 		}
305 	}
306 
307 	/*
308 	 * Test the constructor with no aliases.
309 	 */
testConstructor_NoAliases()310 	public void testConstructor_NoAliases() {
311 		MockCharset c = new MockCharset("mockChar", new String[0]);
312 		assertEquals("mockChar", c.name());
313 		assertEquals("mockChar", c.displayName());
314 		assertEquals("mockChar", c.displayName(Locale.getDefault()));
315 		assertEquals(0, c.aliases().toArray().length);
316 	}
317 
318 	/*
319 	 * Test the constructor with empty aliases.
320 	 *
321 	 */
testConstructor_EmptyAliases()322 	public void testConstructor_EmptyAliases() {
323 		try {
324 			new MockCharset("mockChar", new String[] { "" });
325 		} catch (IllegalCharsetNameException e) {
326                         // FIXME: Commented out since RI does throw IAE
327 			// fail("Should not throw IllegalArgumentException!");
328 		}
329 	}
330 
331 	/*
332 	 * Test the constructor with illegal aliases: starting with neither a digit
333 	 * nor a letter.
334 	 *
335 	 */
testConstructor_IllegalAliases_Initial()336 	public void testConstructor_IllegalAliases_Initial() {
337 		try {
338 			new MockCharset("mockChar", new String[] { "mock", "-123" });
339 		} catch (IllegalCharsetNameException e) {
340 			fail("Should not throw IllegalArgumentException!");
341 		}
342 	}
343 
344 	/*
345 	 * Test the constructor with illegal aliase, illegal character in the
346 	 * middle.
347 	 */
testConstructor_IllegalAliases_Middle()348 	public void testConstructor_IllegalAliases_Middle() {
349 		try {
350 			new MockCharset("mockChar", new String[] { "mock", "22##ab" });
351 			fail("Should throw IllegalCharsetNameException!");
352 		} catch (IllegalCharsetNameException e) {
353 			// expected
354 		}
355 		try {
356 			new MockCharset("mockChar", new String[] { "mock", "22%%ab" });
357 			fail("Should throw IllegalCharsetNameException!");
358 		} catch (IllegalCharsetNameException e) {
359 			// expected
360 		}
361 	}
362 
363 	/*
364 	 * Test the method aliases() with multiple aliases. Most conditions have
365 	 * been tested in the testcases for the constructors.
366 	 */
testAliases_Multiple()367 	public void testAliases_Multiple() {
368 		final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_";
369 		MockCharset c = new MockCharset("mockChar", new String[] { "mock",
370 				mockName, "mock2" });
371 		assertEquals("mockChar", c.name());
372 		assertEquals(3, c.aliases().size());
373 		assertTrue(c.aliases().contains("mock"));
374 		assertTrue(c.aliases().contains(mockName));
375 		assertTrue(c.aliases().contains("mock2"));
376 
377 		try {
378 			c.aliases().clear();
379 			fail("Should throw UnsupportedOperationException!");
380 		} catch (UnsupportedOperationException e) {
381 			// expected
382 		}
383 	}
384 
385 	/*
386 	 * Test the method aliases() with duplicate aliases, one same with its
387 	 * canonical name.
388 	 */
testAliases_Duplicate()389 	public void testAliases_Duplicate() {
390 		final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_";
391 		MockCharset c = new MockCharset("mockChar", new String[] { "mockChar",
392 				"mock", mockName, "mock", "mockChar", "mock", "mock2" });
393 		assertEquals("mockChar", c.name());
394 		assertEquals(4, c.aliases().size());
395 		assertTrue(c.aliases().contains("mockChar"));
396 		assertTrue(c.aliases().contains("mock"));
397 		assertTrue(c.aliases().contains(mockName));
398 		assertTrue(c.aliases().contains("mock2"));
399 	}
400 
401 	/*
402 	 * Test the method canEncode(). Test the default return value.
403 	 */
testCanEncode()404 	public void testCanEncode() {
405 		MockCharset c = new MockCharset("mock", null);
406 		assertTrue(c.canEncode());
407 	}
408 
409 	/*
410 	 * Test the method isRegistered(). Test the default return value.
411 	 */
testIsRegistered()412 	public void testIsRegistered() {
413 		MockCharset c = new MockCharset("mock", null);
414 		assertTrue(c.isRegistered());
415 	}
416 
417 	/*
418 	 * The name() method has been tested by the testcases for the constructor.
419 	 */
testName()420 	public void testName() {
421 		// already covered by testConstructor_XXX series
422 	}
423 
424 	/*
425 	 * The displayName() method have been tested by the testcases for the
426 	 * constructor.
427 	 */
testDisplayName()428 	public void testDisplayName() {
429 		// already covered by testConstructor_XXX series
430 	}
431 
432 	/*
433 	 * Test displayName(Locale) with null.
434 	 */
testDisplayName_Locale_Null()435 	public void testDisplayName_Locale_Null() {
436 		MockCharset c = new MockCharset("mock", null);
437 		assertEquals("mock", c.displayName(null));
438 	}
439 
440 	/*
441 	 * Test the method compareTo(Object) with normal conditions.
442 	 */
testCompareTo_Normal()443 	public void testCompareTo_Normal() {
444 		MockCharset c1 = new MockCharset("mock", null);
445 		assertEquals(0, c1.compareTo(c1));
446 
447 		MockCharset c2 = new MockCharset("Mock", null);
448 		assertEquals(0, c1.compareTo(c2));
449 
450 		c2 = new MockCharset("mock2", null);
451 		assertTrue(c1.compareTo(c2) < 0);
452 		assertTrue(c2.compareTo(c1) > 0);
453 
454 		c2 = new MockCharset("mack", null);
455 		assertTrue(c1.compareTo(c2) > 0);
456 		assertTrue(c2.compareTo(c1) < 0);
457 
458 		c2 = new MockCharset("m.", null);
459 		assertTrue(c1.compareTo(c2) > 0);
460 		assertTrue(c2.compareTo(c1) < 0);
461 
462 		c2 = new MockCharset("m:", null);
463 		assertEquals("mock".compareToIgnoreCase("m:"), c1.compareTo(c2));
464 		assertEquals("m:".compareToIgnoreCase("mock"), c2.compareTo(c1));
465 
466 		c2 = new MockCharset("m-", null);
467 		assertTrue(c1.compareTo(c2) > 0);
468 		assertTrue(c2.compareTo(c1) < 0);
469 
470 		c2 = new MockCharset("m_", null);
471 		assertTrue(c1.compareTo(c2) > 0);
472 		assertTrue(c2.compareTo(c1) < 0);
473 	}
474 
475 	/*
476 	 * Test the method compareTo(Object) with null param.
477 	 */
478 	public void testCompareTo_Null() {
479 		MockCharset c1 = new MockCharset("mock", null);
480 		try {
481 			c1.compareTo(null);
482 			fail("Should throw NullPointerException!");
483 		} catch (NullPointerException e) {
484 			// expected
485 		}
486 	}
487 
488 	/*
489 	 * Test the method compareTo(Object) with another kind of charset object.
490 	 */
491 	public void testCompareTo_DiffCharsetClass() {
492 		MockCharset c1 = new MockCharset("mock", null);
493 		MockCharset2 c2 = new MockCharset2("Mock", new String[] { "myname" });
494 		assertEquals(0, c1.compareTo(c2));
495 		assertEquals(0, c2.compareTo(c1));
496 	}
497 
498 	/*
499 	 * Test the method equals(Object) with null param.
500 	 */
501 	public void testEquals_Normal() {
502 		MockCharset c1 = new MockCharset("mock", null);
503 		MockCharset2 c2 = new MockCharset2("mock", null);
504 		assertTrue(c1.equals(c2));
505 		assertTrue(c2.equals(c1));
506 
507 		c2 = new MockCharset2("Mock", null);
508 		assertFalse(c1.equals(c2));
509 		assertFalse(c2.equals(c1));
510 	}
511 
512 	/*
513 	 * Test the method equals(Object) with normal conditions.
514 	 */
515 	public void testEquals_Null() {
516 		MockCharset c1 = new MockCharset("mock", null);
517 		assertFalse(c1.equals(null));
518 	}
519 
520 	/*
521 	 * Test the method equals(Object) with another kind of charset object.
522 	 */
523 	public void testEquals_NonCharsetObject() {
524 		MockCharset c1 = new MockCharset("mock", null);
525 		assertFalse(c1.equals("test"));
526 	}
527 
528 	/*
529 	 * Test the method equals(Object) with another kind of charset object.
530 	 */
531 	public void testEquals_DiffCharsetClass() {
532 		MockCharset c1 = new MockCharset("mock", null);
533 		MockCharset2 c2 = new MockCharset2("mock", null);
534 		assertTrue(c1.equals(c2));
535 		assertTrue(c2.equals(c1));
536 	}
537 
538 	/*
539 	 * Test the method hashCode().
540 	 */
541 	public void testHashCode_DiffCharsetClass() {
542 		MockCharset c1 = new MockCharset("mock", null);
543 		assertEquals(c1.hashCode(), "mock".hashCode());
544 
545 		final String mockName = "mockChar1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.:-_";
546 		c1 = new MockCharset(mockName, new String[] { "mockChar", "mock",
547 				mockName, "mock", "mockChar", "mock", "mock2" });
548 		assertEquals(mockName.hashCode(), c1.hashCode());
549 	}
550 
551 	/*
552 	 * Test the method encode(CharBuffer) under normal condition.
553 	 */
554 	public void testEncode_CharBuffer_Normal() throws Exception {
555 		MockCharset c1 = new MockCharset("testEncode_CharBuffer_Normal_mock", null);
556 		ByteBuffer bb = c1.encode(CharBuffer.wrap("abcdefg"));
557 		assertEquals("abcdefg", new String(bb.array(), "iso8859-1"));
558 		bb = c1.encode(CharBuffer.wrap(""));
559 		assertEquals("", new String(bb.array(), "iso8859-1"));
560 	}
561 
562 	/*
563 	 * Test the method encode(CharBuffer) with an unmappable char.
564 	 */
565 	public void testEncode_CharBuffer_Unmappable() throws Exception {
566 		Charset c1 = Charset.forName("iso8859-1");
567 		ByteBuffer bb = c1.encode(CharBuffer.wrap("abcd\u5D14efg"));
568 		assertEquals(new String(bb.array(), "iso8859-1"), "abcd"
569 				+ new String(c1.newEncoder().replacement(), "iso8859-1")
570 				+ "efg");
571 	}
572 
573 	/*
574 	 * Test the method encode(CharBuffer) with null CharBuffer.
575 	 */
576 	public void testEncode_CharBuffer_NullCharBuffer() {
577 		MockCharset c = new MockCharset("mock", null);
578 		try {
579 			c.encode((CharBuffer) null);
580 			fail("Should throw NullPointerException!");
581 		} catch (NullPointerException e) {
582 			// expected
583 		}
584 	}
585 
586 	/*
587 	 * Test the method encode(CharBuffer) with null encoder.
588 	 */
589 	public void testEncode_CharBuffer_NullEncoder() {
590 		MockCharset2 c = new MockCharset2("mock2", null);
591 		try {
592 			c.encode(CharBuffer.wrap("hehe"));
593 			fail("Should throw NullPointerException!");
594 		} catch (NullPointerException e) {
595 			// expected
596 		}
597 	}
598 
599 	/*
600 	 * Test the method encode(String) under normal condition.
601 	 */
602 	public void testEncode_String_Normal() throws Exception {
603 		MockCharset c1 = new MockCharset("testEncode_String_Normal_mock", null);
604 		ByteBuffer bb = c1.encode("abcdefg");
605 		assertEquals("abcdefg", new String(bb.array(), "iso8859-1"));
606 		bb = c1.encode("");
607 		assertEquals("", new String(bb.array(), "iso8859-1"));
608 	}
609 
610 	/*
611 	 * Test the method encode(String) with an unmappable char.
612 	 */
613 	public void testEncode_String_Unmappable() throws Exception {
614 		Charset c1 = Charset.forName("iso8859-1");
615 		ByteBuffer bb = c1.encode("abcd\u5D14efg");
616 		assertEquals(new String(bb.array(), "iso8859-1"), "abcd"
617 				+ new String(c1.newEncoder().replacement(), "iso8859-1")
618 				+ "efg");
619 	}
620 
621 	/*
622 	 * Test the method encode(String) with null CharBuffer.
623 	 */
624 	public void testEncode_String_NullString() {
625 		MockCharset c = new MockCharset("mock", null);
626 		try {
627 			c.encode((String) null);
628 			fail("Should throw NullPointerException!");
629 		} catch (NullPointerException e) {
630 			// expected
631 		}
632 	}
633 
634 	/*
635 	 * Test the method encode(String) with null encoder.
636 	 */
637 	public void testEncode_String_NullEncoder() {
638 
639 		MockCharset2 c = new MockCharset2("mock2", null);
640 		try {
641 			c.encode("hehe");
642 			fail("Should throw NullPointerException!");
643 		} catch (NullPointerException e) {
644 			// expected
645 		}
646 	}
647 
648 	/*
649 	 * Test the method decode(ByteBuffer) under normal condition.
650 	 */
651 	public void testDecode_Normal() throws Exception {
652 		MockCharset c1 = new MockCharset("mock", null);
653 		CharBuffer cb = c1.decode(ByteBuffer.wrap("abcdefg"
654 				.getBytes("iso8859-1")));
655 		assertEquals("abcdefg", new String(cb.array()));
656 		cb = c1.decode(ByteBuffer.wrap("".getBytes("iso8859-1")));
657 		assertEquals("", new String(cb.array()));
658 	}
659 
660 	/*
661 	 * Test the method decode(ByteBuffer) with a malformed input.
662 	 */
663 	public void testDecode_Malformed() throws Exception {
664 		Charset c1 = Charset.forName("iso8859-1");
665 		CharBuffer cb = c1.decode(ByteBuffer.wrap("abcd\u5D14efg"
666 				.getBytes("iso8859-1")));
667 		byte[] replacement = c1.newEncoder().replacement();
668 		assertEquals(new String(cb.array()).trim(), "abcd" + new String(replacement, "iso8859-1")
669 				+ "efg");
670 	}
671 
672 	/*
673 	 * Test the method decode(ByteBuffer) with null CharBuffer.
674 	 */
675 	public void testDecode_NullByteBuffer() {
676 		MockCharset c = new MockCharset("mock", null);
677 		try {
678 			c.decode(null);
679 			fail("Should throw NullPointerException!");
680 		} catch (NullPointerException e) {
681 			// expected
682 		}
683 	}
684 
685 	/*
686 	 * Test the method decode(ByteBuffer) with null encoder.
687 	 */
688 	public void testDecode_NullDecoder() {
689 		MockCharset2 c = new MockCharset2("mock2", null);
690 		try {
691 			c.decode(ByteBuffer.wrap("hehe".getBytes()));
692 			fail("Should throw NullPointerException!");
693 		} catch (NullPointerException e) {
694 			// expected
695 		}
696 	}
697 
698 	/*
699 	 * Test the method toString().
700 	 */
701 	public void testToString() {
702 		MockCharset c1 = new MockCharset("mock", null);
703 		assertTrue(-1 != c1.toString().indexOf("mock"));
704 	}
705 
706     /**
707      * @tests java.nio.charset.Charset#availableCharsets()
708      */
709     public void test_availableCharsets() throws Exception {
710         // regression test for Harmony-1051
711         ClassLoader originalClassLoader = Thread.currentThread()
712                 .getContextClassLoader();
713         try {
714             Thread.currentThread().setContextClassLoader(null);
715             SortedMap<String, Charset> charsets = Charset.availableCharsets();
716             // make sure "mockCharset00" is loaded by MockCharsetProvider
717             assertTrue(charsets.containsKey("mockCharset00"));
718         } finally {
719             Thread.currentThread().setContextClassLoader(originalClassLoader);
720         }
721     }
722 
723     /**
724      * @tests java.nio.charset.Charset#availableCharsets()
725      */
726     public void test_forNameLString() throws Exception {
727         // regression test for Harmony-1051
728         ClassLoader originalClassLoader = Thread.currentThread()
729                 .getContextClassLoader();
730         try {
731             Thread.currentThread().setContextClassLoader(null);
732             // make sure "mockCharset00" is loaded by MockCharsetProvider
733             assertNotNull(Charset.forName("mockCharset00"));
734         } finally {
735             Thread.currentThread().setContextClassLoader(originalClassLoader);
736         }
737     }
738 
739 	/*
740 	 * Mock charset class.
741 	 */
742 	static final class MockCharset extends Charset {
743 
744 		public MockCharset(String canonicalName, String[] aliases) {
745 			super(canonicalName, aliases);
746 		}
747 
748 		public boolean contains(Charset cs) {
749 			return false;
750 		}
751 
752 		public CharsetDecoder newDecoder() {
753 			return new MockDecoder(this);
754 		}
755 
756 		public CharsetEncoder newEncoder() {
757 			return new MockEncoder(this);
758 		}
759 	}
760 
761 	/*
762 	 * Another mock charset class.
763 	 */
764 	static class MockCharset2 extends Charset {
765 
766 		public MockCharset2(String canonicalName, String[] aliases) {
767 			super(canonicalName, aliases);
768 		}
769 
770 		public boolean contains(Charset cs) {
771 			return false;
772 		}
773 
774 		public CharsetDecoder newDecoder() {
775 			return null;
776 		}
777 
778 		public CharsetEncoder newEncoder() {
779 			return null;
780 		}
781 	}
782 
783 	/*
784 	 * Mock encoder.
785 	 */
786 	static class MockEncoder extends java.nio.charset.CharsetEncoder {
787 
788 		public MockEncoder(Charset cs) {
789 			super(cs, 1, 3, new byte[] { (byte) '?' });
790 		}
791 
792 		protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) {
793 			while (in.remaining() > 0) {
794 				out.put((byte) in.get());
795 				// out.put((byte) '!');
796 			}
797 			return CoderResult.UNDERFLOW;
798 		}
799 	}
800 
801 	/*
802 	 * Mock decoder.
803 	 */
804 	static class MockDecoder extends java.nio.charset.CharsetDecoder {
805 
MockDecoder(Charset cs)806 		public MockDecoder(Charset cs) {
807 			super(cs, 1, 10);
808 		}
809 
decodeLoop(ByteBuffer in, CharBuffer out)810 		protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
811 			while (in.remaining() > 0) {
812 				out.put((char) in.get());
813 			}
814 			return CoderResult.UNDERFLOW;
815 		}
816 	}
817 
818 	/*
819 	 * Mock charset provider.
820 	 */
821 	public static class MockCharsetProvider extends CharsetProvider {
822 
charsetForName(String charsetName)823 		public Charset charsetForName(String charsetName) {
824 			if ("MockCharset00".equalsIgnoreCase(charsetName)
825 					|| "MockCharset01".equalsIgnoreCase(charsetName)
826 					|| "MockCharset02".equalsIgnoreCase(charsetName)) {
827 				return new MockCharset("mockCharset00", new String[] {
828 						"mockCharset01", "mockCharset02" });
829 			}
830 			return null;
831 		}
832 
charsets()833 		public Iterator charsets() {
834 			Vector v = new Vector();
835 			v.add(new MockCharset("mockCharset00", new String[] {
836 					"mockCharset01", "mockCharset02" }));
837 			return v.iterator();
838 		}
839 	}
840 }
841