001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayOutputStream;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.FileDescriptor;
021    import java.io.FileOutputStream;
022    import java.io.PrintStream;
023    import java.io.UnsupportedEncodingException;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class ConsoleTestUtil {
029    
030            public static UnsyncByteArrayOutputStream hijackStdErr() {
031                    System.err.flush();
032    
033                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
034                            new UnsyncByteArrayOutputStream();
035    
036                    PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
037    
038                    System.setErr(printStream);
039    
040                    return unsyncByteArrayOutputStream;
041            }
042    
043            public static UnsyncByteArrayOutputStream hijackStdOut() {
044                    System.out.flush();
045    
046                    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream =
047                            new UnsyncByteArrayOutputStream();
048    
049                    PrintStream printStream = new PrintStream(unsyncByteArrayOutputStream);
050    
051                    System.setOut(printStream);
052    
053                    return unsyncByteArrayOutputStream;
054            }
055    
056            public static String restoreStdErr(
057                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream)
058                    throws UnsupportedEncodingException {
059    
060                    System.out.flush();
061    
062                    FileOutputStream fileOutputStream = new FileOutputStream(
063                            FileDescriptor.err);
064    
065                    PrintStream printStream = new PrintStream(fileOutputStream);
066    
067                    System.setErr(printStream);
068    
069                    return unsyncByteArrayOutputStream.toString(
070                            StringPool.DEFAULT_CHARSET_NAME);
071            }
072    
073            public static String restoreStdOut(
074                            UnsyncByteArrayOutputStream unsyncByteArrayOutputStream)
075                    throws UnsupportedEncodingException {
076    
077                    System.out.flush();
078    
079                    FileOutputStream fileOutputStream = new FileOutputStream(
080                            FileDescriptor.out);
081    
082                    PrintStream printStream = new PrintStream(fileOutputStream);
083    
084                    System.setOut(printStream);
085    
086                    return unsyncByteArrayOutputStream.toString(
087                            StringPool.DEFAULT_CHARSET_NAME);
088            }
089    
090    }