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