[Cucumber Java] create SM adapters for CucumberJava-3
authorAndrey Vokin <andrey.vokin@jetbrains.com>
Fri, 18 Oct 2019 10:29:51 +0000 (12:29 +0200)
committerintellij-monorepo-bot <intellij-monorepo-bot-no-reply@jetbrains.com>
Mon, 11 Nov 2019 17:12:03 +0000 (17:12 +0000)
adapter for CucumberJava-2 cannot be used because of the fact that 'cucumber.api.TestStep' is a class in cucumber-java-2 and an interface in cucumber-java-3 and further

GitOrigin-RevId: 9dd627253bb7b35a9d0e779c8aa93aa15c08a4f7

plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm2Adapter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm2SMFormatter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmAdapter.java
plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMConverter.java [new file with mode: 0644]
plugins/cucumber-jvm-formatter3/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm3Adapter.java [new file with mode: 0644]
plugins/cucumber-jvm-formatter3/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm3SMFormatter.java

index c1047dcbc697d719dc636660fb364cbee027033d..7c08a7c8d06deaf7c84d4c51e8c2c15575e9b1b7 100644 (file)
@@ -81,27 +81,6 @@ public class CucumberJvm2Adapter {
     }
   }
 
-  public static class IdeaTestSourceReadEvent implements CucumberJvmAdapter.IdeaTestSourceReadEvent {
-    private final String myUri;
-
-    private final String mySource;
-
-    public IdeaTestSourceReadEvent(TestSourceRead testSourceRead) {
-      myUri = testSourceRead.uri;
-      mySource = testSourceRead.source;
-    }
-
-    @Override
-    public String getUri() {
-      return myUri;
-    }
-
-    @Override
-    public String getSource() {
-      return mySource;
-    }
-  }
-
   public static class IdeaTestCase implements CucumberJvmAdapter.IdeaTestCase {
     private final TestCase myRealTestCase;
 
index 695f3308bfb23d0af9f01b0d1fd1fe778025c4bb..d79a11002d04c456f4bf010fcda745ad17a58077 100644 (file)
@@ -1,37 +1,19 @@
 package org.jetbrains.plugins.cucumber.java.run;
 
-import com.intellij.junit4.ExpectedPatterns;
-import com.intellij.rt.execution.junit.ComparisonFailureData;
 import cucumber.api.event.*;
 import cucumber.api.formatter.Formatter;
 
 import java.io.PrintStream;
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatterUtil.*;
 
 @SuppressWarnings("unused")
-public class CucumberJvm2SMFormatter implements Formatter {
-  private static final String EXAMPLES_CAPTION = "Examples:";
-  private static final String SCENARIO_OUTLINE_CAPTION = "Scenario: Line: ";
-  private final Map<String, String> pathToDescription = new HashMap<String, String>();
-  private String currentFilePath;
-  private int currentScenarioOutlineLine;
-  private String currentScenarioOutlineName;
-  private final PrintStream myOut;
-  private final String myCurrentTimeValue;
-
+public class CucumberJvm2SMFormatter extends CucumberJvmSMConverter implements Formatter {
   public CucumberJvm2SMFormatter() {
     //noinspection UseOfSystemOutOrSystemErr
     this(System.out, null);
   }
 
   public CucumberJvm2SMFormatter(PrintStream out, String currentTimeValue) {
-    myOut = out;
-    myCurrentTimeValue = currentTimeValue;
-    outCommand(TEMPLATE_ENTER_THE_MATRIX, getCurrentTime());
-    outCommand(TEMPLATE_SCENARIO_COUNTING_STARTED, "0", getCurrentTime());
+    super(out, currentTimeValue);
   }
 
   private final EventHandler<TestCaseStarted> testCaseStartedHandler = new EventHandler<TestCaseStarted>() {
@@ -79,7 +61,7 @@ public class CucumberJvm2SMFormatter implements Formatter {
   private final EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
     @Override
     public void receive(TestSourceRead event) {
-      CucumberJvm2SMFormatter.this.handleTestSourceRead(new CucumberJvm2Adapter.IdeaTestSourceReadEvent(event));
+      CucumberJvm2SMFormatter.this.handleTestSourceRead(new CucumberJvmAdapter.IdeaTestSourceReadEvent(event.uri, event.source));
     }
   };
 
@@ -95,145 +77,4 @@ public class CucumberJvm2SMFormatter implements Formatter {
     publisher.registerHandlerFor(TestRunFinished.class, this.testRunFinishedHandler);
     publisher.registerHandlerFor(WriteEvent.class, this.writeEventHandler);
   }
-
-  private void handleTestCaseStarted(CucumberJvmAdapter.IdeaTestCaseEvent event) {
-    if (currentFilePath == null) {
-      outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event), getFeatureFileDescription(getEventUri(event)));
-    }
-    else if (!getEventUri(event).equals(currentFilePath)) {
-      closeCurrentScenarioOutline();
-      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), getFeatureFileDescription(currentFilePath));
-      outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event), getFeatureFileDescription(getEventUri(event)));
-    }
-
-    outCommand(TEMPLATE_SCENARIO_STARTED, getCurrentTime());
-
-    if (event.getTestCase().isScenarioOutline()) {
-      int mainScenarioLine = event.getTestCase().getScenarioOutlineLine();
-      if (currentScenarioOutlineLine != mainScenarioLine || currentFilePath == null ||
-          !currentFilePath.equals(getEventUri(event))) {
-        closeCurrentScenarioOutline();
-        currentScenarioOutlineLine = mainScenarioLine;
-        currentScenarioOutlineName = event.getTestCase().getScenarioName();
-        outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + currentScenarioOutlineLine,
-                   currentScenarioOutlineName);
-        outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), "", EXAMPLES_CAPTION);
-      }
-    } else {
-      closeCurrentScenarioOutline();
-    }
-    currentFilePath = getEventUri(event);
-
-    outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + event.getTestCase().getLine(),
-               event.getTestCase().getScenarioName());
-  }
-
-  private void handleTestCaseFinished(CucumberJvmAdapter.IdeaTestCaseEvent event) {
-    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), event.getTestCase().getScenarioName());
-    outCommand(TEMPLATE_SCENARIO_FINISHED, getCurrentTime());
-  }
-
-  private void handleTestRunFinished() {
-    closeCurrentScenarioOutline();
-    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), getFeatureFileDescription(currentFilePath));
-  }
-
-  private void handleWriteEvent(CucumberJvmAdapter.IdeaWriteEvent event) {
-    myOut.println(event.getText());
-  }
-
-  private void handleTestStepStarted(CucumberJvmAdapter.IdeaTestStepEvent event) {
-    outCommand(TEMPLATE_TEST_STARTED, getCurrentTime(), event.getTestStep().getLocation(), event.getTestStep().getStepName());
-  }
-
-  private void handleTestStepFinished(CucumberJvmAdapter.IdeaTestStepFinishedEvent event) {
-    if (event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.PASSED) {
-      // write nothing
-    }
-    else if (event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.SKIPPED ||
-             event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.PENDING) {
-      outCommand(TEMPLATE_TEST_PENDING, event.getTestStep().getStepName(), getCurrentTime());
-    }
-    else {
-      String[] messageAndDetails = getMessageAndDetails(event.getErrorMessage());
-
-      ComparisonFailureData comparisonFailureData = ExpectedPatterns.createExceptionNotification(messageAndDetails[0]);
-      if (comparisonFailureData != null) {
-        outCommand(TEMPLATE_COMPARISON_TEST_FAILED, getCurrentTime(), messageAndDetails[1], messageAndDetails[0],
-                   comparisonFailureData.getExpected(), comparisonFailureData.getActual(), event.getTestStep().getStepName(), "");
-      }
-      else {
-        outCommand(TEMPLATE_TEST_FAILED, getCurrentTime(), "", event.getErrorMessage(), event.getTestStep().getStepName(), "");
-      }
-    }
-    outCommand(TEMPLATE_TEST_FINISHED, getCurrentTime(), String.valueOf(event.getDuration()), event.getTestStep().getStepName());
-  }
-
-  private String getFeatureFileDescription(String uri) {
-    if (pathToDescription.containsKey(uri)) {
-      return pathToDescription.get(uri);
-    }
-    return uri;
-  }
-
-  private void handleTestSourceRead(CucumberJvm2Adapter.IdeaTestSourceReadEvent event) {
-    closeCurrentScenarioOutline();
-    pathToDescription.put(event.getUri(), getFeatureName(event.getSource()));
-  }
-
-  private void closeCurrentScenarioOutline() {
-    if (currentScenarioOutlineLine > 0) {
-      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), EXAMPLES_CAPTION);
-      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), currentScenarioOutlineName);
-      currentScenarioOutlineLine = 0;
-      currentScenarioOutlineName = null;
-    }
-  }
-
-  private static String[] getMessageAndDetails(String errorReport) {
-    if (errorReport == null) {
-      errorReport = "";
-    }
-    String[] messageAndDetails = errorReport.split("\n", 2);
-
-    String message = null;
-    if (messageAndDetails.length > 0) {
-      message = messageAndDetails[0];
-    }
-    if (message == null) {
-      message = "";
-    }
-
-    String details = null;
-    if (messageAndDetails.length > 1) {
-      details = messageAndDetails[1];
-    }
-    if (details == null) {
-      details = "";
-    }
-
-    return new String[] {message, details};
-  }
-
-  private void outCommand(String command, String... parameters) {
-    myOut.println(escapeCommand(command, parameters));
-  }
-
-  private static String getScenarioName(CucumberJvmAdapter.IdeaTestCase testCase) {
-    if (testCase.isScenarioOutline()) {
-      return SCENARIO_OUTLINE_CAPTION + testCase.getLine();
-    }
-    return testCase.getScenarioName();
-  }
-
-  protected String getEventUri(CucumberJvmAdapter.IdeaTestCaseEvent event) {
-    return event.getUri();
-  }
-
-  private String getCurrentTime() {
-    if (myCurrentTimeValue != null) {
-      return myCurrentTimeValue;
-    }
-    return CucumberJvmSMFormatterUtil.getCurrentTime();
-  }
 }
