FileDocCategorySizeDatePackage
LimitedLengthInputStreamTest.javaAPI DocAndroid 5.1 API6882Thu Mar 12 22:22:12 GMT 2015android.content.pm

LimitedLengthInputStreamTest

public class LimitedLengthInputStreamTest extends android.test.AndroidTestCase

Fields Summary
private final byte[]
TEST_STRING1
private InputStream
mTestStream1
Constructors Summary
Methods Summary
private voidcheckReadBytesWithOffsetAndLength_WithString1(int offset, int length)

        byte[] temp = new byte[TEST_STRING1.length];
        byte[] expected = new byte[length];
        byte[] actual = new byte[length];

        System.arraycopy(TEST_STRING1, offset, expected, 0, length);

        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);
        assertEquals(length, is.read(temp, 0, temp.length));

        System.arraycopy(temp, 0, actual, 0, length);
        assertTrue(Arrays.equals(expected, actual));

        assertEquals(-1, is.read(temp, 0, temp.length));
    
private voidcheckReadBytes_WithString1(int offset, int length)

        byte[] temp = new byte[TEST_STRING1.length];
        byte[] expected = new byte[length];
        byte[] actual = new byte[length];

        System.arraycopy(TEST_STRING1, offset, expected, 0, length);

        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);
        assertEquals(length, is.read(temp));

        System.arraycopy(temp, 0, actual, 0, length);
        assertTrue(Arrays.equals(expected, actual));

        assertEquals(-1, is.read(temp));
    
private voidcheckSingleByteRead_WithString1(int offset, int length)

        InputStream is = new LimitedLengthInputStream(mTestStream1, offset, length);

        for (int i = 0; i < length; i++) {
            assertEquals(TEST_STRING1[offset + i], is.read());
        }

        assertEquals(-1, is.read());
    
protected voidsetUp()


    
         
        super.setUp();

        mTestStream1 = new ByteArrayInputStream(TEST_STRING1);
    
public voidtestConstructor_NegativeLength_Failure()

        try {
            InputStream is = new LimitedLengthInputStream(mTestStream1, 0, -1);
            fail("Should throw IOException on negative length");
        } catch (IOException e) {
            // success
        }
    
public voidtestConstructor_NegativeOffset_Failure()

        try {
            InputStream is = new LimitedLengthInputStream(mTestStream1, -1, TEST_STRING1.length);
            fail("Should throw IOException on negative index");
        } catch (IOException e) {
            // success
        }
    
public voidtestConstructor_NullInputStream_Failure()

        try {
            InputStream is = new LimitedLengthInputStream(null, 0, 1);
            fail("Should throw IOException on null input stream");
        } catch (IOException e) {
            // success
        }
    
public voidtestConstructor_OffsetLengthOverflow_Fail()

        try {
        InputStream is = new LimitedLengthInputStream(mTestStream1, Long.MAX_VALUE - 1,
                Long.MAX_VALUE - 1);
            fail("Should fail when offset + length is > Long.MAX_VALUE");
        } catch (IOException e) {
            // success
        }
    
public voidtestReadBytesWithOffsetAndLength_NonZeroOffset_FullLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(3, TEST_STRING1.length - 3);
    
public voidtestReadBytesWithOffsetAndLength_NonZeroOffset_PartialLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(3, 2);
    
public voidtestReadBytesWithOffsetAndLength_ZeroOffset_FullLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(0, TEST_STRING1.length);
    
public voidtestReadBytesWithOffsetAndLength_ZeroOffset_PartialLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(0, 2);
    
public voidtestReadBytesWithOffsetAndLength_ZeroOffset_PastEnd_Success()

        byte[] temp = new byte[TEST_STRING1.length + 10];
        InputStream is = new LimitedLengthInputStream(mTestStream1, 0, TEST_STRING1.length + 10);
        assertEquals(TEST_STRING1.length, is.read(temp, 0, TEST_STRING1.length + 10));

        byte[] actual = new byte[TEST_STRING1.length];
        System.arraycopy(temp, 0, actual, 0, actual.length);
        assertTrue(Arrays.equals(TEST_STRING1, actual));
    
public voidtestReadBytes_NonZeroOffset_FullLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(3, TEST_STRING1.length - 3);
    
public voidtestReadBytes_NonZeroOffset_PartialLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(3, 2);
    
public voidtestReadBytes_ZeroOffset_FullLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(0, TEST_STRING1.length);
    
public voidtestReadBytes_ZeroOffset_PartialLength_Success()

        checkReadBytesWithOffsetAndLength_WithString1(0, 2);
    
public voidtestSingleByteRead_NonZeroOffset_FullLength_Success()

        checkSingleByteRead_WithString1(3, TEST_STRING1.length - 3);
    
public voidtestSingleByteRead_NonZeroOffset_PartialLength_Success()

        checkSingleByteRead_WithString1(3, 2);
    
public voidtestSingleByteRead_ZeroOffset_FullLength_Success()

        checkSingleByteRead_WithString1(0, TEST_STRING1.length);
    
public voidtestSingleByteRead_ZeroOffset_PartialLength_Success()

        checkSingleByteRead_WithString1(0, 2);