• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio;
17 
18 import java.io.EOFException;
19 import java.io.IOException;
20 import org.junit.Test;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24 import static org.junit.Assert.fail;
25 
26 public abstract class ReadUtf8LineTest {
newSource(String s)27   protected abstract BufferedSource newSource(String s);
28 
readLines()29   @Test public void readLines() throws IOException {
30     BufferedSource source = newSource("abc\ndef\n");
31     assertEquals("abc", source.readUtf8LineStrict());
32     assertEquals("def", source.readUtf8LineStrict());
33     try {
34       source.readUtf8LineStrict();
35       fail();
36     } catch (EOFException expected) {
37     }
38   }
39 
emptyLines()40   @Test public void emptyLines() throws IOException {
41     BufferedSource source = newSource("\n\n\n");
42     assertEquals("", source.readUtf8LineStrict());
43     assertEquals("", source.readUtf8LineStrict());
44     assertEquals("", source.readUtf8LineStrict());
45     assertTrue(source.exhausted());
46   }
47 
crDroppedPrecedingLf()48   @Test public void crDroppedPrecedingLf() throws IOException {
49     BufferedSource source = newSource("abc\r\ndef\r\nghi\rjkl\r\n");
50     assertEquals("abc", source.readUtf8LineStrict());
51     assertEquals("def", source.readUtf8LineStrict());
52     assertEquals("ghi\rjkl", source.readUtf8LineStrict());
53   }
54 
bufferedReaderCompatible()55   @Test public void bufferedReaderCompatible() throws IOException {
56     BufferedSource source = newSource("abc\ndef");
57     assertEquals("abc", source.readUtf8Line());
58     assertEquals("def", source.readUtf8Line());
59     assertEquals(null, source.readUtf8Line());
60   }
61 
bufferedReaderCompatibleWithTrailingNewline()62   @Test public void bufferedReaderCompatibleWithTrailingNewline() throws IOException {
63     BufferedSource source = newSource("abc\ndef\n");
64     assertEquals("abc", source.readUtf8Line());
65     assertEquals("def", source.readUtf8Line());
66     assertEquals(null, source.readUtf8Line());
67   }
68 }
69