summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/io/StrictLineReaderTest.java
blob: d5d338157ee25f15d26c736ffa2b605be66ab89c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.io;

import junit.framework.TestCase;

import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStream;
import java.nio.charset.Charsets;

public class StrictLineReaderTest extends TestCase {

    public void testLineReaderConsistencyWithReadAsciiLine () {
        try {
            // Testing with LineReader buffer capacity 32 to check some corner cases.
            StrictLineReader lineReader = new StrictLineReader(createTestInputStream(), 32,
                    Charsets.US_ASCII);
            InputStream refStream = createTestInputStream();
            while (true) {
                try {
                    String refLine = Streams.readAsciiLine(refStream);
                    try {
                        String line = lineReader.readLine();
                        if (!refLine.equals(line)) {
                            fail("line (\""+line+"\") differs from expected (\""+refLine+"\").");
                        }
                    } catch (EOFException eof) {
                        fail("line reader threw EOFException too early.");
                    }
                } catch (EOFException refEof) {
                    try {
                        lineReader.readLine();
                        fail("line reader didn't throw the expected EOFException.");
                    } catch (EOFException eof) {
                        // OK
                        break;
                    }
                }
            }
            refStream.close();
            lineReader.close();
        } catch (IOException ioe) {
            fail("Unexpected IOException " + ioe.toString());
        }
    }

    private InputStream createTestInputStream() {
        return new ByteArrayInputStream((
                /* each source lines below should represent 32 bytes, until the next comment */
                "12 byte line\n18 byte line......\n" +
                "pad\nline spanning two 32-byte bu" +
                "ffers\npad......................\n" +
                "pad\nline spanning three 32-byte " +
                "buffers and ending with LF at th" +
                "e end of a 32 byte buffer......\n" +
                "pad\nLine ending with CRLF split" +
                " at the end of a 32-byte buffer\r" +
                "\npad...........................\n" +
                /* end of 32-byte lines */
                "line ending with CRLF\r\n" +
                "this is a long line with embedded CR \r ending with CRLF and having more than " +
                "32 characters\r\n" +
                "unterminated line - should be dropped"
                ).getBytes());
    }
}