FileDocCategorySizeDatePackage
ProcFileReaderTest.javaAPI DocAndroid 5.1 API6032Thu Mar 12 22:22:12 GMT 2015com.android.internal.util

ProcFileReaderTest

public class ProcFileReaderTest extends android.test.AndroidTestCase
Tests for {@link ProcFileReader}.

Fields Summary
Constructors Summary
Methods Summary
private static ProcFileReaderbuildReader(java.lang.String string)

        return buildReader(string, 2048);
    
private static ProcFileReaderbuildReader(java.lang.String string, int bufferSize)

        return new ProcFileReader(
                new ByteArrayInputStream(string.getBytes(StandardCharsets.US_ASCII)), bufferSize);
    
public voidtestBlankLines()

        final ProcFileReader reader = buildReader("1\n\n2\n\n3\n");

        assertEquals(1, reader.nextInt());
        reader.finishLine();
        assertTrue(reader.hasMoreData());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals(2, reader.nextInt());
        reader.finishLine();
        assertTrue(reader.hasMoreData());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals(3, reader.nextInt());
        reader.finishLine();
        assertFalse(reader.hasMoreData());
    
public voidtestBufferSize()

        // read numbers using very small buffer size, exercising fillBuf()
        final ProcFileReader reader = buildReader("1 21 3 41 5 61 7 81 9 10\n", 3);

        assertEquals(1, reader.nextInt());
        assertEquals(21, reader.nextInt());
        assertEquals(3, reader.nextInt());
        assertEquals(41, reader.nextInt());
        assertEquals(5, reader.nextInt());
        assertEquals(61, reader.nextInt());
        assertEquals(7, reader.nextInt());
        assertEquals(81, reader.nextInt());
        assertEquals(9, reader.nextInt());
        assertEquals(10, reader.nextInt());
        reader.finishLine();
        assertFalse(reader.hasMoreData());
    
public voidtestDelimiterNeverFound()

        final ProcFileReader reader = buildReader("teststringwithoutdelimiters");

        try {
            reader.nextString();
            fail("somehow read a string value?");
        } catch (IOException e) {
            // expected
            assertTrue(e.getMessage().contains("End of stream"));
        }
    
public voidtestEmpty()

        final ProcFileReader reader = buildReader("");

        assertFalse(reader.hasMoreData());
        try {
            reader.finishLine();
            fail("somehow finished line beyond end of stream?");
        } catch (IOException e) {
            // expected
        }
        assertFalse(reader.hasMoreData());
    
public voidtestLargerThanBuffer()

        // try finishing line larger than buffer
        final ProcFileReader reader = buildReader("1 teststringlongerthanbuffer\n", 4);

        assertEquals(1, reader.nextLong());
        try {
            reader.finishLine();
            fail("somehow finished line?");
        } catch (IOException e) {
            // expected
            assertTrue(e.getMessage().contains("already-full buffer"));
        }
    
public voidtestMinMax()

        final ProcFileReader reader = buildReader(
                "1 -1024 9223372036854775807 -9223372036854775808\n");

        assertEquals(1, reader.nextLong());
        assertEquals(-1024, reader.nextLong());
        assertEquals(Long.MAX_VALUE, reader.nextLong());
        assertEquals(Long.MIN_VALUE, reader.nextLong());
        reader.finishLine();
        assertFalse(reader.hasMoreData());
    
public voidtestMixedNumbersSkip()

        final ProcFileReader reader = buildReader("1 2 3\n4 abc_def 5 6 7 8 9\n10\n");

        assertEquals(1, reader.nextInt());
        assertEquals(2, reader.nextInt());
        assertEquals(3, reader.nextInt());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals(4, reader.nextInt());
        assertEquals("abc_def", reader.nextString());
        assertEquals(5, reader.nextInt());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals(10, reader.nextInt());
        reader.finishLine();
        assertFalse(reader.hasMoreData());
    
public voidtestOptionalLongs()

        final ProcFileReader reader = buildReader("123 456\n789\n");

        assertEquals(123L, reader.nextLong());
        assertEquals(456L, reader.nextOptionalLong(-1L));
        assertEquals(-1L, reader.nextOptionalLong(-1L));
        assertEquals(-1L, reader.nextOptionalLong(-1L));
        assertEquals(-1L, reader.nextOptionalLong(-1L));
        reader.finishLine();

        assertEquals(789L, reader.nextOptionalLong(-1L));
        assertEquals(-1L, reader.nextOptionalLong(-1L));
    
public voidtestSingleString()

        final ProcFileReader reader = buildReader("a\nb\nc\n");

        assertEquals("a", reader.nextString());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals("b", reader.nextString());
        reader.finishLine();
        assertTrue(reader.hasMoreData());

        assertEquals("c", reader.nextString());
        reader.finishLine();
        assertFalse(reader.hasMoreData());