All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.uima.test.junit_extension.ManageOutputDevice Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.apache.uima.test.junit_extension;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * This class manupulates the output of the system. Methode output like System.out.println("blah")
 * may be send to something else than the console.
 */
public class ManageOutputDevice {

  private static PrintStream sysOutPS;

  private static PrintStream sysErrPS;
  static {
    sysOutPS = System.out;
    sysErrPS = System.err;
  }

  /**
   * sets the System.out to a file based java.io.PrintStream
   * 
   * @param descriptor
   *          a full qualified filename, see {@link java.io.File#File(String pathname)}
   * @throws FileNotFoundException
   *           -
   */
  public static void setSysOutToFile(String descriptor) throws FileNotFoundException {
    File f = new File(descriptor);
    PrintStream printStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)),
            true);
    System.setOut(printStream);
  }

  /**
   * sets the System.out to the value, which was set, when the class was loaded by the
   * VM.
   */
  public static void setSysOutToDefault() {
    System.setOut(sysOutPS);
  }

  /**
   * sets the System.out to a virtual java.io.ByteArrayOutputStream
   */
  public static void setSysOutToNirvana() {
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
  }

  /**
   * sets the System.err to a file based java.io.PrintStream
   * 
   * @param descriptor
   *          -
   * @throws FileNotFoundException
   *           -
   */
  public static void setSysErrToFile(String descriptor) throws FileNotFoundException {
    File f = new File(descriptor);
    PrintStream printStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(f)),
            true);
    System.setErr(printStream);
  }

  /**
   * sets the System.err to the value, which was set, when this class was loaded by the
   * VM.
   */
  public static void setSysErrToDefault() {
    System.setErr(sysErrPS);
  }

  /**
   * sets the System.err to a virtual java.io.ByteArrayOutputStream
   */
  public static void setSysErrToNirvana() {
    System.setErr(new PrintStream(new ByteArrayOutputStream()));
  }

  /**
   * sets the System.err and System.out to a virtual
   * java.io.ByteArrayOutputStream
   */
  public static void setAllSystemOutputToNirvana() {
    setSysErrToNirvana();
    setSysOutToNirvana();
  }

  /**
   * sets the System.err and System.out to their values, which were set,
   * when this class was loaded.
   */
  public static void setAllSystemOutputToDefault() {
    setSysErrToDefault();
    setSysOutToDefault();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy