1 package org.starobjects.tested.fitnesse;
2
3 import java.util.Collection;
4 import java.util.Date;
5 import java.util.Map;
6
7 import org.nakedobjects.applib.fixtures.LogonFixture;
8 import org.nakedobjects.metamodel.adapter.NakedObject;
9 import org.nakedobjects.metamodel.adapter.oid.AggregatedOid;
10 import org.nakedobjects.metamodel.config.ConfigurationBuilder;
11 import org.nakedobjects.runtime.context.NakedObjectsContext;
12 import org.nakedobjects.runtime.installers.InstallerLookup;
13 import org.nakedobjects.runtime.persistence.PersistenceSession;
14 import org.nakedobjects.runtime.persistence.adaptermanager.AdapterManager;
15 import org.nakedobjects.runtime.system.DeploymentType;
16 import org.nakedobjects.runtime.system.NakedObjectsSystem;
17 import org.nakedobjects.runtime.transaction.NakedObjectTransactionManager;
18 import org.starobjects.tested.fitnesse.internal.AliasesRegistry;
19 import org.starobjects.tested.fitnesse.internal.JavaUtilDateParser;
20 import org.starobjects.tested.fitnesse.internal.ServiceRegistry;
21 import org.starobjects.tested.fitnesse.internal.fixtures.AliasItemsInList;
22 import org.starobjects.tested.fitnesse.internal.fixtures.AliasServices;
23 import org.starobjects.tested.fitnesse.internal.fixtures.CheckList;
24 import org.starobjects.tested.fitnesse.internal.fixtures.CheckSpecificationsLoaded;
25 import org.starobjects.tested.fitnesse.internal.fixtures.DebugClock;
26 import org.starobjects.tested.fitnesse.internal.fixtures.DebugObjectStore;
27 import org.starobjects.tested.fitnesse.internal.fixtures.DebugServices;
28 import org.starobjects.tested.fitnesse.internal.fixtures.SetUpObjects;
29 import org.starobjects.tested.fitnesse.internal.fixtures.UsingNakedObjectsViewer;
30 import org.starobjects.tested.fitnesse.internal.fixtures.UsingNakedObjectsViewer.Mode;
31 import org.starobjects.tested.fitnesse.internal.fixtures.perform.PerformOwner;
32 import org.starobjects.tested.fitnesse.internal.helpers.InitNakedObjects;
33 import org.starobjects.tested.fitnesse.internal.helpers.OpenSession;
34 import org.starobjects.tested.fitnesse.internal.helpers.SetClock;
35 import org.starobjects.tested.fitnesse.internal.helpers.ShutdownNakedObjects;
36 import org.starobjects.tested.fitnesse.internal.helpers.StartClient;
37 import org.starobjects.tested.fitnesse.internal.util.FitnesseUtil;
38 import org.starobjects.tested.fitnesse.internal.util.StringUtil;
39
40 import fit.Fixture;
41 import fit.Parse;
42 import fit.exception.FitFailureException;
43 import fitlibrary.DoFixture;
44
45 public class StoryFixture extends DoFixture implements PerformOwner {
46
47 private AliasesRegistry aliasesRegistry = new AliasesRegistry();
48 private final ServiceRegistry serviceRegistry = new ServiceRegistry();
49
50 private String configDirectory;
51
52 private NakedObjectsSystem nakedObjectsSystem;
53 private InstallerLookup installerLookup;
54 @SuppressWarnings("unused")
55 private ConfigurationBuilder configurationBuilder;
56
57 private boolean exploration;
58
59 public StoryFixture() {
60 registerParseDelegate(java.util.Date.class, new JavaUtilDateParser());
61 }
62
63 public void setConfigDirectory(final String configDirectory) {
64 this.configDirectory = configDirectory;
65 }
66
67 public void enableExploration() {
68 this.exploration = true;
69 }
70
71 public void initNakedObjects() {
72 new InitNakedObjects(this).initialize();
73 }
74
75
76
77
78
79
80
81
82 public void logonAs(final String userName) {
83 switchUser(userName);
84 }
85
86
87
88
89
90
91
92
93 public void logonAsWithRoles(final String userName, final String roleList) {
94 switchUserWithRoles(userName, roleList);
95 }
96
97
98
99
100 public void switchUser(final String userName) {
101 switchUserWithRoles(userName, null);
102 }
103
104
105
106
107 public void switchUserWithRoles(final String userName, final String roleList) {
108 new OpenSession(this).openSession(userName, StringUtil
109 .splitOnCommas(roleList));
110 aliasesRegistry = reAdapt(aliasesRegistry);
111 }
112
113 public Fixture aliasServices() {
114 return new AliasServices(this);
115 }
116
117 public void dateIsNow(final Date dateAndTime) {
118 dateIs(dateAndTime);
119 }
120
121 public void dateIs(final Date dateAndTime) {
122 timeIs(dateAndTime);
123 }
124
125 public void timeIsNow(final Date dateAndTime) {
126 timeIs(dateAndTime);
127 }
128
129 public void timeIs(final Date dateAndTime) {
130 new SetClock(this).setClock(dateAndTime);
131 }
132
133 public void shutdownNakedObjects() {
134 new ShutdownNakedObjects(this).shutdown();
135 }
136
137
138
139
140
141
142 public Fixture setUpObject(final String className) {
143 return setUpObjects(className);
144 }
145
146 public Fixture setUpObjects(final String className) {
147 return new SetUpObjects(this, className, SetUpObjects.Mode.PERSIST);
148 }
149
150
151
152
153
154
155 public Fixture setUpTransientObject(final String className) {
156 return setUpTransientObjects(className);
157 }
158
159 public Fixture setUpTransientObjects(final String className) {
160 return new SetUpObjects(this, className,
161 SetUpObjects.Mode.DO_NOT_PERSIST);
162 }
163
164 public void runViewer() {
165 new StartClient(this).run();
166 }
167
168 public Fixture usingNakedObjectsViewer() {
169 return usingNakedObjectsViewer(Mode.TEST);
170 }
171
172
173
174
175 public Fixture usingNakedObjectsViewerForSetup() {
176 return usingNakedObjectsViewerForSetUp();
177 }
178
179 public Fixture usingNakedObjectsViewerForSetUp() {
180 return usingNakedObjectsViewer(Mode.SETUP);
181 }
182
183
184
185
186 public Fixture usingNakedObjectsViewer(final Mode mode) {
187 serviceRegistry.registerServiceAliasAsIfRequired(aliasesRegistry);
188 return new UsingNakedObjectsViewer(this, mode);
189 }
190
191 public Fixture checkListContains(final String listAlias) {
192 return new CheckList(this, listAlias, CheckList.CheckMode.NOT_EXACT);
193 }
194
195 public Fixture checkListPreciselyContains(final String listAlias) {
196 return new CheckList(this, listAlias, CheckList.CheckMode.EXACT);
197 }
198
199
200
201
202 public Fixture checkListExactlyContains(final String listAlias) {
203 return checkListPreciselyContains(listAlias);
204 }
205
206 public Fixture aliasItemsInList(final String listAlias) {
207 return new AliasItemsInList(this, listAlias);
208 }
209
210
211
212
213
214 public String getConfigDirectory() {
215 return configDirectory;
216 }
217
218 public DeploymentType getDeploymentType() {
219 return exploration ? DeploymentType.EXPLORATION
220 : DeploymentType.PROTOTYPE;
221 }
222
223
224
225
226
227 public Fixture checkSpecificationsLoaded() {
228 return new CheckSpecificationsLoaded(this);
229 }
230
231 public Fixture debugServices() {
232 return new DebugServices(this);
233 }
234
235 public Fixture debugObjectStore() {
236 return new DebugObjectStore(this);
237 }
238
239 public Fixture debugClock() {
240 return new DebugClock(this);
241 }
242
243 public void aliasServiceAs(final String alias, final String serviceClassName) {
244 serviceRegistry.addService(alias, serviceClassName);
245 }
246
247 public Collection<String> getServiceClassNames() {
248 return serviceRegistry.getServiceClassNames();
249 }
250
251
252
253
254
255 public NakedObjectsSystem getSystem() {
256 return nakedObjectsSystem;
257 }
258
259
260
261
262 public void setNakedObjectsSystem(
263 final NakedObjectsSystem nakedObjectsSystem) {
264 this.nakedObjectsSystem = nakedObjectsSystem;
265 }
266
267 public InstallerLookup getInstallerLookup() {
268 return installerLookup;
269 }
270
271
272
273
274 public void setInstallerLookup(final InstallerLookup installerLookup) {
275 this.installerLookup = installerLookup;
276 }
277
278
279
280
281
282 public NakedObject getAliased(final String alias) {
283 return aliasesRegistry.getAliased(alias);
284 }
285
286 public String getAlias(final NakedObject adapter) {
287 return aliasesRegistry.getAlias(adapter);
288 }
289
290
291
292
293
294 public String aliasPrefixedAs(final String prefix, final NakedObject adapter) {
295 return aliasesRegistry.aliasPrefixedAs(prefix, adapter);
296 }
297
298
299
300
301 public void aliasAs(final String alias, final NakedObject adapter) {
302 aliasesRegistry.aliasAs(alias, adapter);
303 }
304
305
306
307
308 private AliasesRegistry reAdapt(final AliasesRegistry oldAliasesRegistry) {
309 final AliasesRegistry newAliasesRegistry = new AliasesRegistry();
310
311
312 for (final Map.Entry<String, NakedObject> aliasAdapter : oldAliasesRegistry) {
313 final String alias = aliasAdapter.getKey();
314 final NakedObject oldAdapter = aliasAdapter.getValue();
315
316 if (oldAdapter.getOid() instanceof AggregatedOid) {
317 continue;
318 }
319 newAliasesRegistry.put(alias, getAdapterManager().adapterFor(
320 oldAdapter.getObject()));
321 }
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349 return newAliasesRegistry;
350 }
351
352
353
354
355
356 public String throwException(final Parse cell, final String msg) {
357 FitnesseUtil.exception(this, cell, msg);
358 throw new FitFailureException(msg);
359 }
360
361 public String throwWrong(final Parse cell, final String msg) {
362 wrong(cell, msg);
363 throw new FitFailureException(msg);
364 }
365
366
367
368
369
370 public PersistenceSession getPersistenceSession() {
371 return NakedObjectsContext.getPersistenceSession();
372 }
373
374 private AdapterManager getAdapterManager() {
375 return getPersistenceSession().getAdapterManager();
376 }
377
378 public NakedObjectTransactionManager getTransactionManager() {
379 return NakedObjectsContext.getTransactionManager();
380 }
381
382 }