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