1 | |
package org.jbehave.core.reporters; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileWriter; |
5 | |
import java.io.IOException; |
6 | |
import java.io.Writer; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.List; |
9 | |
|
10 | |
import org.jbehave.core.embedder.MatchingStepMonitor.StepMatch; |
11 | |
import org.jbehave.core.embedder.PerformableTree.ExamplePerformableScenario; |
12 | |
import org.jbehave.core.embedder.PerformableTree.NormalPerformableScenario; |
13 | |
import org.jbehave.core.embedder.PerformableTree.PerformableRoot; |
14 | |
import org.jbehave.core.embedder.PerformableTree.PerformableScenario; |
15 | |
import org.jbehave.core.embedder.PerformableTree.PerformableStory; |
16 | |
import org.jbehave.core.embedder.PerformableTree.Status; |
17 | |
import org.jbehave.core.failures.PendingStepStrategy; |
18 | |
import org.jbehave.core.model.ExamplesTable; |
19 | |
import org.jbehave.core.model.GivenStory; |
20 | |
import org.jbehave.core.model.Scenario; |
21 | |
import org.jbehave.core.model.Story; |
22 | |
import org.jbehave.core.model.TableTransformers.Formatting; |
23 | |
import org.jbehave.core.model.TableTransformers.FromLandscape; |
24 | |
import org.jbehave.core.model.TableTransformers.Replacing; |
25 | |
import org.jbehave.core.steps.AbstractStepResult.Failed; |
26 | |
import org.jbehave.core.steps.AbstractStepResult.Ignorable; |
27 | |
import org.jbehave.core.steps.AbstractStepResult.NotPerformed; |
28 | |
import org.jbehave.core.steps.AbstractStepResult.Pending; |
29 | |
import org.jbehave.core.steps.AbstractStepResult.Silent; |
30 | |
import org.jbehave.core.steps.AbstractStepResult.Skipped; |
31 | |
import org.jbehave.core.steps.AbstractStepResult.Successful; |
32 | |
import org.jbehave.core.steps.NullStepMonitor; |
33 | |
import org.jbehave.core.steps.StepMonitor; |
34 | |
|
35 | |
import com.thoughtworks.xstream.XStream; |
36 | |
import com.thoughtworks.xstream.io.json.JsonHierarchicalStreamDriver; |
37 | |
|
38 | |
public class CrossReference { |
39 | |
|
40 | 32 | private boolean doJson = true; |
41 | 32 | private boolean doXml = true; |
42 | |
private String name; |
43 | |
|
44 | |
public CrossReference() { |
45 | 32 | this("XREF"); |
46 | 32 | } |
47 | |
|
48 | 32 | public CrossReference(String name) { |
49 | 32 | this.name = name; |
50 | 32 | } |
51 | |
|
52 | |
public CrossReference withJsonOnly() { |
53 | 0 | doJson = true; |
54 | 0 | doXml = false; |
55 | 0 | return this; |
56 | |
} |
57 | |
|
58 | |
public CrossReference withXmlOnly() { |
59 | 0 | doJson = false; |
60 | 0 | doXml = true; |
61 | 0 | return this; |
62 | |
} |
63 | |
|
64 | |
public synchronized void serialise(PerformableRoot root, |
65 | |
File outputDirectory) { |
66 | 18 | XRef xref = new XRef(root); |
67 | 18 | if (doXml) { |
68 | 18 | serialise(xref, "xml", outputDirectory); |
69 | |
} |
70 | 18 | if (doJson) { |
71 | 18 | serialise(xref, "json", outputDirectory); |
72 | |
} |
73 | 18 | } |
74 | |
|
75 | |
private void serialise(Object object, String format, File outputDirectory) { |
76 | |
try { |
77 | 36 | serialise(object, xstream(format), writer(format, outputDirectory)); |
78 | 0 | } catch (IOException e) { |
79 | 0 | throw new RuntimeException(name, e); |
80 | 36 | } |
81 | 36 | } |
82 | |
|
83 | |
private void serialise(Object object, XStream xstream, Writer writer) |
84 | |
throws IOException { |
85 | 36 | writer.write(xstream.toXML(object)); |
86 | 36 | writer.flush(); |
87 | 36 | writer.close(); |
88 | 36 | } |
89 | |
|
90 | |
private Writer writer(String format, File outputDirectory) |
91 | |
throws IOException { |
92 | 36 | String name = fileName(format); |
93 | 36 | File outputDir = new File(outputDirectory, "view"); |
94 | 36 | outputDir.mkdirs(); |
95 | 36 | return new FileWriter(new File(outputDir, name)); |
96 | |
} |
97 | |
|
98 | |
private XStream xstream(String format) { |
99 | 36 | XStream xstream = (format.equals("json") ? new XStream( |
100 | |
new JsonHierarchicalStreamDriver()) : new XStream()); |
101 | 36 | configure(xstream); |
102 | 36 | return xstream; |
103 | |
} |
104 | |
|
105 | |
private void configure(XStream xstream) { |
106 | 36 | xstream.setMode(XStream.NO_REFERENCES); |
107 | 36 | xstream.alias("xref", XRef.class); |
108 | 36 | xstream.alias(name.toLowerCase(), PerformableRoot.class); |
109 | 36 | xstream.alias("performableStory", PerformableStory.class); |
110 | 36 | xstream.alias("performableScenario", PerformableScenario.class); |
111 | 36 | xstream.alias("normalPerformableScenario", NormalPerformableScenario.class); |
112 | 36 | xstream.alias("examplePerformableScenario", ExamplePerformableScenario.class); |
113 | 36 | xstream.alias("status", Status.class); |
114 | 36 | xstream.alias("story", Story.class); |
115 | 36 | xstream.alias("scenario", Scenario.class); |
116 | 36 | xstream.alias("givenStory", GivenStory.class); |
117 | 36 | xstream.alias("failed", Failed.class); |
118 | 36 | xstream.alias("pending", Pending.class); |
119 | 36 | xstream.alias("notPerformed", NotPerformed.class); |
120 | 36 | xstream.alias("successful", Successful.class); |
121 | 36 | xstream.alias("ignorable", Ignorable.class); |
122 | 36 | xstream.alias("silent", Silent.class); |
123 | 36 | xstream.alias("skipped", Skipped.class); |
124 | 36 | xstream.alias("fromLandscape", FromLandscape.class); |
125 | 36 | xstream.alias("formatting", Formatting.class); |
126 | 36 | xstream.alias("replacing", Replacing.class); |
127 | 36 | xstream.alias("stepMatch", StepMatch.class); |
128 | 36 | xstream.omitField(ExamplesTable.class, "parameterConverters"); |
129 | 36 | xstream.omitField(ExamplesTable.class, "tableTrasformers"); |
130 | 36 | xstream.omitField(ExamplesTable.class, "defaults"); |
131 | 36 | } |
132 | |
|
133 | |
private String fileName(String extension) { |
134 | 36 | return name.toLowerCase() + "." + extension; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public synchronized void outputToFiles( |
141 | |
StoryReporterBuilder storyReporterBuilder) { |
142 | 0 | } |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public CrossReference withMetaFilter(String metaFilter) { |
148 | 0 | return this; |
149 | |
} |
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public CrossReference withPendingStepStrategy( |
155 | |
PendingStepStrategy pendingStepStrategy) { |
156 | 0 | return this; |
157 | |
} |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public CrossReference withOutputAfterEachStory(boolean outputAfterEachStory) { |
163 | 0 | return this; |
164 | |
} |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
public CrossReference withThreadSafeDelegateFormat(Format format) { |
170 | 0 | return this; |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
public CrossReference excludingStoriesWithNoExecutedScenarios( |
177 | |
boolean exclude) { |
178 | 0 | return this; |
179 | |
} |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public String getMetaFilter() { |
185 | 0 | return ""; |
186 | |
} |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public StepMonitor getStepMonitor() { |
192 | 0 | return new NullStepMonitor(); |
193 | |
} |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
protected XRefRoot newXRefRoot() { |
199 | 0 | return new XRefRoot(); |
200 | |
} |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
protected Writer makeWriter(File file) throws IOException { |
206 | 0 | return new FileWriter(file); |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
protected void aliasForXRefStory(XStream xstream) { |
213 | 0 | } |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
protected void aliasForXRefRoot(XStream xstream) { |
219 | 0 | } |
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
public StoryReporter createStoryReporter(FilePrintStreamFactory factory, |
225 | |
final StoryReporterBuilder storyReporterBuilder) { |
226 | 0 | return new NullStoryReporter(); |
227 | |
} |
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | 0 | public static class XRefRoot { |
233 | |
} |
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | 0 | public static class XRefStory { |
239 | |
} |
240 | |
|
241 | |
public static class XRef { |
242 | |
private List<PerformableStory> stories; |
243 | 18 | private List<PerformableScenario> scenarios = new ArrayList<PerformableScenario>(); |
244 | |
|
245 | 18 | public XRef(PerformableRoot root) { |
246 | 18 | stories = root.getStories(); |
247 | 18 | for (PerformableStory story : stories) { |
248 | 50 | scenarios.addAll(story.getScenarios()); |
249 | 50 | } |
250 | 18 | } |
251 | |
|
252 | |
} |
253 | |
|
254 | |
} |