| 1 | |
package org.jbehave.core.model; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Iterator; |
| 5 | |
import java.util.List; |
| 6 | |
|
| 7 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 8 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 9 | |
import org.hamcrest.Matcher; |
| 10 | |
import org.jbehave.core.failures.UUIDExceptionWrapper; |
| 11 | |
|
| 12 | |
import static java.util.Arrays.asList; |
| 13 | |
|
| 14 | 23 | public class OutcomesTable { |
| 15 | |
|
| 16 | 1 | private static final List<String> FIELDS = asList("Description", "Value", "Matcher", "Verified"); |
| 17 | |
private static final String NEWLINE = "\n"; |
| 18 | |
private static final String HEADER_SEPARATOR = "|"; |
| 19 | |
private static final String VALUE_SEPARATOR = "|"; |
| 20 | |
|
| 21 | 23 | private List<Outcome<?>> outcomes = new ArrayList<Outcome<?>>(); |
| 22 | 23 | private List<Outcome<?>> failedOutcomes = new ArrayList<Outcome<?>>(); |
| 23 | |
private UUIDExceptionWrapper failureCause; |
| 24 | |
|
| 25 | |
public <T> void addOutcome(String description, T value, Matcher<T> matcher) { |
| 26 | 23 | outcomes.add(new Outcome<T>(description, value, matcher)); |
| 27 | 23 | } |
| 28 | |
|
| 29 | |
public void verify() { |
| 30 | 19 | boolean failed = false; |
| 31 | 19 | failedOutcomes.clear(); |
| 32 | 19 | for (Outcome<?> outcome : outcomes) { |
| 33 | 21 | if (!outcome.isVerified()) { |
| 34 | 18 | failedOutcomes.add(outcome); |
| 35 | 18 | failed = true; |
| 36 | 18 | break; |
| 37 | |
} |
| 38 | |
} |
| 39 | 19 | if (failed) { |
| 40 | 18 | failureCause = new UUIDExceptionWrapper(new OutcomesFailed(this)); |
| 41 | 18 | throw failureCause; |
| 42 | |
} |
| 43 | 1 | } |
| 44 | |
|
| 45 | |
public UUIDExceptionWrapper failureCause() { |
| 46 | 13 | return failureCause; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public List<Outcome<?>> getOutcomes() { |
| 50 | 15 | return outcomes; |
| 51 | |
} |
| 52 | |
|
| 53 | |
public List<Outcome<?>> getFailedOutcomes() { |
| 54 | 2 | return failedOutcomes; |
| 55 | |
} |
| 56 | |
|
| 57 | |
public List<String> getOutcomeFields() { |
| 58 | 12 | return FIELDS; |
| 59 | |
} |
| 60 | |
|
| 61 | |
@Override |
| 62 | |
public String toString() { |
| 63 | 2 | StringBuilder sb = new StringBuilder(); |
| 64 | 2 | for (Iterator<String> iterator = FIELDS.iterator(); iterator.hasNext();) { |
| 65 | 8 | sb.append(HEADER_SEPARATOR).append(iterator.next()); |
| 66 | 8 | if (!iterator.hasNext()) { |
| 67 | 2 | sb.append(HEADER_SEPARATOR).append(NEWLINE); |
| 68 | |
} |
| 69 | |
} |
| 70 | 2 | for (Outcome<?> outcome : outcomes) { |
| 71 | 2 | sb.append(VALUE_SEPARATOR).append(outcome.getDescription()).append(VALUE_SEPARATOR).append( |
| 72 | |
outcome.getValue()).append(VALUE_SEPARATOR).append(outcome.getMatcher()).append(VALUE_SEPARATOR) |
| 73 | |
.append(outcome.isVerified()).append(VALUE_SEPARATOR).append(NEWLINE); |
| 74 | |
} |
| 75 | 2 | return sb.toString(); |
| 76 | |
} |
| 77 | |
|
| 78 | |
public static class Outcome<T> { |
| 79 | |
|
| 80 | |
private final String description; |
| 81 | |
private final T value; |
| 82 | |
private final Matcher<T> matcher; |
| 83 | |
private final boolean verified; |
| 84 | |
|
| 85 | 23 | public Outcome(String description, T value, Matcher<T> matcher) { |
| 86 | 23 | this.description = description; |
| 87 | 23 | this.value = value; |
| 88 | 23 | this.matcher = matcher; |
| 89 | 23 | this.verified = matcher.matches(value); |
| 90 | 23 | } |
| 91 | |
|
| 92 | |
public String getDescription() { |
| 93 | 15 | return description; |
| 94 | |
} |
| 95 | |
|
| 96 | |
public T getValue() { |
| 97 | 15 | return value; |
| 98 | |
} |
| 99 | |
|
| 100 | |
public Matcher<T> getMatcher() { |
| 101 | 14 | return matcher; |
| 102 | |
} |
| 103 | |
|
| 104 | |
public boolean isVerified() { |
| 105 | 47 | return verified; |
| 106 | |
} |
| 107 | |
|
| 108 | |
@Override |
| 109 | |
public String toString() { |
| 110 | 2 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 111 | |
} |
| 112 | |
} |
| 113 | |
|
| 114 | 23 | @SuppressWarnings("serial") |
| 115 | |
public static class OutcomesFailed extends UUIDExceptionWrapper { |
| 116 | |
private OutcomesTable outcomes; |
| 117 | |
|
| 118 | 19 | public OutcomesFailed(OutcomesTable outcomes) { |
| 119 | 19 | this.outcomes = outcomes; |
| 120 | 19 | } |
| 121 | |
|
| 122 | |
public OutcomesTable outcomesTable() { |
| 123 | 19 | return outcomes; |
| 124 | |
} |
| 125 | |
|
| 126 | |
} |
| 127 | |
|
| 128 | |
} |