| 1 | |
package org.jbehave.core.reporters; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.net.URL; |
| 5 | |
import java.util.ArrayList; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Locale; |
| 9 | |
import java.util.Map; |
| 10 | |
import java.util.Properties; |
| 11 | |
|
| 12 | |
import org.jbehave.core.configuration.Keywords; |
| 13 | |
import org.jbehave.core.i18n.LocalizedKeywords; |
| 14 | |
import org.jbehave.core.io.CodeLocations; |
| 15 | |
import org.jbehave.core.io.StoryLocation; |
| 16 | |
import org.jbehave.core.reporters.FilePrintStreamFactory.FileConfiguration; |
| 17 | |
import org.jbehave.core.reporters.FilePrintStreamFactory.FilePathResolver; |
| 18 | |
|
| 19 | |
import static java.util.Arrays.asList; |
| 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 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | 246 | public class StoryReporterBuilder { |
| 123 | |
|
| 124 | 8 | public enum Format { |
| 125 | 1 | CONSOLE(org.jbehave.core.reporters.Format.CONSOLE), |
| 126 | 1 | IDE_CONSOLE(org.jbehave.core.reporters.Format.IDE_CONSOLE), |
| 127 | 1 | TXT(org.jbehave.core.reporters.Format.TXT), |
| 128 | 1 | HTML(org.jbehave.core.reporters.Format.HTML), |
| 129 | 1 | XML(org.jbehave.core.reporters.Format.XML), |
| 130 | 1 | STATS(org.jbehave.core.reporters.Format.STATS); |
| 131 | |
|
| 132 | |
private org.jbehave.core.reporters.Format realFormat; |
| 133 | |
|
| 134 | 6 | Format(org.jbehave.core.reporters.Format realFormat) { |
| 135 | 6 | this.realFormat = realFormat; |
| 136 | 6 | } |
| 137 | |
|
| 138 | |
} |
| 139 | |
|
| 140 | 246 | private List<org.jbehave.core.reporters.Format> formats = new ArrayList<org.jbehave.core.reporters.Format>(); |
| 141 | 246 | private String relativeDirectory = new FileConfiguration().getRelativeDirectory(); |
| 142 | 246 | private FilePathResolver pathResolver = new FileConfiguration().getPathResolver(); |
| 143 | 246 | private URL codeLocation = CodeLocations.codeLocationFromPath("target/classes"); |
| 144 | 246 | private Properties viewResources = FreemarkerViewGenerator.defaultViewProperties(); |
| 145 | 246 | private boolean reportFailureTrace = false; |
| 146 | 246 | private boolean compressFailureTrace = false; |
| 147 | 246 | private Keywords keywords = new LocalizedKeywords(); |
| 148 | |
private CrossReference crossReference; |
| 149 | |
private boolean multiThreading; |
| 150 | |
|
| 151 | |
public File outputDirectory() { |
| 152 | 18 | return filePrintStreamFactory("").outputDirectory(); |
| 153 | |
} |
| 154 | |
|
| 155 | |
public String relativeDirectory() { |
| 156 | 1 | return relativeDirectory; |
| 157 | |
} |
| 158 | |
|
| 159 | |
public FilePathResolver pathResolver() { |
| 160 | 2 | return pathResolver; |
| 161 | |
} |
| 162 | |
|
| 163 | |
public URL codeLocation() { |
| 164 | 1 | return codeLocation; |
| 165 | |
} |
| 166 | |
|
| 167 | |
public List<org.jbehave.core.reporters.Format> formats() { |
| 168 | 2 | return formats; |
| 169 | |
} |
| 170 | |
|
| 171 | |
public List<String> formatNames(boolean toLowerCase) { |
| 172 | 17 | Locale locale = Locale.getDefault(); |
| 173 | 17 | if (keywords instanceof LocalizedKeywords) { |
| 174 | 17 | locale = ((LocalizedKeywords) keywords).getLocale(); |
| 175 | |
} |
| 176 | 17 | List<String> names = new ArrayList<String>(); |
| 177 | 17 | for (org.jbehave.core.reporters.Format format : formats) { |
| 178 | 28 | String name = format.name(); |
| 179 | 28 | if (toLowerCase) { |
| 180 | 14 | name = name.toLowerCase(locale); |
| 181 | |
} |
| 182 | 28 | names.add(name); |
| 183 | 28 | } |
| 184 | 17 | return names; |
| 185 | |
} |
| 186 | |
|
| 187 | |
public Keywords keywords() { |
| 188 | 19 | return keywords; |
| 189 | |
} |
| 190 | |
|
| 191 | |
public boolean multiThreading(){ |
| 192 | 0 | return multiThreading; |
| 193 | |
} |
| 194 | |
|
| 195 | |
public boolean reportFailureTrace() { |
| 196 | 19 | return reportFailureTrace; |
| 197 | |
} |
| 198 | |
|
| 199 | |
public boolean compressFailureTrace() { |
| 200 | 18 | return compressFailureTrace; |
| 201 | |
} |
| 202 | |
|
| 203 | |
public Properties viewResources() { |
| 204 | 15 | return viewResources; |
| 205 | |
} |
| 206 | |
|
| 207 | |
public StoryReporterBuilder withRelativeDirectory(String relativeDirectory) { |
| 208 | 3 | this.relativeDirectory = relativeDirectory; |
| 209 | 3 | return this; |
| 210 | |
} |
| 211 | |
|
| 212 | |
public StoryReporterBuilder withPathResolver(FilePathResolver pathResolver) { |
| 213 | 1 | this.pathResolver = pathResolver; |
| 214 | 1 | return this; |
| 215 | |
} |
| 216 | |
|
| 217 | |
public StoryReporterBuilder withCodeLocation(URL codeLocation) { |
| 218 | 1 | this.codeLocation = codeLocation; |
| 219 | 1 | return this; |
| 220 | |
} |
| 221 | |
|
| 222 | |
public CrossReference crossReference() { |
| 223 | 1 | return crossReference; |
| 224 | |
} |
| 225 | |
|
| 226 | |
public boolean hasCrossReference() { |
| 227 | 11 | return crossReference != null; |
| 228 | |
} |
| 229 | |
|
| 230 | |
public StoryReporterBuilder withCrossReference(CrossReference crossReference) { |
| 231 | 1 | this.crossReference = crossReference; |
| 232 | 1 | return this; |
| 233 | |
} |
| 234 | |
|
| 235 | |
public StoryReporterBuilder withDefaultFormats() { |
| 236 | 7 | return withFormats(Format.STATS); |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
@Deprecated |
| 243 | |
public StoryReporterBuilder withFormats(Format... formats) { |
| 244 | 7 | List<org.jbehave.core.reporters.Format> formatz = new ArrayList<org.jbehave.core.reporters.Format>(); |
| 245 | 14 | for (Format format : formats) { |
| 246 | 7 | formatz.add(format.realFormat); |
| 247 | |
} |
| 248 | 7 | this.formats.addAll(formatz); |
| 249 | 7 | return this; |
| 250 | |
} |
| 251 | |
|
| 252 | |
public StoryReporterBuilder withFormats(org.jbehave.core.reporters.Format... formats) { |
| 253 | 10 | this.formats.addAll(asList(formats)); |
| 254 | 10 | return this; |
| 255 | |
} |
| 256 | |
|
| 257 | |
public StoryReporterBuilder withFailureTrace(boolean reportFailureTrace) { |
| 258 | 1 | this.reportFailureTrace = reportFailureTrace; |
| 259 | 1 | return this; |
| 260 | |
} |
| 261 | |
|
| 262 | |
public StoryReporterBuilder withFailureTraceCompression(boolean compressFailureTrace) { |
| 263 | 0 | this.compressFailureTrace = compressFailureTrace; |
| 264 | 0 | return this; |
| 265 | |
} |
| 266 | |
|
| 267 | |
public StoryReporterBuilder withKeywords(Keywords keywords) { |
| 268 | 3 | this.keywords = keywords; |
| 269 | 3 | return this; |
| 270 | |
} |
| 271 | |
|
| 272 | |
public StoryReporterBuilder withMultiThreading(boolean multiThreading) { |
| 273 | 20 | this.multiThreading = multiThreading; |
| 274 | 20 | return this; |
| 275 | |
} |
| 276 | |
|
| 277 | |
public StoryReporterBuilder withViewResources(Properties resources) { |
| 278 | 1 | this.viewResources = resources; |
| 279 | 1 | return this; |
| 280 | |
} |
| 281 | |
|
| 282 | |
public StoryReporter build(String storyPath) { |
| 283 | 14 | Map<org.jbehave.core.reporters.Format, StoryReporter> delegates = new HashMap<org.jbehave.core.reporters.Format, StoryReporter>(); |
| 284 | 14 | for (org.jbehave.core.reporters.Format format : formats) { |
| 285 | 28 | delegates.put(format, reporterFor(storyPath, format)); |
| 286 | |
} |
| 287 | |
|
| 288 | 14 | DelegatingStoryReporter delegate = new DelegatingStoryReporter(delegates.values()); |
| 289 | 14 | StoryReporter crossReferencing = (crossReference == null ? new NullStoryReporter() : reporterFor(storyPath, |
| 290 | |
crossReference)); |
| 291 | 14 | return new ConcurrentStoryReporter(crossReferencing, delegate, multiThreading); |
| 292 | |
} |
| 293 | |
|
| 294 | |
public Map<String, StoryReporter> build(List<String> storyPaths) { |
| 295 | 0 | Map<String, StoryReporter> reporters = new HashMap<String, StoryReporter>(); |
| 296 | 0 | for (String storyPath : storyPaths) { |
| 297 | 0 | reporters.put(storyPath, build(storyPath)); |
| 298 | |
} |
| 299 | 0 | reporters.put("*", build("*")); |
| 300 | 0 | return reporters; |
| 301 | |
} |
| 302 | |
|
| 303 | |
public StoryReporter reporterFor(String storyPath, Format format) { |
| 304 | 0 | FilePrintStreamFactory factory = filePrintStreamFactory(storyPath); |
| 305 | 0 | return format.realFormat.createStoryReporter(factory, this); |
| 306 | |
} |
| 307 | |
|
| 308 | |
public StoryReporter reporterFor(String storyPath, org.jbehave.core.reporters.Format format) { |
| 309 | 26 | FilePrintStreamFactory factory = filePrintStreamFactory(storyPath); |
| 310 | 26 | return format.createStoryReporter(factory, this); |
| 311 | |
} |
| 312 | |
|
| 313 | |
protected FilePrintStreamFactory filePrintStreamFactory(String storyPath) { |
| 314 | 43 | return new FilePrintStreamFactory(new StoryLocation(codeLocation, storyPath), fileConfiguration("")); |
| 315 | |
} |
| 316 | |
|
| 317 | |
public FileConfiguration fileConfiguration(String extension) { |
| 318 | 67 | return new FileConfiguration(relativeDirectory, extension, pathResolver); |
| 319 | |
} |
| 320 | |
|
| 321 | |
} |