| 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 | 195 | private final List<String> headers = new ArrayList<String>(); |
| 164 | 195 | private final List<Map<String, String>> data = new ArrayList<Map<String, String>>(); |
| 165 | 195 | private final Properties properties = new Properties(); |
| 166 | 195 | private String propertiesAsString = ""; |
| 167 | 195 | private Map<String, String> namedParameters = new HashMap<String, String>(); |
| 168 | 195 | private boolean trim = true; |
| 169 | |
|
| 170 | |
private final Row defaults; |
| 171 | |
|
| 172 | |
public ExamplesTable(String tableAsString) { |
| 173 | 39 | this(tableAsString, HEADER_SEPARATOR, VALUE_SEPARATOR); |
| 174 | 39 | } |
| 175 | |
|
| 176 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator) { |
| 177 | 41 | this(tableAsString, headerSeparator, valueSeparator, IGNORABLE_SEPARATOR, new ParameterConverters()); |
| 178 | 41 | } |
| 179 | |
|
| 180 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, |
| 181 | |
String ignorableSeparator, ParameterConverters parameterConverters) { |
| 182 | 41 | this(tableAsString, headerSeparator, valueSeparator, ignorableSeparator, parameterConverters, |
| 183 | |
new TableTransformers()); |
| 184 | 41 | } |
| 185 | |
|
| 186 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, |
| 187 | 194 | String ignorableSeparator, ParameterConverters parameterConverters, TableTransformers tableTransformers) { |
| 188 | 194 | this.tableAsString = tableAsString; |
| 189 | 194 | this.headerSeparator = headerSeparator; |
| 190 | 194 | this.valueSeparator = valueSeparator; |
| 191 | 194 | this.ignorableSeparator = ignorableSeparator; |
| 192 | 194 | this.parameterConverters = parameterConverters; |
| 193 | 194 | this.tableTransformers = tableTransformers; |
| 194 | 194 | this.defaults = new ConvertedParameters(EMPTY_MAP, parameterConverters); |
| 195 | 194 | parse(); |
| 196 | 194 | } |
| 197 | |
|
| 198 | |
private void parse() { |
| 199 | 194 | String tableWithoutProperties = stripProperties(tableAsString.trim()); |
| 200 | 194 | parseProperties(propertiesAsString); |
| 201 | 194 | trim = parseBoolean(properties.getProperty("trim", "true")); |
| 202 | 194 | parseTable(tableWithoutProperties); |
| 203 | 194 | } |
| 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 | 194 | Pattern pattern = compile("\\{(.*?)\\}\\s*(.*)", DOTALL); |
| 220 | 194 | Matcher matcher = pattern.matcher(tableAsString); |
| 221 | 194 | if (matcher.matches()) { |
| 222 | 6 | propertiesAsString = matcher.group(1); |
| 223 | 6 | return matcher.group(2); |
| 224 | |
} |
| 225 | 188 | return tableAsString; |
| 226 | |
} |
| 227 | |
|
| 228 | |
private void parseProperties(String propertiesAsString) { |
| 229 | 194 | properties.clear(); |
| 230 | 194 | properties.setProperty("ignorableSeparator", ignorableSeparator); |
| 231 | 194 | properties.setProperty("headerSeparator", headerSeparator); |
| 232 | 194 | properties.setProperty("valueSeparator", valueSeparator); |
| 233 | |
try { |
| 234 | 194 | properties.load(new ByteArrayInputStream(propertiesAsString.replace(",", NEW_LINE).getBytes())); |
| 235 | 0 | } catch (IOException e) { |
| 236 | |
|
| 237 | 194 | } |
| 238 | 194 | } |
| 239 | |
|
| 240 | |
private void parseTable(String tableAsString) { |
| 241 | 194 | headers.clear(); |
| 242 | 194 | data.clear(); |
| 243 | 194 | String transformer = properties.getProperty("transformer"); |
| 244 | 194 | if (transformer != null) { |
| 245 | 2 | tableAsString = tableTransformers.transform(transformer, tableAsString, properties); |
| 246 | |
} |
| 247 | 194 | parseByRows(headers, data, tableAsString); |
| 248 | 194 | } |
| 249 | |
|
| 250 | |
private void parseByRows(List<String> headers, List<Map<String, String>> data, String tableAsString) { |
| 251 | 194 | String[] rows = tableAsString.split(ROW_SEPARATOR); |
| 252 | 2502 | for (int row = 0; row < rows.length; row++) { |
| 253 | 2308 | String rowAsString = rows[row].trim(); |
| 254 | 2308 | if (rowAsString.startsWith(properties.getProperty("ignorableSeparator")) || rowAsString.length() == 0) { |
| 255 | |
|
| 256 | 133 | continue; |
| 257 | 2169 | } else if (headers.isEmpty()) { |
| 258 | 62 | headers.addAll(TableUtils.parseRow(rowAsString, properties.getProperty("headerSeparator"), |
| 259 | |
properties.getProperty("commentSeparator"), trim)); |
| 260 | |
} else { |
| 261 | 2107 | List<String> columns = TableUtils.parseRow(rowAsString, properties.getProperty("valueSeparator"), |
| 262 | |
properties.getProperty("commentSeparator"), trim); |
| 263 | 2107 | Map<String, String> map = new LinkedHashMap<String, String>(); |
| 264 | 22330 | for (int column = 0; column < columns.size(); column++) { |
| 265 | 20223 | if (column < headers.size()) { |
| 266 | 20222 | map.put(headers.get(column), columns.get(column)); |
| 267 | |
} |
| 268 | |
} |
| 269 | 2107 | data.add(map); |
| 270 | |
} |
| 271 | |
} |
| 272 | 194 | } |
| 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 | 236 | if (row > data.size() - 1) { |
| 311 | 1 | throw new RowNotFound(row); |
| 312 | |
} |
| 313 | 235 | Map<String, String> values = data.get(row); |
| 314 | 235 | 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 | 235 | 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 | 222 | return data.size(); |
| 348 | |
} |
| 349 | |
|
| 350 | |
public List<Map<String, String>> getRows() { |
| 351 | 52 | List<Map<String, String>> rows = new ArrayList<Map<String, String>>(); |
| 352 | 149 | for (int row = 0; row < getRowCount(); row++) { |
| 353 | 97 | rows.add(getRow(row)); |
| 354 | |
} |
| 355 | 52 | 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 | 60 | if (data.isEmpty()) { |
| 438 | 37 | return EMPTY_VALUE; |
| 439 | |
} |
| 440 | 23 | return format(); |
| 441 | |
} |
| 442 | |
|
| 443 | |
public void outputTo(PrintStream output) { |
| 444 | 1 | output.print(asString()); |
| 445 | 1 | } |
| 446 | |
|
| 447 | |
private String format() { |
| 448 | 23 | StringBuffer sb = new StringBuffer(); |
| 449 | 23 | for (String header : headers) { |
| 450 | 52 | sb.append(headerSeparator).append(header); |
| 451 | 52 | } |
| 452 | 23 | sb.append(headerSeparator).append(NEW_LINE); |
| 453 | 23 | for (Map<String, String> row : getRows()) { |
| 454 | 42 | for (String header : headers) { |
| 455 | 96 | sb.append(valueSeparator); |
| 456 | 96 | sb.append(row.get(header)); |
| 457 | 96 | } |
| 458 | 42 | sb.append(valueSeparator).append(NEW_LINE); |
| 459 | 42 | } |
| 460 | 23 | return sb.toString(); |
| 461 | |
} |
| 462 | |
|
| 463 | |
@Override |
| 464 | |
public String toString() { |
| 465 | 43 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 466 | |
} |
| 467 | |
|
| 468 | |
@SuppressWarnings("serial") |
| 469 | |
public static class RowNotFound extends RuntimeException { |
| 470 | |
|
| 471 | |
public RowNotFound(int row) { |
| 472 | 1 | super(Integer.toString(row)); |
| 473 | 1 | } |
| 474 | |
|
| 475 | |
} |
| 476 | |
|
| 477 | |
@SuppressWarnings("serial") |
| 478 | |
public static class ParametersNotMappableToType extends RuntimeException { |
| 479 | |
|
| 480 | |
public ParametersNotMappableToType(Parameters parameters, Class<?> type, Exception e) { |
| 481 | 0 | super(parameters.values() + " not mappable to type " + type, e); |
| 482 | 0 | } |
| 483 | |
|
| 484 | |
} |
| 485 | |
|
| 486 | |
} |