|
1 | 1 | package cucumber.runtime;
|
2 | 2 |
|
| 3 | +import cucumber.runtime.io.ClasspathResourceLoader; |
| 4 | +import gherkin.formatter.Formatter; |
| 5 | +import gherkin.formatter.Reporter; |
| 6 | +import org.mockito.invocation.InvocationOnMock; |
| 7 | +import org.mockito.stubbing.Answer; |
| 8 | +import cucumber.runtime.formatter.StepMatcher; |
| 9 | +import gherkin.formatter.model.Step; |
| 10 | +import gherkin.formatter.model.Tag; |
| 11 | +import cucumber.api.PendingException; |
| 12 | +import gherkin.I18n; |
| 13 | +import junit.framework.AssertionFailedError; |
3 | 14 | import cucumber.runtime.io.Resource;
|
4 | 15 | import cucumber.runtime.model.CucumberFeature;
|
5 | 16 | import org.junit.Ignore;
|
6 | 17 |
|
7 | 18 | import java.io.ByteArrayInputStream;
|
| 19 | +import java.io.FileNotFoundException; |
8 | 20 | import java.io.IOException;
|
9 | 21 | import java.io.InputStream;
|
| 22 | +import java.io.PrintWriter; |
10 | 23 | import java.io.UnsupportedEncodingException;
|
| 24 | +import java.util.AbstractMap.SimpleEntry; |
11 | 25 | import java.util.ArrayList;
|
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.HashSet; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import static java.util.Arrays.asList; |
| 34 | +import static org.mockito.Mockito.doAnswer; |
| 35 | +import static org.mockito.Matchers.argThat; |
| 36 | +import static org.mockito.Matchers.anyCollectionOf; |
| 37 | +import static org.junit.Assert.fail; |
| 38 | +import static org.mockito.Matchers.any; |
| 39 | +import static org.mockito.Matchers.anyString; |
| 40 | +import static org.mockito.Mockito.doThrow; |
| 41 | +import static org.mockito.Mockito.when; |
| 42 | +import static org.mockito.Mockito.mock; |
12 | 43 |
|
13 | 44 | @Ignore
|
14 | 45 | public class TestHelper {
|
@@ -37,4 +68,138 @@ public String getClassName() {
|
37 | 68 | }, new ArrayList<Object>());
|
38 | 69 | return cucumberFeatures.get(0);
|
39 | 70 | }
|
| 71 | + |
| 72 | + public static void runFeatureWithFormatter(final CucumberFeature feature, final Map<String, String> stepsToResult, final List<SimpleEntry<String, String>> hooks, |
| 73 | + final long stepHookDuration, final Formatter formatter, final Reporter reporter) throws Throwable, FileNotFoundException { |
| 74 | + runFeaturesWithFormatter(Arrays.asList(feature), stepsToResult, Collections.<String,String>emptyMap(), hooks, stepHookDuration, formatter, reporter); |
| 75 | + } |
| 76 | + |
| 77 | + public static void runFeaturesWithFormatter(final List<CucumberFeature> features, final Map<String, String> stepsToResult, |
| 78 | + final List<SimpleEntry<String, String>> hooks, final long stepHookDuration, final Formatter formatter, final Reporter reporter) throws Throwable { |
| 79 | + runFeaturesWithFormatter(features, stepsToResult, Collections.<String,String>emptyMap(), hooks, stepHookDuration, formatter, reporter); |
| 80 | + } |
| 81 | + |
| 82 | + public static void runFeatureWithFormatter(final CucumberFeature feature, final Map<String, String> stepsToLocation, |
| 83 | + final Formatter formatter, final Reporter reporter) throws Throwable { |
| 84 | + runFeaturesWithFormatter(Arrays.asList(feature), Collections.<String,String>emptyMap(), stepsToLocation, |
| 85 | + Collections.<SimpleEntry<String, String>>emptyList(), 0L, formatter, reporter); |
| 86 | + } |
| 87 | + |
| 88 | + private static void runFeaturesWithFormatter(final List<CucumberFeature> features, final Map<String, String> stepsToResult, final Map<String, String> stepsToLocation, |
| 89 | + final List<SimpleEntry<String, String>> hooks, final long stepHookDuration, final Formatter formatter, final Reporter reporter) throws Throwable { |
| 90 | + final RuntimeOptions runtimeOptions = new RuntimeOptions(""); |
| 91 | + final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); |
| 92 | + final ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader(classLoader); |
| 93 | + final RuntimeGlue glue = createMockedRuntimeGlueThatMatchesTheSteps(stepsToResult, stepsToLocation, hooks); |
| 94 | + final Runtime runtime = new Runtime(resourceLoader, classLoader, asList(mock(Backend.class)), runtimeOptions, new StopWatch.Stub(stepHookDuration), glue); |
| 95 | + |
| 96 | + for (CucumberFeature feature : features) { |
| 97 | + feature.run(formatter, reporter, runtime); |
| 98 | + } |
| 99 | + formatter.done(); |
| 100 | + formatter.close(); |
| 101 | + } |
| 102 | + |
| 103 | + private static RuntimeGlue createMockedRuntimeGlueThatMatchesTheSteps(Map<String, String> stepsToResult, Map<String, String> stepsToLocation, |
| 104 | + final List<SimpleEntry<String, String>> hooks) throws Throwable { |
| 105 | + RuntimeGlue glue = mock(RuntimeGlue.class); |
| 106 | + TestHelper.mockSteps(glue, stepsToResult, stepsToLocation); |
| 107 | + TestHelper.mockHooks(glue, hooks); |
| 108 | + return glue; |
| 109 | + } |
| 110 | + |
| 111 | + private static void mockSteps(RuntimeGlue glue, Map<String, String> stepsToResult, Map<String, String> stepsToLocation) throws Throwable { |
| 112 | + for (String stepName : mergeStepSets(stepsToResult, stepsToLocation)) { |
| 113 | + String stepResult = getResultWithDefaultPassed(stepsToResult, stepName); |
| 114 | + if (!"undefined".equals(stepResult)) { |
| 115 | + StepDefinitionMatch matchStep = mock(StepDefinitionMatch.class); |
| 116 | + when(glue.stepDefinitionMatch(anyString(), TestHelper.stepWithName(stepName), (I18n) any())).thenReturn(matchStep); |
| 117 | + mockStepResult(stepResult, stepName, matchStep); |
| 118 | + mockStepLocation(getLocationWithDefaultEmptyString(stepsToLocation, stepName), stepName, matchStep); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void mockStepResult(String stepResult, String stepName, StepDefinitionMatch matchStep) throws Throwable { |
| 124 | + if ("pending".equals(stepResult)) { |
| 125 | + doThrow(new PendingException()).when(matchStep).runStep((I18n) any()); |
| 126 | + } else if ("failed".equals(stepResult)) { |
| 127 | + AssertionFailedError error = TestHelper.mockAssertionFailedError(); |
| 128 | + doThrow(error).when(matchStep).runStep((I18n) any()); |
| 129 | + } else if (!"passed".equals(stepResult) && |
| 130 | + !"skipped".equals(stepResult)) { |
| 131 | + fail("Cannot mock step to the result: " + stepResult); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void mockStepLocation(String stepLocation, String stepName, StepDefinitionMatch matchStep) { |
| 136 | + when(matchStep.getLocation()).thenReturn(stepLocation); |
| 137 | + } |
| 138 | + |
| 139 | + private static void mockHooks(RuntimeGlue glue, final List<SimpleEntry<String, String>> hooks) throws Throwable { |
| 140 | + List<HookDefinition> beforeHooks = new ArrayList<HookDefinition>(); |
| 141 | + List<HookDefinition> afterHooks = new ArrayList<HookDefinition>(); |
| 142 | + for (SimpleEntry<String, String> hookEntry : hooks) { |
| 143 | + TestHelper.mockHook(hookEntry, beforeHooks, afterHooks); |
| 144 | + } |
| 145 | + if (beforeHooks.size() != 0) { |
| 146 | + when(glue.getBeforeHooks()).thenReturn(beforeHooks); |
| 147 | + } |
| 148 | + if (afterHooks.size() != 0) { |
| 149 | + when(glue.getAfterHooks()).thenReturn(afterHooks); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static void mockHook(SimpleEntry<String, String> hookEntry, List<HookDefinition> beforeHooks, |
| 154 | + List<HookDefinition> afterHooks) throws Throwable { |
| 155 | + HookDefinition hook = mock(HookDefinition.class); |
| 156 | + when(hook.matches(anyCollectionOf(Tag.class))).thenReturn(true); |
| 157 | + if (hookEntry.getValue().equals("failed")) { |
| 158 | + AssertionFailedError error = TestHelper.mockAssertionFailedError(); |
| 159 | + doThrow(error).when(hook).execute((cucumber.api.Scenario) any()); |
| 160 | + } |
| 161 | + if ("before".equals(hookEntry.getKey())) { |
| 162 | + beforeHooks.add(hook); |
| 163 | + } else if ("after".equals(hookEntry.getKey())) { |
| 164 | + afterHooks.add(hook); |
| 165 | + } else { |
| 166 | + fail("Only before and after hooks are allowed, hook type found was: " + hookEntry.getKey()); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private static Step stepWithName(String name) { |
| 171 | + return argThat(new StepMatcher(name)); |
| 172 | + } |
| 173 | + |
| 174 | + private static AssertionFailedError mockAssertionFailedError() { |
| 175 | + AssertionFailedError error = mock(AssertionFailedError.class); |
| 176 | + Answer<Object> printStackTraceHandler = new Answer<Object>() { |
| 177 | + @Override |
| 178 | + public Object answer(InvocationOnMock invocation) throws Throwable { |
| 179 | + PrintWriter writer = (PrintWriter) invocation.getArguments()[0]; |
| 180 | + writer.print("the stack trace"); |
| 181 | + return null; |
| 182 | + } |
| 183 | + }; |
| 184 | + doAnswer(printStackTraceHandler).when(error).printStackTrace((PrintWriter) any()); |
| 185 | + return error; |
| 186 | + } |
| 187 | + |
| 188 | + public static SimpleEntry<String, String> hookEntry(String type, String result) { |
| 189 | + return new SimpleEntry<String, String>(type, result); |
| 190 | + } |
| 191 | + |
| 192 | + private static Set<String> mergeStepSets(Map<String, String> stepsToResult, Map<String, String> stepsToLocation) { |
| 193 | + Set<String> steps = new HashSet<String>(stepsToResult.keySet()); |
| 194 | + steps.addAll(stepsToLocation.keySet()); |
| 195 | + return steps; |
| 196 | + } |
| 197 | + |
| 198 | + private static String getResultWithDefaultPassed(Map<String, String> stepsToResult, String step) { |
| 199 | + return stepsToResult.containsKey(step) ? stepsToResult.get(step) : "passed"; |
| 200 | + } |
| 201 | + |
| 202 | + private static String getLocationWithDefaultEmptyString(Map<String, String> stepsToLocation, String step) { |
| 203 | + return stepsToLocation.containsKey(step) ? stepsToLocation.get(step) : ""; |
| 204 | + } |
40 | 205 | }
|
0 commit comments