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