Skip to content

Commit 4e3d258

Browse files
DFUKDFUK
DFUK
authored and
DFUK
committed
Scala fixes for Windows. #139
1 parent f840e73 commit 4e3d258

File tree

12 files changed

+63
-13
lines changed

12 files changed

+63
-13
lines changed

core/src/main/java/cucumber/io/ClasspathResourceLoader.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import cucumber.runtime.CucumberException;
44
import cucumber.runtime.Utils;
55

6-
import java.io.File;
76
import java.lang.annotation.Annotation;
87
import java.lang.reflect.InvocationTargetException;
98
import java.util.Collection;
@@ -22,7 +21,7 @@ public Collection<Class<? extends Annotation>> getAnnotations(String packagePath
2221
public <T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packagePath) {
2322
Collection<Class<? extends T>> result = new HashSet<Class<? extends T>>();
2423
for (Resource classResource : resources(packagePath, ".class")) {
25-
String className = className(classResource.getPath());
24+
String className = classResource.getClassName();
2625
Class<?> clazz = loadClass(className);
2726
if (clazz != null && !parentType.equals(clazz) && parentType.isAssignableFrom(clazz)) {
2827
result.add(clazz.asSubclass(parentType));
@@ -46,7 +45,10 @@ public <T> Collection<? extends T> instantiateSubclasses(Class<T> parentType, St
4645
Collection<T> result = new HashSet<T>();
4746
for (Class<? extends T> clazz : getDescendants(parentType, packagePath)) {
4847
if (Utils.isInstantiable(clazz)) {
49-
result.add(newInstance(constructorParams, constructorArgs, clazz));
48+
try {
49+
result.add(newInstance(constructorParams, constructorArgs, clazz));
50+
} catch (CucumberException ignore) {
51+
}
5052
}
5153
}
5254
return result;
@@ -76,10 +78,6 @@ private <T> T newInstance(Class[] constructorParams, Object[] constructorArgs, C
7678
}
7779
}
7880

79-
private String className(String pathToClass) {
80-
return pathToClass.substring(0, pathToClass.length() - 6).replace(File.separatorChar, '.');
81-
}
82-
8381
private ClassLoader cl() {
8482
return Thread.currentThread().getContextClassLoader();
8583
}

core/src/main/java/cucumber/io/FileResource.java

+6
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public String getPath() {
3030
public InputStream getInputStream() throws IOException {
3131
return new FileInputStream(file);
3232
}
33+
34+
@Override
35+
public String getClassName() {
36+
String path = getPath();
37+
return path.substring(0, path.length() - 6).replace(File.separatorChar, '.');
38+
}
3339
}

core/src/main/java/cucumber/io/Resource.java

+2
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ public interface Resource {
77
String getPath();
88

99
InputStream getInputStream() throws IOException;
10+
11+
String getClassName();
1012
}

core/src/main/java/cucumber/io/ZipResource.java

+6
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ public String getPath() {
2323
public InputStream getInputStream() throws IOException {
2424
return jarFile.getInputStream(jarEntry);
2525
}
26+
27+
@Override
28+
public String getClassName() {
29+
String path = getPath();
30+
return path.substring(0, path.length() - 6).replace('/', '.');
31+
}
2632
}

core/src/test/java/cucumber/runtime/RuntimeTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
import org.junit.Test;
77

88
import java.util.Collections;
9+
import java.util.List;
910

1011
import static cucumber.runtime.TestHelper.feature;
12+
import static java.util.Arrays.asList;
1113
import static org.junit.Assert.assertEquals;
14+
import static org.mockito.Mockito.mock;
1215

1316
public class RuntimeTest {
1417
@Test
@@ -21,7 +24,8 @@ public void runs_feature_with_json_formatter() throws Exception {
2124
" When s\n");
2225
StringBuilder out = new StringBuilder();
2326
JSONFormatter jsonFormatter = new JSONFormatter(out);
24-
new Runtime(Collections.<String>emptyList(), new ClasspathResourceLoader()).run(feature, jsonFormatter, jsonFormatter);
27+
List<Backend> backends = asList(mock(Backend.class));
28+
new Runtime(Collections.<String>emptyList(), new ClasspathResourceLoader(), backends, true).run(feature, jsonFormatter, jsonFormatter);
2529
jsonFormatter.done();
2630
String expected = "[{\"id\":\"feature-name\",\"description\":\"\",\"name\":\"feature name\",\"keyword\":\"Feature\",\"line\":1,\"elements\":[{\"description\":\"\",\"name\":\"background name\",\"keyword\":\"Background\",\"line\":2,\"steps\":[{\"result\":{\"status\":\"undefined\"},\"name\":\"b\",\"keyword\":\"Given \",\"line\":3,\"match\":{}}],\"type\":\"background\"},{\"id\":\"feature-name;scenario-name\",\"description\":\"\",\"name\":\"scenario name\",\"keyword\":\"Scenario\",\"line\":4,\"steps\":[{\"result\":{\"status\":\"undefined\"},\"name\":\"s\",\"keyword\":\"When \",\"line\":5,\"match\":{}}],\"type\":\"scenario\"}],\"uri\":\"test.feature\"}]";
2731
assertEquals(expected, out.toString());

core/src/test/java/cucumber/runtime/TestHelper.java

+5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public InputStream getInputStream() {
3030
throw new RuntimeException(e);
3131
}
3232
}
33+
34+
@Override
35+
public String getClassName() {
36+
throw new UnsupportedOperationException();
37+
}
3338
}, new ArrayList<Object>());
3439
return cucumberFeatures.get(0);
3540
}

