1dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes/* Licensed to the Apache Software Foundation (ASF) under one or more
2dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * contributor license agreements.  See the NOTICE file distributed with
3dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * this work for additional information regarding copyright ownership.
4dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0
5dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * (the "License"); you may not use this file except in compliance with
6dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * the License.  You may obtain a copy of the License at
7dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes *
8dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
9dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes *
10dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * Unless required by applicable law or agreed to in writing, software
11dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
12dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * See the License for the specific language governing permissions and
14dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes * limitations under the License.
15dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes */
16dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
17dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughespackage org.apache.harmony.nio.tests.java.nio;
18dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
19dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport java.nio.BufferOverflowException;
20dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport java.nio.CharBuffer;
21dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughesimport java.nio.ReadOnlyBufferException;
22dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
23dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughespublic class WrappedCharBufferTest2 extends ReadOnlyCharBufferTest {
24dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    protected static final String TEST_STRING = "123456789abcdef12345";
25dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
26dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    protected void setUp() throws Exception {
27dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        super.setUp();
28dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        buf = CharBuffer.wrap(TEST_STRING);
29dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        baseBuf = buf;
30dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
31dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
32dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    protected void tearDown() throws Exception {
33dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        super.tearDown();
34dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        baseBuf = null;
35dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        buf = null;
36dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
37dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
38dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    public void testWrappedCharSequence_IllegalArg() {
39dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        String str = TEST_STRING;
40dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
41dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            CharBuffer.wrap(str, -1, 0);
42dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw Exception"); //$NON-NLS-1$
43dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (IndexOutOfBoundsException e) {
44dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
45dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
46dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
47dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            CharBuffer.wrap(str, 21, 21);
48dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw Exception"); //$NON-NLS-1$
49dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (IndexOutOfBoundsException e) {
50dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
51dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
52dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
53dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            CharBuffer.wrap(str, 2, 1);
54dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw Exception"); //$NON-NLS-1$
55dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (IndexOutOfBoundsException e) {
56dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
57dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
58dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
59dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            CharBuffer.wrap(str, 0, 21);
60dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw Exception"); //$NON-NLS-1$
61dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (IndexOutOfBoundsException e) {
62dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
63dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
64dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
65dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            CharBuffer.wrap((String)null, -1, 21);
66dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw Exception"); //$NON-NLS-1$
67dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (NullPointerException e) {
68dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
69dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
70dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
71dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
72dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    public void testArray() {
73dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
74dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.array();
75dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
76dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (UnsupportedOperationException e) {
77dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
78dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
79dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
80dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    public void testPutcharArrayintint() {
81dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        char array[] = new char[1];
82dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
83dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put(array, 0, array.length);
84dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
85dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (ReadOnlyBufferException e) {
86dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            // expected
87dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
88dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
89dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put((char[]) null, 0, 1);
90dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw NullPointerException"); //$NON-NLS-1$
91dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (ReadOnlyBufferException expected) {
92dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (NullPointerException expected) {
93dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
94dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
95dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put(new char[buf.capacity() + 1], 0, buf.capacity() + 1);
96dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw BufferOverflowException"); //$NON-NLS-1$
97dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (ReadOnlyBufferException expected) {
98dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (BufferOverflowException expected) {
99dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
100dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
101dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put(array, -1, array.length);
102dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            fail("Should throw IndexOutOfBoundsException"); //$NON-NLS-1$
103dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (ReadOnlyBufferException expected) {
104dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        } catch (IndexOutOfBoundsException expected) {
105dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
106dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
107dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes
108dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    public void testPutCharBuffer() {
109dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        CharBuffer other = CharBuffer.allocate(1);
110dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
111dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put(other);
112105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes            fail();
113105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes        } catch (ReadOnlyBufferException expected) {
114dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
115dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
116dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put((CharBuffer) null);
117105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes            fail();
118105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes        } catch (ReadOnlyBufferException expected) {
119105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes        } catch (NullPointerException expected) {
120dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
121dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        try {
122dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes            buf.put(buf);
123105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes            fail();
124105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes        } catch (ReadOnlyBufferException expected) {
125105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes        } catch (IllegalArgumentException expected) {
126dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes        }
127dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes    }
128dce2b2fa9d6b26414a8d5a55918e4d7ca2ab1baaElliott Hughes}
129