Skip to content

Commit 3aa9e0d

Browse files
committed
Merge with master
2 parents f8ecfb4 + 494ec54 commit 3aa9e0d

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

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

+26-11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
public class RuntimeOptionsFactory {
1414
private final Class clazz;
1515
private final Class<? extends Annotation>[] annotationClasses;
16+
private boolean featuresSpecified = false;
17+
private boolean glueSpecified = false;
18+
private boolean formatSpecified = false;
1619

1720
public RuntimeOptionsFactory(Class clazz, Class<? extends Annotation>[] annotationClasses) {
1821
this.clazz = clazz;
@@ -39,11 +42,14 @@ private List<String> buildArgsFromOptions() {
3942
addName(options, args);
4043
addDotCucumber(options, args);
4144
addSnippets(options, args);
45+
addGlue(optionsArray, args);
46+
addFeatures(optionsArray, args);
4247
}
4348
}
44-
addGlue(optionsArray, args, classWithOptions);
45-
addFeatures(optionsArray, args, classWithOptions);
4649
}
50+
addDefaultFeaturePathIfNoFeaturePathIsSpecified(args, clazz);
51+
addDefaultGlueIfNoGlueIsSpecified(args, clazz);
52+
addDefaultFormatIfNoFormatIsSpecified(args);
4753
return args;
4854
}
4955

@@ -92,37 +98,46 @@ private void addFormats(Annotation options, List<String> args) {
9298
args.add("--format");
9399
args.add(format);
94100
}
95-
} else {
101+
formatSpecified = true;
102+
}
103+
}
104+
105+
private void addDefaultFormatIfNoFormatIsSpecified(List<String> args) {
106+
if (!formatSpecified) {
96107
args.add("--format");
97108
args.add("null");
98109
}
99110
}
100111

101-
private void addFeatures(Annotation[] optionsArray, List<String> args, Class clazz) {
102-
boolean specified = false;
112+
private void addFeatures(Annotation[] optionsArray, List<String> args) {
103113
for (Annotation options : optionsArray) {
104114
if (options != null && this.<String[]>invoke(options, "features").length != 0) {
105115
Collections.addAll(args, this.<String[]>invoke(options, "features"));
106-
specified = true;
116+
featuresSpecified = true;
107117
}
108118
}
109-
if (!specified) {
119+
}
120+
121+
private void addDefaultFeaturePathIfNoFeaturePathIsSpecified(List<String> args, Class clazz) {
122+
if (!featuresSpecified) {
110123
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
111124
}
112125
}
113126

114-
private void addGlue(Annotation[] optionsArray, List<String> args, Class clazz) {
115-
boolean specified = false;
127+
private void addGlue(Annotation[] optionsArray, List<String> args) {
116128
for (Annotation options : optionsArray) {
117129
if (options != null && this.<String[]>invoke(options, "glue").length != 0) {
118130
for (String glue : this.<String[]>invoke(options, "glue")) {
119131
args.add("--glue");
120132
args.add(glue);
121133
}
122-
specified = true;
134+
glueSpecified = true;
123135
}
124136
}
125-
if (!specified) {
137+
}
138+
139+
private void addDefaultGlueIfNoGlueIsSpecified(List<String> args, Class clazz) {
140+
if (!glueSpecified) {
126141
args.add("--glue");
127142
args.add(MultiLoader.CLASSPATH_SCHEME + packagePath(clazz));
128143
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ public void create_without_options() throws Exception {
4242
assertFalse(runtimeOptions.isStrict());
4343
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
4444
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
45+
assertEquals(1, runtimeOptions.getFormatters().size());
46+
assertEquals("cucumber.runtime.formatter.NullFormatter", runtimeOptions.getFormatters().get(0).getClass().getName());
47+
}
48+
49+
@Test
50+
public void create_without_options_with_base_class_without_options() throws Exception {
51+
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(WithoutOptionsWithBaseClassWithoutOptions.class, new Class[]{CucumberOptions.class});
52+
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
53+
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
54+
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
55+
assertEquals(1, runtimeOptions.getFormatters().size());
56+
assertEquals("cucumber.runtime.formatter.NullFormatter", runtimeOptions.getFormatters().get(0).getClass().getName());
4557
}
4658

4759
@Test
@@ -158,6 +170,10 @@ static class WithoutOptions {
158170
// empty
159171
}
160172

173+
static class WithoutOptionsWithBaseClassWithoutOptions extends WithoutOptions {
174+
// empty
175+
}
176+
161177
@CucumberOptions(format = "pretty")
162178
static class SubClassWithFormatter extends BaseClassWithFormatter {
163179
// empty

testng/src/main/java/cucumber/api/testng/TestNGCucumberRunner.java

-8
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
import cucumber.runtime.io.ResourceLoaderClassFinder;
1111

1212
import java.io.IOException;
13-
import java.util.ArrayList;
14-
import java.util.HashSet;
15-
import java.util.List;
1613

1714
/**
1815
* Glue code for running Cucumber via TestNG.
@@ -33,11 +30,6 @@ public TestNGCucumberRunner(Class clazz) {
3330
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz, new Class[]{CucumberOptions.class});
3431
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
3532

36-
// remove duplicates from glue path.
37-
List<String> uniqueGlue = new ArrayList<String>(new HashSet<String>(runtimeOptions.getGlue()));
38-
runtimeOptions.getGlue().clear();
39-
runtimeOptions.getGlue().addAll(uniqueGlue);
40-
4133
TestNgReporter reporter = new TestNgReporter(System.out);
4234
runtimeOptions.addFormatter(reporter);
4335
ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);

0 commit comments

Comments
 (0)