1 | |
package org.jbehave.core.steps; |
2 | |
|
3 | |
import java.lang.reflect.Method; |
4 | |
|
5 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
6 | |
import org.apache.commons.lang.builder.ToStringStyle; |
7 | |
import org.jbehave.core.failures.PendingStepFound; |
8 | |
import org.jbehave.core.failures.UUIDExceptionWrapper; |
9 | |
import org.jbehave.core.model.OutcomesTable.OutcomesFailed; |
10 | |
import org.jbehave.core.reporters.StoryReporter; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | 39 | public abstract class AbstractStepResult implements StepResult { |
25 | |
|
26 | |
public static class Failed extends AbstractStepResult { |
27 | |
|
28 | |
public Failed(String step, UUIDExceptionWrapper throwable) { |
29 | 24 | super(step, Type.FAILED, throwable); |
30 | 24 | } |
31 | |
|
32 | |
public Failed(Method method, UUIDExceptionWrapper throwable) { |
33 | 4 | super(asString(method), Type.FAILED, throwable); |
34 | 4 | } |
35 | |
|
36 | |
public void describeTo(StoryReporter reporter) { |
37 | 21 | if (throwable.getCause() instanceof OutcomesFailed) { |
38 | 1 | reporter.failedOutcomes(parametrisedStep(), ((OutcomesFailed) throwable.getCause()).outcomesTable()); |
39 | |
} else { |
40 | 20 | reporter.failed(parametrisedStep(), throwable); |
41 | |
} |
42 | 21 | } |
43 | |
} |
44 | |
|
45 | |
public static class NotPerformed extends AbstractStepResult { |
46 | |
|
47 | |
public NotPerformed(String step) { |
48 | 42 | super(Type.NOT_PERFORMED, step); |
49 | 42 | } |
50 | |
|
51 | |
public void describeTo(StoryReporter reporter) { |
52 | 10 | reporter.notPerformed(parametrisedStep()); |
53 | 10 | } |
54 | |
} |
55 | |
|
56 | |
public static class Pending extends AbstractStepResult { |
57 | |
public Pending(String step) { |
58 | 26 | this(step, new PendingStepFound(step)); |
59 | 26 | } |
60 | |
|
61 | |
public Pending(String step, PendingStepFound e) { |
62 | 27 | super(step, Type.PENDING, e); |
63 | 27 | } |
64 | |
|
65 | |
public void describeTo(StoryReporter reporter) { |
66 | 11 | reporter.pending(parametrisedStep()); |
67 | 11 | } |
68 | |
} |
69 | |
|
70 | |
public static class Successful extends AbstractStepResult { |
71 | |
|
72 | |
public Successful(String step) { |
73 | 142 | super(Type.SUCCESSFUL, step); |
74 | 142 | } |
75 | |
|
76 | |
public Successful(Method method) { |
77 | 35 | super(Type.SUCCESSFUL, asString(method)); |
78 | 35 | } |
79 | |
|
80 | |
public void describeTo(StoryReporter reporter) { |
81 | 74 | reporter.successful(parametrisedStep()); |
82 | 74 | } |
83 | |
|
84 | |
} |
85 | |
|
86 | |
public static class Silent extends Successful { |
87 | |
|
88 | |
public Silent(Method method) { |
89 | 35 | super(method); |
90 | 35 | } |
91 | |
|
92 | |
public void describeTo(StoryReporter reporter) { |
93 | |
|
94 | 0 | } |
95 | |
} |
96 | |
|
97 | |
public static class Ignorable extends AbstractStepResult { |
98 | |
public Ignorable(String step) { |
99 | 4 | super(Type.IGNORABLE, step); |
100 | 4 | } |
101 | |
|
102 | |
public void describeTo(StoryReporter reporter) { |
103 | 1 | reporter.ignorable(step); |
104 | 1 | } |
105 | |
} |
106 | |
|
107 | |
public static class Skipped extends AbstractStepResult { |
108 | |
|
109 | |
public Skipped() { |
110 | 6 | super(Type.SKIPPED, ""); |
111 | 6 | } |
112 | |
|
113 | |
public void describeTo(StoryReporter reporter) { |
114 | |
|
115 | 1 | } |
116 | |
} |
117 | |
|
118 | |
protected final String step; |
119 | |
protected final Type type; |
120 | |
protected final UUIDExceptionWrapper throwable; |
121 | |
private String parametrisedStep; |
122 | |
private long durationInMillis; |
123 | |
|
124 | |
public AbstractStepResult(Type type, String step) { |
125 | 228 | this(step, type, null); |
126 | 228 | } |
127 | |
|
128 | 283 | public AbstractStepResult(String step, Type type, UUIDExceptionWrapper throwable) { |
129 | 284 | this.step = step; |
130 | 283 | this.type = type; |
131 | 284 | this.throwable = throwable; |
132 | 283 | } |
133 | |
|
134 | |
public String parametrisedStep() { |
135 | 126 | return parametrisedStep != null ? parametrisedStep : step; |
136 | |
} |
137 | |
|
138 | |
public StepResult withParameterValues(String parametrisedStep) { |
139 | 127 | this.parametrisedStep = parametrisedStep; |
140 | 128 | return this; |
141 | |
} |
142 | |
|
143 | |
public long durationInMillis(){ |
144 | 2 | return durationInMillis; |
145 | |
} |
146 | |
|
147 | |
public StepResult withDurationInMillis(long millis) { |
148 | 158 | this.durationInMillis = millis; |
149 | 157 | return this; |
150 | |
} |
151 | |
|
152 | |
public UUIDExceptionWrapper getFailure() { |
153 | 115 | return throwable; |
154 | |
} |
155 | |
|
156 | |
@Override |
157 | |
public String toString() { |
158 | 2 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(parametrisedStep()).append(durationInMillis()).toString(); |
159 | |
} |
160 | |
|
161 | |
public static StepResult successful(String step) { |
162 | 141 | return new Successful(step); |
163 | |
} |
164 | |
|
165 | |
public static StepResult successful(Method method) { |
166 | 0 | return new Successful(method); |
167 | |
} |
168 | |
|
169 | |
public static StepResult ignorable(String step) { |
170 | 4 | return new Ignorable(step); |
171 | |
} |
172 | |
|
173 | |
public static StepResult pending(String step) { |
174 | 26 | return new Pending(step); |
175 | |
} |
176 | |
|
177 | |
public static StepResult pending(String step, PendingStepFound e) { |
178 | 1 | return new Pending(step, e); |
179 | |
} |
180 | |
|
181 | |
public static StepResult notPerformed(String step) { |
182 | 42 | return new NotPerformed(step); |
183 | |
} |
184 | |
|
185 | |
public static StepResult failed(String step, UUIDExceptionWrapper e) { |
186 | 24 | return new Failed(step, e); |
187 | |
} |
188 | |
|
189 | |
public static StepResult failed(Method method, UUIDExceptionWrapper e) { |
190 | 4 | return new Failed(method, e); |
191 | |
} |
192 | |
|
193 | |
public static StepResult silent(Method method) { |
194 | 35 | return new Silent(method); |
195 | |
} |
196 | |
|
197 | |
public static StepResult skipped() { |
198 | 6 | return new Skipped(); |
199 | |
} |
200 | |
|
201 | |
private static String asString(Method method) { |
202 | 39 | if (method == null) { |
203 | 0 | return ""; |
204 | |
} |
205 | 39 | StringBuilder sb = new StringBuilder() |
206 | 39 | .append(method.getDeclaringClass().getName()).append(".") |
207 | 39 | .append(method.getName()).append("("); |
208 | 39 | Class<?>[] types = method.getParameterTypes(); |
209 | 63 | for (int i = 0; i < types.length; i++) { |
210 | 24 | Class<?> type = types[i]; |
211 | 24 | sb.append(type.getName()); |
212 | 24 | if (i+1 < types.length) { |
213 | 8 | sb.append(","); |
214 | |
} |
215 | |
} |
216 | 39 | return sb.append(")").toString(); |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
} |