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