Path: blob/master/test/jdk/java/io/LineNumberReader/MarkSplitCRLF.java
41149 views
/*1* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 821828025* @summary Make sure marking a line feed within a CRLF sequence works correctly26* @run testng MarkSplitCRLF27*/2829import java.io.IOException;30import java.io.LineNumberReader;31import java.io.Reader;32import java.io.StringReader;3334import org.testng.annotations.Test;3536import static org.testng.Assert.*;3738public class MarkSplitCRLF {39@Test40public static void testSpecifiedBufferSize() throws IOException {41final String string = "foo\r\nbar";42try (Reader reader =43new LineNumberReader(new StringReader(string), 5)) {44reader.read(); // 'f'45reader.read(); // 'o'46reader.read(); // 'o'47reader.read(); // '\r' -> '\n'48reader.mark(1); // mark position of '\n'49reader.read(); // 'b'50reader.reset(); // reset to '\n' position51assertEquals(reader.read(), 'b');52assertEquals(reader.read(), 'a');53assertEquals(reader.read(), 'r');54}55}5657@Test58public static void testCRNotFollowedByLF() throws IOException {59final String string = "foo\rbar";60try (Reader reader =61new LineNumberReader(new StringReader(string), 5)) {62reader.read(); // 'f'63reader.read(); // 'o'64reader.read(); // 'o'65reader.read(); // '\r'66reader.mark(1); // mark position of next character67reader.read(); // 'b'68reader.reset(); // reset to position after '\r'69assertEquals(reader.read(), 'b');70assertEquals(reader.read(), 'a');71assertEquals(reader.read(), 'r');72}73}7475@Test76public static void testDefaultBufferSize() throws IOException {77StringBuilder sb = new StringBuilder(8195);78for (int i = 0; i < 8190; i++) {79char c = (char)i;80sb.append(c);81}82sb.append('\r');83sb.append('\n');84sb.append('X');85sb.append('Y');86sb.append('Z');87final String string = sb.toString();88try (Reader reader = new LineNumberReader(new StringReader(string))) {89for (int i = 0; i < 8191; i++) reader.read();90reader.mark(1);91reader.read();92reader.reset();93assertEquals(reader.read(), 'X');94assertEquals(reader.read(), 'Y');95assertEquals(reader.read(), 'Z');96}97}98}99100101