View Javadoc

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       * Logon, specifying no roles.
77       * <p>
78       * Unlike the {@link LogonFixture} on regular Naked Objects fixtures, the
79       * logonAs is not automatically remembered until the end of the setup. It
80       * should therefore be invoked at the end of setup explicitly.
81       */
82      public void logonAs(final String userName) {
83          switchUser(userName);
84      }
85  
86      /**
87       * Logon, specifying roles.
88       * <p>
89       * Unlike the {@link LogonFixture} on regular Naked Objects fixtures, the
90       * logonAs is not automatically remembered until the end of the setup. It
91       * should therefore be invoked at the end of setup explicitly.
92       */
93      public void logonAsWithRoles(final String userName, final String roleList) {
94          switchUserWithRoles(userName, roleList);
95      }
96  
97      /**
98       * Switch user, specifying no roles.
99       */
100     public void switchUser(final String userName) {
101         switchUserWithRoles(userName, null);
102     }
103 
104     /**
105      * Switch user, specifying roles.
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      * Allow for singular form.
139      * 
140      * @see #setUpObjects(String)
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      * Allow for singular form.
152      * 
153      * @see #setUpTransientObjects(String)
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      * Allow for mis-spellings.
174      */
175     public Fixture usingNakedObjectsViewerForSetup() {
176         return usingNakedObjectsViewerForSetUp();
177     }
178 
179     public Fixture usingNakedObjectsViewerForSetUp() {
180         return usingNakedObjectsViewer(Mode.SETUP);
181     }
182 
183     /**
184      * Registers any aliases for services first.
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      * @see #checkListPreciselyContains(String)
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     // ConfigDir, DeploymentType
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     // Diagnostics
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     // System
253     // /////////////////////////////////////////////////////////
254 
255     public NakedObjectsSystem getSystem() {
256         return nakedObjectsSystem;
257     }
258 
259     /**
260      * Called by {@link InitNakedObjects}.
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      * Called by {@link InitNakedObjects}.
273      */
274     public void setInstallerLookup(final InstallerLookup installerLookup) {
275         this.installerLookup = installerLookup;
276     }
277 
278     // /////////////////////////////////////////////////////////
279     // Aliasing
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      * Holds a new {@link NakedObject adapter}, automatically assigning it a new
292      * heldAs alias.
293      */
294     public String aliasPrefixedAs(final String prefix, final NakedObject adapter) {
295         return aliasesRegistry.aliasPrefixedAs(prefix, adapter);
296     }
297 
298     /**
299      * Holds a new {@link NakedObject adapter}.
300      */
301     public void aliasAs(final String alias, final NakedObject adapter) {
302         aliasesRegistry.aliasAs(alias, adapter);
303     }
304 
305     /**
306      * Need to recreate aliases whenever logout/login.
307      */
308     private AliasesRegistry reAdapt(final AliasesRegistry oldAliasesRegistry) {
309         final AliasesRegistry newAliasesRegistry = new AliasesRegistry();
310 
311         // first pass: root adapters
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         // for now, not supporting aggregated adapters (difficulty in looking up
324         // the parent adapter
325         // because the Oid changes)
326 
327         // // second pass: aggregated adapters
328         // for (Map.Entry<String,NakedObject> aliasAdapter : oldAliasesRegistry)
329         // {
330         // final String alias = aliasAdapter.getKey();
331         // final NakedObject oldAdapter = aliasAdapter.getValue();
332         //			
333         // if(!(oldAdapter.getOid() instanceof AggregatedOid)) {
334         // continue;
335         // }
336         // AggregatedOid aggregatedOid = (AggregatedOid) oldAdapter.getOid();
337         // final Object parentOid = aggregatedOid.getParentOid();
338         // final NakedObject parentAdapter =
339         // getAdapterManager().getAdapterFor(parentOid);
340         // final String fieldName = aggregatedOid.getFieldName();
341         // final NakedObjectAssociation association =
342         // parentAdapter.getSpecification().getAssociation(fieldName);
343         // final NakedObject newAdapter =
344         // getAdapterManager().adapterFor(oldAdapter.getObject(), parentAdapter,
345         // association);
346         //			
347         // newAliasesRegistry.put(alias, newAdapter);
348         // }
349         return newAliasesRegistry;
350     }
351 
352     // /////////////////////////////////////////////////////////
353     // throwXxx
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     // Dependencies (from context)
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 }