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
17package libcore.java.nio;
18
19import java.nio.ByteBuffer;
20import java.nio.ReadOnlyBufferException;
21import junit.framework.TestCase;
22
23public final class NoArrayTest extends TestCase {
24    public void testReadOnly() {
25        // A read-only buffer must not expose its array.
26        assertNoArray(ByteBuffer.wrap(new byte[32]).asReadOnlyBuffer());
27        assertNoArray(ByteBuffer.allocate(32).asReadOnlyBuffer());
28        assertNoArray(ByteBuffer.allocateDirect(32).asReadOnlyBuffer());
29    }
30
31    private void assertNoArray(ByteBuffer buf) {
32        assertFalse(buf.hasArray());
33        try {
34            buf.array();
35            fail();
36        } catch (ReadOnlyBufferException expected) {
37        } catch (UnsupportedOperationException expected) {
38        }
39        try {
40            buf.arrayOffset();
41            fail();
42        } catch (ReadOnlyBufferException expected) {
43        } catch (UnsupportedOperationException expected) {
44        }
45    }
46}
47