index eec83eb5079aeac8dd66a3dd9293ae1a42335f6b..4e01d75c2fe79078ea79a99e96fa5f4c50b013b6 100644 (file)
@@ -30,10 +30,23 @@ public class CucumberJvmAdapter {
     }
   }
 
-  interface IdeaTestSourceReadEvent {
-    String getUri();
+  public static class IdeaTestSourceReadEvent {
+    private final String myUri;
+
+    private final String mySource;
+
+    public IdeaTestSourceReadEvent(String uri, String source) {
+      myUri = uri;
+      mySource = source;
+    }
 
-    String getSource();
+    public String getUri() {
+      return myUri;
+    }
+
+    public String getSource() {
+      return mySource;
+    }
   }
 
   interface IdeaTestCase {
diff --git a/plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMConverter.java b/plugins/cucumber-jvm-formatter/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvmSMConverter.java
new file mode 100644 (file)
index 0000000..7e74b89
--- /dev/null
@@ -0,0 +1,175 @@
+// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+package org.jetbrains.plugins.cucumber.java.run;
+
+import com.intellij.junit4.ExpectedPatterns;
+import com.intellij.rt.execution.junit.ComparisonFailureData;
+
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatterUtil.*;
+
+public class CucumberJvmSMConverter {
+  private static final String EXAMPLES_CAPTION = "Examples:";
+  private static final String SCENARIO_OUTLINE_CAPTION = "Scenario: Line: ";
+  private final Map<String, String> pathToDescription = new HashMap<String, String>();
+  private String currentFilePath;
+  private int currentScenarioOutlineLine;
+  private String currentScenarioOutlineName;
+  private final PrintStream myOut;
+  private final String myCurrentTimeValue;
+
+  public CucumberJvmSMConverter() {
+    //noinspection UseOfSystemOutOrSystemErr
+    this(System.out, null);
+  }
+
+  public CucumberJvmSMConverter(PrintStream out, String currentTimeValue) {
+    myOut = out;
+    myCurrentTimeValue = currentTimeValue;
+    outCommand(TEMPLATE_ENTER_THE_MATRIX, getCurrentTime());
+    outCommand(TEMPLATE_SCENARIO_COUNTING_STARTED, "0", getCurrentTime());
+  }
+
+  protected void handleTestCaseStarted(CucumberJvmAdapter.IdeaTestCaseEvent event) {
+    if (currentFilePath == null) {
+      outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event), getFeatureFileDescription(getEventUri(event)));
+    }
+    else if (!getEventUri(event).equals(currentFilePath)) {
+      closeCurrentScenarioOutline();
+      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), getFeatureFileDescription(currentFilePath));
+      outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event), getFeatureFileDescription(getEventUri(event)));
+    }
+
+    outCommand(TEMPLATE_SCENARIO_STARTED, getCurrentTime());
+
+    if (event.getTestCase().isScenarioOutline()) {
+      int mainScenarioLine = event.getTestCase().getScenarioOutlineLine();
+      if (currentScenarioOutlineLine != mainScenarioLine || currentFilePath == null ||
+          !currentFilePath.equals(getEventUri(event))) {
+        closeCurrentScenarioOutline();
+        currentScenarioOutlineLine = mainScenarioLine;
+        currentScenarioOutlineName = event.getTestCase().getScenarioName();
+        outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + currentScenarioOutlineLine,
+                   currentScenarioOutlineName);
+        outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), "", EXAMPLES_CAPTION);
+      }
+    } else {
+      closeCurrentScenarioOutline();
+    }
+    currentFilePath = getEventUri(event);
+
+    outCommand(TEMPLATE_TEST_SUITE_STARTED, getCurrentTime(), getEventUri(event) + ":" + event.getTestCase().getLine(),
+               event.getTestCase().getScenarioName());
+  }
+
+  protected void handleTestCaseFinished(CucumberJvmAdapter.IdeaTestCaseEvent event) {
+    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), event.getTestCase().getScenarioName());
+    outCommand(TEMPLATE_SCENARIO_FINISHED, getCurrentTime());
+  }
+
+  protected void handleTestRunFinished() {
+    closeCurrentScenarioOutline();
+    outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), getFeatureFileDescription(currentFilePath));
+  }
+
+  protected void handleWriteEvent(CucumberJvmAdapter.IdeaWriteEvent event) {
+    myOut.println(event.getText());
+  }
+
+  protected void handleTestStepStarted(CucumberJvmAdapter.IdeaTestStepEvent event) {
+    outCommand(TEMPLATE_TEST_STARTED, getCurrentTime(), event.getTestStep().getLocation(), event.getTestStep().getStepName());
+  }
+
+  protected void handleTestStepFinished(CucumberJvmAdapter.IdeaTestStepFinishedEvent event) {
+    if (event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.PASSED) {
+      // write nothing
+    }
+    else if (event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.SKIPPED ||
+             event.getResult() == CucumberJvmAdapter.IdeaTestStepFinishedEvent.Status.PENDING) {
+      outCommand(TEMPLATE_TEST_PENDING, event.getTestStep().getStepName(), getCurrentTime());
+    }
+    else {
+      String[] messageAndDetails = getMessageAndDetails(event.getErrorMessage());
+
+      ComparisonFailureData comparisonFailureData = ExpectedPatterns.createExceptionNotification(messageAndDetails[0]);
+      if (comparisonFailureData != null) {
+        outCommand(TEMPLATE_COMPARISON_TEST_FAILED, getCurrentTime(), messageAndDetails[1], messageAndDetails[0],
+                   comparisonFailureData.getExpected(), comparisonFailureData.getActual(), event.getTestStep().getStepName(), "");
+      }
+      else {
+        outCommand(TEMPLATE_TEST_FAILED, getCurrentTime(), "", event.getErrorMessage(), event.getTestStep().getStepName(), "");
+      }
+    }
+    outCommand(TEMPLATE_TEST_FINISHED, getCurrentTime(), String.valueOf(event.getDuration()), event.getTestStep().getStepName());
+  }
+
+  protected void handleTestSourceRead(CucumberJvmAdapter.IdeaTestSourceReadEvent event) {
+    closeCurrentScenarioOutline();
+    pathToDescription.put(event.getUri(), getFeatureName(event.getSource()));
+  }
+
+  private String getFeatureFileDescription(String uri) {
+    if (pathToDescription.containsKey(uri)) {
+      return pathToDescription.get(uri);
+    }
+    return uri;
+  }
+
+  private void closeCurrentScenarioOutline() {
+    if (currentScenarioOutlineLine > 0) {
+      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), EXAMPLES_CAPTION);
+      outCommand(TEMPLATE_TEST_SUITE_FINISHED, getCurrentTime(), currentScenarioOutlineName);
+      currentScenarioOutlineLine = 0;
+      currentScenarioOutlineName = null;
+    }
+  }
+
+  private static String[] getMessageAndDetails(String errorReport) {
+    if (errorReport == null) {
+      errorReport = "";
+    }
+    String[] messageAndDetails = errorReport.split("\n", 2);
+
+    String message = null;
+    if (messageAndDetails.length > 0) {
+      message = messageAndDetails[0];
+    }
+    if (message == null) {
+      message = "";
+    }
+
+    String details = null;
+    if (messageAndDetails.length > 1) {
+      details = messageAndDetails[1];
+    }
+    if (details == null) {
+      details = "";
+    }
+
+    return new String[] {message, details};
+  }
+
+  private void outCommand(String command, String... parameters) {
+    myOut.println(escapeCommand(command, parameters));
+  }
+
+  private static String getScenarioName(CucumberJvmAdapter.IdeaTestCase testCase) {
+    if (testCase.isScenarioOutline()) {
+      return SCENARIO_OUTLINE_CAPTION + testCase.getLine();
+    }
+    return testCase.getScenarioName();
+  }
+
+  protected String getEventUri(CucumberJvmAdapter.IdeaTestCaseEvent event) {
+    return event.getUri();
+  }
+
+  private String getCurrentTime() {
+    if (myCurrentTimeValue != null) {
+      return myCurrentTimeValue;
+    }
+    return CucumberJvmSMFormatterUtil.getCurrentTime();
+  }
+}
diff --git a/plugins/cucumber-jvm-formatter3/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm3Adapter.java b/plugins/cucumber-jvm-formatter3/src/org/jetbrains/plugins/cucumber/java/run/CucumberJvm3Adapter.java
new file mode 100644 (file)
index 0000000..7021d5a
--- /dev/null
@@ -0,0 +1,180 @@
+// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
+package org.jetbrains.plugins.cucumber.java.run;
+
+import cucumber.api.HookTestStep;
+import cucumber.api.PickleStepTestStep;
+import cucumber.api.TestCase;
+import cucumber.api.TestStep;
+import cucumber.api.event.*;
+import gherkin.events.PickleEvent;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+import static org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatterUtil.FILE_RESOURCE_PREFIX;
+
+public class CucumberJvm3Adapter {
+  public static class IdeaTestCaseEvent implements CucumberJvmAdapter.IdeaTestCaseEvent {
+    private final IdeaTestCase myTestCase;
+
+    public IdeaTestCaseEvent(TestCaseStarted testCaseStarted) {
+      myTestCase = new IdeaTestCase(testCaseStarted.testCase);
+    }
+
+    public IdeaTestCaseEvent(TestCaseFinished testCaseFinished) {
+      myTestCase = new IdeaTestCase(testCaseFinished.testCase);
+    }
+
+    @Override
+    public CucumberJvmAdapter.IdeaTestCase getTestCase() {
+      return myTestCase;
+    }
+
+    @Override
+    public String getUri() {
+      return myTestCase.getUri();
+    }
+  }
+
+  public static class IdeaTestStepEvent implements CucumberJvmAdapter.IdeaTestStepEvent {
+    private final IdeaTestStep myTestStep;
+
+    public IdeaTestStepEvent(TestStepStarted testStepStarted) {
+      this(testStepStarted.testStep);
+    }
+
+    public IdeaTestStepEvent(TestStep testStep) {
+      myTestStep = new IdeaTestStep(testStep);
+    }
+
+    @Override
+    public CucumberJvmAdapter.IdeaTestStep getTestStep() {
+      return myTestStep;
+    }
+  }
+
+  public static class IdeaTestStepFinishedEvent extends IdeaTestStepEvent implements CucumberJvmAdapter.IdeaTestStepFinishedEvent {
+    TestStepFinished myRealEvent;
+
+    public IdeaTestStepFinishedEvent(TestStepFinished testStepFinished) {
+      super(testStepFinished.testStep);
+      myRealEvent = testStepFinished;
+    }
+
+    @Override
+    public Status getResult() {
+      switch (myRealEvent.result.getStatus()) {
+        case PASSED: return Status.PASSED;
+        case PENDING: return Status.PENDING;
+        case SKIPPED: return Status.SKIPPED;
+        default: return Status.FAILED;
+      }
+    }
+
+    @Override
+    public Long getDuration() {
+      return myRealEvent.result.getDuration() != null ? myRealEvent.result.getDuration() / 1000000: 0;
+    }
+
+    @Override
+    public String getErrorMessage() {
+      return myRealEvent.result.getErrorMessage();
+    }
+  }
+
+  public static class IdeaTestCase implements CucumberJvmAdapter.IdeaTestCase {
+    private final TestCase myRealTestCase;
+
+    IdeaTestCase(TestCase realTestCase) {
+      myRealTestCase = realTestCase;
+    }
+
+    @Override
+    public boolean isScenarioOutline() {
+      PickleEvent pickleEvent = getPickleEvent(myRealTestCase);
+      return pickleEvent != null && pickleEvent.pickle.getLocations().size() > 1;
+    }
+
+    @Override
+    public String getUri() {
+      return myRealTestCase.getUri();
+    }
+
+    @Override
+    public int getScenarioOutlineLine() {
+      PickleEvent pickleEvent = getPickleEvent(myRealTestCase);
+      if (pickleEvent != null) {
+        return pickleEvent.pickle.getLocations().get(pickleEvent.pickle.getLocations().size() - 1).getLine();
+      }
+      return 0;
+    }
+
+    @Override
+    public int getLine() {
+      return myRealTestCase.getLine();
+    }
+
+    @Override
+    public String getScenarioName() {
+      return "Scenario: " + myRealTestCase.getName();
+    }
+  }
+
+  public static class IdeaTestStep implements CucumberJvmAdapter.IdeaTestStep {
+    private final TestStep myRealStep;
+
+    public IdeaTestStep(TestStep realStep) {
+      myRealStep = realStep;
+    }
+
+    @Override
+    public String getLocation() {
+      if (myRealStep instanceof HookTestStep) {
+        try {
+          Field definitionMatchField = myRealStep.getClass().getSuperclass().getDeclaredField("stepDefinitionMatch");
+          definitionMatchField.setAccessible(true);
+          Object definitionMatchFieldValue = definitionMatchField.get(myRealStep);
+
+          Field hookDefinitionField = definitionMatchFieldValue.getClass().getDeclaredField("hookDefinition");
+          hookDefinitionField.setAccessible(true);
+          Object hookDefinitionFieldValue = hookDefinitionField.get(definitionMatchFieldValue);
+
+          Field methodField = hookDefinitionFieldValue.getClass().getDeclaredField("method");
+          methodField.setAccessible(true);
+          Object methodFieldValue = methodField.get(hookDefinitionFieldValue);
+          if (methodFieldValue instanceof Method) {
+            Method method = (Method)methodFieldValue;
+            return String.format("java:test://%s/%s", method.getDeclaringClass().getName(), method.getName());
+          }
+        }
+        catch (Exception ignored) {
+        }
+        return "";
+      }
+      PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) myRealStep;
+      return FILE_RESOURCE_PREFIX + pickleStepTestStep.getStepLocation() + ":" + pickleStepTestStep.getStepLine();
+    }
+
+    @Override
+    public String getStepName() {
+      String stepName;
+      if (myRealStep instanceof HookTestStep) {
+        stepName = "Hook: " + ((HookTestStep)myRealStep).getHookType().toString();
+      } else {
+        stepName = ((PickleStepTestStep) myRealStep).getPickleStep().getText();
+      }
+      return stepName;
+    }
+  }
+
+  private static PickleEvent getPickleEvent(TestCase testCase) {
+    try {
+      Field pickleEventField = testCase.getClass().getDeclaredField("pickleEvent");
+      pickleEventField.setAccessible(true);
+      return (PickleEvent)pickleEventField.get(testCase);
+    }
+    catch (Exception ignored) {
+    }
+    return null;
+  }
+}
index bb64a4d09238c9097332e74e20707659e13bb655..9dddeb4adc00e302add1b7de9ace9b26704c6240 100644 (file)
@@ -1,98 +1,73 @@
 package org.jetbrains.plugins.cucumber.java.run;
 
