Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Description: fix bug with Description Generator filtered stories #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package de.codecentric.jbehave.junit.monitoring;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords.StartingWordNotFound;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.embedder.FilteredStory;
import org.jbehave.core.embedder.MetaFilter;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
Expand All @@ -16,6 +12,12 @@
import org.jbehave.core.steps.StepType;
import org.junit.runner.Description;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class JUnitDescriptionGenerator {

DescriptionTextUniquefier uniq = new DescriptionTextUniquefier();
Expand All @@ -25,12 +27,14 @@ public class JUnitDescriptionGenerator {
private List<StepCandidate> allCandidates = new ArrayList<StepCandidate>();

private final Configuration configuration;
private MetaFilter filter;

private String previousNonAndStep;

public JUnitDescriptionGenerator(List<CandidateSteps> candidateSteps,
Configuration configuration) {
Configuration configuration,MetaFilter filter) {
this.configuration = configuration;
this.filter = filter;
for (CandidateSteps candidateStep : candidateSteps) {
allCandidates.addAll(candidateStep.listCandidates());
}
Expand Down Expand Up @@ -157,7 +161,7 @@ private void addNonExistingStep(Description description, String stringStepOneLin
} else {
addPendingStep(description, stringStepOneLine);
}
} catch (StartingWordNotFound e) {
} catch (Keywords.StartingWordNotFound e) {
// WHAT NOW?
}
}
Expand Down Expand Up @@ -201,11 +205,15 @@ private void addCompositeSteps(Description description, String stringStep,

private void addAllScenariosToDescription(Story story,
Description storyDescription) {
FilteredStory filteredStory = new FilteredStory(filter, story, configuration.storyControls());
List<Scenario> scenarios = story.getScenarios();

for (Scenario scenario : scenarios) {
storyDescription.addChild(createDescriptionFrom(scenario));
}
}
if (filteredStory.allowed(scenario)) {
storyDescription.addChild(createDescriptionFrom(scenario));
}
}
}

