Path: blob/master/test/jdk/java/io/LineNumberReader/Read.java
41149 views
/*1* Copyright (c) 1998, 2020, 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 4074875 4063511 823579225@summary Make sure LineNumberReader.read(char, int , int) will increase26the linenumber correctly.27*/2829import java.io.IOException;30import java.io.LineNumberReader;31import java.io.StringReader;32import java.util.function.Consumer;3334public class Read {3536public static void main(String[] args) throws Exception {37testReadChars();38testEofs();39}4041private static void testReadChars() throws Exception {42String s = "aaaa\nbbb\n";43char[] buf = new char[5];44int n = 0;45int line = 0;4647LineNumberReader r = new LineNumberReader(new StringReader(s));4849do {50n = r.read(buf, 0, buf.length);51} while (n > 0);5253line = r.getLineNumber();5455if (line != 2)56throw new Exception("Failed test: Expected line number: 2, got "57+ line);58}5960private static void testEofs() throws Exception {61String string = "first \n second";6263Consumer<LineNumberReader> c = (LineNumberReader r) -> {64try {65while (r.read() != -1)66continue;67} catch (IOException e) {68throw new RuntimeException(e);69}70};71testEof(c, string, 2);7273c = (LineNumberReader r) -> {74try {75char[] buf = new char[128];76while (r.read(buf) != -1)77continue;78} catch (IOException e) {79throw new RuntimeException(e);80}81};82testEof(c, string, 2);8384c = (LineNumberReader r) -> {85try {86while (r.readLine() != null)87continue;88} catch (IOException e) {89throw new RuntimeException(e);90}91};92testEof(c, string, 2);93}9495private static void testEof(Consumer<LineNumberReader> c, String s, int n)96throws Exception {97LineNumberReader r = new LineNumberReader(new StringReader(s));98c.accept(r);99int line;100if ((line = r.getLineNumber()) != n)101throw new Exception("Failed test: Expected line number: " + n +102" , got " + line);103}104}105106107