001    package com.mockrunner.base;
002    
003    import junit.framework.TestCase;
004    
005    import org.mockejb.jndi.MockContextFactory;
006    
007    import com.mockrunner.ejb.EJBTestModule;
008    import com.mockrunner.jdbc.JDBCTestModule;
009    import com.mockrunner.jms.JMSTestModule;
010    import com.mockrunner.mock.ejb.EJBMockObjectFactory;
011    import com.mockrunner.mock.jdbc.JDBCMockObjectFactory;
012    import com.mockrunner.mock.jms.JMSMockObjectFactory;
013    import com.mockrunner.mock.web.ActionMockObjectFactory;
014    import com.mockrunner.mock.web.WebMockObjectFactory;
015    import com.mockrunner.servlet.ServletTestModule;
016    import com.mockrunner.struts.ActionTestModule;
017    import com.mockrunner.tag.TagTestModule;
018    
019    /**
020     * Base class for all standard adapters. Not used for basic adapters.
021     */
022    public abstract class BaseTestCase extends TestCase
023    {
024        private WebMockObjectFactory webMockFactory;
025        private ActionMockObjectFactory actionMockFactory;
026        private JDBCMockObjectFactory jdbcMockFactory;
027        private EJBMockObjectFactory ejbMockFactory;
028        private JMSMockObjectFactory jmsMockFactory;
029        
030        public BaseTestCase()
031        {
032            
033        }
034    
035        public BaseTestCase(String arg0)
036        {
037            super(arg0);
038        }
039        
040        protected void tearDown() throws Exception
041        {
042            super.tearDown();
043            if(null != jdbcMockFactory)
044            {
045                jdbcMockFactory.restoreDrivers();
046                jdbcMockFactory = null;
047            }
048            MockContextFactory.revertSetAsInitial();
049            webMockFactory = null;
050            actionMockFactory = null;
051            ejbMockFactory = null;
052            jmsMockFactory  = null;
053        }
054    
055        /**
056         * Creates the mock object factories. If you
057         * overwrite this method, you must call 
058         * <code>super.setUp()</code>.
059         */
060        protected void setUp() throws Exception
061        {
062            super.setUp();
063        } 
064    
065        /**
066         * Creates a {@link WebMockObjectFactory}. 
067         * @return the created {@link WebMockObjectFactory}
068         */
069        protected WebMockObjectFactory createWebMockObjectFactory()
070        {
071            return new WebMockObjectFactory();
072        }
073    
074        /**
075         * Same as <code>createWebMockObjectFactory(otherFactory, true)</code>
076         */
077        protected WebMockObjectFactory createWebMockObjectFactory(WebMockObjectFactory otherFactory)
078        {
079            return new WebMockObjectFactory(otherFactory);
080        }
081        
082        /**
083         * Creates a {@link com.mockrunner.mock.web.WebMockObjectFactory} based on another one.
084         * The created {@link com.mockrunner.mock.web.WebMockObjectFactory} will have its own
085         * request and response objects. If you set <i>createNewSession</i>
086         * to <code>true</code> it will also have its own session object.
087         * The two factories will share one <code>ServletContext</code>.
088         * Especially important for multithreading tests.
089         * If you set <i>createNewSession</i> to false, the two factories
090         * will share one session. This setting simulates multiple requests
091         * from the same client.
092         * @param otherFactory the other factory
093         * @param createNewSession create a new session for the new factory
094         * @return the created {@link com.mockrunner.mock.web.WebMockObjectFactory}
095         */
096        protected WebMockObjectFactory createWebMockObjectFactory(WebMockObjectFactory otherFactory, boolean createNewSession)
097        {
098            return new WebMockObjectFactory(otherFactory, createNewSession);
099        }
100    
101        /**
102         * Gets the current {@link WebMockObjectFactory}.
103         * @return the {@link WebMockObjectFactory}
104         */
105        protected WebMockObjectFactory getWebMockObjectFactory()
106        {
107            synchronized(ActionMockObjectFactory.class) 
108            {
109                if(webMockFactory == null)
110                {
111                    webMockFactory = getActionMockObjectFactory();
112                }
113            }
114            return webMockFactory;
115        }
116        
117        /**
118         * Sets the current {@link WebMockObjectFactory}.
119         * @param mockFactory the {@link WebMockObjectFactory}
120         */
121        protected void setWebMockObjectFactory(WebMockObjectFactory mockFactory)
122        {
123            this.webMockFactory = mockFactory;
124        }
125        
126        /**
127         * Creates a {@link ActionMockObjectFactory}. 
128         * @return the created {@link ActionMockObjectFactory}
129         */
130        protected ActionMockObjectFactory createActionMockObjectFactory()
131        {
132            return new ActionMockObjectFactory();
133        }
134    
135        /**
136         * Same as <code>createActionMockObjectFactory(otherFactory, true)</code>
137         */
138        protected ActionMockObjectFactory createActionMockObjectFactory(WebMockObjectFactory otherFactory)
139        {
140            return new ActionMockObjectFactory(otherFactory);
141        }
142        
143        /**
144         * Creates a {@link com.mockrunner.mock.web.ActionMockObjectFactory} based on 
145         * another {@link com.mockrunner.mock.web.WebMockObjectFactory}.
146         * @param otherFactory the other factory
147         * @param createNewSession create a new session for the new factory
148         * @return the created {@link com.mockrunner.mock.web.ActionMockObjectFactory}
149         * @see #createWebMockObjectFactory(WebMockObjectFactory, boolean)
150         */
151        protected ActionMockObjectFactory createActionMockObjectFactory(WebMockObjectFactory otherFactory, boolean createNewSession)
152        {
153            return new ActionMockObjectFactory(otherFactory, createNewSession);
154        }
155    
156        /**
157         * Gets the current {@link ActionMockObjectFactory}.
158         * @return the {@link ActionMockObjectFactory}
159         */
160        protected ActionMockObjectFactory getActionMockObjectFactory()
161        {
162            synchronized(ActionMockObjectFactory.class) 
163            {
164                if(actionMockFactory == null)
165                {
166                    actionMockFactory = createActionMockObjectFactory();
167                }
168            }
169            return actionMockFactory;
170        }
171        
172        /**
173         * Sets the current {@link ActionMockObjectFactory}.
174         * @param mockFactory the {@link ActionMockObjectFactory}
175         */
176        protected void setActionMockObjectFactory(ActionMockObjectFactory mockFactory)
177        {
178            this.actionMockFactory = mockFactory;
179        }
180        
181        /**
182         * Creates a {@link JDBCMockObjectFactory}. 
183         * @return the created {@link JDBCMockObjectFactory}
184         */
185        protected JDBCMockObjectFactory createJDBCMockObjectFactory()
186        {
187            return new JDBCMockObjectFactory();
188        }
189        
190        /**
191         * Gets the current {@link JDBCMockObjectFactory}.
192         * @return the {@link JDBCMockObjectFactory}
193         */
194        protected JDBCMockObjectFactory getJDBCMockObjectFactory()
195        {
196            synchronized(JDBCMockObjectFactory.class) 
197            {
198                if(jdbcMockFactory == null)
199                {
200                    jdbcMockFactory = createJDBCMockObjectFactory();
201                }
202            }
203            return jdbcMockFactory;
204        }
205    
206        /**
207         * Sets the current {@link JDBCMockObjectFactory}.
208         * @param mockFactory the {@link JDBCMockObjectFactory}
209         */
210        protected void setJDBCMockObjectFactory(JDBCMockObjectFactory mockFactory)
211        {
212            this.jdbcMockFactory = mockFactory;
213        }
214        
215        /**
216         * Creates a {@link EJBMockObjectFactory}. 
217         * @return the created {@link EJBMockObjectFactory}
218         */
219        protected EJBMockObjectFactory createEJBMockObjectFactory()
220        {
221            return new EJBMockObjectFactory();
222        }
223    
224        /**
225         * Gets the current {@link EJBMockObjectFactory}.
226         * @return the {@link EJBMockObjectFactory}
227         */
228        protected EJBMockObjectFactory getEJBMockObjectFactory()
229        {
230            synchronized(EJBMockObjectFactory.class) 
231            {
232                if(ejbMockFactory == null)
233                {
234                    ejbMockFactory = createEJBMockObjectFactory();
235                }
236            }
237            return ejbMockFactory;
238        }
239    
240        /**
241         * Sets the current {@link JDBCMockObjectFactory}.
242         * @param mockFactory the {@link JDBCMockObjectFactory}
243         */
244        protected void setEJBMockObjectFactory(EJBMockObjectFactory mockFactory)
245        {
246            this.ejbMockFactory = mockFactory;
247        }
248        
249        /**
250         * Creates a {@link JMSMockObjectFactory}. 
251         * @return the created {@link JMSMockObjectFactory}
252         */
253        protected JMSMockObjectFactory createJMSMockObjectFactory()
254        {
255            return new JMSMockObjectFactory();
256        }
257    
258        /**
259         * Gets the current {@link JMSMockObjectFactory}.
260         * @return the {@link JMSMockObjectFactory}
261         */
262        protected JMSMockObjectFactory getJMSMockObjectFactory()
263        {
264            synchronized(JMSMockObjectFactory.class) 
265            {
266                if(jmsMockFactory == null)
267                {
268                    jmsMockFactory = createJMSMockObjectFactory();
269                }
270            }
271            return jmsMockFactory;
272        }
273    
274        /**
275         * Sets the current {@link JMSMockObjectFactory}.
276         * @param mockFactory the {@link JMSMockObjectFactory}
277         */
278        protected void setJMSMockObjectFactory(JMSMockObjectFactory mockFactory)
279        {
280            this.jmsMockFactory = mockFactory;
281        }
282        
283        /**
284         * Creates an {@link com.mockrunner.struts.ActionTestModule} with the specified
285         * {@link WebMockObjectFactory}.
286         * @param mockFactory the {@link ActionMockObjectFactory}
287         * @return the created {@link com.mockrunner.struts.ActionTestModule}
288         */
289        protected ActionTestModule createActionTestModule(ActionMockObjectFactory mockFactory)
290        {
291            return new ActionTestModule(mockFactory);
292        }
293        
294        /**
295         * Creates an {@link com.mockrunner.struts.ActionTestModule} based on the current
296         * {@link WebMockObjectFactory}.
297         * Same as <code>createActionTestModule(getActionMockObjectFactory())</code>.
298         * @return the created {@link com.mockrunner.struts.ActionTestModule}
299         */
300        protected ActionTestModule createActionTestModule()
301        {
302            return new ActionTestModule(getActionMockObjectFactory());
303        }
304        
305        /**
306         * Creates a {@link com.mockrunner.tag.TagTestModule} with the specified
307         * {@link WebMockObjectFactory}.
308         * @return the created {@link com.mockrunner.tag.TagTestModule}
309         */
310        protected TagTestModule createTagTestModule(WebMockObjectFactory mockFactory)
311        {
312            return new TagTestModule(mockFactory);
313        }
314        
315        /**
316         * Creates a {@link com.mockrunner.tag.TagTestModule} based on the current
317         * {@link WebMockObjectFactory}.
318         * Same as <code>createTagTestModule(getWebMockObjectFactory())</code>.
319         * @return the created {@link com.mockrunner.tag.TagTestModule}
320         */
321        protected TagTestModule createTagTestModule()
322        {
323            return new TagTestModule(getWebMockObjectFactory());
324        }
325        
326        /**
327         * Creates a {@link com.mockrunner.servlet.ServletTestModule} with the specified
328         * {@link WebMockObjectFactory}.
329         * @return the created {@link com.mockrunner.servlet.ServletTestModule}
330         */
331        protected ServletTestModule createServletTestModule(WebMockObjectFactory mockFactory)
332        {
333            return new ServletTestModule(mockFactory);
334        }
335    
336        /**
337         * Creates a {@link com.mockrunner.servlet.ServletTestModule} based on the current
338         * {@link WebMockObjectFactory}.
339         * Same as <code>createServletTestModule(getWebMockObjectFactory())</code>.
340         * @return the created {@link com.mockrunner.servlet.ServletTestModule}
341         */
342        protected ServletTestModule createServletTestModule()
343        {
344            return new ServletTestModule(getWebMockObjectFactory());
345        }
346        
347        /**
348         * Creates a {@link com.mockrunner.jdbc.JDBCTestModule} with the specified
349         * {@link JDBCMockObjectFactory}.
350         * @return the created {@link com.mockrunner.jdbc.JDBCTestModule}
351         */
352        protected JDBCTestModule createJDBCTestModule(JDBCMockObjectFactory mockFactory)
353        {
354            return new JDBCTestModule(mockFactory);
355        }
356    
357        /**
358         * Creates a {@link com.mockrunner.jdbc.JDBCTestModule} based on the current
359         * {@link JDBCMockObjectFactory}.
360         * Same as <code>createJDBCTestModule(getJDBCMockObjectFactory())</code>.
361         * @return the created {@link com.mockrunner.jdbc.JDBCTestModule}
362         */
363        protected JDBCTestModule createJDBCTestModule()
364        {
365            return new JDBCTestModule(getJDBCMockObjectFactory());
366        }
367        
368        /**
369         * Creates an {@link com.mockrunner.ejb.EJBTestModule} with the specified
370         * {@link EJBMockObjectFactory}.
371         * @return the created {@link com.mockrunner.ejb.EJBTestModule}
372         */
373        protected EJBTestModule createEJBTestModule(EJBMockObjectFactory mockFactory)
374        {
375            return new EJBTestModule(mockFactory);
376        }
377    
378        /**
379         * Creates an {@link com.mockrunner.ejb.EJBTestModule} based on the current
380         * {@link EJBMockObjectFactory}.
381         * Same as <code>createEJBTestModule(getEJBMockObjectFactory())</code>.
382         * @return the created {@link com.mockrunner.ejb.EJBTestModule}
383         */
384        protected EJBTestModule createEJBTestModule()
385        {
386            return new EJBTestModule(getEJBMockObjectFactory());
387        }
388        
389        /**
390         * Creates a {@link com.mockrunner.jms.JMSTestModule} with the specified
391         * {@link JMSMockObjectFactory}.
392         * @return the created {@link com.mockrunner.jms.JMSTestModule}
393         */
394        protected JMSTestModule createJMSTestModule(JMSMockObjectFactory mockFactory)
395        {
396            return new JMSTestModule(mockFactory);
397        }
398    
399        /**
400         * Creates a {@link com.mockrunner.jms.JMSTestModule} based on the current
401         * {@link JMSMockObjectFactory}.
402         * Same as <code>createJMSTestModule(getJMSMockObjectFactory())</code>.
403         * @return the created {@link com.mockrunner.jms.JMSTestModule}
404         */
405        protected JMSTestModule createJMSTestModule()
406        {
407            return new JMSTestModule(getJMSMockObjectFactory());
408        }
409    }
410