private StepCandidate findMatchingStep(String stringStep) {
for (StepCandidate step : allCandidates) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
package de.codecentric.jbehave.junit.monitoring;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jbehave.core.ConfigurableEmbedder;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.embedder.Embedder;
Expand All @@ -25,6 +19,12 @@
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.Statement;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class JUnitReportingRunner extends BlockJUnit4ClassRunner {
private List<Description> storyDescriptions;
private Embedder configuredEmbedder;
Expand Down Expand Up @@ -81,9 +81,9 @@ public void evaluate() {
// tell the reporter how to handle pending steps
junitReporter.usePendingStepStrategy(configuration
.pendingStepStrategy());

addToStoryReporterFormats(junitReporter);

try {
configuredEmbedder.runStoriesAsPaths(storyPaths);
} catch (Throwable e) {
Expand All @@ -94,7 +94,7 @@ public void evaluate() {
}
};
}

public static EmbedderControls recommandedControls(Embedder embedder) {
return recommendedControls(embedder);
}
Expand Down Expand Up @@ -190,7 +190,7 @@ private void addToStoryReporterFormats(JUnitScenarioReporter junitReporter) {

private List<Description> buildDescriptionFromStories() {
JUnitDescriptionGenerator descriptionGenerator = new JUnitDescriptionGenerator(
candidateSteps, configuration);
candidateSteps, configuration, configuredEmbedder.metaFilter());
StoryRunner storyRunner = new StoryRunner();
List<Description> storyDescriptions = new ArrayList<Description>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
package de.codecentric.jbehave.junit.monitoring;

import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.GivenStories;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
import org.jbehave.core.embedder.MetaFilter;
import org.jbehave.core.embedder.StoryControls;
import org.jbehave.core.model.*;
import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.StepCandidate;
import org.jbehave.core.steps.StepType;
Expand All @@ -47,6 +20,17 @@
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.*;

import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;

public class JUnitDescriptionGeneratorTest {

private static final String DEFAULT_STORY_NAME = "Default Story Name";
Expand All @@ -63,6 +47,18 @@ public class JUnitDescriptionGeneratorTest {
GivenStories givenStories;
@Mock
Configuration configuration;
@Mock
Meta storyMeta;
@Mock
Meta scenarioMeta;
@Mock
MetaFilter metaFilter;

@Mock
Meta scenarioAsMeta;

@Mock
Meta storyAsMeta;

private JUnitDescriptionGenerator generator;
private Description description;
Expand All @@ -71,18 +67,25 @@ public class JUnitDescriptionGeneratorTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
when(steps.listCandidates()).thenReturn(
Arrays.asList(new StepCandidate[] { stepCandidate }));
Arrays.asList(new StepCandidate[]{stepCandidate}));
when(stepCandidate.matches(anyString())).thenReturn(true);
when(stepCandidate.matches(anyString(), anyString())).thenReturn(true);
when(stepCandidate.getStepsType()).then(returnObjectClass());
when(story.getName()).thenReturn(DEFAULT_STORY_NAME);
when(story.getMeta()).thenReturn(storyMeta);
when(story.asMeta(anyString())).thenReturn(storyAsMeta);
when(scenario.getTitle()).thenReturn(DEFAULT_SCENARIO_TITLE);
when(scenario.getMeta()).thenReturn(scenarioMeta);
when(scenario.asMeta(anyString())).thenReturn(scenarioAsMeta);
when(givenStories.getPaths()).thenReturn(
Collections.<String> emptyList());
Collections.<String>emptyList());
when(scenario.getGivenStories()).thenReturn(givenStories);
when(configuration.keywords()).thenReturn(new Keywords());
when(configuration.storyControls()).thenReturn(new StoryControls());
when(metaFilter.allow(org.mockito.Matchers.any(Meta.class))).thenReturn(true);

generator = new JUnitDescriptionGenerator(
Arrays.asList(new CandidateSteps[] { steps }), configuration);
Arrays.asList(new CandidateSteps[]{steps}), configuration, metaFilter);
}

private Answer<Class<?>> returnObjectClass() {
Expand Down Expand Up @@ -171,8 +174,14 @@ public void shouldStripLinebreaksFromScenarioDescriptions() {
not(containsString("\n")));
}

private void addScenarioToStory(Scenario... scenario) {
when(story.getScenarios()).thenReturn(Arrays.asList(scenario));
private void addScenarioToStory(Scenario... scenarios) {
List<Scenario> scenarioList = Arrays.asList(scenarios);
for (Scenario scenario : scenarioList) {
when(scenario.getMeta()).thenReturn(scenarioMeta);
when(scenario.asMeta(anyString())).thenReturn(scenarioAsMeta);

}
when(story.getScenarios()).thenReturn(scenarioList);
}

@Test
Expand All @@ -190,9 +199,9 @@ public void shouldStripCarriageReturnsFromScenarioDescriptions() {
@Test
public void shouldCopeWithSeeminglyDuplicateSteps() throws Exception {
when(scenario.getSteps()).thenReturn(
Arrays.asList(new String[] { "Given Step1", "Given Step1" }));
Arrays.asList(new String[]{"Given Step1", "Given Step1"}));
generateScenarioDescription();
assertThat(description.getChildren(),
assertThat(description.getChildren(),
everyItem(whoseDisplayName(startsWith("Given Step1"))));
assertThat(description.getChildren().size(), is(2));
assertThat(description.getChildren(),
Expand Down Expand Up @@ -233,7 +242,7 @@ public void shouldGenerateDescriptionForExampleTablesOnScenario() {
assertThat(description.getChildren().size(), is(NUM_ROWS));
for (Description exampleDescription : description.getChildren()) {
assertThat(exampleDescription.getChildren(),
hasItem(Matchers.<Description> hasProperty("displayName",
hasItem(Matchers.<Description>hasProperty("displayName",
startsWith("Given Step1"))));
assertThat(exampleDescription,
hasProperty("displayName", startsWith("Example: " + row)));
Expand All @@ -244,25 +253,25 @@ public void shouldGenerateDescriptionForExampleTablesOnScenario() {
public void shouldGenerateChildrenForComposedSteps() {
addStepToScenario();
when(stepCandidate.composedSteps()).thenReturn(
new String[] { "compositeStep1", "compositeStep2" });
new String[]{"compositeStep1", "compositeStep2"});
StepCandidate composedStep1 = stepCandidateMock("compositeStep1");
StepCandidate composedStep2 = stepCandidateMock("compositeStep2");
when(stepCandidate.matches(anyString(), anyString())).thenReturn(false);
when(stepCandidate.matches(eq("Given Step1"), anyString())).thenReturn(
true);
when(stepCandidate.isComposite()).thenReturn(true);
when(steps.listCandidates()).thenReturn(
Arrays.asList(new StepCandidate[] { stepCandidate,
composedStep1, composedStep2 }));
Arrays.asList(new StepCandidate[]{stepCandidate,
composedStep1, composedStep2}));
generator = new JUnitDescriptionGenerator(
Arrays.asList(new CandidateSteps[] { steps }), configuration);
Arrays.asList(new CandidateSteps[]{steps}), configuration, metaFilter);

generateScenarioDescription();

Description composedStep = firstChild(description);
verify(stepCandidate, times(0)).getStepsInstance();
assertThat(composedStep.getChildren(),
everyItem(Matchers.<Description> hasProperty("displayName",
everyItem(Matchers.<Description>hasProperty("displayName",
startsWith("compositeStep"))));
assertThat(composedStep.getChildren().size(), is(2));
assertThat(composedStep.isSuite(), is(true));
Expand Down Expand Up @@ -290,13 +299,13 @@ public void shouldCreateDescriptionForAndStep() {
public void shouldGenerateDescriptionForPendingSteps() {
addStepToScenario();
when(steps.listCandidates()).thenReturn(
Arrays.asList(new StepCandidate[] { null }));
Arrays.asList(new StepCandidate[]{null}));
when(stepCandidate.matches(anyString())).thenReturn(false);
when(stepCandidate.matches(anyString(), anyString())).thenReturn(false);
generateScenarioDescription();
assertThat(description.getChildren().size(), is(1));
assertThat(description.getChildren(),
everyItem(Matchers.<Description> hasProperty("displayName",
everyItem(Matchers.<Description>hasProperty("displayName",
containsString("PENDING"))));
assertThat(generator.getTestCases(), is(1));
}
Expand Down Expand Up @@ -347,7 +356,7 @@ private void generateStoryDescription() {
}

private Matcher<Description> whoseDisplayName(Matcher<String> startsWith) {
return Matchers.<Description> hasProperty("displayName", startsWith);
return Matchers.<Description>hasProperty("displayName", startsWith);
}

private StepCandidate stepCandidateMock(String name) {
Expand Down