/
RuntimeOptionsFactoryTest.java
199 lines (164 loc) · 7.07 KB
/
RuntimeOptionsFactoryTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package cucumber.runtime;
import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.PrettyFormatter;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import static cucumber.runtime.RuntimeOptionsFactory.packageName;
import static cucumber.runtime.RuntimeOptionsFactory.packagePath;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class RuntimeOptionsFactoryTest {
@Test
public void create_strict() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(Strict.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertTrue(runtimeOptions.isStrict());
}
@Test
public void create_non_strict() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(NotStrict.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertFalse(runtimeOptions.isStrict());
}
@Test
public void create_without_options() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(WithoutOptions.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertFalse(runtimeOptions.isStrict());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
assertPluginExists(runtimeOptions.getPlugins(), "cucumber.runtime.formatter.NullFormatter");
}
@Test
public void create_without_options_with_base_class_without_options() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(WithoutOptionsWithBaseClassWithoutOptions.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getFeaturePaths());
assertEquals(asList("classpath:cucumber/runtime"), runtimeOptions.getGlue());
assertPluginExists(runtimeOptions.getPlugins(), "cucumber.runtime.formatter.NullFormatter");
}
@Test
public void create_with_no_name() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(NoName.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertTrue(runtimeOptions.getFilters().isEmpty());
}
@Test
public void create_with_multiple_names() throws Exception {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(MultipleNames.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
List<Object> filters = runtimeOptions.getFilters();
assertEquals(2, filters.size());
Iterator<Object> iterator = filters.iterator();
assertEquals("name1", getRegexpPattern(iterator.next()));
assertEquals("name2", getRegexpPattern(iterator.next()));
}
@Test
public void create_with_snippets() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(Snippets.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertEquals(SnippetType.CAMELCASE, runtimeOptions.getSnippetType());
}
private String getRegexpPattern(Object pattern) {
return ((Pattern) pattern).pattern();
}
@Test
public void finds_path_for_class_in_package() {
assertEquals("java/lang", packagePath(String.class));
}
@Test
public void finds_path_for_class_in_toplevel_package() {
assertEquals("", packageName("TopLevelClass"));
}
@Test
public void create_null_formatter_when_no_formatter_plugin_is_defined() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithNoFormatterPlugin.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertPluginExists(runtimeOptions.getPlugins(), "cucumber.runtime.formatter.NullFormatter");
}
@Test
public void create_default_summary_printer_when_no_summary_printer_plugin_is_defined() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(ClassWithNoSummaryPrinterPlugin.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertPluginExists(runtimeOptions.getPlugins(), "cucumber.runtime.DefaultSummaryPrinter");
}
@Test
public void inherit_plugin_from_baseclass() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithFormatter.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
List<Object> plugins = runtimeOptions.getPlugins();
assertPluginExists(plugins, "cucumber.runtime.formatter.CucumberJSONFormatter");
assertPluginExists(plugins, "cucumber.runtime.formatter.CucumberPrettyFormatter");
}
@Test
public void override_monochrome_flag_from_baseclass() {
RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(SubClassWithMonoChromeTrue.class);
RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();
assertTrue(runtimeOptions.isMonochrome());
}
private void assertPluginExists(List<Object> plugins, String pluginName) {
boolean found = false;
for (Object plugin : plugins) {
if (plugin.getClass().getName() == pluginName) {
found = true;
}
}
assertTrue(pluginName + " not found among the plugins", found);
}
@CucumberOptions(snippets = SnippetType.CAMELCASE)
static class Snippets {
// empty
}
@CucumberOptions(strict = true)
static class Strict {
// empty
}
@CucumberOptions
static class NotStrict {
// empty
}
@CucumberOptions(name = {"name1", "name2"})
static class MultipleNames {
// empty
}
@CucumberOptions
static class NoName {
// empty
}
static class WithoutOptions {
// empty
}
static class WithoutOptionsWithBaseClassWithoutOptions extends WithoutOptions {
// empty
}
@CucumberOptions(plugin = "pretty")
static class SubClassWithFormatter extends BaseClassWithFormatter {
// empty
}
@CucumberOptions(plugin = "json:test-json-report.json")
static class BaseClassWithFormatter {
// empty
}
@CucumberOptions(monochrome = true)
static class SubClassWithMonoChromeTrue extends BaseClassWithMonoChromeFalse {
// empty
}
@CucumberOptions(monochrome = false)
static class BaseClassWithMonoChromeFalse {
// empty
}
@CucumberOptions(plugin = "cucumber.runtime.formatter.AnyStepDefinitionReporter")
static class ClassWithNoFormatterPlugin {
// empty
}
@CucumberOptions(plugin = "pretty")
static class ClassWithNoSummaryPrinterPlugin {
// empty
}
}