-import cucumber.api.HookTestStep;
-import cucumber.api.PickleStepTestStep;
-import cucumber.api.TestCase;
-import cucumber.api.TestStep;
-import cucumber.api.event.TestCaseStarted;
-import cucumber.api.event.TestStepFinished;
-import cucumber.api.event.TestStepStarted;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
-import static org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatterUtil.FILE_RESOURCE_PREFIX;
+import cucumber.api.event.*;
+import cucumber.api.formatter.Formatter;
 
 @SuppressWarnings("unused")
-public class CucumberJvm3SMFormatter extends CucumberJvm2SMFormatter {
+public class CucumberJvm3SMFormatter extends CucumberJvmSMConverter implements Formatter {
   public CucumberJvm3SMFormatter() {
     super();
   }
 
-  @Override
-  protected String getEventUri(TestCaseStarted event) {
-    return event.testCase.getUri();
-  }
+  private final EventHandler<TestCaseStarted> testCaseStartedHandler = new EventHandler<TestCaseStarted>() {
+    @Override
+    public void receive(TestCaseStarted event) {
+      CucumberJvm3SMFormatter.this.handleTestCaseStarted(new CucumberJvm3Adapter.IdeaTestCaseEvent(event));
+    }
+  };
 
-  @Override
-  protected int getEventLine(TestCaseStarted event) {
-    return event.testCase.getLine();
-  }
+  private final EventHandler<TestCaseFinished> testCaseFinishedHandler = new EventHandler<TestCaseFinished>() {
+    @Override
+    public void receive(TestCaseFinished event) {
+      handleTestCaseFinished(new CucumberJvm3Adapter.IdeaTestCaseEvent(event));
+    }
+  };
 
-  @Override
-  protected String getEventName(TestCaseStarted event) {
-    return event.testCase.getName();
-  }
+  private final EventHandler<TestRunFinished> testRunFinishedHandler = new EventHandler<TestRunFinished>() {
+    @Override
+    public void receive(TestRunFinished event) {
+      CucumberJvm3SMFormatter.this.handleTestRunFinished();
+    }
+  };
 
-  @Override
-  protected String getStepLocation(TestStepStarted testStepStarted) {
-    return getStepLocation(testStepStarted.testStep);
-  }
+  private final EventHandler<WriteEvent> writeEventHandler = new EventHandler<WriteEvent>() {
+    @Override
+    public void receive(WriteEvent event) {
+      CucumberJvm3SMFormatter.this.handleWriteEvent(new CucumberJvmAdapter.IdeaWriteEvent(event.text));
+    }
+  };
 
-  @Override
-  protected String getStepLocation(TestStepFinished testStepFinished) {
-    return getStepLocation(testStepFinished.testStep);
-  }
+  private final EventHandler<TestStepStarted> testStepStartedHandler = new EventHandler<TestStepStarted>() {
+    @Override
+    public void receive(TestStepStarted event) {
+      handleTestStepStarted(new CucumberJvm3Adapter.IdeaTestStepEvent(event));
+    }
+  };
 
-  @Override
-  protected String getStepName(TestStepStarted testStepStarted) {
-    return getStepName(testStepStarted.testStep);
-  }
+  private final EventHandler<TestStepFinished> testStepFinishedHandler = new EventHandler<TestStepFinished>() {
+    @Override
+    public void receive(TestStepFinished event) {
+      handleTestStepFinished(new CucumberJvm3Adapter.IdeaTestStepFinishedEvent(event));
+    }
+  };
 
-  @Override
-  protected String getStepName(TestStepFinished testStepFinished) {
-    return getStepName(testStepFinished.testStep);
-  }
+  private final EventHandler<TestSourceRead> testSourceReadHandler = new EventHandler<TestSourceRead>() {
+    @Override
+    public void receive(TestSourceRead event) {
+      CucumberJvm3SMFormatter.this.handleTestSourceRead(new CucumberJvmAdapter.IdeaTestSourceReadEvent(event.uri, event.source));
+    }
+  };
 
   @Override
-  protected String getScenarioNameFromTestCase(TestCase testCase) {
-    return "Scenario: " + testCase.getName();
-  }
+  public void setEventPublisher(EventPublisher publisher) {
+    publisher.registerHandlerFor(TestCaseStarted.class, this.testCaseStartedHandler);
+    publisher.registerHandlerFor(TestCaseFinished.class, this.testCaseFinishedHandler);
 
-  private static String getStepLocation(TestStep step) {
-    if (step instanceof HookTestStep) {
-      try {
-        Field definitionMatchField = step.getClass().getSuperclass().getDeclaredField("stepDefinitionMatch");
-        definitionMatchField.setAccessible(true);
-        Object definitionMatchFieldValue = definitionMatchField.get(step);
+    publisher.registerHandlerFor(TestStepStarted.class, this.testStepStartedHandler);
+    publisher.registerHandlerFor(TestStepFinished.class, this.testStepFinishedHandler);
+    publisher.registerHandlerFor(TestSourceRead.class, this.testSourceReadHandler);
 
-        Field hookDefinitionField = definitionMatchFieldValue.getClass().getDeclaredField("hookDefinition");
-        hookDefinitionField.setAccessible(true);
-        Object hookDefinitionFieldValue = hookDefinitionField.get(definitionMatchFieldValue);
-
-        Field methodField = hookDefinitionFieldValue.getClass().getDeclaredField("method");
-        methodField.setAccessible(true);
-        Object methodFieldValue = methodField.get(hookDefinitionFieldValue);
-        if (methodFieldValue instanceof Method) {
-          Method method = (Method)methodFieldValue;
-          return String.format("java:test://%s/%s", method.getDeclaringClass().getName(), method.getName());
-        }
-      }
-      catch (Exception ignored) {
-      }
-      return "";
-    }
-    PickleStepTestStep pickleStepTestStep = (PickleStepTestStep) step;
-    return FILE_RESOURCE_PREFIX + pickleStepTestStep.getStepLocation() + ":" + pickleStepTestStep.getStepLine();
-  }
-
-  private static String getStepName(TestStep step) {
-    String stepName;
-    if (step instanceof HookTestStep) {
-      stepName = "Hook: " + ((HookTestStep)step).getHookType().toString();
-    } else {
-      stepName = ((PickleStepTestStep) step).getPickleStep().getText();
-    }
-    return stepName;
+    publisher.registerHandlerFor(TestRunFinished.class, this.testRunFinishedHandler);
+    publisher.registerHandlerFor(WriteEvent.class, this.writeEventHandler);
   }
 }