1 | |
package org.jbehave.core.junit; |
2 | |
|
3 | |
import java.util.List; |
4 | |
|
5 | |
import org.jbehave.core.annotations.UsingPaths; |
6 | |
import org.jbehave.core.configuration.AnnotationBuilder; |
7 | |
import org.jbehave.core.embedder.Embedder; |
8 | |
import org.jbehave.core.embedder.EmbedderMonitor; |
9 | |
import org.jbehave.core.embedder.EmbedderMonitorDecorator; |
10 | |
import org.jbehave.core.io.StoryNameResolver; |
11 | |
import org.jbehave.core.io.UnderscoredToCapitalized; |
12 | |
import org.junit.runner.Description; |
13 | |
import org.junit.runner.Runner; |
14 | |
import org.junit.runner.notification.Failure; |
15 | |
import org.junit.runner.notification.RunNotifier; |
16 | |
import org.junit.runners.model.InitializationError; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | 0 | public class AnnotatedPathRunner extends AnnotatedEmbedderRunner { |
26 | |
|
27 | |
private final AnnotationBuilder annotationBuilder; |
28 | |
private final StoryNameResolver nameResolver; |
29 | |
private final List<String> paths; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public AnnotatedPathRunner(Class<?> annotatedClass) throws InitializationError { |
38 | 0 | super(annotatedClass); |
39 | 0 | this.annotationBuilder = annotationBuilder(); |
40 | 0 | this.nameResolver = storyNameResolver(); |
41 | 0 | this.paths = annotationBuilder.findPaths(); |
42 | 0 | } |
43 | |
|
44 | |
protected StoryNameResolver storyNameResolver() { |
45 | 0 | return new UnderscoredToCapitalized(); |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public Description getDescription() { |
50 | 0 | Description description = Description.createSuiteDescription(testClass()); |
51 | 0 | for (String path : paths) |
52 | 0 | description.addChild(createDescriptionForPath(path)); |
53 | |
|
54 | 0 | return description; |
55 | |
} |
56 | |
|
57 | |
private Description createDescriptionForPath(String path) { |
58 | 0 | String name = nameResolver.resolveName(path); |
59 | 0 | return Description.createTestDescription(testClass(), name); |
60 | |
} |
61 | |
|
62 | |
@Override |
63 | |
protected void collectInitializationErrors(List<Throwable> errors) { |
64 | |
|
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
protected void validateInstanceMethods(List<Throwable> errors) { |
69 | |
|
70 | 0 | } |
71 | |
|
72 | |
@Override |
73 | |
public void run(RunNotifier notifier) { |
74 | 0 | Embedder embedder = annotationBuilder.buildEmbedder(); |
75 | 0 | NotifierEmbedderMonitor notifierEmbedderMonitor = new NotifierEmbedderMonitor(embedder.embedderMonitor(), |
76 | |
notifier); |
77 | 0 | embedder.useEmbedderMonitor(notifierEmbedderMonitor); |
78 | |
|
79 | |
try { |
80 | 0 | embedder.runStoriesAsPaths(paths); |
81 | |
} finally { |
82 | 0 | notifierEmbedderMonitor.storyFinished(); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | 0 | private final class NotifierEmbedderMonitor extends EmbedderMonitorDecorator { |
91 | |
private final RunNotifier notifier; |
92 | |
private Description currentStory; |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 0 | private NotifierEmbedderMonitor(EmbedderMonitor delegate, RunNotifier notifier) { |
101 | 0 | super(delegate); |
102 | 0 | this.notifier = notifier; |
103 | 0 | } |
104 | |
|
105 | |
@Override |
106 | |
public void runningStory(String path) { |
107 | 0 | super.runningStory(path); |
108 | 0 | storyFinished(); |
109 | 0 | currentStory = createDescriptionForPath(path); |
110 | 0 | notifier.fireTestStarted(currentStory); |
111 | 0 | } |
112 | |
|
113 | |
@Override |
114 | |
public void storyFailed(String path, Throwable cause) { |
115 | 0 | super.storyFailed(path, cause); |
116 | 0 | notifier.fireTestFailure(new Failure(currentStory, cause)); |
117 | 0 | notifier.fireTestFinished(currentStory); |
118 | 0 | currentStory = null; |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
private void storyFinished() { |
125 | 0 | if (currentStory == null) { |
126 | 0 | return; |
127 | |
} |
128 | 0 | notifier.fireTestFinished(currentStory); |
129 | 0 | } |
130 | |
} |
131 | |
} |