1.1 --- a/rt/emul/compact/src/main/java/java/lang/System.java Fri Nov 22 14:20:49 2013 +0100
1.2 +++ b/rt/emul/compact/src/main/java/java/lang/System.java Tue Dec 10 17:14:15 2013 +0100
1.3 @@ -17,9 +17,11 @@
1.4 */
1.5 package java.lang;
1.6
1.7 +import java.io.BufferedOutputStream;
1.8 import java.io.ByteArrayInputStream;
1.9 -import java.io.ByteArrayOutputStream;
1.10 +import java.io.IOException;
1.11 import java.io.InputStream;
1.12 +import java.io.OutputStream;
1.13 import java.io.PrintStream;
1.14 import java.util.Properties;
1.15 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.16 @@ -88,9 +90,42 @@
1.17
1.18 public final static PrintStream err;
1.19
1.20 + public static void setOut(PrintStream out) {
1.21 + throw new SecurityException();
1.22 + }
1.23 +
1.24 + public static void setIn(InputStream in) {
1.25 + throw new SecurityException();
1.26 + }
1.27 +
1.28 + public static void setErr(PrintStream err) {
1.29 + throw new SecurityException();
1.30 + }
1.31 +
1.32 static {
1.33 in = new ByteArrayInputStream(new byte[0]);
1.34 - out = err = new PrintStream(new ByteArrayOutputStream());
1.35 + out = new PrintStream(new BufferedOutputStream(new SystemStream("log")));
1.36 + err = new PrintStream(new BufferedOutputStream(new SystemStream("warn")));
1.37 }
1.38 -
1.39 +
1.40 + private static final class SystemStream extends OutputStream {
1.41 + private final String method;
1.42 +
1.43 + public SystemStream(String method) {
1.44 + this.method = method;
1.45 + }
1.46 +
1.47 + @Override
1.48 + public void write(byte b[], int off, int len) throws IOException {
1.49 + write(method, new String(b, off, len, "UTF-8"));
1.50 + }
1.51 +
1.52 + @JavaScriptBody(args = { "method", "b" }, body = "if (typeof console !== 'undefined') console[method](b.toString());")
1.53 + private static native void write(String method, String b);
1.54 +
1.55 + @Override
1.56 + public void write(int b) throws IOException {
1.57 + write(new byte[] { (byte)b });
1.58 + }
1.59 + } // end of SystemStream
1.60 }
2.1 --- a/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/SystemTest.java Fri Nov 22 14:20:49 2013 +0100
2.2 +++ b/rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/SystemTest.java Tue Dec 10 17:14:15 2013 +0100
2.3 @@ -17,6 +17,9 @@
2.4 */
2.5 package org.apidesign.bck2brwsr.tck;
2.6
2.7 +import java.io.ByteArrayOutputStream;
2.8 +import java.io.PrintStream;
2.9 +import org.apidesign.bck2brwsr.core.JavaScriptBody;
2.10 import org.apidesign.bck2brwsr.vmtest.Compare;
2.11 import org.apidesign.bck2brwsr.vmtest.VMTest;
2.12 import org.testng.annotations.Factory;
2.13 @@ -30,6 +33,30 @@
2.14 return System.getProperty("os.name") != null;
2.15 }
2.16
2.17 + @Compare public String captureStdOut() throws Exception {
2.18 + Object capture = initCapture();
2.19 + System.out.println("Ahoj");
2.20 + return textCapture(capture);
2.21 + }
2.22 +
2.23 + @JavaScriptBody(args = {}, body = ""
2.24 + + "var lines = [];"
2.25 + + "console.log = function(l) { lines.push(l); };"
2.26 + + "return lines;")
2.27 + Object initCapture() {
2.28 + ByteArrayOutputStream os = new ByteArrayOutputStream();
2.29 + PrintStream ps = new PrintStream(os);
2.30 +
2.31 + System.setOut(ps);
2.32 + return os;
2.33 + }
2.34 +
2.35 + @JavaScriptBody(args = { "o" }, body = "return o.join('');")
2.36 + String textCapture(Object o) throws java.io.IOException {
2.37 + ByteArrayOutputStream b = (ByteArrayOutputStream) o;
2.38 + return new String(b.toByteArray(), "UTF-8");
2.39 + }
2.40 +
2.41 @Factory public static Object[] create() {
2.42 return VMTest.create(SystemTest.class);
2.43 }