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.CharBuffer;
     19 import java.nio.ReadOnlyBufferException;
     20 
     21 public class ReadOnlyCharBufferTest extends CharBufferTest {
     22 
     23     protected void setUp() throws Exception {
     24         super.setUp();
     25         loadTestData1(buf);
     26         buf = buf.asReadOnlyBuffer();
     27         baseBuf = buf;
     28     }
     29 
     30     protected void tearDown() throws Exception {
     31         buf = null;
     32         baseBuf = null;
     33         super.tearDown();
     34     }
     35 
     36     public void testIsReadOnly() {
     37         assertTrue(buf.isReadOnly());
     38     }
     39 
     40     public void testHasArray() {
     41         assertFalse(buf.hasArray());
     42     }
     43 
     44     public void testArray() {
     45         try {
     46             buf.array();
     47             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
     48         } catch (ReadOnlyBufferException e) {
     49         }
     50     }
     51 
     52     public void testHashCode() {
     53         CharBuffer duplicate = buf.duplicate();
     54         assertEquals(buf.hashCode(), duplicate.hashCode());
     55     }
     56 
     57     public void testArrayOffset() {
     58         try {
     59             buf.arrayOffset();
     60             fail("Should throw Exception"); //$NON-NLS-1$
     61         } catch (UnsupportedOperationException e) {
     62         }
     63     }
     64 
     65     public void testCompact() {
     66         try {
     67             buf.compact();
     68             fail("Should throw Exception"); //$NON-NLS-1$
     69         } catch (ReadOnlyBufferException e) {
     70             // expected
     71         }
     72     }
     73 
     74     public void testPutchar() {
     75         try {
     76             buf.put((char) 0);
     77             fail("Should throw Exception"); //$NON-NLS-1$
     78         } catch (ReadOnlyBufferException e) {
     79             // expected
     80         }
     81     }
     82 
     83     public void testPutcharArray() {
     84         char array[] = new char[1];
     85         try {
     86             buf.put(array);
     87             fail("Should throw Exception"); //$NON-NLS-1$
     88         } catch (ReadOnlyBufferException e) {
     89             // expected
     90         }
     91         try {
     92             buf.put((char[]) null);
     93             fail("Should throw Exception"); //$NON-NLS-1$
     94         } catch (NullPointerException e) {
     95             // expected
     96         }
     97     }
     98 
     99     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.put(new char[buf.capacity() + 1], 0, buf.capacity() + 1);
    115             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    116         } catch (ReadOnlyBufferException e) {
    117             // expected
    118         }
    119         try {
    120             buf.put(array, -1, array.length);
    121             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    122         } catch (ReadOnlyBufferException e) {
    123             // expected
    124         }
    125     }
    126 
    127     public void testPutCharBuffer() {
    128         CharBuffer other = CharBuffer.allocate(1);
    129         try {
    130             buf.put(other);
    131             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    132         } catch (ReadOnlyBufferException e) {
    133             // expected
    134         }
    135         try {
    136             buf.put((CharBuffer) null);
    137             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    138         } catch (ReadOnlyBufferException e) {
    139             // expected
    140         }
    141         try {
    142             buf.put(buf);
    143             fail("Should throw IllegalArgumentException"); //$NON-NLS-1$
    144         } catch (IllegalArgumentException e) {
    145             // expected
    146         }
    147     }
    148 
    149     public void testPutintchar() {
    150         try {
    151             buf.put(0, (char) 0);
    152             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    153         } catch (ReadOnlyBufferException e) {
    154             // expected
    155         }
    156         try {
    157             buf.put(-1, (char) 0);
    158             fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$
    159         } catch (ReadOnlyBufferException e) {
    160             // expected
    161         }
    162     }
    163 
    164     public void testPutStringintint() {
    165         buf.clear();
    166         String str = String.valueOf(new char[buf.capacity()]);
    167         try {
    168             buf.put(str, 0, str.length());
    169             fail("Should throw Exception"); //$NON-NLS-1$
    170         } catch (ReadOnlyBufferException e) {
    171             // expected
    172         }
    173         try {
    174             buf.put((String) null, 0, 0);
    175             fail("Should throw Exception"); //$NON-NLS-1$
    176         } catch (ReadOnlyBufferException expected) {
    177         } catch (NullPointerException expected) {
    178         }
    179         try {
    180             buf.put(str, -1, str.length());
    181             fail("Should throw Exception"); //$NON-NLS-1$
    182         } catch (IndexOutOfBoundsException expected) {
    183         } catch (NullPointerException expected) {
    184         }
    185         String longStr = String.valueOf(new char[buf.capacity()+1]);
    186         try {
    187             buf.put(longStr, 0, longStr.length());
    188             fail("Should throw Exception"); //$NON-NLS-1$
    189         } catch (ReadOnlyBufferException e) {
    190             // expected
    191         }
    192     }
    193 
    194     public void testPutString() {
    195         String str = " ";
    196         try {
    197             buf.put(str);
    198             fail("Should throw Exception"); //$NON-NLS-1$
    199         } catch (ReadOnlyBufferException e) {
    200             // expected
    201         }
    202         try {
    203             buf.put((String)null);
    204             fail("Should throw Exception"); //$NON-NLS-1$
    205         } catch (NullPointerException e) {
    206             // expected
    207         }
    208     }
    209 }
    210