J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* @test |
| 25 | @summary Stochastic test of LineNumberReader |
| 26 | */ |
| 27 | |
| 28 | import java.io.*; |
| 29 | |
| 30 | |
| 31 | public class LineNumbers { |
| 32 | |
| 33 | static String enc = "UTF8"; |
| 34 | static String inEnc; |
| 35 | static String outEnc; |
| 36 | static int limit = 500; |
| 37 | |
| 38 | public static void main(String[] args) throws Exception { |
| 39 | PrintWriter log |
| 40 | = new PrintWriter(new OutputStreamWriter(System.err), true); |
| 41 | |
| 42 | if (inEnc == null) |
| 43 | inEnc = enc; |
| 44 | if (outEnc == null) |
| 45 | outEnc = enc; |
| 46 | |
| 47 | PipedOutputStream co = new PipedOutputStream(); |
| 48 | PipedInputStream ci = new PipedInputStream(co); |
| 49 | |
| 50 | BufferedWriter w = new BufferedWriter(new OutputStreamWriter(co, outEnc)); |
| 51 | LineNumberReader r = new LineNumberReader(new InputStreamReader(ci, inEnc)); |
| 52 | |
| 53 | Thread t1 = new Thread(new RandomLineSource(w, limit)); |
| 54 | Thread t2 = new Thread(new LineNumberSink(r, limit, log)); |
| 55 | t1.start(); |
| 56 | t2.start(); |
| 57 | t1.join(); |
| 58 | t2.join(); |
| 59 | } |
| 60 | |
| 61 | } |
| 62 | |
| 63 | |
| 64 | class LineNumberSink implements Runnable { |
| 65 | |
| 66 | LineNumberReader r; |
| 67 | int limit; |
| 68 | PrintWriter log; |
| 69 | |
| 70 | LineNumberSink(LineNumberReader r, int limit, PrintWriter log) { |
| 71 | this.r = r; |
| 72 | this.limit = limit; |
| 73 | this.log = log; |
| 74 | } |
| 75 | |
| 76 | public void run() { |
| 77 | String s; |
| 78 | int n = 0; |
| 79 | |
| 80 | try { |
| 81 | while ((s = r.readLine()) != null) { |
| 82 | n++; |
| 83 | int ln = r.getLineNumber(); |
| 84 | log.println("[" + ln + "] " + s.length()); |
| 85 | log.println(s); |
| 86 | if (log.checkError()) |
| 87 | log.println("Conversion errors"); /* #### */ |
| 88 | if (n != ln) |
| 89 | throw new RuntimeException("Line number mismatch: Expected " + n + ", got " + ln); |
| 90 | } |
| 91 | if (n != limit) |
| 92 | throw new RuntimeException("Incorrect line count"); |
| 93 | } |
| 94 | catch (IOException x) { |
| 95 | throw new RuntimeException(x.toString()); |
| 96 | } |
| 97 | log.println(n + " lines read"); |
| 98 | } |
| 99 | |
| 100 | } |