| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package org.apache.qpid.proton.codec; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertFalse; |
| 21 |
import static org.junit.Assert.assertTrue; |
| 22 |
import static org.junit.Assert.fail; |
| 23 |
|
| 24 |
import java.nio.BufferOverflowException; |
| 25 |
import java.nio.ByteBuffer; |
| 26 |
|
| 27 |
import org.junit.Test; |
| 28 |
|
| 29 |
/** |
| 30 |
* Tests for built in ByteBuffer wrapper of WritableBuffer |
| 31 |
*/ |
| 32 |
public class WritableBufferTest { |
| 33 |
|
| 34 |
@Test |
| 35 |
public void testCreateAllocatedWrapper() { |
| 36 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.allocate(10); |
| 37 |
|
| 38 |
assertEquals(10, buffer.remaining()); |
| 39 |
assertEquals(0, buffer.position()); |
| 40 |
assertTrue(buffer.hasRemaining()); |
| 41 |
} |
| 42 |
|
| 43 |
@Test |
| 44 |
public void testCreateByteArrayWrapper() { |
| 45 |
byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 46 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 47 |
|
| 48 |
assertEquals(10, buffer.remaining()); |
| 49 |
assertEquals(0, buffer.position()); |
| 50 |
assertTrue(buffer.hasRemaining()); |
| 51 |
} |
| 52 |
|
| 53 |
@Test |
| 54 |
public void testLimit() { |
| 55 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 56 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 57 |
|
| 58 |
assertEquals(data.capacity(), buffer.limit()); |
| 59 |
} |
| 60 |
|
| 61 |
@Test |
| 62 |
public void testRemaining() { |
| 63 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 64 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 65 |
|
| 66 |
assertEquals(data.limit(), buffer.remaining()); |
| 67 |
buffer.put((byte) 0); |
| 68 |
assertEquals(data.limit() - 1, buffer.remaining()); |
| 69 |
} |
| 70 |
|
| 71 |
@Test |
| 72 |
public void testHasRemaining() { |
| 73 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 74 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 75 |
|
| 76 |
assertTrue(buffer.hasRemaining()); |
| 77 |
buffer.put((byte) 0); |
| 78 |
assertTrue(buffer.hasRemaining()); |
| 79 |
data.position(data.limit()); |
| 80 |
assertFalse(buffer.hasRemaining()); |
| 81 |
} |
| 82 |
|
| 83 |
@Test |
| 84 |
public void testEnsureRemainingThrowsWhenExpected() { |
| 85 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 86 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 87 |
|
| 88 |
assertEquals(data.capacity(), buffer.limit()); |
| 89 |
try { |
| 90 |
buffer.ensureRemaining(1024); |
| 91 |
fail("Should have thrown an error on request for more than is available."); |
| 92 |
} catch (BufferOverflowException boe) {} |
| 93 |
|
| 94 |
try { |
| 95 |
buffer.ensureRemaining(-1); |
| 96 |
fail("Should have thrown an error on request for negative space."); |
| 97 |
} catch (IllegalArgumentException iae) {} |
| 98 |
} |
| 99 |
|
| 100 |
@Test |
| 101 |
public void testEnsureRemainingDefaultImplementation() { |
| 102 |
WritableBuffer buffer = new DefaultWritableBuffer(); |
| 103 |
|
| 104 |
try { |
| 105 |
buffer.ensureRemaining(1024); |
| 106 |
} catch (IndexOutOfBoundsException iobe) { |
| 107 |
fail("Should not have thrown an error on request for more than is available."); |
| 108 |
} |
| 109 |
|
| 110 |
try { |
| 111 |
buffer.ensureRemaining(-1); |
| 112 |
} catch (IllegalArgumentException iae) { |
| 113 |
fail("Should not have thrown an error on request for negative space."); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void testGetPosition() { |
| 119 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 120 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 121 |
|
| 122 |
assertEquals(0, buffer.position()); |
| 123 |
data.put((byte) 0); |
| 124 |
assertEquals(1, buffer.position()); |
| 125 |
} |
| 126 |
|
| 127 |
@Test |
| 128 |
public void testSetPosition() { |
| 129 |
ByteBuffer data = ByteBuffer.allocate(100); |
| 130 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 131 |
|
| 132 |
assertEquals(0, data.position()); |
| 133 |
buffer.position(1); |
| 134 |
assertEquals(1, data.position()); |
| 135 |
} |
| 136 |
|
| 137 |
@Test |
| 138 |
public void testPutByteBuffer() { |
| 139 |
ByteBuffer input = ByteBuffer.allocate(1024); |
| 140 |
input.put((byte) 1); |
| 141 |
input.flip(); |
| 142 |
|
| 143 |
ByteBuffer data = ByteBuffer.allocate(1024); |
| 144 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 145 |
|
| 146 |
assertEquals(0, buffer.position()); |
| 147 |
buffer.put(input); |
| 148 |
assertEquals(1, buffer.position()); |
| 149 |
} |
| 150 |
|
| 151 |
@Test |
| 152 |
public void testPutString() { |
| 153 |
String ascii = new String("ASCII"); |
| 154 |
|
| 155 |
ByteBuffer data = ByteBuffer.allocate(1024); |
| 156 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 157 |
|
| 158 |
assertEquals(0, buffer.position()); |
| 159 |
buffer.put(ascii); |
| 160 |
assertEquals(ascii.length(), buffer.position()); |
| 161 |
} |
| 162 |
|
| 163 |
@Test |
| 164 |
public void testPutReadableBuffer() { |
| 165 |
doPutReadableBufferTestImpl(true); |
| 166 |
doPutReadableBufferTestImpl(false); |
| 167 |
} |
| 168 |
|
| 169 |
private void doPutReadableBufferTestImpl(boolean readOnly) { |
| 170 |
ByteBuffer buf = ByteBuffer.allocate(1024); |
| 171 |
buf.put((byte) 1); |
| 172 |
buf.flip(); |
| 173 |
if(readOnly) { |
| 174 |
buf = buf.asReadOnlyBuffer(); |
| 175 |
} |
| 176 |
|
| 177 |
ReadableBuffer input = new ReadableBuffer.ByteBufferReader(buf); |
| 178 |
|
| 179 |
if(readOnly) { |
| 180 |
assertFalse("Expected buffer not to hasArray()", input.hasArray()); |
| 181 |
} else { |
| 182 |
assertTrue("Expected buffer to hasArray()", input.hasArray()); |
| 183 |
} |
| 184 |
|
| 185 |
ByteBuffer data = ByteBuffer.allocate(1024); |
| 186 |
WritableBuffer buffer = WritableBuffer.ByteBufferWrapper.wrap(data); |
| 187 |
|
| 188 |
assertEquals(0, buffer.position()); |
| 189 |
buffer.put(input); |
| 190 |
assertEquals(1, buffer.position()); |
| 191 |
} |
| 192 |
|
| 193 |
//----- WritableBuffer implementation with no default overrides ----------// |
| 194 |
|
| 195 |
private static class DefaultWritableBuffer implements WritableBuffer { |
| 196 |
|
| 197 |
private final WritableBuffer backing; |
| 198 |
|
| 199 |
public DefaultWritableBuffer() { |
| 200 |
backing = WritableBuffer.ByteBufferWrapper.allocate(1024); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public void put(byte b) { |
| 205 |
backing.put(b); |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public void putFloat(float f) { |
| 210 |
backing.putFloat(f); |
| 211 |
} |
| 212 |
|
| 213 |
@Override |
| 214 |
public void putDouble(double d) { |
| 215 |
backing.putDouble(d); |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public void put(byte[] src, int offset, int length) { |
| 220 |
backing.put(src, offset, length); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public void putShort(short s) { |
| 225 |
backing.putShort(s); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public void putInt(int i) { |
| 230 |
backing.putInt(i); |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public void putLong(long l) { |
| 235 |
backing.putLong(l); |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public boolean hasRemaining() { |
| 240 |
return backing.hasRemaining(); |
| 241 |
} |
| 242 |
|
| 243 |
@Override |
| 244 |
public int remaining() { |
| 245 |
return backing.remaining(); |
| 246 |
} |
| 247 |
|
| 248 |
@Override |
| 249 |
public int position() { |
| 250 |
return backing.position(); |
| 251 |
} |
| 252 |
|
| 253 |
@Override |
| 254 |
public void position(int position) { |
| 255 |
backing.position(position); |
| 256 |
} |
| 257 |
|
| 258 |
@Override |
| 259 |
public void put(ByteBuffer payload) { |
| 260 |
backing.put(payload); |
| 261 |
} |
| 262 |
|
| 263 |
@Override |
| 264 |
public void put(ReadableBuffer payload) { |
| 265 |
backing.put(payload); |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public int limit() { |
| 270 |
return backing.limit(); |
| 271 |
} |
| 272 |
} |
| 273 |
} |