1 /*
   2  * Copyright (c) 1998, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 4074875 4063511 8235792
  26    @summary Make sure LineNumberReader.read(char, int , int) will increase
  27             the linenumber correctly.
  28    */
  29 
  30 import java.io.IOException;
  31 import java.io.LineNumberReader;
  32 import java.io.StringReader;
  33 import java.util.function.Consumer;
  34 
  35 public class Read {
  36 
  37     public static void main(String[] args) throws Exception {
  38         testReadChars();
  39         testEofs();
  40     }
  41 
  42     private static void testReadChars() throws Exception {
  43         String s = "aaaa\nbbb\n";
  44         char[] buf = new char[5];
  45         int n = 0;
  46         int line = 0;
  47 
  48         LineNumberReader r = new LineNumberReader(new StringReader(s));
  49 
  50         do {
  51             n = r.read(buf, 0, buf.length);
  52         } while (n > 0);
  53 
  54         line = r.getLineNumber();
  55 
  56         if (line != 2)
  57             throw new Exception("Failed test: Expected line number: 2, got "
  58                                 + line);
  59     }
  60 
  61     private static void testEofs() throws Exception {
  62         String string = "first \n second";
  63 
  64         Consumer<LineNumberReader> c = (LineNumberReader r) -> {
  65             try {
  66                 while (r.read() != -1)
  67                     continue;
  68             } catch (IOException e) {
  69                 throw new RuntimeException(e);
  70             }
  71         };
  72         testEof(c, string, 2);
  73 
  74         c = (LineNumberReader r) -> {
  75             try {
  76                 char[] buf = new char[128];
  77                 while (r.read(buf) != -1)
  78                     continue;
  79             } catch (IOException e) {
  80                 throw new RuntimeException(e);
  81             }
  82         };
  83         testEof(c, string, 2);
  84 
  85         c = (LineNumberReader r) -> {
  86             try {
  87                 while (r.readLine() != null)
  88                     continue;
  89             } catch (IOException e) {
  90                 throw new RuntimeException(e);
  91             }
  92         };
  93         testEof(c, string, 2);
  94     }
  95 
  96     private static void testEof(Consumer<LineNumberReader> c, String s, int n)
  97         throws Exception {
  98         LineNumberReader r = new LineNumberReader(new StringReader(s));
  99         c.accept(r);
 100         int line;
 101         if ((line = r.getLineNumber()) != n)
 102             throw new Exception("Failed test: Expected line number: " + n  +
 103                 " , got " + line);
 104     }
 105 }