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.tests.java.nio;
18 
19 import java.nio.BufferOverflowException;
20 import java.nio.CharBuffer;
21 import java.nio.ReadOnlyBufferException;
22 
23 public class WrappedCharBufferTest2 extends ReadOnlyCharBufferTest {
24     protected static final String TEST_STRING = "123456789abcdef12345";
25 
setUp()26     protected void setUp() throws Exception {
27         super.setUp();
28         buf = CharBuffer.wrap(TEST_STRING);
29         baseBuf = buf;
30     }
31 
tearDown()32     protected void tearDown() throws Exception {
33         super.tearDown();
34         baseBuf = null;
35         buf = null;
36     }
37 
testWrappedCharSequence_IllegalArg()38     public void testWrappedCharSequence_IllegalArg() {
39         String str = TEST_STRING;
40         try {
41             CharBuffer.wrap(str, -1, 0);
42             fail("Should throw Exception"); //$NON-NLS-1$
43         } catch (IndexOutOfBoundsException e) {
44             // expected
45         }
46         try {
47             CharBuffer.wrap(str, 21, 21);
48             fail("Should throw Exception"); //$NON-NLS-1$
49         } catch (IndexOutOfBoundsException e) {
50             // expected
51         }
52         try {
53             CharBuffer.wrap(str, 2, 1);
54             fail("Should throw Exception"); //$NON-NLS-1$
55         } catch (IndexOutOfBoundsException e) {
56             // expected
57         }
58         try {
59             CharBuffer.wrap(str, 0, 21);
60             fail("Should throw Exception"); //$NON-NLS-1$
61         } catch (IndexOutOfBoundsException e) {
62             // expected
63         }
64         try {
65             CharBuffer.wrap((String)null, -1, 21);
66             fail("Should throw Exception"); //$NON-NLS-1$
67         } catch (NullPointerException e) {
68             // expected
69         }
70     }
71 
testArray()72     public void testArray() {
73         try {
74             buf.array();
75             fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
76         } catch (UnsupportedOperationException e) {
77         }
78     }
79 
testPutcharArrayintint()80     public void testPutcharArrayintint() {
81         char array[] = new char[1];
82         try {
83             buf.put(array, 0, array.length);
84             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
85         } catch (ReadOnlyBufferException e) {
86             // expected
87         }
88         try {
89             buf.put((char[]) null, 0, 1);
90             fail("Should throw NullPointerException"); //$NON-NLS-1$
91         } catch (ReadOnlyBufferException expected) {
92         } catch (NullPointerException expected) {
93         }
94         try {
95             buf.put(new char[buf.capacity() + 1], 0, buf.capacity() + 1);
96             fail("Should throw BufferOverflowException"); //$NON-NLS-1$
97         } catch (ReadOnlyBufferException expected) {
98         } catch (BufferOverflowException expected) {
99         }
100         try {
101             buf.put(array, -1, array.length);
102             fail("Should throw IndexOutOfBoundsException"); //$NON-NLS-1$
103         } catch (ReadOnlyBufferException expected) {
104         } catch (IndexOutOfBoundsException expected) {
105         }
106     }
107 
testPutCharBuffer()108     public void testPutCharBuffer() {
109         CharBuffer other = CharBuffer.allocate(1);
110         try {
111             buf.put(other);
112             fail();
113         } catch (ReadOnlyBufferException expected) {
114         }
115         try {
116             buf.put((CharBuffer) null);
117             fail();
118         } catch (ReadOnlyBufferException expected) {
119         } catch (NullPointerException expected) {
120         }
121         try {
122             buf.put(buf);
123             fail();
124         } catch (ReadOnlyBufferException expected) {
125         } catch (IllegalArgumentException expected) {
126         }
127     }
128 }
129