| 1 | |
package org.jbehave.core.model; |
| 2 | |
|
| 3 | |
import java.io.ByteArrayInputStream; |
| 4 | |
import java.io.IOException; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.Collections; |
| 7 | |
import java.util.HashMap; |
| 8 | |
import java.util.LinkedHashMap; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.Properties; |
| 12 | |
import java.util.regex.Matcher; |
| 13 | |
import java.util.regex.Pattern; |
| 14 | |
|
| 15 | |
import org.apache.commons.lang.StringUtils; |
| 16 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 17 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 18 | |
import org.jbehave.core.steps.ChainedRow; |
| 19 | |
import org.jbehave.core.steps.ConvertedParameters; |
| 20 | |
import org.jbehave.core.steps.ParameterConverters; |
| 21 | |
import org.jbehave.core.steps.Parameters; |
| 22 | |
import org.jbehave.core.steps.Row; |
| 23 | |
|
| 24 | |
import static java.lang.Boolean.parseBoolean; |
| 25 | |
import static java.util.regex.Pattern.DOTALL; |
| 26 | |
import static java.util.regex.Pattern.compile; |
| 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 | |
public class ExamplesTable { |
| 85 | 1 | private static final Map<String, String> EMPTY_MAP = Collections.emptyMap(); |
| 86 | |
|
| 87 | 1 | public static final ExamplesTable EMPTY = new ExamplesTable(""); |
| 88 | |
|
| 89 | |
private static final String HEADER_SEPARATOR = "|"; |
| 90 | |
private static final String VALUE_SEPARATOR = "|"; |
| 91 | |
private static final String IGNORABLE_SEPARATOR = "|--"; |
| 92 | 163 | private final List<Map<String, String>> data = new ArrayList<Map<String, String>>(); |
| 93 | |
private final String tableAsString; |
| 94 | |
private final String headerSeparator; |
| 95 | |
private final String valueSeparator; |
| 96 | |
private final String ignorableSeparator; |
| 97 | |
private final ParameterConverters parameterConverters; |
| 98 | 163 | private final List<String> headers = new ArrayList<String>(); |
| 99 | 163 | private final Properties properties = new Properties(); |
| 100 | 163 | private Map<String, String> namedParameters = new HashMap<String, String>(); |
| 101 | 163 | private boolean trim = true; |
| 102 | |
|
| 103 | |
private final Row defaults; |
| 104 | |
|
| 105 | |
public ExamplesTable(String tableAsString) { |
| 106 | 31 | this(tableAsString, HEADER_SEPARATOR, VALUE_SEPARATOR); |
| 107 | 31 | } |
| 108 | |
|
| 109 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator) { |
| 110 | 33 | this(tableAsString, headerSeparator, valueSeparator, IGNORABLE_SEPARATOR, new ParameterConverters()); |
| 111 | 33 | } |
| 112 | |
|
| 113 | |
public ExamplesTable(String tableAsString, String headerSeparator, String valueSeparator, |
| 114 | 162 | String ignorableSeparator, ParameterConverters parameterConverters) { |
| 115 | 162 | this.tableAsString = tableAsString; |
| 116 | 162 | this.headerSeparator = headerSeparator; |
| 117 | 162 | this.valueSeparator = valueSeparator; |
| 118 | 162 | this.ignorableSeparator = ignorableSeparator; |
| 119 | 162 | this.parameterConverters = parameterConverters; |
| 120 | 162 | this.defaults = new ConvertedParameters(EMPTY_MAP, parameterConverters); |
| 121 | 162 | parse(); |
| 122 | 162 | } |
| 123 | |
|
| 124 | 1 | private ExamplesTable(ExamplesTable other, Row defaults) { |
| 125 | 1 | this.data.addAll(other.data); |
| 126 | 1 | this.tableAsString = other.tableAsString; |
| 127 | 1 | this.headerSeparator = other.headerSeparator; |
| 128 | 1 | this.valueSeparator = other.valueSeparator; |
| 129 | 1 | this.ignorableSeparator = other.ignorableSeparator; |
| 130 | 1 | this.parameterConverters = other.parameterConverters; |
| 131 | 1 | this.headers.addAll(other.headers); |
| 132 | 1 | this.properties.putAll(other.properties); |
| 133 | 1 | this.defaults = defaults; |
| 134 | 1 | } |
| 135 | |
|
| 136 | |
private void parse() { |
| 137 | 162 | data.clear(); |
| 138 | 162 | headers.clear(); |
| 139 | 162 | String[] rows = splitInRows(stripProperties(tableAsString.trim())); |
| 140 | 401 | for (int row = 0; row < rows.length; row++) { |
| 141 | 239 | String rowAsString = rows[row]; |
| 142 | 239 | if (rowAsString.startsWith(ignorableSeparator)) { |
| 143 | |
|
| 144 | 2 | continue; |
| 145 | 237 | } else if (row == 0) { |
| 146 | 162 | List<String> columns = columnsFor(rowAsString, headerSeparator); |
| 147 | 162 | headers.addAll(columns); |
| 148 | 162 | } else { |
| 149 | 75 | List<String> columns = columnsFor(rowAsString, valueSeparator); |
| 150 | 75 | Map<String, String> map = createRowMap(); |
| 151 | 230 | for (int column = 0; column < columns.size(); column++) { |
| 152 | 155 | map.put(headers.get(column), columns.get(column)); |
| 153 | |
} |
| 154 | 75 | data.add(map); |
| 155 | |
} |
| 156 | |
} |
| 157 | 162 | } |
| 158 | |
|
| 159 | |
private String stripProperties(String tableAsString) { |
| 160 | 162 | Pattern pattern = compile("\\{(.*?)\\}\\s*(.*)", DOTALL); |
| 161 | 162 | Matcher matcher = pattern.matcher(tableAsString); |
| 162 | 162 | if (matcher.matches()) { |
| 163 | 1 | parseProperties(matcher.group(1)); |
| 164 | 1 | return matcher.group(2); |
| 165 | |
} |
| 166 | 161 | return tableAsString; |
| 167 | |
} |
| 168 | |
|
| 169 | |
private void parseProperties(String propertiesAsString) { |
| 170 | 1 | properties.clear(); |
| 171 | |
try { |
| 172 | 1 | properties.load(new ByteArrayInputStream(propertiesAsString.replace(",", "\n").getBytes())); |
| 173 | 0 | } catch (IOException e) { |
| 174 | |
|
| 175 | 1 | } |
| 176 | 1 | trim = parseBoolean(properties.getProperty("trim", "true")); |
| 177 | 1 | } |
| 178 | |
|
| 179 | |
private String[] splitInRows(String table) { |
| 180 | 162 | return table.split("\n"); |
| 181 | |
} |
| 182 | |
|
| 183 | |
private List<String> columnsFor(String row, String separator) { |
| 184 | 237 | List<String> columns = new ArrayList<String>(); |
| 185 | |
|
| 186 | 817 | for (String column : row.split(buildRegex(separator), -1)) { |
| 187 | 580 | columns.add(valueOf(column)); |
| 188 | |
} |
| 189 | |
|
| 190 | 237 | if (StringUtils.isBlank(columns.get(0))) { |
| 191 | 231 | columns.remove(0); |
| 192 | |
} |
| 193 | 237 | int lastIndex = columns.size() - 1; |
| 194 | 237 | if (lastIndex != -1 && StringUtils.isBlank(columns.get(lastIndex))) { |
| 195 | 110 | columns.remove(lastIndex); |
| 196 | |
} |
| 197 | 237 | return columns; |
| 198 | |
} |
| 199 | |
|
| 200 | |
private String valueOf(String column) { |
| 201 | 580 | return trim ? column.trim() : column; |
| 202 | |
} |
| 203 | |
|
| 204 | |
private String buildRegex(String separator) { |
| 205 | 237 | char[] chars = separator.toCharArray(); |
| 206 | 237 | StringBuffer sb = new StringBuffer(); |
| 207 | 476 | for (char c : chars) { |
| 208 | 239 | sb.append("\\").append(c); |
| 209 | |
} |
| 210 | 237 | return sb.toString(); |
| 211 | |
} |
| 212 | |
|
| 213 | |
protected Map<String, String> createRowMap() { |
| 214 | 75 | return new LinkedHashMap<String, String>(); |
| 215 | |
} |
| 216 | |
|
| 217 | |
public ExamplesTable withDefaults(Parameters defaults) { |
| 218 | 1 | return new ExamplesTable(this, new ChainedRow(defaults, this.defaults)); |
| 219 | |
} |
| 220 | |
|
| 221 | |
public ExamplesTable withNamedParameters(Map<String, String> namedParameters) { |
| 222 | 1 | this.namedParameters = namedParameters; |
| 223 | 1 | return this; |
| 224 | |
} |
| 225 | |
|
| 226 | |
public Properties getProperties() { |
| 227 | 1 | return properties; |
| 228 | |
} |
| 229 | |
|
| 230 | |
public List<String> getHeaders() { |
| 231 | 22 | return headers; |
| 232 | |
} |
| 233 | |
|
| 234 | |
public Map<String, String> getRow(int row) { |
| 235 | 105 | return data.get(row); |
| 236 | |
} |
| 237 | |
|
| 238 | |
public Parameters getRowAsParameters(int row) { |
| 239 | 5 | return getRowAsParameters(row, false); |
| 240 | |
} |
| 241 | |
|
| 242 | |
public Parameters getRowAsParameters(int row, boolean replaceNamedParameters) { |
| 243 | 9 | Map<String, String> rowValues = getRow(row); |
| 244 | 9 | return createParameters((replaceNamedParameters ? replaceNamedParameters(rowValues) : rowValues)); |
| 245 | |
} |
| 246 | |
|
| 247 | |
private Map<String, String> replaceNamedParameters(Map<String, String> row) { |
| 248 | 1 | Map<String, String> replaced = new HashMap<String, String>(); |
| 249 | 1 | for (String key : row.keySet()) { |
| 250 | 2 | String replacedValue = row.get(key); |
| 251 | 2 | for (String namedKey : namedParameters.keySet()) { |
| 252 | 2 | String namedValue = namedParameters.get(namedKey); |
| 253 | 2 | replacedValue = replacedValue.replaceAll(namedKey, namedValue); |
| 254 | 2 | } |
| 255 | 2 | replaced.put(key, replacedValue); |
| 256 | 2 | } |
| 257 | 1 | return replaced; |
| 258 | |
} |
| 259 | |
|
| 260 | |
public int getRowCount() { |
| 261 | 48 | return data.size(); |
| 262 | |
} |
| 263 | |
|
| 264 | |
public List<Map<String, String>> getRows() { |
| 265 | 23 | return data; |
| 266 | |
} |
| 267 | |
|
| 268 | |
|
| 269 | |
public List<Parameters> getRowsAsParameters() { |
| 270 | 2 | return getRowsAsParameters(false); |
| 271 | |
} |
| 272 | |
|
| 273 | |
public List<Parameters> getRowsAsParameters(boolean replaceNamedParameters) { |
| 274 | 3 | List<Parameters> rows = new ArrayList<Parameters>(); |
| 275 | |
|
| 276 | 7 | for ( int row = 0; row < getRowCount(); row++ ){ |
| 277 | 4 | rows.add(getRowAsParameters(row, replaceNamedParameters)); |
| 278 | |
} |
| 279 | |
|
| 280 | 3 | return rows; |
| 281 | |
} |
| 282 | |
|
| 283 | |
private Parameters createParameters(Map<String, String> values) { |
| 284 | 9 | return new ConvertedParameters(new ChainedRow(new ConvertedParameters(values, parameterConverters), defaults), |
| 285 | |
parameterConverters); |
| 286 | |
} |
| 287 | |
|
| 288 | |
public String getHeaderSeparator() { |
| 289 | 2 | return headerSeparator; |
| 290 | |
} |
| 291 | |
|
| 292 | |
public String getValueSeparator() { |
| 293 | 2 | return valueSeparator; |
| 294 | |
} |
| 295 | |
|
| 296 | |
public String asString() { |
| 297 | 16 | return tableAsString; |
| 298 | |
} |
| 299 | |
|
| 300 | |
@Override |
| 301 | |
public String toString() { |
| 302 | 31 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 303 | |
} |
| 304 | |
|
| 305 | |
} |