1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 package com.google.protobuf; 32 33 import junit.framework.TestCase; 34 35 import java.io.ByteArrayOutputStream; 36 import java.io.IOException; 37 import java.nio.ByteBuffer; 38 import java.util.Arrays; 39 import java.util.Random; 40 41 /** 42 * Tests for {@link ByteBufferWriter}. 43 */ 44 public class ByteBufferWriterTest extends TestCase { 45 testHeapBuffer()46 public void testHeapBuffer() throws IOException { 47 // Test a small and large buffer. 48 testWrite(ByteBuffer.allocate(100)); 49 testWrite(ByteBuffer.allocate(1024 * 100)); 50 } 51 testDirectBuffer()52 public void testDirectBuffer() throws IOException { 53 // Test a small and large buffer. 54 testWrite(ByteBuffer.allocateDirect(100)); 55 testWrite(ByteBuffer.allocateDirect(1024 * 100)); 56 } 57 testWrite(ByteBuffer buffer)58 private void testWrite(ByteBuffer buffer) throws IOException { 59 fillRandom(buffer); 60 ByteArrayOutputStream os = new ByteArrayOutputStream(buffer.remaining()); 61 ByteBufferWriter.write(buffer, os); 62 assertEquals(0, buffer.position()); 63 assertTrue(Arrays.equals(toArray(buffer), os.toByteArray())); 64 } 65 fillRandom(ByteBuffer buf)66 private void fillRandom(ByteBuffer buf) { 67 byte[] bytes = new byte[buf.remaining()]; 68 new Random().nextBytes(bytes); 69 buf.put(bytes); 70 buf.flip(); 71 return; 72 } 73 toArray(ByteBuffer buf)74 private byte[] toArray(ByteBuffer buf) { 75 int originalPosition = buf.position(); 76 byte[] bytes = new byte[buf.remaining()]; 77 buf.get(bytes); 78 buf.position(originalPosition); 79 return bytes; 80 } 81 } 82