1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.thrift.transport; 20 21 import java.nio.ByteBuffer; 22 23 import org.apache.thrift.TConfiguration; 24 import org.junit.Test; 25 import static org.junit.Assert.*; 26 27 public class TestAutoExpandingBufferWriteTransport { 28 29 private TConfiguration config = new TConfiguration(); 30 31 @Test testIt()32 public void testIt() throws Exception { 33 AutoExpandingBufferWriteTransport t = new AutoExpandingBufferWriteTransport(config, 1, 0); 34 assertEquals(0, t.getLength()); 35 assertEquals(1, t.getBuf().array().length); 36 byte[] b1 = new byte[]{1,2,3}; 37 t.write(b1); 38 assertEquals(3, t.getLength()); 39 assertTrue(t.getBuf().array().length >= 3); 40 assertEquals(ByteBuffer.wrap(b1), ByteBuffer.wrap(t.getBuf().array(), 0, 3)); 41 42 t.reset(); 43 assertEquals(0, t.getLength()); 44 assertTrue(t.getBuf().array().length >= 3); 45 byte[] b2 = new byte[]{4,5}; 46 t.write(b2); 47 assertEquals(2, t.getLength()); 48 assertEquals(ByteBuffer.wrap(b2), ByteBuffer.wrap(t.getBuf().array(), 0, 2)); 49 50 AutoExpandingBufferWriteTransport uut = new AutoExpandingBufferWriteTransport(config, 8, 4); 51 assertEquals(4, uut.getLength()); 52 assertEquals(8, uut.getBuf().array().length); 53 uut.write(b1); 54 assertEquals(7, uut.getLength()); 55 assertEquals(8, uut.getBuf().array().length); 56 assertEquals(ByteBuffer.wrap(b1), ByteBuffer.wrap(uut.getBuf().array(), 4, 3)); 57 } 58 59 @Test(expected = IllegalArgumentException.class) testBadInitialSize()60 public void testBadInitialSize() throws IllegalArgumentException, TTransportException { 61 new AutoExpandingBufferWriteTransport(config, 0, 0); 62 } 63 64 @Test(expected = IllegalArgumentException.class) testBadFrontReserveSize()65 public void testBadFrontReserveSize() throws IllegalArgumentException, TTransportException { 66 new AutoExpandingBufferWriteTransport(config, 4, -1); 67 } 68 69 @Test(expected = IllegalArgumentException.class) testTooSmallFrontReserveSize()70 public void testTooSmallFrontReserveSize() throws IllegalArgumentException, TTransportException { 71 new AutoExpandingBufferWriteTransport(config, 4, 5); 72 } 73 } 74