rt/emul/compact/src/test/java/org/apidesign/bck2brwsr/tck/SystemTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 10 Dec 2013 17:14:15 +0100
changeset 1416 a0a31f8f7dc1
parent 1328 1d1d70f6828b
child 1417 f45b7126d21d
permissions -rw-r--r--
Tim wanted functional System.out, so let's make it do something useful
jaroslav@1328
     1
/**
jaroslav@1328
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1328
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1328
     4
 *
jaroslav@1328
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1328
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1328
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1328
     8
 *
jaroslav@1328
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1328
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1328
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1328
    12
 * GNU General Public License for more details.
jaroslav@1328
    13
 *
jaroslav@1328
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1328
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1328
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1328
    17
 */
jaroslav@1328
    18
package org.apidesign.bck2brwsr.tck;
jaroslav@1328
    19
jaroslav@1416
    20
import java.io.ByteArrayOutputStream;
jaroslav@1416
    21
import java.io.PrintStream;
jaroslav@1416
    22
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@1328
    23
import org.apidesign.bck2brwsr.vmtest.Compare;
jaroslav@1328
    24
import org.apidesign.bck2brwsr.vmtest.VMTest;
jaroslav@1328
    25
import org.testng.annotations.Factory;
jaroslav@1328
    26
jaroslav@1328
    27
/**
jaroslav@1328
    28
 *
jaroslav@1328
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@1328
    30
 */
jaroslav@1328
    31
public class SystemTest {
jaroslav@1328
    32
    @Compare public boolean nonNullOSName() {
jaroslav@1328
    33
        return System.getProperty("os.name") != null;
jaroslav@1328
    34
    }
jaroslav@1328
    35
jaroslav@1416
    36
    @Compare public String captureStdOut() throws Exception {
jaroslav@1416
    37
        Object capture = initCapture();
jaroslav@1416
    38
        System.out.println("Ahoj");
jaroslav@1416
    39
        return textCapture(capture);
jaroslav@1416
    40
    }
jaroslav@1416
    41
    
jaroslav@1416
    42
    @JavaScriptBody(args = {}, body = ""
jaroslav@1416
    43
        + "var lines = [];"
jaroslav@1416
    44
        + "console.log = function(l) { lines.push(l); };"
jaroslav@1416
    45
        + "return lines;")
jaroslav@1416
    46
    Object initCapture() {
jaroslav@1416
    47
        ByteArrayOutputStream os = new ByteArrayOutputStream();
jaroslav@1416
    48
        PrintStream ps = new PrintStream(os);
jaroslav@1416
    49
        
jaroslav@1416
    50
        System.setOut(ps);
jaroslav@1416
    51
        return os;
jaroslav@1416
    52
    }
jaroslav@1416
    53
    
jaroslav@1416
    54
    @JavaScriptBody(args = { "o" }, body = "return o.join('');")
jaroslav@1416
    55
    String textCapture(Object o) throws java.io.IOException {
jaroslav@1416
    56
        ByteArrayOutputStream b = (ByteArrayOutputStream) o;
jaroslav@1416
    57
        return new String(b.toByteArray(), "UTF-8");
jaroslav@1416
    58
    }
jaroslav@1416
    59
    
jaroslav@1328
    60
    @Factory public static Object[] create() {
jaroslav@1328
    61
        return VMTest.create(SystemTest.class);
jaroslav@1328
    62
    }
jaroslav@1328
    63
}