LineNumbers.java revision 2362
871N/A/*
871N/A * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
871N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
871N/A *
871N/A * This code is free software; you can redistribute it and/or modify it
871N/A * under the terms of the GNU General Public License version 2 only, as
871N/A * published by the Free Software Foundation.
871N/A *
871N/A * This code is distributed in the hope that it will be useful, but WITHOUT
871N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
871N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
871N/A * version 2 for more details (a copy is included in the LICENSE file that
871N/A * accompanied this code).
871N/A *
871N/A * You should have received a copy of the GNU General Public License version
871N/A * 2 along with this work; if not, write to the Free Software Foundation,
871N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
871N/A *
873N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
871N/A * or visit www.oracle.com if you need additional information or have any
871N/A * questions.
871N/A */
871N/A
871N/A/* @test
5061N/A @summary Stochastic test of LineNumberReader
871N/A */
3832N/A
871N/Aimport java.io.*;
871N/A
2175N/A
2175N/Apublic class LineNumbers {
2175N/A
871N/A static String enc = "UTF8";
871N/A static String inEnc;
871N/A static String outEnc;
3832N/A static int limit = 500;
3832N/A
3832N/A public static void main(String[] args) throws Exception {
871N/A PrintWriter log
3832N/A = new PrintWriter(new OutputStreamWriter(System.err), true);
3832N/A
3832N/A if (inEnc == null)
871N/A inEnc = enc;
871N/A if (outEnc == null)
871N/A outEnc = enc;
3832N/A
3832N/A PipedOutputStream co = new PipedOutputStream();
3832N/A PipedInputStream ci = new PipedInputStream(co);
927N/A
3832N/A BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc));
3832N/A LineNumberReader r = new LineNumberReader(new InputStreamReader(ci, inEnc));
3832N/A
3832N/A Thread t1 = new Thread(new RandomLineSource(w, limit));
871N/A Thread t2 = new Thread(new LineNumberSink(r, limit, log));
3832N/A t1.start();
3832N/A t2.start();
4723N/A t1.join();
3832N/A t2.join();
900N/A }
3832N/A
3832N/A}
964N/A
3832N/A
3832N/Aclass LineNumberSink implements Runnable {
3832N/A
3832N/A LineNumberReader r;
1835N/A int limit;
1835N/A PrintWriter log;
3832N/A
5353N/A LineNumberSink(LineNumberReader r, int limit, PrintWriter log) {
1835N/A this.r = r;
1835N/A this.limit = limit;
1835N/A this.log = log;
3832N/A }
4458N/A
4458N/A public void run() {
4458N/A String s;
3832N/A int n = 0;
3832N/A
3832N/A try {
3832N/A while ((s = r.readLine()) != null) {
3832N/A n++;
3832N/A int ln = r.getLineNumber();
4458N/A log.println("[" + ln + "] " + s.length());
4458N/A log.println(s);
4458N/A if (log.checkError())
4458N/A log.println("Conversion errors"); /* #### */
4458N/A if (n != ln)
4458N/A throw new RuntimeException("Line number mismatch: Expected " + n + ", got " + ln);
3832N/A }
3832N/A if (n != limit)
3832N/A throw new RuntimeException("Incorrect line count");
3832N/A }
3832N/A catch (IOException x) {
3832N/A throw new RuntimeException(x.toString());
3832N/A }
3832N/A log.println(n + " lines read");
3832N/A }
3832N/A
3832N/A}
3832N/A