Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConfigurableEmbedder |
|
| 1.3636363636363635;1.364 |
1 | package org.jbehave.core; | |
2 | ||
3 | import static java.util.Arrays.asList; | |
4 | ||
5 | import java.util.ArrayList; | |
6 | import java.util.List; | |
7 | ||
8 | import org.jbehave.core.configuration.Configuration; | |
9 | import org.jbehave.core.embedder.Embedder; | |
10 | import org.jbehave.core.junit.JUnitStories; | |
11 | import org.jbehave.core.junit.JUnitStory; | |
12 | import org.jbehave.core.steps.CandidateSteps; | |
13 | import org.jbehave.core.steps.InjectableStepsFactory; | |
14 | ||
15 | /** | |
16 | * <p> | |
17 | * Abstract implementation of {@link Embeddable} which allows to configure the | |
18 | * {@link Embedder} used to run the stories, using the {@link Configuration} and | |
19 | * the {@link InjectableStepsFactory} specified. | |
20 | * </p> | |
21 | * <p> | |
22 | * The instances of the {@link Configuration} and the | |
23 | * {@link InjectableStepsFactory} can be provided either via the | |
24 | * {@link #useConfiguration(Configuration)} and | |
25 | * {@link #useStepsFactory(InjectableStepsFactory)} methods or overriding the | |
26 | * {@link #configuration()} and {@link #stepsFactory()} methods. | |
27 | * </p> | |
28 | * <p> | |
29 | * If overriding the {@link #configuration()} method and providing an | |
30 | * {@link InjectableStepsFactory} which requires a {@link Configuration}, then | |
31 | * care must be taken to avoid re-instantiating the {@link Configuration}. E.g.: | |
32 | * | |
33 | * <pre> | |
34 | * {@code | |
35 | * public Configuration configuration() { | |
36 | * if ( super.hasConfiguration() ){ | |
37 | * return super.configuration(); | |
38 | * } | |
39 | * return new MostUsefulConfiguration()...; | |
40 | * } | |
41 | * </pre> | |
42 | * | |
43 | * </p> | |
44 | * <p> | |
45 | * Note that no defaults are provided by this implementation. If no values are | |
46 | * set by the subclasses, then <code>null</code> values are passed to the | |
47 | * configured {@link Embedder}, which is responsible for setting the default | |
48 | * values. | |
49 | * </p> | |
50 | * <p> | |
51 | * Typically, users that use JUnit will find it easier to extend other | |
52 | * implementations, such as {@link JUnitStory} or {@link JUnitStories}, which | |
53 | * implement the {@link#run()} using the configured {@link Embedder} and | |
54 | * annotate it with JUnit's annotations. | |
55 | * </p> | |
56 | */ | |
57 | 16 | public abstract class ConfigurableEmbedder implements Embeddable { |
58 | ||
59 | 16 | private Embedder embedder = new Embedder(); |
60 | private Configuration configuration; | |
61 | private InjectableStepsFactory stepsFactory; | |
62 | private List<CandidateSteps> candidateSteps; | |
63 | ||
64 | public void useEmbedder(Embedder embedder) { | |
65 | 14 | this.embedder = embedder; |
66 | 14 | } |
67 | ||
68 | public void useConfiguration(Configuration configuration) { | |
69 | 7 | this.configuration = configuration; |
70 | 7 | } |
71 | ||
72 | public Configuration configuration() { | |
73 | 2 | return configuration; |
74 | } | |
75 | ||
76 | public boolean hasConfiguration() { | |
77 | 0 | return configuration != null; |
78 | } | |
79 | ||
80 | /** | |
81 | * @deprecated Use {@link #useStepsFactory(InjectableStepsFactory)} | |
82 | */ | |
83 | public void addSteps(CandidateSteps... steps) { | |
84 | 4 | addSteps(asList(steps)); |
85 | 4 | } |
86 | ||
87 | /** | |
88 | * @deprecated Use {@link #useStepsFactory(InjectableStepsFactory)} | |
89 | */ | |
90 | public void addSteps(List<CandidateSteps> steps) { | |
91 | 6 | if ( candidateSteps == null ){ |
92 | 6 | this.candidateSteps = new ArrayList<CandidateSteps>(); |
93 | } | |
94 | 6 | this.candidateSteps.addAll(steps); |
95 | 6 | } |
96 | ||
97 | /** | |
98 | * @deprecated Use {@link #stepsFactory()} | |
99 | */ | |
100 | public List<CandidateSteps> candidateSteps() { | |
101 | 5 | return candidateSteps; |
102 | } | |
103 | ||
104 | public void useStepsFactory(InjectableStepsFactory stepsFactory) { | |
105 | 2 | this.stepsFactory = stepsFactory; |
106 | 2 | } |
107 | ||
108 | public InjectableStepsFactory stepsFactory() { | |
109 | 4 | return stepsFactory; |
110 | } | |
111 | ||
112 | public boolean hasStepsFactory() { | |
113 | 0 | return stepsFactory != null; |
114 | } | |
115 | ||
116 | public Embedder configuredEmbedder() { | |
117 | 11 | if (configuration == null) { |
118 | 3 | configuration = configuration(); |
119 | } | |
120 | 11 | embedder.useConfiguration(configuration); |
121 | 11 | if (candidateSteps == null) { |
122 | 5 | candidateSteps = candidateSteps(); |
123 | } | |
124 | 11 | embedder.useCandidateSteps(candidateSteps); |
125 | 11 | if (stepsFactory == null) { |
126 | 7 | stepsFactory = stepsFactory(); |
127 | } | |
128 | 11 | embedder.useStepsFactory(stepsFactory); |
129 | 11 | return embedder; |
130 | } | |
131 | ||
132 | } |