TeamCityConsoleOutput.java

  1. package org.jbehave.core.reporters;

  2. import java.text.DateFormat;
  3. import java.text.MessageFormat;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;

  6. import org.apache.commons.lang3.StringUtils;
  7. import org.jbehave.core.configuration.Keywords;
  8. import org.jbehave.core.i18n.LocalizedKeywords;
  9. import org.jbehave.core.model.Scenario;
  10. import org.jbehave.core.steps.StepCreator.PendingStep;
  11. import org.jbehave.core.steps.Timing;

  12. /**
  13.  * Decorates console output to allow TeamCity build script interaction: *
  14.  * https://confluence.jetbrains.com/display/TCD9/Build+Script+Interaction+with+TeamCity
  15.  *
  16.  * <p>Scenarios are interpreted as TeamCity tests. Pending scenarios are considered as ignored.</p>
  17.  */
  18. public class TeamCityConsoleOutput extends ConsoleOutput {

  19.     private static final String TEAMCITY_EVENT = "##teamcity[{0} name=''{1}'']\n";
  20.     private static final String TEAMCITY_EVENT_MESSAGE = "##teamcity[{0} name=''{1}'' message=''{2}'']\n";
  21.     private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
  22.             "yyyyMMdd-HH:mm:ss");

  23.     private String eventName;
  24.     private Keywords keywords;

  25.     public TeamCityConsoleOutput() {
  26.         this(new LocalizedKeywords());
  27.     }

  28.     public TeamCityConsoleOutput(Keywords keywords) {
  29.         super(keywords);
  30.         this.keywords = keywords;
  31.     }

  32.     @Override
  33.     public void beforeScenario(Scenario scenario) {
  34.         this.eventName = keywords.scenario() + scenarioTitle(scenario);
  35.         print(format("testStarted", eventName));
  36.         super.beforeScenario(scenario);
  37.     }

  38.     @Override
  39.     public void afterScenario(Timing timing) {
  40.         super.afterScenario(timing);
  41.         print(format("testFinished", eventName));
  42.         this.eventName = null;
  43.     }

  44.     @Override
  45.     public void pending(PendingStep step) {
  46.         super.pending(step);
  47.         print(format("testIgnored", eventName));
  48.     }

  49.     @Override
  50.     public void failed(String step, Throwable storyFailure) {
  51.         super.failed(step, storyFailure);
  52.         print(format("testFailed", eventName, storyFailure.getMessage()));
  53.     }

  54.     private String format(String event, String name) {
  55.         return MessageFormat.format(TEAMCITY_EVENT, event, name);
  56.     }

  57.     private String format(String event, String name, String message) {
  58.         return MessageFormat.format(TEAMCITY_EVENT_MESSAGE, event, name,
  59.                 message);
  60.     }

  61.     private String scenarioTitle(Scenario scenario) {
  62.         String scenarioTitle = scenario.getTitle();
  63.         if (StringUtils.isEmpty(scenarioTitle)) {
  64.             scenarioTitle = "scenario-" + DATE_FORMAT.format(new Date());
  65.         }
  66.         return scenarioTitle;
  67.     }

  68. }