001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.harmony.nio.tests.java.nio;
017:
018: import java.nio.CharBuffer;
019: import java.nio.ReadOnlyBufferException;
020:
021: public class ReadOnlyCharBufferTest extends CharBufferTest {
022:
023: protected void setUp() throws Exception {
024: super .setUp();
025: loadTestData1(buf);
026: buf = buf.asReadOnlyBuffer();
027: baseBuf = buf;
028: }
029:
030: protected void tearDown() throws Exception {
031: buf = null;
032: baseBuf = null;
033: super .tearDown();
034: }
035:
036: public void testIsReadOnly() {
037: assertTrue(buf.isReadOnly());
038: }
039:
040: public void testHasArray() {
041: assertFalse(buf.hasArray());
042: }
043:
044: public void testArray() {
045: try {
046: buf.array();
047: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
048: } catch (ReadOnlyBufferException e) {
049: }
050: }
051:
052: public void testHashCode() {
053: CharBuffer duplicate = buf.duplicate();
054: assertEquals(buf.hashCode(), duplicate.hashCode());
055: }
056:
057: public void testArrayOffset() {
058: try {
059: buf.arrayOffset();
060: fail("Should throw Exception"); //$NON-NLS-1$
061: } catch (UnsupportedOperationException e) {
062: }
063: }
064:
065: public void testCompact() {
066: try {
067: buf.compact();
068: fail("Should throw Exception"); //$NON-NLS-1$
069: } catch (ReadOnlyBufferException e) {
070: // expected
071: }
072: }
073:
074: public void testPutchar() {
075: try {
076: buf.put((char) 0);
077: fail("Should throw Exception"); //$NON-NLS-1$
078: } catch (ReadOnlyBufferException e) {
079: // expected
080: }
081: }
082:
083: public void testPutcharArray() {
084: char array[] = new char[1];
085: try {
086: buf.put(array);
087: fail("Should throw Exception"); //$NON-NLS-1$
088: } catch (ReadOnlyBufferException e) {
089: // expected
090: }
091: try {
092: buf.put((char[]) null);
093: fail("Should throw Exception"); //$NON-NLS-1$
094: } catch (NullPointerException e) {
095: // expected
096: }
097: }
098:
099: public void testPutcharArrayintint() {
100: char array[] = new char[1];
101: try {
102: buf.put(array, 0, array.length);
103: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
104: } catch (ReadOnlyBufferException e) {
105: // expected
106: }
107: try {
108: buf.put((char[]) null, 0, 1);
109: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
110: } catch (ReadOnlyBufferException e) {
111: // expected
112: }
113: try {
114: buf
115: .put(new char[buf.capacity() + 1], 0, buf
116: .capacity() + 1);
117: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
118: } catch (ReadOnlyBufferException e) {
119: // expected
120: }
121: try {
122: buf.put(array, -1, array.length);
123: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
124: } catch (ReadOnlyBufferException e) {
125: // expected
126: }
127: }
128:
129: public void testPutCharBuffer() {
130: CharBuffer other = CharBuffer.allocate(1);
131: try {
132: buf.put(other);
133: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
134: } catch (ReadOnlyBufferException e) {
135: // expected
136: }
137: try {
138: buf.put((CharBuffer) null);
139: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
140: } catch (ReadOnlyBufferException e) {
141: // expected
142: }
143: try {
144: buf.put(buf);
145: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
146: } catch (ReadOnlyBufferException e) {
147: // expected
148: }
149: }
150:
151: public void testPutintchar() {
152: try {
153: buf.put(0, (char) 0);
154: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
155: } catch (ReadOnlyBufferException e) {
156: // expected
157: }
158: try {
159: buf.put(-1, (char) 0);
160: fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
161: } catch (ReadOnlyBufferException e) {
162: // expected
163: }
164: }
165:
166: public void testPutStringintint() {
167: buf.clear();
168: String str = String.valueOf(new char[buf.capacity()]);
169: try {
170: buf.put(str, 0, str.length());
171: fail("Should throw Exception"); //$NON-NLS-1$
172: } catch (ReadOnlyBufferException e) {
173: // expected
174: }
175: try {
176: buf.put((String) null, 0, 0);
177: fail("Should throw Exception"); //$NON-NLS-1$
178: } catch (NullPointerException e) {
179: // expected
180: }
181: try {
182: buf.put(str, -1, str.length());
183: fail("Should throw Exception"); //$NON-NLS-1$
184: } catch (IndexOutOfBoundsException e) {
185: // expected
186: }
187: String longStr = String.valueOf(new char[buf.capacity() + 1]);
188: try {
189: buf.put(longStr, 0, longStr.length());
190: fail("Should throw Exception"); //$NON-NLS-1$
191: } catch (ReadOnlyBufferException e) {
192: // expected
193: }
194: }
195:
196: public void testPutString() {
197: String str = " ";
198: try {
199: buf.put(str);
200: fail("Should throw Exception"); //$NON-NLS-1$
201: } catch (ReadOnlyBufferException e) {
202: // expected
203: }
204: try {
205: buf.put((String) null);
206: fail("Should throw Exception"); //$NON-NLS-1$
207: } catch (NullPointerException e) {
208: // expected
209: }
210: }
211: }
|