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