Example usage for java.lang System setOut

List of usage examples for java.lang System setOut

Introduction

In this page you can find the example usage for java.lang System setOut.

Prototype

public static void setOut(PrintStream out) 

Source Link

Document

Reassigns the "standard" output stream.

Usage

From source file:org.nodatime.tzvalidate.JodaDump.java

private static Map<String, DateTimeZone> getZones(String source) throws IOException {
    if (source == null) {
        return DateTimeZone.getAvailableIDs().stream().collect(Collectors.toMap(id -> id, DateTimeZone::forID));
    }//  w w  w  . j av  a 2s . com

    File directory = new File(source);
    if (!directory.isDirectory()) {
        throw new IOException(directory + " is not a directory");
    }
    File[] files = KNOWN_FILES.stream().map(f -> new File(directory, f)).toArray(File[]::new);
    ZoneInfoCompiler compiler = new ZoneInfoCompiler();
    PrintStream out = System.out;
    // Ignore standard output while compiling...
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    Map<String, DateTimeZone> zones = compiler.compile(null, files);
    System.setOut(out);
    return zones;
}

From source file:com.amazonaws.codepipeline.jenkinsplugin.TestUtils.java

public static ByteArrayOutputStream setOutputStream() {
    final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    System.setOut(new PrintStream(outContent));
    return outContent;
}

From source file:com.example.FirestoreSampleAppTests.java

@BeforeClass
public static void prepare() {
    assumeThat("Firestore-sample tests are disabled. Please use '-Dit.firestore=true' " + "to enable them. ",
            System.getProperty("it.firestore"), is("true"));

    systemOut = System.out;/*from  w ww. j av  a  2  s .  c  o  m*/
    baos = new ByteArrayOutputStream();
    TeeOutputStream out = new TeeOutputStream(systemOut, baos);
    System.setOut(new PrintStream(out));
}

From source file:Main.java

public Console() {
    JTextArea textArea = new JTextArea(24, 80);
    textArea.setBackground(Color.BLACK);
    textArea.setForeground(Color.LIGHT_GRAY);
    textArea.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    System.setOut(new PrintStream(new OutputStream() {
        @Override//  w  w w. jav  a 2  s. c  o  m
        public void write(int b) throws IOException {
            textArea.append(String.valueOf((char) b));
        }
    }));
    frame.add(textArea);
}

From source file:org.apache.axis2.util.PrettyPrinter.java

/**
 * Pretty prints contents of the java source file.
 *
 * @param file/*from  ww  w. j  av  a2  s. c  o m*/
 */
public static void prettify(File file) {
    // If the user has set "axis2.jalopy=false" on the system property,
    // then just return back to caller
    String property = System.getProperty("axis2.jalopy");
    if ((property == null) || !JavaUtils.isTrueExplicitly(property)) {
        return;
    }
    PrintStream backupOutputStream = System.out;
    PrintStream backupErrorStream = System.err;
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    System.setErr(new PrintStream(new ByteArrayOutputStream()));
    try {
        Class clazzConfigurator = Loader.loadClass("org.apache.log4j.PropertyConfigurator");
        Method configure = clazzConfigurator.getMethod("configure", new Class[] { Properties.class });
        Properties properties = new Properties();
        properties.setProperty("log4j.logger.de.hunsicker.jalopy.io",
                System.getProperty("log4j.logger.de.hunsicker.jalopy.io", "FATAL"));
        configure.invoke(null, new Object[] { properties });

        // Create an instance of the Jalopy bean
        Class clazz = Loader.loadClass("de.hunsicker.jalopy.Jalopy");
        Object prettifier = clazz.newInstance();

        // Set the input file
        Method input = clazz.getMethod("setInput", new Class[] { File.class });
        input.invoke(prettifier, new Object[] { file });

        // Set the output file
        Method output = clazz.getMethod("setOutput", new Class[] { File.class });
        output.invoke(prettifier, new Object[] { file });

        Class clazz2 = Loader.loadClass("de.hunsicker.jalopy.storage.Convention");
        Method instance = clazz2.getMethod("getInstance", new Class[] {});
        Object settings = instance.invoke(null, new Object[] {});

        Class clazz3 = Loader.loadClass("de.hunsicker.jalopy.storage.ConventionKeys");
        Field field = clazz3.getField("COMMENT_FORMAT_MULTI_LINE");
        Object key = field.get(null);
        Method put = clazz2.getMethod("put", new Class[] { key.getClass(), String.class });
        put.invoke(settings, new Object[] { key, "true" });

        // format and overwrite the given input file
        Method format = clazz.getMethod("format", new Class[] {});
        format.invoke(prettifier, new Object[] {});
        log.debug("Pretty printed file : " + file);
    } catch (ClassNotFoundException e) {
        log.debug("Jalopy/Log4j not found - unable to pretty print " + file);
    } catch (Exception e) {
        log.warn("Exception occurred while trying to pretty print file " + file, e);
    } catch (Throwable t) {
        log.debug("Exception occurred while trying to pretty print file " + file, t);
    } finally {
        System.setOut(backupOutputStream);
        System.setErr(backupErrorStream);
    }
}