core/src/test/java/cucumber/runtime/autocomplete/StepdefGeneratorTest.java

+5
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public InputStream getInputStream() {
9393
throw new RuntimeException(e);
9494
}
9595
}
96+
97+
@Override
98+
public String getClassName() {
99+
throw new UnsupportedOperationException();
100+
}
96101
}, emptyList());
97102
return features;
98103
}

java/src/test/java/cucumber/runtime/java/ObjectFactoryHolderTest.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.lang.reflect.Field;
99

10+
import static org.junit.Assert.assertEquals;
1011
import static org.junit.Assert.assertTrue;
1112
import static org.mockito.Mockito.mock;
1213

@@ -17,21 +18,21 @@ public void resetObjectFactory() {
1718
}
1819

1920
@Test
20-
public void testFactory() throws Exception {
21-
ObjectFactoryHolder.setFactory(new MockObjectFactory());
21+
public void uses_default_java_object_factory_if_none_is_set() throws Exception {
22+
ObjectFactoryHolder.setFactory(new StubObjectFactory());
2223
JavaBackend backend = new JavaBackend(mock(ResourceLoader.class));
2324

2425
// do it by reflection to not change the API
2526
Field field = JavaBackend.class.getDeclaredField("objectFactory");
2627
field.setAccessible(true);
27-
assertTrue(field.get(backend) instanceof MockObjectFactory);
28+
assertEquals(StubObjectFactory.class, field.get(backend).getClass());
2829

2930
ObjectFactoryHolder.setFactory(null);
3031
backend = new JavaBackend(mock(ResourceLoader.class));
31-
assertTrue(field.get(backend) instanceof DefaultJavaObjectFactory);
32+
assertEquals(DefaultJavaObjectFactory.class, field.get(backend).getClass());
3233
}
3334

34-
public static class MockObjectFactory implements ObjectFactory {
35+
private static class StubObjectFactory implements ObjectFactory {
3536
@Override
3637
public void createInstances() {
3738
}

scala/cucumber-scala.iml

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<content url="file://$MODULE_DIR$">
3333
<sourceFolder url="file://$MODULE_DIR$/src/main/scala" isTestSource="false" />
3434
<sourceFolder url="file://$MODULE_DIR$/src/test/scala" isTestSource="true" />
35+
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" isTestSource="true" />
3536
</content>
3637
<orderEntry type="inheritedJdk" />
3738
<orderEntry type="sourceFolder" forTests="false" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: Cukes
2+
Scenario: in the belly
3+
Given I have 4 "cukes" in my belly
4+
Then I am "happy"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package cucumber.runtime
2+
3+
import org.junit.runner.RunWith
4+
import cucumber.junit.Cucumber
5+
6+
@RunWith(classOf[Cucumber])
7+
class RunCukesTest
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cucumber.runtime
2+
3+
import junit.framework.Assert._
4+
5+
class RpnCalculatorStepDefinitions extends ScalaDsl with EN {
6+
Given("""^I have (\d+) "([^"]*)" in my belly$"""){ (arg0:Int, arg1:String) =>
7+
}
8+
9+
Then("""^I am "([^"]*)"$"""){ (arg0:String) =>
10+
}
11+
}

0 commit comments

Comments
 (0)