1 | |
package org.jbehave.core.model; |
2 | |
|
3 | |
import java.io.ByteArrayInputStream; |
4 | |
import java.io.IOException; |
5 | |
import java.io.PrintStream; |
6 | |
import java.lang.reflect.Field; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.Collections; |
9 | |
import java.util.HashMap; |
10 | |
import java.util.LinkedHashMap; |
11 | |
import java.util.List; |
12 | |
import java.util.Map; |
13 | |
import java.util.Properties; |
14 | |
import java.util.regex.Matcher; |
15 | |
import java.util.regex.Pattern; |
16 | |
|
17 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
18 | |
import org.apache.commons.lang.builder.ToStringStyle; |
19 | |
import org.jbehave.core.annotations.Parameter; |
20 | |
import org.jbehave.core.model.TableTransformers.TableTransformer; |
21 | |
import org.jbehave.core.steps.ChainedRow; |
22 | |
import org.jbehave.core.steps.ConvertedParameters; |
23 | |
import org.jbehave.core.steps.ParameterConverters; |
24 | |
import org.jbehave.core.steps.Parameters; |
25 | |
import org.jbehave.core.steps.Row; |
26 | |
|
27 | |
import static java.lang.Boolean.parseBoolean; |
28 | |
import static java.util.regex.Pattern.DOTALL; |
29 | |
import static java.util.regex.Pattern.compile; |
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 | |
public class ExamplesTable { |
146 | 1 | private static final Map<String, String> EMPTY_MAP = Collections.emptyMap(); |
147 | |
private static final String EMPTY_VALUE = ""; |
148 | |
|
149 | 1 | public static final ExamplesTable EMPTY = new ExamplesTable(""); |
150 | |
|
151 | |
private static final String ROW_SEPARATOR = "\r?\n"; |
152 | |
private static final String NEW_LINE = "\n"; |
153 | |
private static final String HEADER_SEPARATOR = "|"; |
154 | |
private static final String VALUE_SEPARATOR = "|"; |
155 | |
private static final String IGNORABLE_SEPARATOR = "|--"; |
156 | |
|
157 | |
private final String tableAsString; |
158 | |
private final String headerSeparator; |
159 | |
private final String valueSeparator; |
160 | |
private final String ignorableSeparator; |
161 | |
private final ParameterConverters parameterConverters; |
162 | |
private final TableTransformers tableTransformers; |
163 | 197 | private final List<String> headers = new ArrayList<String>(); |
164 | 197 | private final List<Map<String, String>> data = new ArrayList<Map<String, String>>(); |
165 | 197 | private final Properties properties = new Properties(); |
166 | 197 | private String propertiesAsString = ""; |
167 | 197 | private Map<String, String> namedParameters = new HashMap<String, String>(); |
168 | 197 | private boolean trim = true; |
169 | |
|
170 | |
private final Row defaults; |
171 | |
|
172 | |
public ExamplesTable(String tableAsString) { |
173 | 40 | this(tableAsString, HEADER_SEPARATOR, VALUE_SEPARATOR); |
174 | 40 | } |
175 | |
|
176 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator) { |
177 | 42 | this(tableAsString, headerSeparator, valueSeparator, IGNORABLE_SEPARATOR, new ParameterConverters()); |
178 | 42 | } |
179 | |
|
180 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, |
181 | |
String ignorableSeparator, ParameterConverters parameterConverters) { |
182 | 42 | this(tableAsString, headerSeparator, valueSeparator, ignorableSeparator, parameterConverters, |
183 | |
new TableTransformers()); |
184 | 42 | } |
185 | |
|
186 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, |
187 | 196 | String ignorableSeparator, ParameterConverters parameterConverters, TableTransformers tableTransformers) { |
188 | 196 | this.tableAsString = tableAsString; |
189 | 196 | this.headerSeparator = headerSeparator; |
190 | 196 | this.valueSeparator = valueSeparator; |
191 | 196 | this.ignorableSeparator = ignorableSeparator; |
192 | 196 | this.parameterConverters = parameterConverters; |
193 | 196 | this.tableTransformers = tableTransformers; |
194 | 196 | this.defaults = new ConvertedParameters(EMPTY_MAP, parameterConverters); |
195 | 196 | parse(); |
196 | 196 | } |
197 | |
|
198 | |
private void parse() { |
199 | 196 | String tableWithoutProperties = stripProperties(tableAsString.trim()); |
200 | 196 | parseProperties(propertiesAsString); |
201 | 196 | trim = parseBoolean(properties.getProperty("trim", "true")); |
202 | 196 | parseTable(tableWithoutProperties); |
203 | 196 | } |
204 | |
|
205 | 1 | private ExamplesTable(ExamplesTable other, Row defaults) { |
206 | 1 | this.data.addAll(other.data); |
207 | 1 | this.tableAsString = other.tableAsString; |
208 | 1 | this.headerSeparator = other.headerSeparator; |
209 | 1 | this.valueSeparator = other.valueSeparator; |
210 | 1 | this.ignorableSeparator = other.ignorableSeparator; |
211 | 1 | this.parameterConverters = other.parameterConverters; |
212 | 1 | this.tableTransformers = other.tableTransformers; |
213 | 1 | this.headers.addAll(other.headers); |
214 | 1 | this.properties.putAll(other.properties); |
215 | 1 | this.defaults = defaults; |
216 | 1 | } |
217 | |
|
218 | |
private String stripProperties(String tableAsString) { |
219 | 196 | Pattern pattern = compile("\\{(.*?)\\}\\s*(.*)", DOTALL); |
220 | 196 | Matcher matcher = pattern.matcher(tableAsString); |
221 | 196 | if (matcher.matches()) { |
222 | 6 | propertiesAsString = matcher.group(1); |
223 | 6 | return matcher.group(2); |
224 | |
} |
225 | 190 | return tableAsString; |
226 | |
} |
227 | |
|
228 | |
private void parseProperties(String propertiesAsString) { |
229 | 196 | properties.clear(); |
230 | 196 | properties.setProperty("ignorableSeparator", ignorableSeparator); |
231 | 196 | properties.setProperty("headerSeparator", headerSeparator); |
232 | 196 | properties.setProperty("valueSeparator", valueSeparator); |
233 | |
try { |
234 | 196 | properties.load(new ByteArrayInputStream(propertiesAsString.replace(",", NEW_LINE).getBytes())); |
235 | 0 | } catch (IOException e) { |
236 | |
|
237 | 196 | } |
238 | 196 | } |
239 | |
|
240 | |
private void parseTable(String tableAsString) { |
241 | 196 | headers.clear(); |
242 | 196 | data.clear(); |
243 | 196 | String transformer = properties.getProperty("transformer"); |
244 | 196 | if (transformer != null) { |
245 | 2 | tableAsString = tableTransformers.transform(transformer, tableAsString, properties); |
246 | |
} |
247 | 196 | parseByRows(headers, data, tableAsString); |
248 | 196 | } |
249 | |
|
250 | |
private void parseByRows(List<String> headers, List<Map<String, String>> data, String tableAsString) { |
251 | 196 | String[] rows = tableAsString.split(ROW_SEPARATOR); |
252 | 2508 | for (int row = 0; row < rows.length; row++) { |
253 | 2312 | String rowAsString = rows[row].trim(); |
254 | 2312 | if (rowAsString.startsWith(properties.getProperty("ignorableSeparator")) || rowAsString.length() == 0) { |
255 | |
|
256 | 134 | continue; |
257 | 2172 | } else if (headers.isEmpty()) { |
258 | 63 | headers.addAll(TableUtils.parseRow(rowAsString, properties.getProperty("headerSeparator"), |
259 | |
properties.getProperty("commentSeparator"), trim)); |
260 | |
} else { |
261 | 2109 | List<String> columns = TableUtils.parseRow(rowAsString, properties.getProperty("valueSeparator"), |
262 | |
properties.getProperty("commentSeparator"), trim); |
263 | 2109 | Map<String, String> map = new LinkedHashMap<String, String>(); |
264 | 22336 | for (int column = 0; column < columns.size(); column++) { |
265 | 20227 | if (column < headers.size()) { |
266 | 20226 | map.put(headers.get(column), columns.get(column)); |
267 | |
} |
268 | |
} |
269 | 2109 | data.add(map); |
270 | |
} |
271 | |
} |
272 | 196 | } |
273 | |
|
274 | |
public ExamplesTable withDefaults(Parameters defaults) { |
275 | 1 | return new ExamplesTable(this, new ChainedRow(defaults, this.defaults)); |
276 | |
} |
277 | |
|
278 | |
public ExamplesTable withNamedParameters(Map<String, String> namedParameters) { |
279 | 1 | this.namedParameters = namedParameters; |
280 | 1 | return this; |
281 | |
} |
282 | |
|
283 | |
public ExamplesTable withRowValues(int row, Map<String, String> values) { |
284 | 2 | getRow(row).putAll(values); |
285 | 2 | for (String header : values.keySet()) { |
286 | 3 | if (!headers.contains(header)) { |
287 | 1 | headers.add(header); |
288 | |
} |
289 | 3 | } |
290 | 2 | return this; |
291 | |
} |
292 | |
|
293 | |
public ExamplesTable withRows(List<Map<String, String>> values) { |
294 | 1 | this.data.clear(); |
295 | 1 | this.data.addAll(values); |
296 | 1 | this.headers.clear(); |
297 | 1 | this.headers.addAll(values.get(0).keySet()); |
298 | 1 | return this; |
299 | |
} |
300 | |
|
301 | |
public Properties getProperties() { |
302 | 4 | return properties; |
303 | |
} |
304 | |
|
305 | |
public List<String> getHeaders() { |
306 | 27 | return headers; |
307 | |
} |
308 | |
|
309 | |
public Map<String, String> getRow(int row) { |
310 | 242 | if (row > data.size() - 1) { |
311 | 1 | throw new RowNotFound(row); |
312 | |
} |
313 | 241 | Map<String, String> values = data.get(row); |
314 | 241 | if (headers.size() != values.keySet().size()) { |
315 | 2 | for (String header : headers) { |
316 | 5 | if (!values.containsKey(header)) { |
317 | 2 | values.put(header, EMPTY_VALUE); |
318 | |
} |
319 | 5 | } |
320 | |
} |
321 | 241 | return values; |
322 | |
} |
323 | |
|
324 | |
public Parameters getRowAsParameters(int row) { |
325 | 9 | return getRowAsParameters(row, false); |
326 | |
} |
327 | |
|
328 | |
public Parameters getRowAsParameters(int row, boolean replaceNamedParameters) { |
329 | 23 | Map<String, String> rowValues = getRow(row); |
330 | 22 | return createParameters((replaceNamedParameters ? replaceNamedParameters(rowValues) : rowValues)); |
331 | |
} |
332 | |
|
333 | |
private Map<String, String> replaceNamedParameters(Map<String, String> row) { |
334 | 1 | Map<String, String> replaced = new HashMap<String, String>(); |
335 | 1 | for (String key : row.keySet()) { |
336 | 2 | String replacedValue = row.get(key); |
337 | 2 | for (String namedKey : namedParameters.keySet()) { |
338 | 2 | String namedValue = namedParameters.get(namedKey); |
339 | 2 | replacedValue = replacedValue.replaceAll(namedKey, namedValue); |
340 | 2 | } |
341 | 2 | replaced.put(key, replacedValue); |
342 | 2 | } |
343 | 1 | return replaced; |
344 | |
} |
345 | |
|
346 | |
public int getRowCount() { |
347 | 231 | return data.size(); |
348 | |
} |
349 | |
|
350 | |
public List<Map<String, String>> getRows() { |
351 | 54 | List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); |
352 | 155 | for (int row = 0; row < getRowCount(); row++) { |
353 | 101 | rows.add(getRow(row)); |
354 | |
} |
355 | 54 | return rows; |
356 | |
} |
357 | |
|
358 | |
public List<Parameters> getRowsAsParameters() { |
359 | 9 | return getRowsAsParameters(false); |
360 | |
} |
361 | |
|
362 | |
public List<Parameters> getRowsAsParameters(boolean replaceNamedParameters) { |
363 | 10 | List<Parameters> rows = new ArrayList<Parameters>(); |
364 | |
|
365 | 24 | for (int row = 0; row < getRowCount(); row++) { |
366 | 14 | rows.add(getRowAsParameters(row, replaceNamedParameters)); |
367 | |
} |
368 | |
|
369 | 10 | return rows; |
370 | |
} |
371 | |
|
372 | |
public <T> List<T> getRowsAs(Class<T> type) { |
373 | 4 | return getRowsAs(type, new HashMap<String, String>()); |
374 | |
} |
375 | |
|
376 | |
public <T> List<T> getRowsAs(Class<T> type, Map<String, String> fieldNameMapping) { |
377 | 5 | List<T> rows = new ArrayList<T>(); |
378 | |
|
379 | 5 | for (Parameters parameters : getRowsAsParameters()) { |
380 | 6 | rows.add(mapToType(parameters, type, fieldNameMapping)); |
381 | 6 | } |
382 | |
|
383 | 5 | return rows; |
384 | |
} |
385 | |
|
386 | |
private <T> T mapToType(Parameters parameters, Class<T> type, Map<String, String> fieldNameMapping) { |
387 | |
try { |
388 | 6 | T instance = type.newInstance(); |
389 | 6 | Map<String, String> values = parameters.values(); |
390 | 6 | for (String name : values.keySet()) { |
391 | 12 | Field field = findField(type, name, fieldNameMapping); |
392 | 12 | Class<?> fieldType = (Class<?>) field.getGenericType(); |
393 | 12 | Object value = parameters.valueAs(name, fieldType); |
394 | 12 | field.setAccessible(true); |
395 | 12 | field.set(instance, value); |
396 | 12 | } |
397 | 6 | return instance; |
398 | 0 | } catch (Exception e) { |
399 | 0 | throw new ParametersNotMappableToType(parameters, type, e); |
400 | |
} |
401 | |
} |
402 | |
|
403 | |
private <T> Field findField(Class<T> type, String name, Map<String, String> fieldNameMapping) |
404 | |
throws NoSuchFieldException { |
405 | |
|
406 | 12 | String fieldName = fieldNameMapping.get(name); |
407 | 12 | if (fieldName == null) { |
408 | 10 | fieldName = name; |
409 | |
} |
410 | |
|
411 | 33 | for (Field field : type.getDeclaredFields()) { |
412 | 23 | if (field.isAnnotationPresent(Parameter.class)) { |
413 | 3 | Parameter parameter = field.getAnnotation(Parameter.class); |
414 | 3 | if (fieldName.equals(parameter.name())) { |
415 | 2 | return field; |
416 | |
} |
417 | |
} |
418 | |
} |
419 | |
|
420 | 10 | return type.getDeclaredField(fieldName); |
421 | |
} |
422 | |
|
423 | |
private Parameters createParameters(Map<String, String> values) { |
424 | 22 | return new ConvertedParameters(new ChainedRow(new ConvertedParameters(values, parameterConverters), defaults), |
425 | |
parameterConverters); |
426 | |
} |
427 | |
|
428 | |
public String getHeaderSeparator() { |
429 | 2 | return headerSeparator; |
430 | |
} |
431 | |
|
432 | |
public String getValueSeparator() { |
433 | 2 | return valueSeparator; |
434 | |
} |
435 | |
|
436 | |
public String asString() { |
437 | 65 | if (data.isEmpty()) { |
438 | 41 | return EMPTY_VALUE; |
439 | |
} |
440 | 24 | return format(); |
441 | |
} |
442 | |
|
443 | |
public void outputTo(PrintStream output) { |
444 | 1 | output.print(asString()); |
445 | 1 | } |
446 | |
|
447 | |
private String format() { |
448 | 24 | StringBuffer sb = new StringBuffer(); |
449 | 24 | if ( !propertiesAsString.isEmpty() ){ |
450 | 3 | sb.append("{").append(propertiesAsString).append("}").append(NEW_LINE); |
451 | |
} |
452 | 24 | for (String header : headers) { |
453 | 54 | sb.append(headerSeparator).append(header); |
454 | 54 | } |
455 | 24 | sb.append(headerSeparator).append(NEW_LINE); |
456 | 24 | for (Map<String, String> row : getRows()) { |
457 | 44 | for (String header : headers) { |
458 | 100 | sb.append(valueSeparator); |
459 | 100 | sb.append(row.get(header)); |
460 | 100 | } |
461 | 44 | sb.append(valueSeparator).append(NEW_LINE); |
462 | 44 | } |
463 | 24 | return sb.toString(); |
464 | |
} |
465 | |
|
466 | |
@Override |
467 | |
public String toString() { |
468 | 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
469 | |
} |
470 | |
|
471 | |
@SuppressWarnings("serial") |
472 | |
public static class RowNotFound extends RuntimeException { |
473 | |
|
474 | |
public RowNotFound(int row) { |
475 | 1 | super(Integer.toString(row)); |
476 | 1 | } |
477 | |
|
478 | |
} |
479 | |
|
480 | |
@SuppressWarnings("serial") |
481 | |
public static class ParametersNotMappableToType extends RuntimeException { |
482 | |
|
483 | |
public ParametersNotMappableToType(Parameters parameters, Class<?> type, Exception e) { |
484 | 0 | super(parameters.values() + " not mappable to type " + type, e); |
485 | 0 | } |
486 | |
|
487 | |
} |
488 | |
|
489 | |
} |