Skip to content

Commit a0778cd

Browse files
committed
Package XStream and diffutils inside core. Closes #179.
1 parent 1ce3d6f commit a0778cd

File tree

9 files changed

+243
-213
lines changed

9 files changed

+243
-213
lines changed

History.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## In Git
22

3+
* [Java] You must use `cucumber.runtime.xstream` instead of `com.thoughtworks.xstream` for custom converters.
4+
* [Core] XStream and Diffutils are now packaged inside the cucumber-core jar under new package names. ([#179](https://github.com/cucumber/cucumber-jvm/issues/179) Aslak Hellesøy)
35
* [Core] Fail if no features are found ([#163](https://github.com/cucumber/cucumber-jvm/issues/163) Aslak Hellesøy)
46
* [Core] Fail if duplicate features are detected ([#165](https://github.com/cucumber/cucumber-jvm/issues/165) Aslak Hellesøy)
57

core/pom.xml

+38-5
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@
1414
<name>Cucumber-JVM: Core</name>
1515

1616
<dependencies>
17+
<!-- These dependencies are not packaged because conflicts are unlikely -->
1718
<dependency>
1819
<groupId>info.cukes</groupId>
1920
<artifactId>gherkin</artifactId>
2021
</dependency>
22+
<dependency>
23+
<groupId>info.cukes</groupId>
24+
<artifactId>cucumber-html</artifactId>
25+
</dependency>
26+
<!-- These dependencies are packaged to prevent conflicts with users' own code -->
2127
<dependency>
2228
<groupId>com.thoughtworks.xstream</groupId>
2329
<artifactId>xstream</artifactId>
30+
<scope>provided</scope>
2431
</dependency>
2532
<dependency>
2633
<groupId>com.googlecode.java-diff-utils</groupId>
2734
<artifactId>diffutils</artifactId>
28-
</dependency>
29-
<dependency>
30-
<groupId>info.cukes</groupId>
31-
<artifactId>cucumber-html</artifactId>
35+
<scope>provided</scope>
3236
</dependency>
3337

3438
<dependency>
@@ -66,7 +70,7 @@
6670
<plugin>
6771
<groupId>org.apache.maven.plugins</groupId>
6872
<artifactId>maven-jar-plugin</artifactId>
69-
<version>2.3.1</version>
73+
<version>2.3.2</version>
7074
<configuration>
7175
<archive>
7276
<manifest>
@@ -75,6 +79,35 @@
7579
</archive>
7680
</configuration>
7781
</plugin>
82+
<plugin>
83+
<groupId>org.sonatype.plugins</groupId>
84+
<artifactId>jarjar-maven-plugin</artifactId>
85+
<version>1.5</version>
86+
<executions>
87+
<execution>
88+
<phase>package</phase>
89+
<goals>
90+
<goal>jarjar</goal>
91+
</goals>
92+
<configuration>
93+
<includes>
94+
<include>com.thoughtworks.xstream:xstream</include>
95+
<include>com.googlecode.java-diff-utils:diffutils</include>
96+
</includes>
97+
<rules>
98+
<rule>
99+
<pattern>com.thoughtworks.xstream.**</pattern>
100+
<result>cucumber.runtime.xstream.@1</result>
101+
</rule>
102+
<rule>
103+
<pattern>difflib.**</pattern>
104+
<result>cucumber.difflib.@1</result>
105+
</rule>
106+
</rules>
107+
</configuration>
108+
</execution>
109+
</executions>
110+
</plugin>
78111
</plugins>
79112
</build>
80113
</project>

java/src/main/java/cucumber/annotation/DateFormat.java renamed to core/src/main/java/cucumber/DateFormat.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cucumber.annotation;
1+
package cucumber;
22

33
import java.lang.annotation.ElementType;
44
import java.lang.annotation.Retention;

core/src/main/java/cucumber/runtime/ParameterType.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package cucumber.runtime;
22

3+
import cucumber.DateFormat;
4+
5+
import java.lang.annotation.Annotation;
6+
import java.lang.reflect.Method;
37
import java.lang.reflect.ParameterizedType;
48
import java.lang.reflect.Type;
9+
import java.util.ArrayList;
10+
import java.util.List;
511

612
/**
713
* This class composes all interesting parameter information into one object.
@@ -39,4 +45,21 @@ public String getDateFormat() {
3945
public String toString() {
4046
return type.toString();
4147
}
48+
49+
public static List<ParameterType> fromMethod(Method method) {
50+
List<ParameterType> result = new ArrayList<ParameterType>();
51+
Type[] genericParameterTypes = method.getGenericParameterTypes();
52+
Annotation[][] annotations = method.getParameterAnnotations();
53+
for (int i = 0; i < genericParameterTypes.length; i++) {
54+
String dateFormat = null;
55+
for (Annotation annotation : annotations[i]) {
56+
if (annotation instanceof DateFormat) {
57+
dateFormat = ((DateFormat) annotation).value();
58+
break;
59+
}
60+
}
61+
result.add(new ParameterType(genericParameterTypes[i], dateFormat));
62+
}
63+
return result;
64+
}
4265
}

java/src/test/java/cucumber/runtime/java/JavaTableConversionTest.java renamed to core/src/test/java/cucumber/table/FromDataTableTest.java

+103-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
package cucumber.runtime.java;
1+
package cucumber.table;
22

33
import com.thoughtworks.xstream.annotations.XStreamConverter;
4+
import com.thoughtworks.xstream.converters.Converter;
5+
import com.thoughtworks.xstream.converters.MarshallingContext;
6+
import com.thoughtworks.xstream.converters.UnmarshallingContext;
47
import com.thoughtworks.xstream.converters.javabean.JavaBeanConverter;
5-
import cucumber.annotation.DateFormat;
8+
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
9+
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
10+
import cucumber.DateFormat;
611
import cucumber.runtime.CucumberException;
12+
import cucumber.runtime.ParameterType;
713
import cucumber.runtime.StepDefinition;
814
import cucumber.runtime.StepDefinitionMatch;
915
import cucumber.runtime.converters.LocalizedXStreams;
10-
import cucumber.table.DataTable;
1116
import gherkin.I18n;
1217
import gherkin.formatter.Argument;
1318
import gherkin.formatter.model.Comment;
@@ -24,13 +29,12 @@
2429
import java.util.List;
2530
import java.util.Map;
2631
import java.util.TimeZone;
27-
import java.util.regex.Pattern;
2832

2933
import static java.util.Arrays.asList;
3034
import static java.util.Collections.emptyList;
3135
import static junit.framework.Assert.assertEquals;
3236

33-
public class JavaTableConversionTest {
37+
public class FromDataTableTest {
3438
@Rule
3539
public ExpectedException thrown = ExpectedException.none();
3640

@@ -40,6 +44,7 @@ public class JavaTableConversionTest {
4044
public static class StepDefs {
4145
public List<UserPojo> listOfPojos;
4246
public List<UserBean> listOfBeans;
47+
public List<UserWithNameField> listOfUsersWithNameField;
4348
public List<List<Double>> listOfListOfDoubles;
4449
public List<Map<String, String>> listOfMapsOfStringToString;
4550
public List<Map<String, Object>> listOfMapsOfStringToObject;
@@ -54,6 +59,10 @@ public void listOfBeans(@DateFormat("yyyy-MM-dd") List<UserBean> listOfBeans) {
5459
this.listOfBeans = listOfBeans;
5560
}
5661

62+
public void listOfUsersWithNameField(@DateFormat("yyyy-MM-dd") List<UserWithNameField> listOfUsersWithNameField) {
63+
this.listOfUsersWithNameField = listOfUsersWithNameField;
64+
}
65+
5766
public void listOfListOfDoubles(List<List<Double>> listOfListOfDoubles) {
5867
this.listOfListOfDoubles = listOfListOfDoubles;
5968
}
@@ -94,6 +103,15 @@ public void transforms_to_list_of_beans() throws Throwable {
94103
assertEquals(sidsBirthday(), stepDefs.listOfBeans.get(0).getBirthDate());
95104
}
96105

106+
@Test
107+
public void converts_table_to_list_of_class_with_special_fields() throws Throwable {
108+
Method m = StepDefs.class.getMethod("listOfUsersWithNameField", List.class);
109+
StepDefs stepDefs = runStepDef(m, listOfDatesAndNamesWithHeader());
110+
assertEquals(sidsBirthday(), stepDefs.listOfUsersWithNameField.get(0).birthDate);
111+
assertEquals("Sid", stepDefs.listOfUsersWithNameField.get(0).name.first);
112+
assertEquals("Vicious", stepDefs.listOfUsersWithNameField.get(0).name.last);
113+
}
114+
97115
@Test
98116
public void transforms_to_list_of_single_values() throws Throwable {
99117
Method m = StepDefs.class.getMethod("listOfListOfDoubles", List.class);
@@ -152,7 +170,7 @@ public void does_not_transform_to_list_of_non_generic_map() throws Throwable {
152170

153171
private StepDefs runStepDef(Method method, List<DataTableRow> rows) throws Throwable {
154172
StepDefs stepDefs = new StepDefs();
155-
StepDefinition stepDefinition = new JavaStepDefinition(method, Pattern.compile("whatever"), new SingletonFactory(stepDefs));
173+
StepDefinition stepDefinition = new DirectStepDef(stepDefs, method);
156174

157175
Step stepWithRows = new Step(NO_COMMENTS, "Given ", "something", 10, rows, null);
158176

@@ -169,6 +187,13 @@ private List<DataTableRow> listOfDatesWithHeader() {
169187
return rows;
170188
}
171189

190+
private List<DataTableRow> listOfDatesAndNamesWithHeader() {
191+
List<DataTableRow> rows = new ArrayList<DataTableRow>();
192+
rows.add(new DataTableRow(NO_COMMENTS, asList("Birth Date", "Name"), 1));
193+
rows.add(new DataTableRow(NO_COMMENTS, asList("1957-05-10", "Sid Vicious"), 2));
194+
return rows;
195+
}
196+
172197
private List<DataTableRow> listOfDoublesWithoutHeader() {
173198
List<DataTableRow> rows = new ArrayList<DataTableRow>();
174199
rows.add(new DataTableRow(NO_COMMENTS, asList("100.5", "99.5"), 2));
@@ -201,4 +226,76 @@ public void setBirthDate(Date birthDate) {
201226
this.birthDateX = birthDate;
202227
}
203228
}
229+
230+
public static class UserWithNameField {
231+
public Name name;
232+
public Date birthDate;
233+
}
234+
235+
@XStreamConverter(NameConverter.class)
236+
public static class Name {
237+
public String first;
238+
public String last;
239+
}
240+
241+
public static class NameConverter implements Converter {
242+
@Override
243+
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
244+
throw new UnsupportedOperationException();
245+
}
246+
247+
@Override
248+
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
249+
Name name = new Name();
250+
String[] firstLast = reader.getValue().split(" ");
251+
name.first = firstLast[0];
252+
name.last = firstLast[1];
253+
return name;
254+
}
255+
256+
@Override
257+
public boolean canConvert(Class type) {
258+
return type.equals(Name.class);
259+
}
260+
}
261+
262+
private class DirectStepDef implements StepDefinition {
263+
private final Object target;
264+
private final Method method;
265+
266+
public DirectStepDef(Object target, Method method) {
267+
this.target = target;
268+
this.method = method;
269+
}
270+
271+
@Override
272+
public List<Argument> matchedArguments(Step step) {
273+
throw new UnsupportedOperationException();
274+
}
275+
276+
@Override
277+
public String getLocation() {
278+
return getClass().getName();
279+
}
280+
281+
@Override
282+
public List<ParameterType> getParameterTypes() {
283+
return ParameterType.fromMethod(method);
284+
}
285+
286+
@Override
287+
public void execute(I18n i18n, Object[] args) throws Throwable {
288+
method.invoke(target, args);
289+
}
290+
291+
@Override
292+
public boolean isDefinedAt(StackTraceElement stackTraceElement) {
293+
return false;
294+
}
295+
296+
@Override
297+
public String getPattern() {
298+
throw new UnsupportedOperationException();
299+
}
300+
}
204301
}

0 commit comments

Comments
 (0)