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 */ 16package okio; 17 18import java.io.EOFException; 19import java.io.IOException; 20import java.util.Arrays; 21import java.util.List; 22import org.junit.Before; 23import org.junit.Test; 24import org.junit.runner.RunWith; 25import org.junit.runners.Parameterized; 26 27import static org.junit.Assert.assertEquals; 28import static org.junit.Assert.assertTrue; 29import static org.junit.Assert.fail; 30 31@RunWith(Parameterized.class) 32public final class ReadUtf8LineTest { 33 private interface Factory { 34 BufferedSource create(Buffer data); 35 } 36 37 // ANDROID-BEGIN 38 // @Parameterized.Parameters(name = "{0}") 39 @Parameterized.Parameters 40 // ANDROID-END 41 public static List<Object[]> parameters() { 42 return Arrays.asList( 43 new Object[] { new Factory() { 44 @Override public BufferedSource create(Buffer data) { 45 return data; 46 } 47 48 @Override public String toString() { 49 return "Buffer"; 50 } 51 }}, 52 new Object[] { new Factory() { 53 @Override public BufferedSource create(Buffer data) { 54 return new RealBufferedSource(data); 55 } 56 57 @Override public String toString() { 58 return "RealBufferedSource"; 59 } 60 }}, 61 new Object[] { new Factory() { 62 @Override public BufferedSource create(Buffer data) { 63 return new RealBufferedSource(new ForwardingSource(data) { 64 @Override public long read(Buffer sink, long byteCount) throws IOException { 65 return super.read(sink, Math.min(1, byteCount)); 66 } 67 }); 68 } 69 70 @Override public String toString() { 71 return "Slow RealBufferedSource"; 72 } 73 }} 74 ); 75 } 76 77 // ANDROID-BEGIN 78 // @Parameterized.Parameter 79 private final Factory factory; 80 public ReadUtf8LineTest(Factory factory) { 81 this.factory = factory; 82 } 83 // ANDROID-END 84 85 private Buffer data; 86 private BufferedSource source; 87 88 @Before public void setUp() { 89 data = new Buffer(); 90 source = factory.create(data); 91 } 92 93 @Test public void readLines() throws IOException { 94 data.writeUtf8("abc\ndef\n"); 95 assertEquals("abc", source.readUtf8LineStrict()); 96 assertEquals("def", source.readUtf8LineStrict()); 97 try { 98 source.readUtf8LineStrict(); 99 fail(); 100 } catch (EOFException expected) { 101 assertEquals("\\n not found: size=0 content=...", expected.getMessage()); 102 } 103 } 104 105 @Test public void eofExceptionProvidesLimitedContent() throws IOException { 106 data.writeUtf8("aaaaaaaabbbbbbbbccccccccdddddddde"); 107 try { 108 source.readUtf8LineStrict(); 109 fail(); 110 } catch (EOFException expected) { 111 assertEquals("\\n not found: size=33 content=616161616161616162626262626262626363636363636363" 112 + "6464646464646464...", expected.getMessage()); 113 } 114 } 115 116 @Test public void emptyLines() throws IOException { 117 data.writeUtf8("\n\n\n"); 118 assertEquals("", source.readUtf8LineStrict()); 119 assertEquals("", source.readUtf8LineStrict()); 120 assertEquals("", source.readUtf8LineStrict()); 121 assertTrue(source.exhausted()); 122 } 123 124 @Test public void crDroppedPrecedingLf() throws IOException { 125 data.writeUtf8("abc\r\ndef\r\nghi\rjkl\r\n"); 126 assertEquals("abc", source.readUtf8LineStrict()); 127 assertEquals("def", source.readUtf8LineStrict()); 128 assertEquals("ghi\rjkl", source.readUtf8LineStrict()); 129 } 130 131 @Test public void bufferedReaderCompatible() throws IOException { 132 data.writeUtf8("abc\ndef"); 133 assertEquals("abc", source.readUtf8Line()); 134 assertEquals("def", source.readUtf8Line()); 135 assertEquals(null, source.readUtf8Line()); 136 } 137 138 @Test public void bufferedReaderCompatibleWithTrailingNewline() throws IOException { 139 data.writeUtf8("abc\ndef\n"); 140 assertEquals("abc", source.readUtf8Line()); 141 assertEquals("def", source.readUtf8Line()); 142 assertEquals(null, source.readUtf8Line()); 143 } 144} 145