Skip to content

Commit 5b29856

Browse files
committed
Groovy-Cucumber: support multiple World's: post review fix
1 parent 86aeb3f commit 5b29856

File tree

8 files changed

+145
-120
lines changed

8 files changed

+145
-120
lines changed

groovy/src/main/java/cucumber/runtime/groovy/GroovyBackend.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cucumber.runtime.groovy;
22

3-
import cucumber.runtime.*;
3+
import cucumber.runtime.Backend;
4+
import cucumber.runtime.ClassFinder;
5+
import cucumber.runtime.CucumberException;
6+
import cucumber.runtime.Glue;
7+
import cucumber.runtime.UnreportedStepExecutor;
48
import cucumber.runtime.io.Resource;
59
import cucumber.runtime.io.ResourceLoader;
610
import cucumber.runtime.io.ResourceLoaderClassFinder;
@@ -18,7 +22,11 @@
1822

1923
import java.io.IOException;
2024
import java.io.InputStreamReader;
21-
import java.util.*;
25+
import java.util.Collection;
26+
import java.util.HashSet;
27+
import java.util.LinkedList;
28+
import java.util.List;
29+
import java.util.Set;
2230
import java.util.regex.Pattern;
2331

2432
import static cucumber.runtime.io.MultiLoader.packageName;
@@ -150,6 +158,10 @@ public void invoke(Closure body, Object[] args) throws Throwable {
150158
}
151159
}
152160

161+
GroovyWorld getGroovyWorld() {
162+
return world;
163+
}
164+
153165
private static StackTraceElement currentLocation() {
154166
Throwable t = new Throwable();
155167
StackTraceElement[] stackTraceElements = t.getStackTrace();

groovy/src/main/java/cucumber/runtime/groovy/GroovyWorld.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cucumber.runtime.groovy;
22

3-
import groovy.lang.*;
3+
import groovy.lang.GroovyObject;
4+
import groovy.lang.GroovyObjectSupport;
5+
import groovy.lang.MissingMethodException;
6+
import groovy.lang.MissingPropertyException;
7+
import groovy.lang.Tuple;
48
import org.codehaus.groovy.runtime.MetaClassHelper;
59

610
import java.util.LinkedList;
@@ -34,6 +38,10 @@ public Object invokeMethod(String name, Object args) {
3438
return findWorldWithMethod(name, args).invokeMethod(name, args);
3539
}
3640

41+
int worldsCount() {
42+
return worlds.size();
43+
}
44+
3745
private GroovyObject findWorldWithProperty(String property) {
3846
if (worlds.isEmpty()) {
3947
throw new MissingPropertyException(property, GroovyWorld.class);

groovy/src/test/groovy/cucumber/runtime/groovy/GroovyBackendTest.groovy

-52
This file was deleted.

groovy/src/test/groovy/cucumber/runtime/groovy/GroovyWorldTest.groovy

-58
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cucumber.runtime.groovy
2+
3+
class WorldWithPropertyAndMethod {
4+
def aProperty
5+
void aMethod(List<Integer> args) {}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cucumber.runtime.groovy;
2+
3+
import cucumber.runtime.io.ResourceLoader;
4+
import org.codehaus.groovy.runtime.MethodClosure;
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.Mock;
9+
import org.mockito.runners.MockitoJUnitRunner;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
import static junit.framework.Assert.assertNull;
13+
14+
@RunWith(MockitoJUnitRunner.class)
15+
public class GroovyBackendTest {
16+
@Mock
17+
ResourceLoader resourceLoader;
18+
19+
GroovyBackend backend;
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
backend = new GroovyBackend(resourceLoader);
24+
}
25+
26+
@Test
27+
public void should_build_world_by_calling_the_closure() {
28+
backend.registerWorld(new MethodClosure(this, "worldClosureCall"));
29+
backend.buildWorld();
30+
31+
GroovyWorld groovyWorld = backend.getGroovyWorld();
32+
assertEquals(1, groovyWorld.worldsCount());
33+
}
34+
35+
@Test
36+
public void should_build_world_object_even_if_closure_world_was_not_added() {
37+
assertNull(backend.getGroovyWorld());
38+
39+
backend.buildWorld();
40+
41+
assertEquals(0, backend.getGroovyWorld().worldsCount());
42+
}
43+
44+
@Test
45+
public void should_clean_up_worlds_after_dispose() {
46+
backend.registerWorld(new MethodClosure(this, "worldClosureCall"));
47+
backend.buildWorld();
48+
49+
backend.disposeWorld();
50+
51+
assertNull(backend.getGroovyWorld());
52+
}
53+
54+
@SuppressWarnings("UnusedDeclaration")
55+
private AnotherCustomWorld worldClosureCall() {
56+
return new AnotherCustomWorld();
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cucumber.runtime.groovy;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.Assert.assertEquals;
10+
11+
public class GroovyWorldTest {
12+
GroovyWorld world;
13+
14+
@Before
15+
public void setUp() {
16+
world = new GroovyWorld();
17+
}
18+
19+
@Test(expected = RuntimeException.class)
20+
public void should_not_register_pure_java_object() {
21+
world.registerWorld("JAVA");
22+
}
23+
24+
@Test
25+
public void should_support_more_then_one_World() {
26+
world.registerWorld(new CustomWorld());
27+
world.registerWorld(new AnotherCustomWorld());
28+
29+
world.setProperty("lastAte", "groovy");
30+
assertEquals("groovy", world.getProperty("lastAte"));
31+
32+
world.setProperty("aProperty", 1);
33+
assertEquals(1, world.getProperty("aProperty"));
34+
35+
List<Integer> intArgs = Arrays.asList(1,2);
36+
world.invokeMethod("aMethod", intArgs);
37+
assertEquals(intArgs, world.getProperty("methodArgs"));
38+
39+
world.invokeMethod("aMethod", null);
40+
assertEquals("no args", world.getProperty("methodArgs"));
41+
}
42+
43+
@Test(expected = RuntimeException.class)
44+
public void should_detect_double_property_definition() {
45+
world.registerWorld(new WorldWithPropertyAndMethod());
46+
world.registerWorld(new AnotherCustomWorld());
47+
48+
world.getProperty("aProperty");
49+
}
50+
51+
@Test(expected = RuntimeException.class)
52+
public void should_detect_double_method_definition() {
53+
world.registerWorld(new WorldWithPropertyAndMethod());
54+
world.registerWorld(new AnotherCustomWorld());
55+
56+
world.invokeMethod("aMethod", new Integer[]{1,2});
57+
}
58+
}

groovy/src/test/java/cucumber/runtime/groovy/ParallelTest.java

-7
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,6 @@ public class ParallelTest {
1515
@Mock
1616
Closure closure;
1717

18-
@Test(expected = RuntimeException.class)
19-
public void exception_throw_when_world_already_set_on_same_thread() {
20-
GroovyBackend groovyBackend = new GroovyBackend(resourceLoader);
21-
groovyBackend.registerWorld(closure);
22-
groovyBackend.registerWorld(closure);
23-
}
24-
2518
@Test
2619
public void can_have_a_new_backend_on_a_different_thread() {
2720
new GroovyBackend(resourceLoader);

0 commit comments

Comments
 (0)