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

[engine] Resolve identical Jbehave story names #3666

Merged
Merged
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,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,18 +16,32 @@

package org.vividus;

import java.util.List;
import java.util.stream.Collectors;

import org.jbehave.core.embedder.PerformableTree;
import org.jbehave.core.model.Story;
import org.jbehave.core.steps.StepCollector.Stage;

public class BatchedPerformableTree extends PerformableTree
{
private static final IdenticalStoryNamesResolver IDENTICAL_STORY_NAMES_RESOLVER = new IdenticalStoryNamesResolver();

private boolean failFast;
private boolean reportBeforeStories;
private boolean reportAfterStories;

@Override
public void performBeforeOrAfterStories(RunContext context, Stage stage)
{
if (Stage.BEFORE.equals(stage))
{
List<Story> currentBatchStories = getRoot().getStories().stream()
.filter(p -> p.getStatus() == null)
.map(PerformableStory::getStory)
.collect(Collectors.toList());
IDENTICAL_STORY_NAMES_RESOLVER.resolveIdenticalNames(currentBatchStories);
}
if (reportBeforeStories && Stage.BEFORE.equals(stage) || Stage.AFTER.equals(stage)
&& (reportAfterStories || failFast && !context.getFailures().isEmpty()))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus;

import java.io.File;
import java.net.URI;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.io.FilenameUtils;
import org.jbehave.core.model.Story;

public class IdenticalStoryNamesResolver
{
private static final char UNIX_PATH_SEPARATOR = '/';

public void resolveIdenticalNames(List<Story> stories)
{
for (Story story : stories)
{
String storyName = story.getName();
List<Story> sameNameStories = stories.stream()
.filter(s -> storyName.equals(s.getName()))
.collect(Collectors.toList());

if (sameNameStories.size() > 1)
{
Path currentPath = getPathSafely(story.getPath());
List<Path> similarPaths = sameNameStories.stream()
.map(s -> getPathSafely(s.getPath()))
.collect(Collectors.toList());

Path commonPath = findCommonPath(currentPath, similarPaths);
updateStoryNames(commonPath, sameNameStories);
}
}
}

private Path findCommonPath(Path referencePath, List<Path> paths)
{
Path root = referencePath.getRoot();
Path commonPath = root != null ? root : Paths.get("");

for (Path directory : referencePath)
{
Path possibleCommonPath = commonPath.resolve(directory);
if (paths.stream().anyMatch(path -> !path.startsWith(possibleCommonPath)))
{
break;
}
commonPath = possibleCommonPath;
}
return commonPath;
}

private void updateStoryNames(Path commonPath, List<Story> stories)
{
for (Story story : stories)
{
String prefix = getPrefix(commonPath, getPathSafely(story.getPath()));
if (prefix != null)
{
if (File.separatorChar == '\\')
{
prefix = prefix.replace(File.separatorChar, UNIX_PATH_SEPARATOR);
}
story.namedAs(prefix + UNIX_PATH_SEPARATOR + story.getName());
}
}
}

private String getPrefix(Path commonPath, Path storyPath)
{
Path prefix = commonPath.relativize(storyPath).getParent();
return prefix != null ? prefix.toString() : null;
}

private Path getPathSafely(String path)
{
try
{
return Paths.get(path);
}
catch (InvalidPathException e)
{
Path resultPath = Paths.get(URI.create(FilenameUtils.getFullPath(path)));
return Paths.get(resultPath.toString(), FilenameUtils.getName(path));
draker94 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vividus;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.jbehave.core.embedder.PerformableTree;
import org.jbehave.core.embedder.PerformableTree.PerformableStory;
import org.jbehave.core.io.StoryLocation;
import org.jbehave.core.reporters.FilePrintStreamFactory.ResolveToSimpleName;
import org.vividus.context.RunContext;

public class ResolveToUniqueSimpleName extends ResolveToSimpleName
{
private static final char UNIX_PATH_SEPARATOR = '/';

private final RunContext runContext;
private final PerformableTree performableTree;

public ResolveToUniqueSimpleName(RunContext runContext, PerformableTree performableTree)
{
this.runContext = runContext;
this.performableTree = performableTree;
}

@Override
public String resolveName(StoryLocation storyLocation, String extension)
{
String storyPath = storyLocation.getStoryPath();

if ("BeforeStories".equals(storyPath) || "AfterStories".equals(storyPath))
{
return super.resolveName(storyLocation, extension);
}

String storyName = performableTree.getRoot().getStories().stream()
.map(PerformableStory::getStory)
.filter(s -> s.getPath().equals(storyPath))
.findFirst().get().getName();
String storyNameOutput = StringUtils.removeEnd(storyName, "story")
.replace(UNIX_PATH_SEPARATOR, '.') + extension;

return runContext.getRunningBatchKey() + File.separator + storyNameOutput;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@

<bean id="testResourceLoader" class="org.vividus.resource.TestResourceLoader" />

<bean id="batchedPerformableTree" class="org.vividus.BatchedPerformableTree" />

<bean id="storyReporterBuilder" class="org.vividus.ExtendedStoryReporterBuilder">
<property name="reportFailureTrace" value="true" />
<property name="storyReporter" ref="storyReporter" />
<property name="codeLocation" value="${bdd.report-directory}" />
<property name="formats" value="${bdd.configuration.formats}" />
<property name="pathResolver">
<bean class="org.jbehave.core.reporters.FilePrintStreamFactory.ResolveToSimpleName" />
<bean class="org.vividus.ResolveToUniqueSimpleName"/>
</property>
</bean>

Expand Down Expand Up @@ -83,9 +85,7 @@
<property name="embedderMonitor">
<bean class="org.vividus.log.LoggingEmbedderMonitor" />
</property>
<property name="performableTree">
<bean class="org.vividus.BatchedPerformableTree" />
</property>
<property name="performableTree" ref="batchedPerformableTree"/>
<property name="generateViewAfterBatches" value="${bdd.generate-view-after-batches}" />
</bean>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,19 @@

package org.vividus;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

import org.jbehave.core.embedder.PerformableTree.PerformableRoot;
import org.jbehave.core.embedder.PerformableTree.PerformableStory;
import org.jbehave.core.embedder.PerformableTree.RunContext;
import org.jbehave.core.embedder.PerformableTree.Status;
import org.jbehave.core.failures.BatchFailures;
import org.jbehave.core.model.Story;
import org.jbehave.core.reporters.StoryReporter;
import org.jbehave.core.steps.StepCollector.Stage;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -89,6 +95,40 @@ void testPerformBeforeOrAfterStoriesPerformNoneForAfter()
testPerformBeforeOrAfterStoriesPerformNone(Stage.AFTER);
}

@Test
void shouldResolveIdenticalNamesBeforeStories()
{
PerformableRoot root = batchedPerformableTree.getRoot();
Story storyFromPreviousBatch = mock();
PerformableStory processedStoryFromPreviousBatch = mock();
when(processedStoryFromPreviousBatch.getStatus()).thenReturn(Status.SUCCESSFUL);
when(processedStoryFromPreviousBatch.getStory()).thenReturn(storyFromPreviousBatch);
Story storyName = new Story("file:/tests/name.story");
Story storyFolderName = new Story("file:/tests/folder/name.story");
root.add(processedStoryFromPreviousBatch);
verify(storyFromPreviousBatch).getPath();
root.add(createPerformableStory(storyName));
root.add(createPerformableStory(storyFolderName));
batchedPerformableTree.performBeforeOrAfterStories(runContext, Stage.BEFORE);
verifyNoMoreInteractions(storyFromPreviousBatch);
assertEquals("folder/name.story", storyFolderName.getName());
}

@Test
void shouldNotInteractWithStoriesAfterStories()
{
PerformableRoot root = batchedPerformableTree.getRoot();
Story storyMock = mock();
PerformableStory performableStoryMock = mock();
when(performableStoryMock.getStory()).thenReturn(storyMock);
when(storyMock.getPath()).thenReturn("");
root.add(performableStoryMock);
verify(storyMock).getPath();

batchedPerformableTree.performBeforeOrAfterStories(runContext, Stage.AFTER);
verifyNoMoreInteractions(storyMock);
}

private BatchFailures getFailures()
{
BatchFailures failures = new BatchFailures();
Expand All @@ -106,4 +146,9 @@ private void mockRunContext()
{
when(runContext.reporter()).thenReturn(mock(StoryReporter.class));
}

private PerformableStory createPerformableStory(Story story)
{
return new PerformableStory(story, null, false);
}
}
Loading