Skip to content
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,39 +1,24 @@
package io.cucumber.core.order;

import io.cucumber.core.gherkin.Pickle;
import io.cucumber.core.gherkin.Step;
import io.cucumber.plugin.event.Location;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class PickleOrderTest {

@Mock
Pickle firstPickle;

@Mock
Pickle secondPickle;

@Mock
Pickle thirdPickle;

@Test
void lexical_uri_order() {
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.lexicalUriOrder();
List<Pickle> pickles = order.orderPickles(Arrays.asList(thirdPickle, secondPickle, firstPickle));
Expand All @@ -42,11 +27,9 @@ void lexical_uri_order() {

@Test
void reverse_lexical_uri_order() {
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.reverseLexicalUriOrder();
List<Pickle> pickles = order.orderPickles(Arrays.asList(secondPickle, thirdPickle, firstPickle));
Expand All @@ -55,9 +38,67 @@ void reverse_lexical_uri_order() {

@Test
void random_order() {
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.random(42);
List<Pickle> pickles = order.orderPickles(Arrays.asList(firstPickle, secondPickle, thirdPickle));
assertThat(pickles, contains(secondPickle, firstPickle, thirdPickle));
}

private static class StubPickle implements Pickle {
private final Location location;
private final URI uri;

public StubPickle(Location location, URI uri) {
this.location = location;
this.uri = uri;
}

@Override
public String getKeyword() {
return null;
}

@Override
public String getLanguage() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public Location getLocation() {
return location;
}

@Override
public Location getScenarioLocation() {
return null;
}

@Override
public List<Step> getSteps() {
return null;
}

@Override
public List<String> getTags() {
return null;
}

@Override
public URI getUri() {
return uri;
}

@Override
public String getId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
import io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
Expand All @@ -27,8 +26,6 @@
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.hamcrest.number.OrderingComparison.lessThan;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

class CanonicalEventOrderTest {

Expand Down Expand Up @@ -81,10 +78,7 @@ class CanonicalEventOrderTest {
new Result(Status.PASSED, Duration.ZERO, null));

private static TestCaseStarted createTestCaseEvent(Instant instant, URI uri, int line) {
final TestCase testCase = mock(TestCase.class);
given(testCase.getUri()).willReturn(uri);
given(testCase.getLocation()).willReturn(new Location(line, -1));
return new TestCaseStarted(instant, testCase);
return new TestCaseStarted(instant, new StubTestCase(uri, new Location(line, -1)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package io.cucumber.core.plugin;

import io.cucumber.plugin.event.Argument;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.Step;
import io.cucumber.plugin.event.StepArgument;

import java.net.URI;
import java.util.List;
import java.util.UUID;

public class StubPickleStepTestStep implements PickleStepTestStep {
private final String pattern;
private final String stepText;

public StubPickleStepTestStep() {
this.pattern = null;
this.stepText = null;
}

public StubPickleStepTestStep(String pattern, String stepText) {
this.pattern = pattern;
this.stepText = stepText;
}

@Override
public String getPattern() {
return pattern;
}

@Override
public Step getStep() {
return null;
}

@Override
public List<Argument> getDefinitionArgument() {
return null;
}

@Override
public StepArgument getStepArgument() {
return null;
}

@Override
public int getStepLine() {
return 0;
}

@Override
public URI getUri() {
return null;
}

@Override
public String getStepText() {
return stepText;
}

@Override
public String getCodeLocation() {
return null;
}

@Override
public UUID getId() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package io.cucumber.core.plugin;

import io.cucumber.plugin.event.Location;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestStep;

import java.net.URI;
import java.util.List;
import java.util.UUID;

public class StubTestCase implements TestCase {
private final URI uri;
private final Location location;

public StubTestCase() {
this.uri = null;
this.location = null;
}

public StubTestCase(URI uri, Location location) {
this.uri = uri;
this.location = location;
}

@Override
public Integer getLine() {
return null;
}

@Override
public Location getLocation() {
return location;
}

@Override
public String getKeyword() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public String getScenarioDesignation() {
return null;
}

@Override
public List<String> getTags() {
return null;
}

@Override
public List<TestStep> getTestSteps() {
return null;
}

@Override
public URI getUri() {
return uri;
}

@Override
public UUID getId() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import static java.util.Collections.enumeration;
import static java.util.Collections.singletonList;
Expand All @@ -20,8 +22,6 @@
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@WithLogRecordListener
class ClasspathScannerTest {
Expand Down Expand Up @@ -64,19 +64,32 @@ void scanForClassesInNonExistingPackage() {

@Test
void scanForResourcesInUnsupportedFileSystem(LogRecordListener logRecordListener) throws IOException {
ClassLoader classLoader = mock(ClassLoader.class);
ClasspathScanner scanner = new ClasspathScanner(() -> classLoader);
URLStreamHandler handler = new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) {
return null;
}
};
URL resourceUrl = new URL(null, "bundle-resource:com/cucumber/bundle", handler);
when(classLoader.getResources("com/cucumber/bundle")).thenReturn(enumeration(singletonList(resourceUrl)));
ClassLoader classLoader = new MockClassLoader(
Map.of("com/cucumber/bundle", enumeration(singletonList(resourceUrl))));
ClasspathScanner scanner = new ClasspathScanner(() -> classLoader);
assertThat(scanner.scanForClassesInPackage("com.cucumber.bundle"), empty());
assertThat(logRecordListener.getLogRecords().get(0).getMessage(),
containsString("Failed to find resources for 'bundle-resource:com/cucumber/bundle'"));
}

public static class MockClassLoader extends ClassLoader {
private final Map<String, Enumeration<URL>> resources;

public MockClassLoader(Map<String, Enumeration<URL>> resources) {
this.resources = resources;
}

@Override
public Enumeration<URL> getResources(String name) {
return resources.get(name);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.cucumber.core.runner;

import io.cucumber.core.eventbus.IncrementingUuidGenerator;
import io.cucumber.core.feature.TestFeatureParser;
import io.cucumber.core.gherkin.Feature;
import io.cucumber.core.gherkin.Step;
import io.cucumber.core.plugin.StubTestCase;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

Expand All @@ -13,7 +15,6 @@
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

class AmbiguousStepDefinitionMatchTest {

Expand All @@ -25,10 +26,12 @@ class AmbiguousStepDefinitionMatchTest {
private final AmbiguousStepDefinitionsException e = new AmbiguousStepDefinitionsException(step, emptyList());
private final AmbiguousPickleStepDefinitionsMatch match = new AmbiguousPickleStepDefinitionsMatch(
URI.create("file:path/to.feature"), step, e);
private final TestCaseState mockTestCaseState = new TestCaseState(new StubEventBus(),
new IncrementingUuidGenerator().generateId(), new StubTestCase());

@Test
void throws_ambiguous_step_definitions_exception_when_run() {
Executable testMethod = () -> match.runStep(mock(TestCaseState.class));
Executable testMethod = () -> match.runStep(mockTestCaseState);
AmbiguousStepDefinitionsException actualThrown = assertThrows(AmbiguousStepDefinitionsException.class,
testMethod);
assertThat(actualThrown.getMessage(), is(equalTo(
Expand All @@ -37,7 +40,7 @@ void throws_ambiguous_step_definitions_exception_when_run() {

@Test
void throws_ambiguous_step_definitions_exception_when_dry_run() {
Executable testMethod = () -> match.dryRunStep(mock(TestCaseState.class));
Executable testMethod = () -> match.dryRunStep(mockTestCaseState);
AmbiguousStepDefinitionsException actualThrown = assertThrows(AmbiguousStepDefinitionsException.class,
testMethod);
assertThat(actualThrown.getMessage(), is(equalTo(
Expand Down
Loading