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 17 package org.apache.harmony.nio.tests.java.nio; 18 19 import java.nio.Buffer; 20 import java.nio.ByteBuffer; 21 import java.nio.InvalidMarkException; 22 23 import junit.framework.TestCase; 24 25 /** 26 * Tests a java.nio.Buffer instance. 27 */ 28 public class AbstractBufferTest extends TestCase { 29 30 protected Buffer baseBuf; 31 32 protected void setUp() throws Exception{ 33 super.setUp(); 34 baseBuf = ByteBuffer.allocate(10); 35 } 36 37 protected void tearDown() throws Exception{ 38 super.tearDown(); 39 } 40 41 public void testCapacity() { 42 assertTrue(0 <= baseBuf.position() && baseBuf.position() <= baseBuf.limit() 43 && baseBuf.limit() <= baseBuf.capacity()); 44 } 45 46 public void testClear() { 47 // save state 48 int oldPosition = baseBuf.position(); 49 int oldLimit = baseBuf.limit(); 50 51 Buffer ret = baseBuf.clear(); 52 assertSame(ret, baseBuf); 53 assertEquals(baseBuf.position(), 0); 54 assertEquals(baseBuf.limit(), baseBuf.capacity()); 55 try { 56 baseBuf.reset(); 57 fail("Should throw Exception"); //$NON-NLS-1$S 58 } catch (InvalidMarkException e) { 59 // expected 60 } 61 62 // restore state 63 baseBuf.limit(oldLimit); 64 baseBuf.position(oldPosition); 65 } 66 67 public void testFlip() { 68 // save state 69 int oldPosition = baseBuf.position(); 70 int oldLimit = baseBuf.limit(); 71 72 Buffer ret = baseBuf.flip(); 73 assertSame(ret, baseBuf); 74 assertEquals(baseBuf.position(), 0); 75 assertEquals(baseBuf.limit(), oldPosition); 76 try { 77 baseBuf.reset(); 78 fail("Should throw Exception"); //$NON-NLS-1$ 79 } catch (InvalidMarkException e) { 80 // expected 81 } 82 83 // restore state 84 baseBuf.limit(oldLimit); 85 baseBuf.position(oldPosition); 86 } 87 88 public void testHasRemaining() { 89 // save state 90 int oldPosition = baseBuf.position(); 91 int oldLimit = baseBuf.limit(); 92 93 assertEquals(baseBuf.hasRemaining(), baseBuf.position() < baseBuf.limit()); 94 baseBuf.position(baseBuf.limit()); 95 assertFalse(baseBuf.hasRemaining()); 96 97 // restore state 98 baseBuf.limit(oldLimit); 99 baseBuf.position(oldPosition); 100 } 101 102 public void testIsReadOnly() { 103 baseBuf.isReadOnly(); 104 } 105 106 /* 107 * Class under test for int limit() 108 */ 109 public void testLimit() { 110 assertTrue(0 <= baseBuf.position() && baseBuf.position() <= baseBuf.limit() 111 && baseBuf.limit() <= baseBuf.capacity()); 112 } 113 114 /* 115 * Class under test for Buffer limit(int) 116 */ 117 public void testLimitint() { 118 // save state 119 int oldPosition = baseBuf.position(); 120 int oldLimit = baseBuf.limit(); 121 122 Buffer ret = baseBuf.limit(baseBuf.limit()); 123 assertSame(ret, baseBuf); 124 125 baseBuf.mark(); 126 baseBuf.limit(baseBuf.capacity()); 127 assertEquals(baseBuf.limit(), baseBuf.capacity()); 128 // position should not change 129 assertEquals(baseBuf.position(), oldPosition); 130 // mark should be valid 131 baseBuf.reset(); 132 133 if (baseBuf.capacity() > 0) { 134 baseBuf.limit(baseBuf.capacity()); 135 baseBuf.position(baseBuf.capacity()); 136 baseBuf.mark(); 137 baseBuf.limit(baseBuf.capacity() - 1); 138 // position should be the new limit 139 assertEquals(baseBuf.position(), baseBuf.limit()); 140 // mark should be invalid 141 try { 142 baseBuf.reset(); 143 fail("Should throw Exception"); //$NON-NLS-1$ 144 } catch (InvalidMarkException e) { 145 // expected 146 } 147 } 148 149 try { 150 baseBuf.limit(-1); 151 fail("Should throw Exception"); //$NON-NLS-1$ 152 } catch (IllegalArgumentException e) { 153 // expected 154 } 155 try { 156 baseBuf.limit(baseBuf.capacity() + 1); 157 fail("Should throw Exception"); //$NON-NLS-1$ 158 } catch (IllegalArgumentException e) { 159 // expected 160 } 161 162 // restore state 163 baseBuf.limit(oldLimit); 164 baseBuf.position(oldPosition); 165 } 166 167 public void testMark() { 168 // save state 169 int oldPosition = baseBuf.position(); 170 int oldLimit = baseBuf.limit(); 171 172 Buffer ret = baseBuf.mark(); 173 assertSame(ret, baseBuf); 174 175 baseBuf.mark(); 176 baseBuf.position(baseBuf.limit()); 177 baseBuf.reset(); 178 assertEquals(baseBuf.position(), oldPosition); 179 180 baseBuf.mark(); 181 baseBuf.position(baseBuf.limit()); 182 baseBuf.reset(); 183 assertEquals(baseBuf.position(), oldPosition); 184 185 // restore state 186 baseBuf.limit(oldLimit); 187 baseBuf.position(oldPosition); 188 } 189 190 /* 191 * Class under test for int position() 192 */ 193 public void testPosition() { 194 assertTrue(0 <= baseBuf.position() && baseBuf.position() <= baseBuf.limit() 195 && baseBuf.limit() <= baseBuf.capacity()); 196 } 197 198 /* 199 * Class under test for Buffer position(int) 200 */ 201 public void testPositionint() { 202 // save state 203 int oldPosition = baseBuf.position(); 204 int oldLimit = baseBuf.limit(); 205 206 try { 207 baseBuf.position(-1); 208 fail("Should throw Exception"); //$NON-NLS-1$ 209 } catch (IllegalArgumentException e) { 210 // expected 211 } 212 try { 213 baseBuf.position(baseBuf.limit() + 1); 214 fail("Should throw Exception"); //$NON-NLS-1$ 215 } catch (IllegalArgumentException e) { 216 // expected 217 } 218 219 baseBuf.mark(); 220 baseBuf.position(baseBuf.position()); 221 baseBuf.reset(); 222 assertEquals(baseBuf.position(), oldPosition); 223 224 baseBuf.position(0); 225 assertEquals(baseBuf.position(), 0); 226 baseBuf.position(baseBuf.limit()); 227 assertEquals(baseBuf.position(), baseBuf.limit()); 228 229 if (baseBuf.capacity() > 0) { 230 baseBuf.limit(baseBuf.capacity()); 231 baseBuf.position(baseBuf.limit()); 232 baseBuf.mark(); 233 baseBuf.position(baseBuf.limit() - 1); 234 assertEquals(baseBuf.position(), baseBuf.limit() - 1); 235 // mark should be invalid 236 try { 237 baseBuf.reset(); 238 fail("Should throw Exception"); //$NON-NLS-1$ 239 } catch (InvalidMarkException e) { 240 // expected 241 } 242 } 243 244 Buffer ret = baseBuf.position(0); 245 assertSame(ret, baseBuf); 246 247 // restore state 248 baseBuf.limit(oldLimit); 249 baseBuf.position(oldPosition); 250 } 251 252 public void testRemaining() { 253 assertEquals(baseBuf.remaining(), baseBuf.limit() - baseBuf.position()); 254 } 255 256 public void testReset() { 257 // save state 258 int oldPosition = baseBuf.position(); 259 int oldLimit = baseBuf.limit(); 260 261 baseBuf.mark(); 262 baseBuf.position(baseBuf.limit()); 263 baseBuf.reset(); 264 assertEquals(baseBuf.position(), oldPosition); 265 266 baseBuf.mark(); 267 baseBuf.position(baseBuf.limit()); 268 baseBuf.reset(); 269 assertEquals(baseBuf.position(), oldPosition); 270 271 Buffer ret = baseBuf.reset(); 272 assertSame(ret, baseBuf); 273 274 baseBuf.clear(); 275 try { 276 baseBuf.reset(); 277 fail("Should throw Exception"); //$NON-NLS-1$ 278 } catch (InvalidMarkException e) { 279 // expected 280 } 281 282 // restore state 283 baseBuf.limit(oldLimit); 284 baseBuf.position(oldPosition); 285 } 286 287 public void testRewind() { 288 // save state 289 int oldPosition = baseBuf.position(); 290 int oldLimit = baseBuf.limit(); 291 292 Buffer ret = baseBuf.rewind(); 293 assertEquals(baseBuf.position(), 0); 294 assertSame(ret, baseBuf); 295 try { 296 baseBuf.reset(); 297 fail("Should throw Exception"); //$NON-NLS-1$ 298 } catch (InvalidMarkException e) { 299 // expected 300 } 301 302 // restore state 303 baseBuf.limit(oldLimit); 304 baseBuf.position(oldPosition); 305 } 306 } 307