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 */
16package org.apache.harmony.nio.tests.java.nio;
17
18import dalvik.annotation.TestLevel;
19import dalvik.annotation.TestTargetNew;
20import dalvik.annotation.TestTargetClass;
21
22import java.nio.ReadOnlyBufferException;
23import java.nio.ShortBuffer;
24
25@TestTargetClass(java.nio.ShortBuffer.class)
26public class ReadOnlyShortBufferTest extends ShortBufferTest {
27    protected void setUp() throws Exception {
28        super.setUp();
29        buf = buf.asReadOnlyBuffer();
30        baseBuf = buf;
31    }
32
33    protected void tearDown() throws Exception {
34        super.tearDown();
35    }
36
37    @TestTargetNew(
38        level = TestLevel.PARTIAL_COMPLETE,
39        notes = "Verifies isReadOnly method for read only ShortBuffer.",
40        method = "isReadOnly",
41        args = {}
42    )
43    public void testIsReadOnly() {
44        assertTrue(buf.isReadOnly());
45    }
46
47    @TestTargetNew(
48        level = TestLevel.PARTIAL_COMPLETE,
49        notes = "Verifies hasArray method for read only ShortBuffer.",
50        method = "hasArray",
51        args = {}
52    )
53    public void testHasArray() {
54        assertFalse(buf.hasArray());
55    }
56
57    @TestTargetNew(
58        level = TestLevel.PARTIAL_COMPLETE,
59        notes = "Verifies ReadOnlyBufferException.",
60        method = "array",
61        args = {}
62    )
63    public void testArray() {
64        try {
65            buf.array();
66            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
67        } catch (ReadOnlyBufferException e) {
68            //expected
69        }
70    }
71
72    @TestTargetNew(
73        level = TestLevel.PARTIAL_COMPLETE,
74        notes = "",
75        method = "hashCode",
76        args = {}
77    )
78    public void testHashCode() {
79        ShortBuffer duplicate = buf.duplicate();
80        assertEquals(buf.hashCode(), duplicate.hashCode());
81    }
82
83    @TestTargetNew(
84        level = TestLevel.PARTIAL_COMPLETE,
85        notes = "Verifies ReadOnlyBufferException.",
86        method = "arrayOffset",
87        args = {}
88    )
89    public void testArrayOffset() {
90        try {
91            buf.arrayOffset();
92            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
93        } catch (ReadOnlyBufferException e) {
94            //expected
95        }
96    }
97
98    @TestTargetNew(
99        level = TestLevel.PARTIAL_COMPLETE,
100        notes = "Verifies ReadOnlyBufferException.",
101        method = "compact",
102        args = {}
103    )
104    public void testCompact() {
105        try {
106            buf.compact();
107            fail("Should throw Exception"); //$NON-NLS-1$
108        } catch (ReadOnlyBufferException e) {
109            // expected
110        }
111    }
112
113    @TestTargetNew(
114        level = TestLevel.PARTIAL_COMPLETE,
115        notes = "Verifies ReadOnlyBufferException.",
116        method = "put",
117        args = {short.class}
118    )
119    public void testPutshort() {
120        try {
121            buf.put((short)0);
122            fail("Should throw Exception"); //$NON-NLS-1$
123        } catch (ReadOnlyBufferException e) {
124            // expected
125        }
126    }
127
128    @TestTargetNew(
129        level = TestLevel.PARTIAL_COMPLETE,
130        notes = "Verifies ReadOnlyBufferException.",
131        method = "put",
132        args = {short[].class}
133    )
134    public void testPutshortArray() {
135        short array[] = new short[1];
136        try {
137            buf.put(array);
138            fail("Should throw Exception"); //$NON-NLS-1$
139        } catch (ReadOnlyBufferException e) {
140            // expected
141        }
142        try {
143            buf.put((short[]) null);
144            fail("Should throw Exception"); //$NON-NLS-1$
145        } catch (NullPointerException e) {
146            // expected
147        }
148    }
149
150    @TestTargetNew(
151        level = TestLevel.PARTIAL_COMPLETE,
152        notes = "Verifies ReadOnlyBufferException.",
153        method = "put",
154        args = {short[].class, int.class, int.class}
155    )
156    public void testPutshortArrayintint() {
157        short array[] = new short[1];
158        try {
159            buf.put(array, 0, array.length);
160            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
161        } catch (ReadOnlyBufferException e) {
162            // expected
163        }
164        try {
165            buf.put((short[]) null, 0, 1);
166            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
167        } catch (ReadOnlyBufferException e) {
168            // expected
169        }
170        try {
171            buf.put(new short[buf.capacity() + 1], 0, buf.capacity() + 1);
172            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
173        } catch (ReadOnlyBufferException e) {
174            // expected
175        }
176        try {
177            buf.put(array, -1, array.length);
178            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
179        } catch (ReadOnlyBufferException e) {
180            // expected
181        }
182    }
183
184    @TestTargetNew(
185        level = TestLevel.PARTIAL_COMPLETE,
186        notes = "Verifies ReadOnlyBufferException.",
187        method = "put",
188        args = {java.nio.ShortBuffer.class}
189    )
190    public void testPutShortBuffer() {
191        ShortBuffer other = ShortBuffer.allocate(1);
192        try {
193            buf.put(other);
194            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
195        } catch (ReadOnlyBufferException e) {
196            // expected
197        }
198        try {
199            buf.put((ShortBuffer) null);
200            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
201        } catch (ReadOnlyBufferException e) {
202            // expected
203        }
204        try {
205            buf.put(buf);
206            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
207        } catch (ReadOnlyBufferException e) {
208            // expected
209        }
210    }
211
212    @TestTargetNew(
213        level = TestLevel.PARTIAL_COMPLETE,
214        notes = "Verifies ReadOnlyBufferException.",
215        method = "put",
216        args = {int.class, short.class}
217    )
218    public void testPutintshort() {
219        try {
220            buf.put(0, (short) 0);
221            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
222        } catch (ReadOnlyBufferException e) {
223            // expected
224        }
225        try {
226            buf.put(-1, (short) 0);
227            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
228        } catch (ReadOnlyBufferException e) {
229            // expected
230        }
231    }
232}
233