Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache
      5  * License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * 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 
     18 package com.android.tradefed.util;
     19 
     20 import junit.framework.TestCase;
     21 
     22 import java.io.ByteArrayOutputStream;
     23 import java.io.IOException;
     24 
     25 /**
     26  * Unit tests for {@link FixedByteArrayOutputStream}.
     27  */
     28 public class FixedByteArrayOutputStreamTest extends TestCase {
     29 
     30     private static final byte BUF_SIZE = 30;
     31     private static final String TEXT = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, "
     32             + "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
     33             + "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut "
     34             + "aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
     35             + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint "
     36             + "occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit "
     37             + "anim id est laborum."; // 446 bytes
     38     private FixedByteArrayOutputStream mOutStream;
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         super.setUp();
     43         mOutStream = new FixedByteArrayOutputStream(BUF_SIZE);
     44     }
     45 
     46     /**
     47      * Util method to write a string into the {@link FixedByteArrayOutputStream} under test and
     48      *
     49      * @param text
     50      * @return content as String
     51      */
     52     private String writeTextIntoStreamAndReturn(String text) throws IOException {
     53         mOutStream.write(text.getBytes());
     54         ByteArrayOutputStream baos = new ByteArrayOutputStream();
     55         StreamUtil.copyStreams(mOutStream.getData(), baos);
     56         baos.close();
     57         return baos.toString();
     58     }
     59     /**
     60      * Test the stream works when data written is less than buffer size.
     61      */
     62     public void testLessThanBuffer() throws IOException {
     63         String text = TEXT.substring(0, BUF_SIZE - 5);
     64         assertEquals(text, writeTextIntoStreamAndReturn(text));
     65     }
     66 
     67     /**
     68      * Test the stream works when data written is exactly equal to buffer size.
     69      */
     70     public void testEqualsBuffer() throws IOException {
     71         String text = TEXT.substring(0, BUF_SIZE);
     72         assertEquals(text, writeTextIntoStreamAndReturn(text));
     73     }
     74 
     75     /**
     76      * Test the stream works when data written is 1 greater than buffer size.
     77      */
     78     public void testBufferPlusOne() throws IOException {
     79         String text = TEXT.substring(0, BUF_SIZE + 1);
     80         String expected = text.substring(1);
     81         assertEquals(expected, writeTextIntoStreamAndReturn(text));
     82     }
     83 
     84     /**
     85      * Test the stream works when data written is much greater than buffer size.
     86      */
     87     public void testBufferPlusPlus() throws IOException {
     88         String expected = TEXT.substring(TEXT.length() - BUF_SIZE);
     89         assertEquals(expected, writeTextIntoStreamAndReturn(TEXT));
     90     }
     91 
     92     /**
     93      * Testing the buffer wrap around scenario
     94      * @throws IOException
     95      */
     96     public void testWriteWithWrap() throws IOException {
     97         String prefix = "foobar";
     98         // larger than 24b because need to overflow the buffer, less than 30b because need to avoid
     99         // the shortcut in code when incoming is larger than buffer
    100         String followup = "stringlenbetween24band30b";
    101         String full = prefix + followup;
    102         String expected = full.substring(full.length() - BUF_SIZE);
    103         mOutStream.write(prefix.getBytes());
    104         assertEquals(expected, writeTextIntoStreamAndReturn(followup));
    105     }
    106 
    107     /**
    108      * Test writing using byte array with an offset
    109      * @throws IOException
    110      */
    111     public void testLessThanBufferWithOffset() throws IOException {
    112         String text = TEXT.substring(0, BUF_SIZE);
    113         int offset = 5;
    114         String expected = text.substring(offset);
    115         mOutStream.write(text.getBytes(), offset, text.length() - offset);
    116         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    117         StreamUtil.copyStreams(mOutStream.getData(), baos);
    118         baos.close();
    119         assertEquals(expected, baos.toString());
    120     }
    121 
    122     /**
    123      * Test writing using byte array with an offset
    124      * @throws IOException
    125      */
    126     public void testWriteWithOffsetAndWrap() throws IOException {
    127         String prefix = "foobar";
    128         // similar to testWriteWithWrap, but add a tail to account for offset
    129         String followup = "stringlenbetween24band30b____";
    130         int offset = 4;
    131         String full = prefix + followup.substring(offset);
    132         String expected = full.substring(full.length() - BUF_SIZE);
    133         mOutStream.write(prefix.getBytes());
    134         mOutStream.write(followup.getBytes(), offset, followup.length() - offset);
    135         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    136         StreamUtil.copyStreams(mOutStream.getData(), baos);
    137         baos.close();
    138         assertEquals(expected, baos.toString());
    139     }
    140 }
    141