1 /* 2 * Copyright (C) 2011 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.IOException; 23 import java.io.InputStream; 24 25 /** 26 * Unit tests for {@link SizeLimitedOutputStreamTest} 27 */ 28 public class SizeLimitedOutputStreamTest extends TestCase { 29 30 /** 31 * Test the file size limiting. 32 */ 33 public void testMaxFileSizeHelper() throws IOException { 34 final byte[] data = new byte[29]; 35 36 // fill data with values 37 for (byte i = 0; i < data.length; i++) { 38 data[i] = i; 39 } 40 41 // use a max size of 20 - expect first 10 bytes to get dropped 42 SizeLimitedOutputStream outStream = new SizeLimitedOutputStream(20, 4, "foo", "bar"); 43 try { 44 outStream.write(data); 45 outStream.close(); 46 InputStream readStream = outStream.getData(); 47 byte[] readData = new byte[64]; 48 int readDataPos = 0; 49 int read; 50 while ((read = readStream.read()) != -1) { 51 readData[readDataPos] = (byte)read; 52 readDataPos++; 53 } 54 int bytesRead = readDataPos; 55 assertEquals(19, bytesRead); 56 assertEquals(10, readData[0]); 57 assertEquals(28, readData[18]); 58 } finally { 59 outStream.delete(); 60 } 61 } 62 } 63