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.tests.java.nio;
17
18import java.nio.DoubleBuffer;
19import java.nio.ReadOnlyBufferException;
20
21public class ReadOnlyDoubleBufferTest extends DoubleBufferTest {
22
23	protected void setUp() throws Exception {
24		super.setUp();
25		buf = buf.asReadOnlyBuffer();
26		baseBuf = buf;
27	}
28
29	protected void tearDown() throws Exception {
30		super.tearDown();
31	}
32
33    public void testIsReadOnly() {
34        assertTrue(buf.isReadOnly());
35    }
36
37    public void testHasArray() {
38        assertFalse(buf.hasArray());
39    }
40
41    public void testArray() {
42        try {
43            buf.array();
44            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
45        } catch (ReadOnlyBufferException e) {
46        }
47    }
48
49    public void testHashCode() {
50        DoubleBuffer duplicate = buf.duplicate();
51        assertEquals(buf.hashCode(), duplicate.hashCode());
52    }
53
54    public void testArrayOffset() {
55        try {
56            buf.arrayOffset();
57            fail("Should throw Exception"); //$NON-NLS-1$
58        } catch (UnsupportedOperationException e) {
59        }
60    }
61
62    public void testCompact() {
63        try {
64            buf.compact();
65            fail("Should throw Exception"); //$NON-NLS-1$
66        } catch (ReadOnlyBufferException e) {
67            // expected
68        }
69    }
70
71    public void testPutdouble() {
72        try {
73            buf.put(0);
74            fail("Should throw Exception"); //$NON-NLS-1$
75        } catch (ReadOnlyBufferException e) {
76            // expected
77        }
78    }
79
80    public void testPutdoubleArray() {
81        double array[] = new double[1];
82        try {
83            buf.put(array);
84            fail("Should throw Exception"); //$NON-NLS-1$
85        } catch (ReadOnlyBufferException e) {
86            // expected
87        }
88        try {
89            buf.put((double[]) null);
90            fail("Should throw Exception"); //$NON-NLS-1$
91        } catch (NullPointerException e) {
92            // expected
93        }
94    }
95
96    public void testPutdoubleArrayintint() {
97        double array[] = new double[1];
98        try {
99            buf.put(array, 0, array.length);
100            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
101        } catch (ReadOnlyBufferException e) {
102            // expected
103        }
104        try {
105            buf.put((double[]) null, 0, 1);
106            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
107        } catch (ReadOnlyBufferException e) {
108            // expected
109        }
110        try {
111            buf.put(new double[buf.capacity() + 1], 0, buf.capacity() + 1);
112            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
113        } catch (ReadOnlyBufferException e) {
114            // expected
115        }
116        try {
117            buf.put(array, -1, array.length);
118            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
119        } catch (ReadOnlyBufferException e) {
120            // expected
121        }
122    }
123
124    public void testPutDoubleBuffer() {
125        DoubleBuffer other = DoubleBuffer.allocate(1);
126        try {
127            buf.put(other);
128            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
129        } catch (ReadOnlyBufferException e) {
130            // expected
131        }
132        try {
133            buf.put((DoubleBuffer) null);
134            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
135        } catch (ReadOnlyBufferException e) {
136            // expected
137        }
138        try {
139            buf.put(buf);
140            fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
141        } catch (IllegalArgumentException e) {
142            // expected
143        }
144    }
145
146    public void testPutintdouble() {
147        try {
148            buf.put(0, (double) 0);
149            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
150        } catch (ReadOnlyBufferException e) {
151            // expected
152        }
153        try {
154            buf.put(-1, (double) 0);
155            fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
156        } catch (ReadOnlyBufferException e) {
157            // expected
158        }
159    }
160}
161