001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }