From eb54507e429df9d2f333f105958b25591c8cb67c Mon Sep 17 00:00:00 2001 From: Ben Hamilton Date: Wed, 25 Jan 2017 16:36:33 -0800 Subject: [PATCH 1/2] Consolidate multiple copies of StreamVacuum, make use UTF-8 --- .../antlr/v4/test/runtime/StreamVacuum.java | 47 ++++++++++++++++++ .../v4/test/runtime/cpp/BaseCppTest.java | 40 +-------------- .../test/runtime/csharp/BaseCSharpTest.java | 37 +------------- .../antlr/v4/test/runtime/go/BaseGoTest.java | 43 +--------------- .../v4/test/runtime/java/BaseJavaTest.java | 38 +------------- .../javascript/browser/BaseBrowserTest.java | 40 --------------- .../runtime/javascript/node/BaseNodeTest.java | 47 +----------------- .../test/runtime/python/BasePythonTest.java | 43 +--------------- .../v4/test/runtime/swift/BaseSwiftTest.java | 49 +------------------ 9 files changed, 56 insertions(+), 328 deletions(-) create mode 100644 runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java new file mode 100644 index 000000000..ecbd6fd84 --- /dev/null +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved. + * Use of this file is governed by the BSD 3-clause license that + * can be found in the LICENSE.txt file in the project root. + */ +package org.antlr.v4.test.runtime; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public final class StreamVacuum implements Runnable { + private StringBuilder buf = new StringBuilder(); + private BufferedReader in; + private Thread sucker; + public StreamVacuum(InputStream in) { + this.in = new BufferedReader( new InputStreamReader(in, StandardCharsets.UTF_8) ); + } + public void start() { + sucker = new Thread(this); + sucker.start(); + } + @Override + public void run() { + try { + String line = in.readLine(); + while (line!=null) { + buf.append(line); + buf.append('\n'); + line = in.readLine(); + } + } + catch (IOException ioe) { + System.err.println("can't read output from process"); + } + } + /** wait for the thread to finish */ + public void join() throws InterruptedException { + sucker.join(); + } + @Override + public String toString() { + return buf.toString(); + } +} diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java index fbe0999e8..a9f324032 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/cpp/BaseCppTest.java @@ -36,6 +36,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; @@ -46,11 +47,7 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; -import java.io.BufferedReader; import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URISyntaxException; @@ -750,41 +747,6 @@ public class BaseCppTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - public StreamVacuum(InputStream in) { - this.in = new BufferedReader( new InputStreamReader(in) ); - } - public void start() { - sucker = new Thread(this); - sucker.start(); - } - @Override - public void run() { - try { - String line = in.readLine(); - while (line!=null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java index 84ddd08d2..8433a9e25 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/csharp/BaseCSharpTest.java @@ -13,6 +13,7 @@ import org.antlr.v4.runtime.WritableToken; import org.antlr.v4.runtime.misc.Utils; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.GrammarSemanticsMessage; import org.junit.rules.TestRule; @@ -31,7 +32,6 @@ import javax.xml.transform.stream.StreamResult; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathFactory; -import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -535,41 +535,6 @@ public class BaseCSharpTest implements RuntimeTestSupport /*, SpecialRuntimeTest } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - public StreamVacuum(InputStream in) { - this.in = new BufferedReader( new InputStreamReader(in) ); - } - public void start() { - sucker = new Thread(this); - sucker.start(); - } - @Override - public void run() { - try { - String line = in.readLine(); - while (line!=null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java index e12858b4c..df3d2b2fd 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseGoTest.java @@ -33,6 +33,7 @@ import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; @@ -43,8 +44,6 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; @@ -52,7 +51,6 @@ import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; @@ -572,45 +570,6 @@ public class BaseGoTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - - public StreamVacuum(InputStream in) { - this.in = new BufferedReader(new InputStreamReader(in)); - } - - public void start() { - sucker = new Thread(this); - sucker.start(); - } - - @Override - public void run() { - try { - String line = in.readLine(); - while (line != null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception { ANTLRMessage foundMsg = null; diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java index b8e348d4b..a939df3fa 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/java/BaseJavaTest.java @@ -40,6 +40,7 @@ import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.BaseRuntimeTest; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.GrammarSemanticsMessage; @@ -53,7 +54,6 @@ import javax.tools.JavaCompiler; import javax.tools.JavaFileObject; import javax.tools.StandardJavaFileManager; import javax.tools.ToolProvider; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -703,6 +703,7 @@ public class BaseJavaTest implements RuntimeTestSupport { try { String[] args = new String[] { "java", "-classpath", tmpdir+pathSep+CLASSPATH, + "-Dfile.encoding=UTF-8", className, new File(tmpdir, "input").getAbsolutePath() }; // String cmdLine = Utils.join(args, " "); @@ -824,41 +825,6 @@ public class BaseJavaTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - public StreamVacuum(InputStream in) { - this.in = new BufferedReader( new InputStreamReader(in) ); - } - public void start() { - sucker = new Thread(this); - sucker.start(); - } - @Override - public void run() { - try { - String line = in.readLine(); - while (line!=null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/browser/BaseBrowserTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/browser/BaseBrowserTest.java index 781257add..d7e01e517 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/browser/BaseBrowserTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/browser/BaseBrowserTest.java @@ -53,13 +53,8 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.BindException; import java.util.ArrayList; import java.util.Arrays; @@ -529,41 +524,6 @@ public abstract class BaseBrowserTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - public StreamVacuum(InputStream in) { - this.in = new BufferedReader( new InputStreamReader(in) ); - } - public void start() { - sucker = new Thread(this); - sucker.start(); - } - @Override - public void run() { - try { - String line = in.readLine(); - while (line!=null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/BaseNodeTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/BaseNodeTest.java index 22e9df979..a47da786c 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/BaseNodeTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/javascript/node/BaseNodeTest.java @@ -33,6 +33,7 @@ import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; @@ -43,15 +44,8 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileOutputStream; import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -525,45 +519,6 @@ public class BaseNodeTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - - public StreamVacuum(InputStream in) { - this.in = new BufferedReader(new InputStreamReader(in)); - } - - public void start() { - sucker = new Thread(this); - sucker.start(); - } - - @Override - public void run() { - try { - String line = in.readLine(); - while (line != null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception { ANTLRMessage foundMsg = null; diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java index 052a422b4..d9ace312d 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/python/BasePythonTest.java @@ -36,6 +36,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.semantics.SemanticPipeline; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.antlr.v4.tool.ANTLRMessage; import org.antlr.v4.tool.DOTGenerator; import org.antlr.v4.tool.Grammar; @@ -49,13 +50,7 @@ import org.stringtemplate.v4.ST; import org.stringtemplate.v4.STGroup; import org.stringtemplate.v4.STGroupString; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -494,6 +489,7 @@ public abstract class BasePythonTest implements RuntimeTestSupport { try { ProcessBuilder builder = new ProcessBuilder( pythonPath, modulePath, inputPath ); builder.environment().put("PYTHONPATH",runtimePath); + builder.environment().put("PYTHONIOENCODING", "utf-8"); builder.directory(new File(tmpdir)); Process process = builder.start(); StreamVacuum stdoutVacuum = new StreamVacuum(process.getInputStream()); @@ -652,41 +648,6 @@ public abstract class BasePythonTest implements RuntimeTestSupport { } } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - public StreamVacuum(InputStream in) { - this.in = new BufferedReader( new InputStreamReader(in) ); - } - public void start() { - sucker = new Thread(this); - sucker.start(); - } - @Override - public void run() { - try { - String line = in.readLine(); - while (line!=null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - } - } - /** wait for the thread to finish */ - public void join() throws InterruptedException { - sucker.join(); - } - @Override - public String toString() { - return buf.toString(); - } - } - protected void checkGrammarSemanticsError(ErrorQueue equeue, GrammarSemanticsMessage expectedMessage) throws Exception diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java index 7da2dfab4..e229f6981 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java @@ -9,15 +9,11 @@ package org.antlr.v4.test.runtime.swift; import org.antlr.v4.Tool; import org.antlr.v4.test.runtime.ErrorQueue; import org.antlr.v4.test.runtime.RuntimeTestSupport; +import org.antlr.v4.test.runtime.StreamVacuum; import org.stringtemplate.v4.ST; -import java.io.BufferedReader; -import java.io.BufferedWriter; import java.io.File; -import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; @@ -119,49 +115,6 @@ public class BaseSwiftTest implements RuntimeTestSupport { return runProcess(argsString, ANTLR_FRAMEWORK_DIR); } - public static class StreamVacuum implements Runnable { - StringBuilder buf = new StringBuilder(); - BufferedReader in; - Thread sucker; - - public StreamVacuum(InputStream in) { - this.in = new BufferedReader(new InputStreamReader(in)); - } - - public void start() { - sucker = new Thread(this); - sucker.start(); - } - - @Override - public void run() { - try { - String line = in.readLine(); - while (line != null) { - buf.append(line); - buf.append('\n'); - line = in.readLine(); - } - } - catch (IOException ioe) { - System.err.println("can't read output from process"); - ioe.printStackTrace(System.err); - } - } - - /** - * wait for the thread to finish - */ - public void join() throws InterruptedException { - sucker.join(); - } - - @Override - public String toString() { - return buf.toString(); - } - } - public String tmpdir = null; /** From f0947f4365d908cdbc63578ad65faf53cbc808ca Mon Sep 17 00:00:00 2001 From: "Ben Hamilton (Ben Gertzfield)" Date: Mon, 30 Jan 2017 09:56:02 -0700 Subject: [PATCH 2/2] Update copyright in StreamVacuum.java --- .../test/org/antlr/v4/test/runtime/StreamVacuum.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java index ecbd6fd84..4ab3ab959 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/StreamVacuum.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012-2016 The ANTLR Project. All rights reserved. + * Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. * Use of this file is governed by the BSD 3-clause license that * can be found in the LICENSE.txt file in the project root. */