1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.directory.server.core.integ;
20
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import org.apache.directory.server.core.integ.annotations.ApplyLdifFiles;
27 import org.apache.directory.server.core.integ.annotations.ApplyLdifs;
28 import org.apache.directory.server.core.integ.annotations.CleanupLevel;
29 import org.apache.directory.server.core.integ.annotations.Factory;
30 import org.apache.directory.server.core.integ.annotations.Mode;
31
32
33 import org.junit.runner.Description;
34
35
36
37
38
39
40
41
42 public class InheritableSettings
43 {
44
45 public static final SetupMode DEFAULT_MODE = SetupMode.ROLLBACK;
46
47
48 public static final DirectoryServiceFactory DEFAULT_FACTORY = DirectoryServiceFactory.DEFAULT;
49
50
51 private final InheritableSettings parent;
52
53
54 private final Description description;
55
56
57 private static final Level DEFAULT_CLEANUP_LEVEL = Level.SUITE;
58
59
60
61
62
63
64
65 public InheritableSettings( Description description )
66 {
67 this.description = description;
68 this.parent = null;
69 }
70
71
72
73
74
75
76
77
78
79 public InheritableSettings( Description description, InheritableSettings parent )
80 {
81 this.description = description;
82 this.parent = parent;
83
84 if ( description.isSuite() && ! isSuiteLevel() )
85 {
86 throw new IllegalStateException( String.format( "The parent must be null for %s suite",
87 description.getDisplayName() ) );
88 }
89 }
90
91
92
93
94
95 public Description getDescription()
96 {
97 return description;
98 }
99
100
101
102
103
104 public InheritableSettings getParent()
105 {
106 return parent;
107 }
108
109
110
111
112
113 public boolean isSuiteLevel()
114 {
115 return parent == null;
116 }
117
118
119
120
121
122 public boolean isClassLevel()
123 {
124 return ( parent != null ) && ( parent.getParent() == null );
125 }
126
127
128
129
130
131 public boolean isMethodLevel()
132 {
133 return ( parent != null ) && ( parent.getParent() != null );
134 }
135
136
137
138
139
140 public SetupMode getMode()
141 {
142 SetupMode parentMode = DEFAULT_MODE;
143
144 if ( parent != null )
145 {
146 parentMode = parent.getMode();
147 }
148
149
150 Mode annotation = description.getAnnotation( Mode.class );
151
152 if ( annotation == null )
153 {
154 return parentMode;
155 }
156 else
157 {
158 return annotation.value();
159 }
160 }
161
162
163
164
165
166
167
168 public DirectoryServiceFactory getFactory() throws IllegalAccessException, InstantiationException
169 {
170 DirectoryServiceFactory parentFactory = DEFAULT_FACTORY;
171
172 if ( parent != null )
173 {
174 parentFactory = parent.getFactory();
175 }
176
177 Factory annotation = description.getAnnotation( Factory.class );
178
179 if ( annotation == null )
180 {
181 return parentFactory;
182 }
183 else
184 {
185 return ( DirectoryServiceFactory ) annotation.value().newInstance();
186 }
187 }
188
189
190
191
192
193
194
195
196 public List<String> getLdifs( List<String> ldifs )
197 {
198 if ( ldifs == null )
199 {
200 ldifs = new ArrayList<String>();
201 }
202
203 if ( parent != null )
204 {
205 parent.getLdifs( ldifs );
206 }
207
208 ApplyLdifs annotation = description.getAnnotation( ApplyLdifs.class );
209
210 if ( ( annotation != null ) && ( annotation.value() != null ) )
211 {
212 ldifs.addAll( Arrays.asList( annotation.value() ) );
213 }
214
215 return ldifs;
216 }
217
218
219
220
221
222
223
224
225 public List<String> getLdifFiles( List<String> ldifFiles )
226 {
227 if ( ldifFiles == null )
228 {
229 ldifFiles = new ArrayList<String>();
230 }
231
232 if ( parent != null )
233 {
234 parent.getLdifFiles( ldifFiles );
235 }
236
237 ApplyLdifFiles annotation = description.getAnnotation( ApplyLdifFiles.class );
238
239 if ( annotation != null && annotation.value() != null )
240 {
241 ldifFiles.addAll( Arrays.asList( annotation.value() ) );
242 }
243
244 return ldifFiles;
245 }
246
247
248
249
250
251 public Level getCleanupLevel()
252 {
253 Level parentCleanupLevel = DEFAULT_CLEANUP_LEVEL;
254
255 if ( parent != null )
256 {
257 parentCleanupLevel = parent.getCleanupLevel();
258 }
259
260 CleanupLevel annotation = description.getAnnotation( CleanupLevel.class );
261
262 if ( annotation == null )
263 {
264 return parentCleanupLevel;
265 }
266 else
267 {
268 return annotation.value();
269 }
270 }
271 }