1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package org.apache.harmony.tests.java.nio;
19
20import java.io.IOException;
21import java.nio.BufferOverflowException;
22import java.nio.BufferUnderflowException;
23import java.nio.ByteOrder;
24import java.nio.CharBuffer;
25import java.nio.InvalidMarkException;
26import java.nio.ReadOnlyBufferException;
27
28/**
29 * Tests java.nio.CharBuffer
30 *
31 */
32public class CharBufferTest extends AbstractBufferTest {
33	protected static final int SMALL_TEST_LENGTH = 5;
34
35	protected static final int BUFFER_LENGTH = 20;
36
37	protected CharBuffer buf;
38
39	private static char[] chars = "123456789a".toCharArray();
40
41	protected void setUp() throws Exception{
42		char[] charscopy = new char[chars.length];
43		System.arraycopy(chars, 0, charscopy, 0, chars.length);
44		buf = CharBuffer.wrap(charscopy);
45		baseBuf = buf;
46	}
47
48	protected void tearDown() throws Exception{
49		buf = null;
50		baseBuf = null;
51	}
52
53	public void testArray() {
54		char array[] = buf.array();
55		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
56
57		loadTestData1(array, buf.arrayOffset(), buf.capacity());
58		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
59
60		loadTestData2(array, buf.arrayOffset(), buf.capacity());
61		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
62
63		loadTestData1(buf);
64		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
65
66		loadTestData2(buf);
67		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
68	}
69
70	public void testArrayOffset() {
71		char array[] = buf.array();
72		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
73
74		loadTestData1(array, buf.arrayOffset(), buf.capacity());
75		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
76
77		loadTestData2(array, buf.arrayOffset(), buf.capacity());
78		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
79
80		loadTestData1(buf);
81		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
82
83		loadTestData2(buf);
84		assertContentEquals(buf, array, buf.arrayOffset(), buf.capacity());
85	}
86
87	public void testAsReadOnlyBuffer() {
88		buf.clear();
89		buf.mark();
90		buf.position(buf.limit());
91
92		// readonly's contents should be the same as buf
93		CharBuffer readonly = buf.asReadOnlyBuffer();
94		assertNotSame(buf, readonly);
95		assertTrue(readonly.isReadOnly());
96		assertEquals(buf.position(), readonly.position());
97		assertEquals(buf.limit(), readonly.limit());
98		assertEquals(buf.isDirect(), readonly.isDirect());
99		assertEquals(buf.order(), readonly.order());
100		assertEquals(buf.capacity(), readonly.capacity());
101		assertContentEquals(buf, readonly);
102
103		// readonly's position, mark, and limit should be independent to buf
104		readonly.reset();
105		assertEquals(readonly.position(), 0);
106		readonly.clear();
107		assertEquals(buf.position(), buf.limit());
108		buf.reset();
109		assertEquals(buf.position(), 0);
110
111		buf.clear();
112		int originalPosition = (buf.position() + buf.limit()) / 2;
113		buf.position(originalPosition);
114		buf.mark();
115		buf.position(buf.limit());
116
117		// readonly's contents should be the same as buf
118		readonly = buf.asReadOnlyBuffer();
119		assertNotSame(buf, readonly);
120		assertTrue(readonly.isReadOnly());
121		assertEquals(buf.position(), readonly.position());
122		assertEquals(buf.limit(), readonly.limit());
123		assertEquals(buf.isDirect(), readonly.isDirect());
124		assertEquals(buf.order(), readonly.order());
125		assertEquals(buf.capacity(), readonly.capacity());
126		assertContentEquals(buf, readonly);
127
128		// readonly's position, mark, and limit should be independent to buf
129		readonly.reset();
130		assertEquals(readonly.position(), originalPosition);
131		readonly.clear();
132		assertEquals(buf.position(), buf.limit());
133		buf.reset();
134		assertEquals(buf.position(), originalPosition);
135	}
136
137	public void testCompact() {
138		// case: buffer is full
139		buf.clear();
140		buf.mark();
141		loadTestData1(buf);
142		CharBuffer ret = buf.compact();
143		assertSame(ret, buf);
144		assertEquals(buf.position(), buf.capacity());
145		assertEquals(buf.limit(), buf.capacity());
146		assertContentLikeTestData1(buf, 0, (char) 0, buf.capacity());
147		try {
148			buf.reset();
149			fail("Should throw Exception"); //$NON-NLS-1$
150		} catch (InvalidMarkException e) {
151			// expected
152		}
153
154		// case: buffer is empty
155		buf.position(0);
156		buf.limit(0);
157		buf.mark();
158		ret = buf.compact();
159		assertSame(ret, buf);
160		assertEquals(buf.position(), 0);
161		assertEquals(buf.limit(), buf.capacity());
162		assertContentLikeTestData1(buf, 0, (char) 0, buf.capacity());
163		try {
164			buf.reset();
165			fail("Should throw Exception"); //$NON-NLS-1$
166		} catch (InvalidMarkException e) {
167			// expected
168		}
169
170		// case: normal
171		assertTrue(buf.capacity() > 5);
172		buf.position(1);
173		buf.limit(5);
174		buf.mark();
175		ret = buf.compact();
176		assertSame(ret, buf);
177		assertEquals(buf.position(), 4);
178		assertEquals(buf.limit(), buf.capacity());
179		assertContentLikeTestData1(buf, 0, (char) 1, 4);
180		try {
181			buf.reset();
182			fail("Should throw Exception"); //$NON-NLS-1$
183		} catch (InvalidMarkException e) {
184			// expected
185		}
186	}
187
188	public void testCompareTo() {
189		// compare to self
190		assertEquals(0, buf.compareTo(buf));
191
192		assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
193		buf.clear();
194		CharBuffer other = CharBuffer.allocate(buf.capacity());
195		other.put(buf);
196		other.clear();
197		buf.clear();
198		assertEquals(0, buf.compareTo(other));
199		assertEquals(0, other.compareTo(buf));
200		buf.position(1);
201		assertTrue(buf.compareTo(other) > 0);
202		assertTrue(other.compareTo(buf) < 0);
203		other.position(2);
204		assertTrue(buf.compareTo(other) < 0);
205		assertTrue(other.compareTo(buf) > 0);
206		buf.position(2);
207		assertTrue(buf.compareTo(other) == 0);
208		assertTrue(other.compareTo(buf) == 0);
209		other.limit(SMALL_TEST_LENGTH);
210		assertTrue(buf.compareTo(other) > 0);
211		assertTrue(other.compareTo(buf) < 0);
212	}
213
214	public void testDuplicate() {
215		// mark the position 0
216		buf.clear();
217		buf.mark();
218		buf.position(buf.limit());
219
220		// duplicate's contents should be the same as buf
221		CharBuffer duplicate = buf.duplicate();
222		assertNotSame(buf, duplicate);
223		assertEquals(buf.position(), duplicate.position());
224		assertEquals(buf.limit(), duplicate.limit());
225		assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
226		assertEquals(buf.isDirect(), duplicate.isDirect());
227		assertEquals(buf.order(), duplicate.order());
228		assertEquals(buf.capacity(), duplicate.capacity());
229		assertContentEquals(buf, duplicate);
230
231		// duplicate's position, mark, and limit should be independent to
232		// buf
233		duplicate.reset();
234		assertEquals(duplicate.position(), 0);
235		duplicate.clear();
236		assertEquals(buf.position(), buf.limit());
237		buf.reset();
238		assertEquals(buf.position(), 0);
239
240		// mark another position
241		buf.clear();
242		int originalPosition = (buf.position() + buf.limit()) / 2;
243		buf.position(originalPosition);
244		buf.mark();
245		buf.position(buf.limit());
246
247		// duplicate's contents should be the same as buf
248		duplicate = buf.duplicate();
249		assertNotSame(buf, duplicate);
250		assertEquals(buf.position(), duplicate.position());
251		assertEquals(buf.limit(), duplicate.limit());
252		assertEquals(buf.isReadOnly(), duplicate.isReadOnly());
253		assertEquals(buf.isDirect(), duplicate.isDirect());
254		assertEquals(buf.order(), duplicate.order());
255		assertEquals(buf.capacity(), duplicate.capacity());
256		assertContentEquals(buf, duplicate);
257
258		// duplicate's position, mark, and limit should be independent to
259		// buf
260		duplicate.reset();
261		assertEquals(duplicate.position(), originalPosition);
262		duplicate.clear();
263		assertEquals(buf.position(), buf.limit());
264		buf.reset();
265		assertEquals(buf.position(), originalPosition);
266
267		// duplicate share the same content with buf
268		if (!duplicate.isReadOnly()) {
269			loadTestData1(buf);
270			assertContentEquals(buf, duplicate);
271			loadTestData2(duplicate);
272			assertContentEquals(buf, duplicate);
273		}
274	}
275
276	public void testEquals() {
277		// equal to self
278		assertTrue(buf.equals(buf));
279		CharBuffer readonly = buf.asReadOnlyBuffer();
280		assertTrue(buf.equals(readonly));
281		CharBuffer duplicate = buf.duplicate();
282		assertTrue(buf.equals(duplicate));
283
284		// always false, if type mismatch
285		assertFalse(buf.equals(Boolean.TRUE));
286
287		assertTrue(buf.capacity() > 5);
288
289		buf.limit(buf.capacity()).position(0);
290		readonly.limit(readonly.capacity()).position(1);
291		assertFalse(buf.equals(readonly));
292
293		buf.limit(buf.capacity() - 1).position(0);
294		duplicate.limit(duplicate.capacity()).position(0);
295		assertFalse(buf.equals(duplicate));
296	}
297
298	/*
299	 * Class under test for char get()
300	 */
301	public void testGet() {
302		buf.clear();
303		for (int i = 0; i < buf.capacity(); i++) {
304			assertEquals(buf.position(), i);
305			assertEquals(buf.get(), buf.get(i));
306		}
307		try {
308			buf.get();
309			fail("Should throw Exception"); //$NON-NLS-1$
310		} catch (BufferUnderflowException e) {
311			// expected
312		}
313	}
314
315	/*
316	 * Class under test for java.nio.CharBuffer get(char[])
317	 */
318	public void testGetcharArray() {
319		char array[] = new char[1];
320		buf.clear();
321		for (int i = 0; i < buf.capacity(); i++) {
322			assertEquals(buf.position(), i);
323			CharBuffer ret = buf.get(array);
324			assertEquals(array[0], buf.get(i));
325			assertSame(ret, buf);
326		}
327		try {
328			buf.get(array);
329			fail("Should throw Exception"); //$NON-NLS-1$
330		} catch (BufferUnderflowException e) {
331			// expected
332		}
333	}
334
335	/*
336	 * Class under test for java.nio.CharBuffer get(char[], int, int)
337	 */
338	public void testGetcharArrayintint() {
339		buf.clear();
340		char array[] = new char[buf.capacity()];
341
342		try {
343			buf.get(new char[buf.capacity() + 1], 0, buf.capacity() + 1);
344			fail("Should throw Exception"); //$NON-NLS-1$
345		} catch (BufferUnderflowException e) {
346			// expected
347		}
348		assertEquals(buf.position(), 0);
349		try {
350			buf.get(array, -1, array.length);
351			fail("Should throw Exception"); //$NON-NLS-1$
352		} catch (IndexOutOfBoundsException e) {
353			// expected
354		}
355		buf.get(array, array.length, 0);
356		try {
357			buf.get(array, array.length + 1, 1);
358			fail("Should throw Exception"); //$NON-NLS-1$
359		} catch (IndexOutOfBoundsException e) {
360			// expected
361		}
362		assertEquals(buf.position(), 0);
363		try {
364			buf.get(array, 2, -1);
365			fail("Should throw Exception"); //$NON-NLS-1$
366		} catch (IndexOutOfBoundsException e) {
367			// expected
368		}
369        try {
370            buf.get((char[])null, 2, -1);
371            fail("Should throw Exception"); //$NON-NLS-1$
372        } catch (NullPointerException e) {
373            // expected
374        }
375        try {
376            buf.get(array, 2, array.length);
377            fail("Should throw Exception"); //$NON-NLS-1$
378        } catch (IndexOutOfBoundsException e) {
379            // expected
380        }
381        try {
382            buf.get(array, 1, Integer.MAX_VALUE);
383            fail("Should throw Exception"); //$NON-NLS-1$
384        } catch (BufferUnderflowException expected) {
385        } catch (IndexOutOfBoundsException expected) {
386        }
387        try {
388            buf.get(array, Integer.MAX_VALUE, 1);
389            fail("Should throw Exception"); //$NON-NLS-1$
390        } catch (IndexOutOfBoundsException e) {
391            // expected
392        }
393		assertEquals(buf.position(), 0);
394
395		buf.clear();
396		CharBuffer ret = buf.get(array, 0, array.length);
397		assertEquals(buf.position(), buf.capacity());
398		assertContentEquals(buf, array, 0, array.length);
399		assertSame(ret, buf);
400	}
401
402	/*
403	 * Class under test for char get(int)
404	 */
405	public void testGetint() {
406		buf.clear();
407		for (int i = 0; i < buf.capacity(); i++) {
408			assertEquals(buf.position(), i);
409			assertEquals(buf.get(), buf.get(i));
410		}
411		try {
412			buf.get(-1);
413			fail("Should throw Exception"); //$NON-NLS-1$
414		} catch (IndexOutOfBoundsException e) {
415			// expected
416		}
417		try {
418			buf.get(buf.limit());
419			fail("Should throw Exception"); //$NON-NLS-1$
420		} catch (IndexOutOfBoundsException e) {
421			// expected
422		}
423	}
424
425	public void testHashCode() {
426		buf.clear();
427		loadTestData1(buf);
428		CharBuffer readonly = buf.asReadOnlyBuffer();
429		CharBuffer duplicate = buf.duplicate();
430		assertTrue(buf.hashCode() == readonly.hashCode());
431		assertTrue(buf.capacity() > SMALL_TEST_LENGTH);
432		duplicate.position(buf.capacity() / 2);
433		assertTrue(buf.hashCode() != duplicate.hashCode());
434	}
435
436	/*
437	 * Class under test for java.nio.CharBuffer put(char)
438	 */
439	public void testPutchar() {
440		buf.clear();
441		for (int i = 0; i < buf.capacity(); i++) {
442			assertEquals(buf.position(), i);
443			CharBuffer ret = buf.put((char) i);
444			assertEquals(buf.get(i), (char) i);
445			assertSame(ret, buf);
446		}
447		try {
448			buf.put((char) 0);
449			fail("Should throw Exception"); //$NON-NLS-1$
450		} catch (BufferOverflowException e) {
451			// expected
452		}
453	}
454
455	/*
456	 * Class under test for java.nio.CharBuffer put(char[])
457	 */
458	public void testPutcharArray() {
459		char array[] = new char[1];
460
461		buf.clear();
462		for (int i = 0; i < buf.capacity(); i++) {
463			assertEquals(buf.position(), i);
464			array[0] = (char) i;
465			CharBuffer ret = buf.put(array);
466			assertEquals(buf.get(i), (char) i);
467			assertSame(ret, buf);
468		}
469		try {
470			buf.put(array);
471			fail("Should throw Exception"); //$NON-NLS-1$
472		} catch (BufferOverflowException e) {
473			// expected
474		}
475		try {
476			buf.put((char[]) null);
477			fail("Should throw Exception"); //$NON-NLS-1$
478		} catch (NullPointerException e) {
479			// expected
480		}
481	}
482
483	/*
484	 * Class under test for java.nio.CharBuffer put(char[], int, int)
485	 */
486	public void testPutcharArrayintint() {
487		buf.clear();
488		char array[] = new char[buf.capacity()];
489		try {
490			buf.put((char[]) null, 0, 1);
491			fail("Should throw NullPointerException"); //$NON-NLS-1$
492		} catch (NullPointerException e) {
493			// expected
494		}
495		try {
496			buf.put(new char[buf.capacity() + 1], 0, buf.capacity() + 1);
497			fail("Should throw Exception"); //$NON-NLS-1$
498		} catch (BufferOverflowException e) {
499			// expected
500		}
501		assertEquals(buf.position(), 0);
502		try {
503			buf.put(array, -1, array.length);
504			fail("Should throw Exception"); //$NON-NLS-1$
505		} catch (IndexOutOfBoundsException e) {
506			// expected
507		}
508		try {
509			buf.put(array, array.length + 1, 0);
510			fail("Should throw Exception"); //$NON-NLS-1$
511		} catch (IndexOutOfBoundsException e) {
512			// expected
513		}
514		buf.put(array, array.length, 0);
515		assertEquals(buf.position(), 0);
516		try {
517			buf.put(array, 0, -1);
518			fail("Should throw Exception"); //$NON-NLS-1$
519		} catch (IndexOutOfBoundsException e) {
520			// expected
521		}
522        try {
523            buf.put((char[])null, 0, -1);
524            fail("Should throw Exception"); //$NON-NLS-1$
525        } catch (NullPointerException e) {
526            // expected
527        }
528        try {
529            buf.put(array, 2, array.length);
530            fail("Should throw Exception"); //$NON-NLS-1$
531        } catch (IndexOutOfBoundsException e) {
532            // expected
533        }
534        try {
535            buf.put(array, Integer.MAX_VALUE, 1);
536            fail("Should throw Exception"); //$NON-NLS-1$
537        } catch (IndexOutOfBoundsException e) {
538            // expected
539        }
540        try {
541            buf.put(array, 1, Integer.MAX_VALUE);
542            fail("Should throw Exception"); //$NON-NLS-1$
543        } catch (BufferOverflowException expected) {
544        } catch (IndexOutOfBoundsException expected) {
545        }
546		assertEquals(buf.position(), 0);
547
548		loadTestData2(array, 0, array.length);
549		CharBuffer ret = buf.put(array, 0, array.length);
550		assertEquals(buf.position(), buf.capacity());
551		assertContentEquals(buf, array, 0, array.length);
552		assertSame(ret, buf);
553	}
554
555	/*
556	 * Class under test for java.nio.CharBuffer put(java.nio.CharBuffer)
557	 */
558	public void testPutCharBuffer() {
559		CharBuffer other = CharBuffer.allocate(buf.capacity());
560
561		try {
562			buf.put((CharBuffer) null);
563			fail("Should throw Exception"); //$NON-NLS-1$
564		} catch (NullPointerException e) {
565			// expected
566		}
567		try {
568			buf.put(buf);
569			fail("Should throw Exception"); //$NON-NLS-1$
570		} catch (IllegalArgumentException e) {
571			// expected
572		}
573		try {
574			buf.put(CharBuffer.allocate(buf.capacity() + 1));
575			fail("Should throw Exception"); //$NON-NLS-1$
576		} catch (BufferOverflowException e) {
577			// expected
578		}
579        try {
580            buf.flip();
581            buf.put((CharBuffer)null);
582            fail("Should throw Exception"); //$NON-NLS-1$
583        } catch (NullPointerException e) {
584            // expected
585        }
586
587		loadTestData2(other);
588		other.clear();
589		buf.clear();
590		CharBuffer ret = buf.put(other);
591		assertEquals(other.position(), other.capacity());
592		assertEquals(buf.position(), buf.capacity());
593		assertContentEquals(other, buf);
594		assertSame(ret, buf);
595	}
596
597	/*
598	 * Class under test for java.nio.CharBuffer put(int, char)
599	 */
600	public void testPutintchar() {
601		buf.clear();
602		for (int i = 0; i < buf.capacity(); i++) {
603			assertEquals(buf.position(), 0);
604			CharBuffer ret = buf.put(i, (char) i);
605			assertEquals(buf.get(i), (char) i);
606			assertSame(ret, buf);
607		}
608		try {
609			buf.put(-1, (char) 0);
610			fail("Should throw Exception"); //$NON-NLS-1$
611		} catch (IndexOutOfBoundsException e) {
612			// expected
613		}
614		try {
615			buf.put(buf.limit(), (char) 0);
616			fail("Should throw Exception"); //$NON-NLS-1$
617		} catch (IndexOutOfBoundsException e) {
618			// expected
619		}
620	}
621
622	public void testSlice() {
623		assertTrue(buf.capacity() > 5);
624		buf.position(1);
625		buf.limit(buf.capacity() - 1);
626
627		CharBuffer slice = buf.slice();
628		assertEquals(buf.isReadOnly(), slice.isReadOnly());
629		assertEquals(buf.isDirect(), slice.isDirect());
630		assertEquals(buf.order(), slice.order());
631		assertEquals(slice.position(), 0);
632		assertEquals(slice.limit(), buf.remaining());
633		assertEquals(slice.capacity(), buf.remaining());
634		try {
635			slice.reset();
636			fail("Should throw Exception"); //$NON-NLS-1$
637		} catch (InvalidMarkException e) {
638			// expected
639		}
640
641		// slice share the same content with buf
642		if (!slice.isReadOnly()) {
643			loadTestData1(slice);
644			assertContentLikeTestData1(buf, 1, (char) 0, slice.capacity());
645			buf.put(2, (char) 500);
646			assertEquals(slice.get(1), 500);
647		}
648	}
649
650	public void testToString() {
651		String expected = "";
652		for (int i = buf.position(); i < buf.limit(); i++) {
653			expected += buf.get(i);
654		}
655		String str = buf.toString();
656		assertEquals(expected, str);
657	}
658
659	public void testCharAt() {
660		for (int i = 0; i < buf.remaining(); i++) {
661			assertEquals(buf.get(buf.position() + i), buf.charAt(i));
662		}
663		try {
664			buf.charAt(-1);
665			fail("Should throw Exception"); //$NON-NLS-1$
666		} catch (IndexOutOfBoundsException e) {
667			// expected
668		}
669		try {
670			buf.charAt(buf.remaining());
671			fail("Should throw Exception"); //$NON-NLS-1$
672		} catch (IndexOutOfBoundsException e) {
673			// expected
674		}
675	}
676
677	public void testLength() {
678		assertEquals(buf.length(), buf.remaining());
679	}
680
681	public void testSubSequence() {
682		try {
683			buf.subSequence(-1, buf.length());
684			fail("Should throw Exception"); //$NON-NLS-1$
685		} catch (IndexOutOfBoundsException e) {
686			// expected
687		}
688		try {
689			buf.subSequence(buf.length() + 1, buf.length() + 1);
690			fail("Should throw Exception"); //$NON-NLS-1$
691		} catch (IndexOutOfBoundsException e) {
692			// expected
693		}
694		assertEquals(buf.subSequence(buf.length(), buf.length()).length(), 0);
695		try {
696			buf.subSequence(1, 0);
697			fail("Should throw Exception"); //$NON-NLS-1$
698		} catch (IndexOutOfBoundsException e) {
699			// expected
700		}
701		try {
702			buf.subSequence(1, buf.length() + 1);
703			fail("Should throw Exception"); //$NON-NLS-1$
704		} catch (IndexOutOfBoundsException e) {
705			// expected
706		}
707
708		assertEquals(buf.subSequence(0, buf.length()).toString(), buf
709				.toString());
710
711		if (buf.length() >= 2) {
712			assertEquals(buf.subSequence(1, buf.length() - 1).toString(), buf
713					.toString().substring(1, buf.length() - 1));
714		}
715	}
716
717	public void testPutString() {
718		String str = " ";
719
720		buf.clear();
721		for (int i = 0; i < buf.capacity(); i++) {
722			assertEquals(buf.position(), i);
723			str = "" + (char) i;
724			CharBuffer ret = buf.put(str);
725			assertEquals(buf.get(i), (char) i);
726			assertSame(ret, buf);
727		}
728		try {
729			buf.put(str);
730			fail("Should throw Exception"); //$NON-NLS-1$
731		} catch (BufferOverflowException e) {
732			// expected
733		}
734		try {
735			buf.put((String) null);
736			fail("Should throw Exception"); //$NON-NLS-1$
737		} catch (NullPointerException e) {
738			// expected
739		}
740	}
741
742	public void testPutStringintint() {
743		buf.clear();
744		String str = String.valueOf(new char[buf.capacity()]);
745
746		// Throw a BufferOverflowException and no character is transfered to
747		// CharBuffer
748		try {
749			buf.put(String.valueOf(new char[buf.capacity() + 1]), 0, buf
750					.capacity() + 1);
751			fail("Should throw Exception"); //$NON-NLS-1$
752		} catch (BufferOverflowException e) {
753			// expected
754		}
755		try {
756			buf.put((String) null, 0, buf.capacity() + 1);
757			fail("Should throw Exception"); //$NON-NLS-1$
758		} catch (NullPointerException e) {
759			// expected
760		}
761		assertEquals(0, buf.position());
762
763		buf.clear();
764		try {
765			buf.put(str, -1, str.length());
766			fail("Should throw Exception"); //$NON-NLS-1$
767		} catch (IndexOutOfBoundsException e) {
768			// expected
769		}
770		try {
771			buf.put(str, str.length() + 1, str.length() + 2);
772			fail("Should throw Exception"); //$NON-NLS-1$
773		} catch (IndexOutOfBoundsException e) {
774			// expected
775		}
776		try {
777			buf.put((String) null, -1, 0);
778			fail("Should throw Exception"); //$NON-NLS-1$
779		} catch (NullPointerException e) {
780			// expected
781		}
782		buf.put(str, str.length(), str.length());
783		assertEquals(buf.position(), 0);
784		try {
785			buf.put(str, 2, 1);
786			fail("Should throw Exception"); //$NON-NLS-1$
787		} catch (IndexOutOfBoundsException e) {
788			// expected
789		}
790		try {
791			buf.put(str, 2, str.length() + 1);
792			fail("Should throw Exception"); //$NON-NLS-1$
793		} catch (IndexOutOfBoundsException e) {
794			// expected
795		}
796		assertEquals(buf.position(), 0);
797
798		char array[] = new char[buf.capacity()];
799		loadTestData2(array, 0, array.length);
800		str = String.valueOf(array);
801
802		CharBuffer ret = buf.put(str, 0, str.length());
803		assertEquals(buf.position(), buf.capacity());
804		assertContentEquals(buf, str.toCharArray(), 0, str.length());
805		assertSame(ret, buf);
806	}
807
808	void loadTestData1(char array[], int offset, int length) {
809		for (int i = 0; i < length; i++) {
810			array[offset + i] = (char) i;
811		}
812	}
813
814	void loadTestData2(char array[], int offset, int length) {
815		for (int i = 0; i < length; i++) {
816			array[offset + i] = (char) (length - i);
817		}
818	}
819
820	void loadTestData1(CharBuffer buf) {
821		buf.clear();
822		for (int i = 0; i < buf.capacity(); i++) {
823			buf.put(i, (char) i);
824		}
825	}
826
827	void loadTestData2(CharBuffer buf) {
828		buf.clear();
829		for (int i = 0; i < buf.capacity(); i++) {
830			buf.put(i, (char) (buf.capacity() - i));
831		}
832	}
833
834	private void assertContentEquals(CharBuffer buf, char array[], int offset,
835			int length) {
836		for (int i = 0; i < length; i++) {
837			assertEquals(buf.get(i), array[offset + i]);
838		}
839	}
840
841	private void assertContentEquals(CharBuffer buf, CharBuffer other) {
842		assertEquals(buf.capacity(), other.capacity());
843		for (int i = 0; i < buf.capacity(); i++) {
844			assertEquals(buf.get(i), other.get(i));
845		}
846	}
847
848	private void assertContentLikeTestData1(CharBuffer buf, int startIndex,
849			char startValue, int length) {
850		char value = startValue;
851		for (int i = 0; i < length; i++) {
852			assertEquals(buf.get(startIndex + i), value);
853			value = (char) (value + 1);
854		}
855	}
856
857	public void testAppendSelf() throws Exception {
858		CharBuffer cb = CharBuffer.allocate(10);
859		CharBuffer cb2 = cb.duplicate();
860		cb.append(cb);
861		assertEquals(10, cb.position());
862		cb.clear();
863		assertEquals(cb2, cb);
864
865		cb.put("abc");
866		cb2 = cb.duplicate();
867		cb.append(cb);
868		assertEquals(10, cb.position());
869		cb.clear();
870		cb2.clear();
871		assertEquals(cb2, cb);
872
873		cb.put("edfg");
874		cb.clear();
875		cb2 = cb.duplicate();
876		cb.append(cb);
877		assertEquals(10, cb.position());
878		cb.clear();
879		cb2.clear();
880		assertEquals(cb, cb2);
881	}
882
883	public void testAppendOverFlow() throws IOException {
884		CharBuffer cb = CharBuffer.allocate(1);
885		CharSequence cs = "String";
886		cb.put('A');
887		try {
888			cb.append('C');
889			fail("should throw BufferOverflowException.");
890		} catch (BufferOverflowException ex) {
891			// expected;
892		}
893		try {
894			cb.append(cs);
895			fail("should throw BufferOverflowException.");
896		} catch (BufferOverflowException ex) {
897			// expected;
898		}
899		try {
900			cb.append(cs, 1, 2);
901			fail("should throw BufferOverflowException.");
902		} catch (BufferOverflowException ex) {
903			// expected;
904		}
905	}
906
907	public void testReadOnlyMap() throws IOException {
908		CharBuffer cb = CharBuffer.wrap("ABCDE").asReadOnlyBuffer();
909		CharSequence cs = "String";
910		try {
911			cb.append('A');
912			fail("should throw ReadOnlyBufferException.");
913		} catch (ReadOnlyBufferException ex) {
914			// expected;
915		}
916		try {
917			cb.append(cs);
918			fail("should throw ReadOnlyBufferException.");
919		} catch (ReadOnlyBufferException ex) {
920			// expected;
921		}
922		try {
923			cb.append(cs, 1, 2);
924			fail("should throw ReadOnlyBufferException.");
925		} catch (ReadOnlyBufferException ex) {
926			// expected;
927		}
928		cb.append(cs, 1, 1);
929	}
930
931	public void testAppendCNormal() throws IOException {
932		CharBuffer cb = CharBuffer.allocate(2);
933		cb.put('A');
934		assertSame(cb, cb.append('B'));
935		assertEquals('B', cb.get(1));
936	}
937
938	public void testAppendCharSequenceNormal() throws IOException {
939		CharBuffer cb = CharBuffer.allocate(10);
940		cb.put('A');
941		assertSame(cb, cb.append("String"));
942		assertEquals("AString", cb.flip().toString());
943		cb.append(null);
944		assertEquals("null", cb.flip().toString());
945	}
946
947	public void testAppendCharSequenceIINormal() throws IOException {
948		CharBuffer cb = CharBuffer.allocate(10);
949		cb.put('A');
950		assertSame(cb, cb.append("String", 1, 3));
951		assertEquals("Atr", cb.flip().toString());
952
953		cb.append(null, 0, 1);
954		assertEquals("n", cb.flip().toString());
955	}
956
957	public void testAppendCharSequenceII_IllegalArgument() throws IOException {
958		CharBuffer cb = CharBuffer.allocate(10);
959		cb.append("String", 0, 0);
960		cb.append("String", 2, 2);
961		try {
962			cb.append("String", -1, 1);
963			fail("should throw IndexOutOfBoundsException.");
964		} catch (IndexOutOfBoundsException ex) {
965			// expected;
966		}
967		try {
968			cb.append("String", -1, -1);
969			fail("should throw IndexOutOfBoundsException.");
970		} catch (IndexOutOfBoundsException ex) {
971			// expected;
972		}
973		try {
974			cb.append("String", 3, 2);
975			fail("should throw IndexOutOfBoundsException.");
976		} catch (IndexOutOfBoundsException ex) {
977			// expected;
978		}
979		try {
980			cb.append("String", 3, 0);
981			fail("should throw IndexOutOfBoundsException.");
982		} catch (IndexOutOfBoundsException ex) {
983			// expected;
984		}
985		try {
986			cb.append("String", 3, 110);
987			fail("should throw IndexOutOfBoundsException.");
988		} catch (IndexOutOfBoundsException ex) {
989			// expected;
990		}
991	}
992
993	public void testReadCharBuffer() throws IOException {
994		CharBuffer source = CharBuffer.wrap("String");
995		CharBuffer target = CharBuffer.allocate(10);
996		assertEquals(6, source.read(target));
997		assertEquals("String", target.flip().toString());
998		// return -1 when nothing to read
999		assertEquals(-1, source.read(target));
1000		// NullPointerException
1001		try {
1002			assertEquals(-1, source.read(null));
1003			fail("should throw NullPointerException.");
1004		} catch (NullPointerException ex) {
1005			// expected;
1006		}
1007
1008	}
1009
1010	public void testReadReadOnly() throws IOException {
1011		CharBuffer source = CharBuffer.wrap("String");
1012		CharBuffer target = CharBuffer.allocate(10).asReadOnlyBuffer();
1013		try {
1014			source.read(target);
1015			fail("should throw ReadOnlyBufferException.");
1016		} catch (ReadOnlyBufferException ex) {
1017			// expected;
1018		}
1019		// if target has no remaining, needn't to check the isReadOnly
1020		target.flip();
1021		assertEquals(0, source.read(target));
1022	}
1023
1024	public void testReadOverflow() throws IOException {
1025		CharBuffer source = CharBuffer.wrap("String");
1026		CharBuffer target = CharBuffer.allocate(1);
1027		assertEquals(1, source.read(target));
1028		assertEquals("S", target.flip().toString());
1029		assertEquals(1, source.position());
1030	}
1031
1032	public void testReadSelf() throws Exception {
1033		CharBuffer source = CharBuffer.wrap("abuffer");
1034		try {
1035			source.read(source);
1036			fail("should throw IAE.");
1037		} catch (IllegalArgumentException e) {
1038            //expected
1039		}
1040	}
1041
1042    public void testRead_scenario1() throws Exception {
1043        char[] charArray = new char[] { 'a', 'b' };
1044        CharBuffer charBuffer = CharBuffer.wrap(charArray);
1045        try {
1046            charBuffer.read(charBuffer);
1047            fail("should throw IllegalArgumentException");
1048        } catch (IllegalArgumentException e) {
1049            // expected
1050        }
1051        charBuffer.put(charArray);
1052        assertEquals(-1, charBuffer.read(charBuffer));
1053    }
1054
1055    public void testRead_scenario2() throws Exception {
1056        CharBuffer charBufferA = CharBuffer.allocate(0);
1057        CharBuffer allocateBuffer = CharBuffer.allocate(1);
1058        CharBuffer charBufferB = CharBuffer.wrap(allocateBuffer);
1059        assertEquals(-1, charBufferA.read(charBufferB));
1060
1061        allocateBuffer.append(allocateBuffer);
1062        charBufferB = CharBuffer.wrap(allocateBuffer);
1063        assertEquals(-1, charBufferA.read(charBufferB));
1064    }
1065
1066	public void testIsDirect() {
1067		assertFalse(buf.isDirect());
1068	}
1069
1070	public void testHasArray() {
1071		assertTrue(buf.hasArray());
1072	}
1073
1074	public void testOrder() {
1075		assertEquals(ByteOrder.nativeOrder(), buf.order());
1076	}
1077
1078	public void testIsReadOnly() {
1079		assertFalse(buf.isReadOnly());
1080	}
1081}
1082