1// Copyright 2016 Google Inc. All rights reserved. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package com.google.archivepatcher.applier; 16 17import org.junit.Assert; 18import org.junit.Test; 19import org.junit.runner.RunWith; 20import org.junit.runners.JUnit4; 21 22import java.io.IOException; 23import java.io.InputStream; 24 25/** 26 * Tests for {@link PartiallyCompressingOutputStream}. 27 */ 28@RunWith(JUnit4.class) 29@SuppressWarnings("javadoc") 30public class LimitedInputStreamTest { 31 /** 32 * An input stream that never ends and always does nothing. 33 */ 34 private static class ForeverInputStream extends InputStream { 35 @Override 36 public int read() throws IOException { 37 return 0; 38 } 39 40 @Override 41 public int read(byte[] b) throws IOException { 42 return b.length; 43 } 44 45 @Override 46 public int read(byte[] b, int off, int len) throws IOException { 47 return len; 48 } 49 } 50 51 @SuppressWarnings("resource") 52 @Test 53 public void testRead_WithLimit0() throws IOException { 54 LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 0); 55 Assert.assertEquals(-1, stream.read()); 56 } 57 58 @SuppressWarnings("resource") 59 @Test 60 public void testRead_WithLimit1() throws IOException { 61 LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 1); 62 Assert.assertEquals(0, stream.read()); 63 Assert.assertEquals(-1, stream.read()); 64 } 65 66 @SuppressWarnings("resource") 67 @Test 68 public void testRead_WithLimit100() throws IOException { 69 LimitedInputStream stream = new LimitedInputStream(new ForeverInputStream(), 100); 70 Assert.assertEquals(100, stream.read(new byte[1000])); 71 Assert.assertEquals(-1, stream.read()); 72 } 73 74 @SuppressWarnings("resource") 75 @Test(expected = IllegalArgumentException.class) 76 public void testSetLimit_BadValue() { 77 new LimitedInputStream(new ForeverInputStream(), -1); 78 } 79} 80