1 | |
package org.jbehave.core.reporters; |
2 | |
|
3 | |
import static java.util.Arrays.asList; |
4 | |
|
5 | |
import java.io.File; |
6 | |
import java.net.URL; |
7 | |
import java.util.ArrayList; |
8 | |
import java.util.HashMap; |
9 | |
import java.util.List; |
10 | |
import java.util.Map; |
11 | |
import java.util.Properties; |
12 | |
|
13 | |
import org.jbehave.core.configuration.Keywords; |
14 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
15 | |
import org.jbehave.core.io.CodeLocations; |
16 | |
import org.jbehave.core.io.StoryLocation; |
17 | |
import org.jbehave.core.reporters.FilePrintStreamFactory.FileConfiguration; |
18 | |
import org.jbehave.core.reporters.FilePrintStreamFactory.FilePathResolver; |
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | 213 | public class StoryReporterBuilder { |
112 | |
|
113 | 10 | public enum Format { |
114 | 1 | CONSOLE, IDE_CONSOLE, TXT, HTML, XML, STATS |
115 | |
} |
116 | |
|
117 | 213 | private List<Format> formats = new ArrayList<Format>(); |
118 | 213 | private String relativeDirectory = new FileConfiguration().getRelativeDirectory(); |
119 | 213 | private FilePathResolver pathResolver = new FileConfiguration().getPathResolver(); |
120 | 213 | private URL codeLocation = CodeLocations.codeLocationFromPath("target/classes"); |
121 | 213 | private Properties viewResources = FreemarkerViewGenerator.defaultViewProperties(); |
122 | 213 | private boolean reportFailureTrace = false; |
123 | 213 | private Keywords keywords = new LocalizedKeywords(); |
124 | |
|
125 | |
public File outputDirectory() { |
126 | 17 | return filePrintStreamFactory("").outputDirectory(); |
127 | |
} |
128 | |
|
129 | |
public String relativeDirectory(){ |
130 | 1 | return relativeDirectory; |
131 | |
} |
132 | |
|
133 | |
public FilePathResolver pathResolver(){ |
134 | 2 | return pathResolver; |
135 | |
} |
136 | |
|
137 | |
public URL codeLocation() { |
138 | 1 | return codeLocation; |
139 | |
} |
140 | |
|
141 | |
public List<Format> formats() { |
142 | 1 | return formats; |
143 | |
} |
144 | |
|
145 | |
public List<String> formatNames(boolean toLowerCase) { |
146 | 19 | List<String> names = new ArrayList<String>(); |
147 | 19 | for (Format format : formats) { |
148 | 14 | String name = format.name(); |
149 | 14 | if (toLowerCase) { |
150 | 7 | name = name.toLowerCase(); |
151 | |
} |
152 | 14 | names.add(name); |
153 | 14 | } |
154 | 19 | return names; |
155 | |
} |
156 | |
|
157 | |
public Keywords keywords() { |
158 | 1 | return keywords; |
159 | |
} |
160 | |
|
161 | |
public boolean reportFailureTrace() { |
162 | 1 | return reportFailureTrace; |
163 | |
} |
164 | |
|
165 | |
public Properties viewResources() { |
166 | 18 | return viewResources; |
167 | |
} |
168 | |
|
169 | |
public StoryReporterBuilder withRelativeDirectory(String relativeDirectory) { |
170 | 3 | this.relativeDirectory = relativeDirectory; |
171 | 3 | return this; |
172 | |
} |
173 | |
|
174 | |
public StoryReporterBuilder withPathResolver(FilePathResolver pathResolver){ |
175 | 1 | this.pathResolver = pathResolver; |
176 | 1 | return this; |
177 | |
} |
178 | |
|
179 | |
public StoryReporterBuilder withCodeLocation(URL codeLocation) { |
180 | 1 | this.codeLocation = codeLocation; |
181 | 1 | return this; |
182 | |
} |
183 | |
|
184 | |
public StoryReporterBuilder withDefaultFormats() { |
185 | 6 | return withFormats(Format.STATS); |
186 | |
} |
187 | |
|
188 | |
public StoryReporterBuilder withFormats(Format... formats) { |
189 | 15 | this.formats.addAll(asList(formats)); |
190 | 15 | return this; |
191 | |
} |
192 | |
|
193 | |
public StoryReporterBuilder withFailureTrace(boolean reportFailureTrace) { |
194 | 1 | this.reportFailureTrace = reportFailureTrace; |
195 | 1 | return this; |
196 | |
} |
197 | |
|
198 | |
public StoryReporterBuilder withKeywords(Keywords keywords) { |
199 | 1 | this.keywords = keywords; |
200 | 1 | return this; |
201 | |
} |
202 | |
|
203 | |
public StoryReporterBuilder withViewResources(Properties resources) { |
204 | 1 | this.viewResources = resources; |
205 | 1 | return this; |
206 | |
} |
207 | |
|
208 | |
public StoryReporter build(String storyPath) { |
209 | 36 | Map<Format, StoryReporter> delegates = new HashMap<Format, StoryReporter>(); |
210 | 36 | for (Format format : formats) { |
211 | 21 | delegates.put(format, reporterFor(storyPath, format)); |
212 | |
} |
213 | 36 | return new DelegatingStoryReporter(delegates.values()); |
214 | |
} |
215 | |
|
216 | |
public Map<String, StoryReporter> build(List<String> storyPaths) { |
217 | 17 | Map<String, StoryReporter> reporters = new HashMap<String, StoryReporter>(); |
218 | 17 | for (String storyPath : storyPaths) { |
219 | 24 | reporters.put(storyPath, build(storyPath)); |
220 | |
} |
221 | 17 | return reporters; |
222 | |
} |
223 | |
|
224 | |
public StoryReporter reporterFor(String storyPath, Format format) { |
225 | 19 | FilePrintStreamFactory factory = filePrintStreamFactory(storyPath); |
226 | 19 | switch (format) { |
227 | |
case CONSOLE: |
228 | |
default: |
229 | 1 | return new ConsoleOutput(keywords).doReportFailureTrace(reportFailureTrace); |
230 | |
case IDE_CONSOLE: |
231 | 1 | return new IdeOnlyConsoleOutput(keywords).doReportFailureTrace(reportFailureTrace); |
232 | |
case TXT: |
233 | 7 | factory.useConfiguration(fileConfiguration("txt")); |
234 | 7 | return new TxtOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace); |
235 | |
case HTML: |
236 | 3 | factory.useConfiguration(fileConfiguration("html")); |
237 | 3 | return new HtmlOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace); |
238 | |
case XML: |
239 | 1 | factory.useConfiguration(fileConfiguration("xml")); |
240 | 1 | return new XmlOutput(factory.createPrintStream(), keywords).doReportFailureTrace(reportFailureTrace); |
241 | |
case STATS: |
242 | 6 | factory.useConfiguration(fileConfiguration("stats")); |
243 | 6 | return new PostStoryStatisticsCollector(factory.createPrintStream()); |
244 | |
} |
245 | |
} |
246 | |
|
247 | |
protected FilePrintStreamFactory filePrintStreamFactory(String storyPath) { |
248 | 35 | return new FilePrintStreamFactory(new StoryLocation(codeLocation, storyPath), fileConfiguration("")); |
249 | |
} |
250 | |
|
251 | |
protected FileConfiguration fileConfiguration(String extension) { |
252 | 54 | return new FileConfiguration(relativeDirectory, extension, pathResolver); |
253 | |
} |
254 | |
|
255 | |
} |