IdeOnlyConsoleOutput.java

  1. package org.jbehave.core.reporters;

  2. import static java.util.Arrays.asList;

  3. import java.io.ByteArrayOutputStream;
  4. import java.io.PrintStream;
  5. import java.util.List;
  6. import java.util.Properties;

  7. import org.jbehave.core.configuration.Keywords;

  8. /**
  9.  * Outputs to the console only if running in an IDE. Command line builds (Maven,
  10.  * Ant) will produce nothing for this particular PrintStreamOutput
  11.  * specialisation.
  12.  */
  13. public class IdeOnlyConsoleOutput extends TxtOutput {

  14.     public IdeOnlyConsoleOutput() {
  15.         super(output());
  16.     }

  17.     public IdeOnlyConsoleOutput(Keywords keywords) {
  18.         super(output(), keywords);
  19.     }

  20.     public IdeOnlyConsoleOutput(Properties outputPatterns, Keywords keywords, boolean reportErrors) {
  21.         super(output(), outputPatterns, keywords, reportErrors);
  22.     }

  23.     static PrintStream output() {
  24.         if (inIDE()) {
  25.             return System.out;
  26.         }
  27.         return new PrintStream(new ByteArrayOutputStream());
  28.     }

  29.     static boolean inIDE() {
  30.         List<String> idePackages = asList("com.intellij", "org.eclipse");
  31.         for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
  32.             for (String idePackage : idePackages) {
  33.                 if (ste.getClassName().startsWith(idePackage)) {
  34.                     return true;
  35.                 }
  36.             }
  37.         }
  38.         return false;
  39.     }
  40. }