Home | History | Annotate | Download | only in nio
      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 package org.apache.harmony.tests.java.nio;
     17 
     18 import java.nio.ByteBuffer;
     19 import java.nio.ByteOrder;
     20 import java.nio.DirectByteBuffer;
     21 import java.nio.LongBuffer;
     22 import java.nio.NIOAccess;
     23 import libcore.io.SizeOf;
     24 
     25 public class DirectLongBufferTest extends LongBufferTest {
     26     public void setUp(){
     27         buf = ByteBuffer.allocateDirect(BUFFER_LENGTH*8).asLongBuffer();
     28         loadTestData1(buf);
     29         baseBuf = buf;
     30     }
     31 
     32     public void tearDown(){
     33         buf = null;
     34         baseBuf = null;
     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 UnsupportedOperationException"); //$NON-NLS-1$
     45         } catch (UnsupportedOperationException e) {
     46         }
     47     }
     48 
     49     public void testArrayOffset() {
     50         try {
     51             buf.arrayOffset();
     52             fail("Should throw UnsupportedOperationException"); //$NON-NLS-1$
     53         } catch (UnsupportedOperationException e) {
     54             //expected
     55         }
     56     }
     57 
     58     // http://b/28964300
     59     public void testJNIAccessByAddress() throws Exception {
     60         DirectByteBuffer directByteBuffer = (DirectByteBuffer) ByteBuffer.allocateDirect(10);
     61         directByteBuffer.put((byte)'a');
     62         LongBuffer longBuffer = directByteBuffer.asLongBuffer();
     63         long byteBufferBasePointer = NIOAccess.getBasePointer(directByteBuffer);
     64         long longBufferBasePointer = NIOAccess.getBasePointer(longBuffer);
     65         assertEquals(byteBufferBasePointer, longBufferBasePointer);
     66 
     67         // Check if the NIOAccess method adds up the current position value.
     68         longBuffer.put(1L);
     69         assertEquals(longBufferBasePointer + SizeOf.LONG, NIOAccess.getBasePointer(longBuffer));
     70     }
     71 
     72     public void testIsDirect() {
     73         assertTrue(buf.isDirect());
     74     }
     75 
     76     public void testOrder() {
     77         assertEquals(ByteOrder.BIG_ENDIAN, buf.order());
     78     }
     79 }
     80