SystemStreamRuleImpl.java

  1. /**
  2.  * Powerunit - A JDK1.8 test framework
  3.  * Copyright (C) 2014 Mathieu Boretti.
  4.  *
  5.  * This file is part of Powerunit
  6.  *
  7.  * Powerunit is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation, either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * Powerunit is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20. package ch.powerunit.rules.impl;

  21. import java.io.PrintStream;
  22. import java.util.Objects;

  23. import ch.powerunit.TestContext;
  24. import ch.powerunit.rules.SystemStreamRule;

  25. /**
  26.  * Implementation
  27.  * @author borettim
  28.  * @since 0.4.0
  29.  */
  30. public final class SystemStreamRuleImpl implements SystemStreamRule {

  31.     private PrintStream oldErr;

  32.     private PrintStream oldOut;
  33.    
  34.     private final PrintStream remplacementOut;
  35.    
  36.     private final PrintStream remplacementErr;

  37.     /**
  38.      * @param remplacementOut
  39.      * @param remplacementErr
  40.      */
  41.     public SystemStreamRuleImpl(PrintStream remplacementOut,
  42.             PrintStream remplacementErr) {
  43.         this.remplacementOut = Objects.requireNonNull(remplacementOut,"remplacementOut can't be null");
  44.         this.remplacementErr = Objects.requireNonNull(remplacementErr,"remplacementErr can't be null");
  45.     }

  46.     /*
  47.      * (non-Javadoc)
  48.      *
  49.      * @see
  50.      * ch.powerunit.rules.TestListenerRule#onStart(ch.powerunit.TestContext)
  51.      */
  52.     @Override
  53.     public void onStart(TestContext<Object> context) {
  54.         oldErr = System.err;
  55.         oldOut = System.out;
  56.         try {
  57.             System.setErr(remplacementErr);
  58.             System.setOut(remplacementOut);
  59.         } catch (SecurityException e) {
  60.             // ignored
  61.         }
  62.     }

  63.     /*
  64.      * (non-Javadoc)
  65.      *
  66.      * @see ch.powerunit.rules.TestListenerRule#onEnd(ch.powerunit.TestContext)
  67.      */
  68.     @Override
  69.     public void onEnd(TestContext<Object> context) {
  70.         try {
  71.             System.setErr(oldErr);
  72.             System.setOut(oldOut);
  73.         } catch (SecurityException e) {
  74.             // ignored
  75.         }
  76.     }

  77.     @Override
  78.     public PrintStream getRealSystemOut() {
  79.         return oldOut;
  80.     }

  81.     @Override
  82.     public PrintStream getRealSystemErr() {
  83.         return oldErr;
  84.     }

  85. }