From source file:com.example.FirestoreSampleAppTests.java

@AfterClass
public static void bringBack() {
    System.setOut(systemOut);
}

From source file:alluxio.cli.fs.command.ChownCommandTest.java

@Before
public void setupStreams() {
    System.setOut(new PrintStream(mOutput));
    System.setErr(new PrintStream(mError));
}

From source file:org.aksw.gerbil.web.config.RootConfig.java

protected static void replaceSystemStreams() {
    System.setOut(new PrintStream(new ConsoleLogger(false), true));
    System.setErr(new PrintStream(new ConsoleLogger(true), true));
}

From source file:org.timer4method.test.Timer4MethodTest.java

@Before
public void setUpStreams() {
    System.setOut(new PrintStream(outContent));
}

From source file:net.jperf.LogParserTest.java

public void testLogParserMain() throws Exception {
    PrintStream realOut = System.out;
    ByteArrayOutputStream fakeOut = new ByteArrayOutputStream();
    System.setOut(new PrintStream(fakeOut, true));
    try {/*ww w .jav  a 2s  . c o m*/
        //usage
        realOut.println("-- Usage Test --");
        LogParser.runMain(new String[] { "--help" });
        realOut.println(fakeOut.toString());
        assertTrue(fakeOut.toString().indexOf("Usage") >= 0);
        fakeOut.reset();

        //log on std in, write to std out
        InputStream realIn = System.in;
        ByteArrayInputStream fakeIn = new ByteArrayInputStream(testLog.getBytes());
        System.setIn(fakeIn);
        try {
            realOut.println("-- Std in -> Std out Test --");
            LogParser.runMain(new String[0]);
            realOut.println(fakeOut.toString());
            assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0
                    && fakeOut.toString().indexOf("tag3") >= 0);
            fakeOut.reset();
        } finally {
            System.setIn(realIn);
        }

        //Log from a file
        FileUtils.writeStringToFile(new File("./target/logParserTest.log"), testLog);

        //log from file, write to std out
        realOut.println("-- File in -> Std out Test --");
        LogParser.runMain(new String[] { "./target/logParserTest.log" });
        realOut.println(fakeOut.toString());
        assertTrue(fakeOut.toString().indexOf("tag") >= 0 && fakeOut.toString().indexOf("tag2") >= 0
                && fakeOut.toString().indexOf("tag3") >= 0);
        fakeOut.reset();

        //CSV format test
        realOut.println("-- File in -> Std out Test with CSV --");
        LogParser.runMain(new String[] { "-f", "csv", "./target/logParserTest.log" });
        realOut.println(fakeOut.toString());
        assertTrue(fakeOut.toString().indexOf("\"tag\",") >= 0 && fakeOut.toString().indexOf("\"tag2\",") >= 0
                && fakeOut.toString().indexOf("\"tag3\",") >= 0);
        fakeOut.reset();

        //log from file, write to file
        realOut.println("-- File in -> File out Test --");
        LogParser.runMain(new String[] { "-o", "./target/statistics.out", "./target/logParserTest.log" });
        String statsOut = FileUtils.readFileToString(new File("./target/statistics.out"));
        realOut.println(statsOut);
        assertTrue(
                statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0);

        //log from file, write to file, different timeslice
        realOut.println("-- File in -> File out with different timeslice Test --");
        LogParser.runMain(new String[] { "-o", "./target/statistics.out", "--timeslice", "120000",
                "./target/logParserTest.log" });
        statsOut = FileUtils.readFileToString(new File("./target/statistics.out"));
        realOut.println(statsOut);
        assertTrue(
                statsOut.indexOf("tag") >= 0 && statsOut.indexOf("tag2") >= 0 && statsOut.indexOf("tag3") >= 0);

        //missing param test
        realOut.println("-- Missing param test --");
        assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "-o" }));

        //unknown arg test
        realOut.println("-- Unknown arg test --");
        assertEquals(1, LogParser.runMain(new String[] { "./target/logParserTest.log", "--foo" }));
        realOut.println(fakeOut);
        assertTrue(fakeOut.toString().indexOf("Unknown") >= 0);

        //graphing test
        realOut.println("-- File in -> File out with graphing --");
        LogParser.runMain(new String[] { "-o", "./target/statistics.out", "-g", "./target/perfGraphs.out",
                "./src/test/resources/net/jperf/dummyLog.txt" });
        statsOut = FileUtils.readFileToString(new File("./target/statistics.out"));
        realOut.println(statsOut);
        String graphsOut = FileUtils.readFileToString(new File("./target/perfGraphs.out"));
        realOut.println(graphsOut);
        assertTrue(graphsOut.indexOf("chtt=TPS") > 0 && graphsOut.indexOf("chtt=Mean") > 0);
    } finally {
        System.setOut(realOut);
    }
}