Skip to content

Commit 65f3bd6

Browse files
committed
Refactor RuntimeOptions towards somthing that can be extracted as an interface. That will make it easier to provide multiple implementations.
1 parent b0e36c9 commit 65f3bd6

File tree

6 files changed

+76
-49
lines changed

6 files changed

+76
-49
lines changed

android/src/cucumber/api/android/CucumberInstrumentation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public void onStart() {
165165
}
166166

167167
AndroidReporter reporter = new AndroidReporter(numScenarios);
168-
runtimeOptions.formatters.clear();
169-
runtimeOptions.formatters.add(reporter);
168+
runtimeOptions.getFormatters().clear();
169+
runtimeOptions.getFormatters().add(reporter);
170170

171171
for (CucumberFeature cucumberFeature : cucumberFeatures) {
172172
Formatter formatter = runtimeOptions.formatter(classLoader);

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public Runtime(ResourceLoader resourceLoader, ClassLoader classLoader, Collectio
6060
glue = new RuntimeGlue(undefinedStepsTracker, new LocalizedXStreams(classLoader));
6161

6262
for (Backend backend : backends) {
63-
backend.loadGlue(glue, runtimeOptions.glue);
63+
backend.loadGlue(glue, runtimeOptions.getGlue());
6464
backend.setUnreportedStepExecutor(this);
6565
}
6666
this.runtimeOptions = runtimeOptions;
@@ -128,7 +128,7 @@ public byte exitStatus() {
128128
}
129129

130130
private boolean hasUndefinedOrPendingStepsAndIsStrict() {
131-
return runtimeOptions.strict && hasUndefinedOrPendingSteps();
131+
return runtimeOptions.isStrict() && hasUndefinedOrPendingSteps();
132132
}
133133

134134
private boolean hasUndefinedOrPendingSteps() {
@@ -169,7 +169,7 @@ public void runAfterHooks(Reporter reporter, Set<Tag> tags) {
169169
}
170170

171171
private void runHooks(List<HookDefinition> hooks, Reporter reporter, Set<Tag> tags, boolean isBefore) {
172-
if (!runtimeOptions.dryRun) {
172+
if (!runtimeOptions.isDryRun()) {
173173
for (HookDefinition hook : hooks) {
174174
runHookIfTagsMatch(hook, reporter, tags, isBefore);
175175
}
@@ -244,7 +244,7 @@ public void runStep(String uri, Step step, Reporter reporter, I18n i18n) {
244244
return;
245245
}
246246

247-
if (runtimeOptions.dryRun) {
247+
if (runtimeOptions.isDryRun()) {
248248
skipNextStep = true;
249249
}
250250

@@ -279,6 +279,6 @@ public static boolean isPending(Throwable t) {
279279
}
280280

281281
public void writeStepdefsJson() throws IOException {
282-
glue.writeStepdefsJson(runtimeOptions.featurePaths, runtimeOptions.dotCucumber);
282+
glue.writeStepdefsJson(runtimeOptions.getFeaturePaths(), runtimeOptions.getDotCucumber());
283283
}
284284
}

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

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public class RuntimeOptions {
2828
public static final String USAGE = FixJava.readResource("/cucumber/runtime/USAGE.txt");
2929
private static final Pattern SHELLWORDS_PATTERN = Pattern.compile("[^\\s']+|'([^']*)'");
3030

31-
public final List<String> glue = new ArrayList<String>();
32-
public final List<Object> filters = new ArrayList<Object>();
33-
public final List<Formatter> formatters = new ArrayList<Formatter>();
34-
public final List<String> featurePaths = new ArrayList<String>();
31+
private final List<String> glue = new ArrayList<String>();
32+
private final List<Object> filters = new ArrayList<Object>();
33+
private final List<Formatter> formatters = new ArrayList<Formatter>();
34+
private final List<String> featurePaths = new ArrayList<String>();
3535
private final FormatterFactory formatterFactory;
36-
public URL dotCucumber;
37-
public boolean dryRun;
38-
public boolean strict = false;
39-
public boolean monochrome = false;
36+
private URL dotCucumber;
37+
private boolean dryRun;
38+
private boolean strict = false;
39+
private boolean monochrome = false;
4040

4141
public RuntimeOptions(Properties properties, String... argv) {
4242
/* IMPORTANT! Make sure USAGE.txt is always uptodate if this class changes */
@@ -48,7 +48,7 @@ public RuntimeOptions(Properties properties, String... argv) {
4848

4949
parse(new ArrayList<String>(asList(argv)));
5050
if (properties.containsKey("cucumber.options")) {
51-
parse(cucumberOptionsSplit(properties.getProperty("cucumber.options")));
51+
parse(shellWords(properties.getProperty("cucumber.options")));
5252
}
5353

5454
if (formatters.isEmpty()) {
@@ -57,9 +57,9 @@ public RuntimeOptions(Properties properties, String... argv) {
5757
setFormatterOptions();
5858
}
5959

60-
private List<String> cucumberOptionsSplit(String property) {
60+
private List<String> shellWords(String cmdline) {
6161
List<String> matchList = new ArrayList<String>();
62-
Matcher shellwordsMatcher = SHELLWORDS_PATTERN.matcher(property);
62+
Matcher shellwordsMatcher = SHELLWORDS_PATTERN.matcher(cmdline);
6363
while (shellwordsMatcher.find()) {
6464
if (shellwordsMatcher.group(1) != null) {
6565
matchList.add(shellwordsMatcher.group(1));
@@ -116,8 +116,7 @@ private void parse(List<String> args) {
116116
}
117117
}
118118

119-
private void printUsage()
120-
{
119+
private void printUsage() {
121120
System.out.println(USAGE);
122121
}
123122

@@ -171,4 +170,32 @@ private void setStrictOnStrictAwareFormatters(Formatter formatter) {
171170
strictAware.setStrict(strict);
172171
}
173172
}
173+
174+
public List<String> getGlue() {
175+
return glue;
176+
}
177+
178+
public boolean isStrict() {
179+
return strict;
180+
}
181+
182+
public boolean isDryRun() {
183+
return dryRun;
184+
}
185+
186+
public List<String> getFeaturePaths() {
187+
return featurePaths;
188+
}
189+
190+
public URL getDotCucumber() {
191+
return dotCucumber;
192+
}
193+
194+
public List<Formatter> getFormatters() {
195+
return formatters;
196+
}
197+
198+
public List<Object> getFilters() {
199+
return filters;
200+
}
174201
}

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,62 +37,62 @@ public void has_usage() {
3737
@Test
3838
public void assigns_feature_paths() {
3939
RuntimeOptions options = new RuntimeOptions(new Properties(), "--glue", "somewhere", "somewhere_else");
40-
assertEquals(asList("somewhere_else"), options.featurePaths);
40+
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
4141
}
4242

4343
@Test
4444
public void strips_options() {
4545
RuntimeOptions options = new RuntimeOptions(new Properties(), " --glue ", "somewhere", "somewhere_else");
46-
assertEquals(asList("somewhere_else"), options.featurePaths);
46+
assertEquals(asList("somewhere_else"), options.getFeaturePaths());
4747
}
4848

4949
@Test
5050
public void assigns_glue() {
5151
RuntimeOptions options = new RuntimeOptions(new Properties(), "--glue", "somewhere");
52-
assertEquals(asList("somewhere"), options.glue);
52+
assertEquals(asList("somewhere"), options.getGlue());
5353
}
5454

5555
@Test
5656
public void assigns_dotcucumber() throws MalformedURLException {
5757
RuntimeOptions options = new RuntimeOptions(new Properties(), "--dotcucumber", "somewhere", "--glue", "somewhere");
58-
assertEquals(new URL("file:somewhere/"), options.dotCucumber);
58+
assertEquals(new URL("file:somewhere/"), options.getDotCucumber());
5959
}
6060

6161
@Test
6262
public void creates_formatter() {
6363
RuntimeOptions options = new RuntimeOptions(new Properties(), "--format", "html:some/dir", "--glue", "somewhere");
64-
assertEquals("cucumber.runtime.formatter.HTMLFormatter", options.formatters.get(0).getClass().getName());
64+
assertEquals("cucumber.runtime.formatter.HTMLFormatter", options.getFormatters().get(0).getClass().getName());
6565
}
6666

6767
@Test
6868
public void assigns_strict() {
6969
RuntimeOptions options = new RuntimeOptions(new Properties(), "--strict", "--glue", "somewhere");
70-
assertTrue(options.strict);
70+
assertTrue(options.isStrict());
7171
}
7272

7373
@Test
7474
public void assigns_strict_short() {
7575
RuntimeOptions options = new RuntimeOptions(new Properties(), "-s", "--glue", "somewhere");
76-
assertTrue(options.strict);
76+
assertTrue(options.isStrict());
7777
}
7878

7979
@Test
8080
public void default_strict() {
8181
RuntimeOptions options = new RuntimeOptions(new Properties(), "--glue", "somewhere");
82-
assertFalse(options.strict);
82+
assertFalse(options.isStrict());
8383
}
8484

8585
@Test
8686
public void name_without_spaces_is_preserved() {
8787
RuntimeOptions options = new RuntimeOptions(new Properties(), "--name", "someName");
88-
Pattern actualPattern = (Pattern) options.filters.iterator().next();
88+
Pattern actualPattern = (Pattern) options.getFilters().iterator().next();
8989
assertEquals("someName", actualPattern.pattern());
9090
}
9191

9292
@Test
9393
public void name_with_spaces_is_preserved() {
9494
RuntimeOptions options = new RuntimeOptions(new Properties(), "--name", "some Name");
95-
Pattern actualPattern = (Pattern) options.filters.iterator().next();
95+
Pattern actualPattern = (Pattern) options.getFilters().iterator().next();
9696
assertEquals("some Name", actualPattern.pattern());
9797
}
9898

@@ -101,7 +101,7 @@ public void ensure_name_with_spaces_works_with_cucumber_options() {
101101
Properties properties = new Properties();
102102
properties.setProperty("cucumber.options", "--name 'some Name'");
103103
RuntimeOptions options = new RuntimeOptions(properties);
104-
Pattern actualPattern = (Pattern) options.filters.iterator().next();
104+
Pattern actualPattern = (Pattern) options.getFilters().iterator().next();
105105
assertEquals("some Name", actualPattern.pattern());
106106
}
107107

@@ -110,59 +110,59 @@ public void ensure_multiple_cucumber_options_with_spaces_parse_correctly() throw
110110
Properties properties = new Properties();
111111
properties.setProperty("cucumber.options", "--name 'some Name' --dotcucumber 'some file\\path'");
112112
RuntimeOptions options = new RuntimeOptions(properties);
113-
Pattern actualPattern = (Pattern) options.filters.iterator().next();
113+
Pattern actualPattern = (Pattern) options.getFilters().iterator().next();
114114
assertEquals("some Name", actualPattern.pattern());
115-
assertEquals(new URL("file:some file\\path/"), options.dotCucumber);
115+
assertEquals(new URL("file:some file\\path/"), options.getDotCucumber());
116116
}
117117

118118
@Test
119119
public void overrides_options_with_system_properties_without_clobbering_non_overridden_ones() {
120120
Properties properties = new Properties();
121121
properties.setProperty("cucumber.options", "--glue lookatme andmememe");
122122
RuntimeOptions options = new RuntimeOptions(properties, "--strict", "--glue", "somewhere", "somewhere_else");
123-
assertEquals(asList("somewhere_else", "andmememe"), options.featurePaths);
124-
assertEquals(asList("somewhere", "lookatme"), options.glue);
125-
assertTrue(options.strict);
123+
assertEquals(asList("somewhere_else", "andmememe"), options.getFeaturePaths());
124+
assertEquals(asList("somewhere", "lookatme"), options.getGlue());
125+
assertTrue(options.isStrict());
126126
}
127127

128128
@Test
129129
public void ensure_cli_glue_is_preserved_when_cucumber_options_property_defined() {
130130
Properties properties = new Properties();
131131
properties.setProperty("cucumber.options", "--tags @foo");
132132
RuntimeOptions runtimeOptions = new RuntimeOptions(properties, "--glue", "somewhere");
133-
assertEquals(asList("somewhere"), runtimeOptions.glue);
133+
assertEquals(asList("somewhere"), runtimeOptions.getGlue());
134134
}
135135

136136
@Test
137137
public void ensure_feature_paths_are_appended_to_when_cucumber_options_property_defined() {
138138
Properties properties = new Properties();
139139
properties.setProperty("cucumber.options", "somewhere_else");
140140
RuntimeOptions runtimeOptions = new RuntimeOptions(properties, "somewhere");
141-
assertEquals(asList("somewhere", "somewhere_else"), runtimeOptions.featurePaths);
141+
assertEquals(asList("somewhere", "somewhere_else"), runtimeOptions.getFeaturePaths());
142142
}
143143

144144
@Test
145145
public void clobber_filters_from_cli_if_filters_specified_in_cucumber_options_property() {
146146
Properties properties = new Properties();
147147
properties.setProperty("cucumber.options", "--tags @clobber_with_this");
148148
RuntimeOptions runtimeOptions = new RuntimeOptions(properties, "--tags", "@should_be_clobbered");
149-
assertEquals(asList("@clobber_with_this"), runtimeOptions.filters);
149+
assertEquals(asList("@clobber_with_this"), runtimeOptions.getFilters());
150150
}
151151

152152
@Test
153153
public void preserves_filters_from_cli_if_filters_not_specified_in_cucumber_options_property() {
154154
Properties properties = new Properties();
155155
properties.setProperty("cucumber.options", "--strict");
156156
RuntimeOptions runtimeOptions = new RuntimeOptions(properties, "--tags", "@keep_this");
157-
assertEquals(asList("@keep_this"), runtimeOptions.filters);
157+
assertEquals(asList("@keep_this"), runtimeOptions.getFilters());
158158
}
159159

160160
@Test
161161
public void allows_removal_of_strict_in_cucumber_options_property() {
162162
Properties properties = new Properties();
163163
properties.setProperty("cucumber.options", "--no-strict");
164164
RuntimeOptions runtimeOptions = new RuntimeOptions(properties, "--strict");
165-
assertFalse(runtimeOptions.strict);
165+
assertFalse(runtimeOptions.isStrict());
166166
}
167167

168168
@Test

junit/src/main/java/cucumber/api/junit/Cucumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Cucumber(Class clazz) throws InitializationError, IOException {
5858
ResourceLoader resourceLoader = new MultiLoader(classLoader);
5959
runtime = new Runtime(resourceLoader, classLoader, runtimeOptions);
6060

61-
jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader), runtimeOptions.strict);
61+
jUnitReporter = new JUnitReporter(runtimeOptions.reporter(classLoader), runtimeOptions.formatter(classLoader), runtimeOptions.isStrict());
6262
addChildren(runtimeOptions.cucumberFeatures(resourceLoader));
6363
}
6464

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,28 @@ public class RuntimeOptionsFactoryTest {
2121
public void create_strict() throws Exception {
2222
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(Strict.class);
2323
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
24-
assertTrue(runtimeOptions.strict);
24+
assertTrue(runtimeOptions.isStrict());
2525
}
2626

2727
@Test
2828
public void create_non_strict() throws Exception {
2929
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(NotStrict.class);
3030
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
31-
assertFalse(runtimeOptions.strict);
31+
assertFalse(runtimeOptions.isStrict());
3232
}
3333

3434
@Test
3535
public void create_without_options() throws Exception {
3636
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(WithoutOptions.class);
3737
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
38-
assertFalse(runtimeOptions.strict);
38+
assertFalse(runtimeOptions.isStrict());
3939
}
4040

4141
@Test
4242
public void create_with_no_name() throws Exception {
4343
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(NoName.class);
4444
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
45-
assertTrue(runtimeOptions.filters.isEmpty());
45+
assertTrue(runtimeOptions.getFilters().isEmpty());
4646
}
4747

4848
@Test
@@ -51,7 +51,7 @@ public void create_with_multiple_names() throws Exception {
5151

5252
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
5353

54-
List<Object> filters = runtimeOptions.filters;
54+
List<Object> filters = runtimeOptions.getFilters();
5555
assertEquals(2, filters.size());
5656
Iterator<Object> iterator = filters.iterator();
5757
assertEquals("name1", getRegexpPattern(iterator.next()));
@@ -62,14 +62,14 @@ public void create_with_multiple_names() throws Exception {
6262
public void create_with_dotcucumber_dir() throws MalformedURLException {
6363
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(DotCucumberFile.class);
6464
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
65-
assertEquals(new URL("file:somewhere/.cucumber/"), runtimeOptions.dotCucumber);
65+
assertEquals(new URL("file:somewhere/.cucumber/"), runtimeOptions.getDotCucumber());
6666
}
6767

6868
@Test
6969
public void create_with_dotcucumber_url() throws MalformedURLException {
7070
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(DotCucumberUrl.class);
7171
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
72-
assertEquals(new URL("https://some.where/.cucumber/"), runtimeOptions.dotCucumber);
72+
assertEquals(new URL("https://some.where/.cucumber/"), runtimeOptions.getDotCucumber());
7373
}
7474

7575
private String getRegexpPattern(Object pattern) {

0 commit comments

Comments
 (0)