001: /*
002: * @(#)InterfaceTestCaseUTest.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.junit.v1.iftc;
028:
029: import org.easymock.EasyMock;
030: import org.easymock.MockControl;
031: import junit.framework.Test;
032: import junit.framework.TestCase;
033: import junit.framework.TestSuite;
034: import junit.framework.AssertionFailedError;
035:
036: /**
037: * Tests the InterfaceTestCase class.
038: *
039: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
040: * @since March 1, 2002
041: * @version $Date: 2003/05/29 13:05:54 $
042: */
043: public class InterfaceTestCaseUTest extends TestCase {
044:
045: //-------------------------------------------------------------------------
046: // Standard JUnit Class-specific declarations
047:
048: private static final Class THIS_CLASS = InterfaceTestCaseUTest.class;
049: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
050: .getLogger(InterfaceTestCaseUTest.class);
051:
052: public InterfaceTestCaseUTest(String name) {
053: super (name);
054: }
055:
056: //-------------------------------------------------------------------------
057: // Tests
058:
059: public static class MyInterfaceTestCase extends InterfaceTestCase {
060: public MyInterfaceTestCase(String name, Class interfaceClass,
061: ImplFactory f) {
062: super (name, interfaceClass, f);
063: }
064: }
065:
066: public void testConstructor1() {
067: LOG.debug("Entering testConstructor1");
068: ImplFactory f = new ImplFactory() {
069: public Object createImplObject() {
070: return new Runnable() {
071: public void run() {
072: }
073: };
074: }
075: };
076: InterfaceTestCase itc = new MyInterfaceTestCase("test",
077: Runnable.class, f);
078: assertEquals("Did not store the interface class correctly.",
079: Runnable.class, itc.getInterfaceClass());
080: LOG.debug("Leaving testConstructor1");
081: }
082:
083: public void testConstructor2() {
084: LOG.debug("Entering testConstructor2");
085: try {
086: new MyInterfaceTestCase("test", null, null);
087: fail("Did not throw IllegalArgumentException.");
088: } catch (IllegalArgumentException e) {
089: // test exception?
090: }
091: LOG.debug("Leaving testConstructor2");
092: }
093:
094: public void testConstructor3() {
095: LOG.debug("Entering testConstructor3");
096: try {
097: new MyInterfaceTestCase("test", Runnable.class, null);
098: fail("Did not throw IllegalArgumentException.");
099: } catch (IllegalArgumentException e) {
100: // test exception?
101: }
102: LOG.debug("Leaving testConstructor3");
103: }
104:
105: public void testConstructor4() {
106: LOG.debug("Entering testConstructor4");
107: ImplFactory f = new ImplFactory() {
108: public Object createImplObject() {
109: return new Runnable() {
110: public void run() {
111: }
112: };
113: }
114: };
115: try {
116: new MyInterfaceTestCase("test", null, f);
117: fail("Did not throw IllegalArgumentException.");
118: } catch (IllegalArgumentException e) {
119: // test exception?
120: }
121: LOG.debug("Leaving testConstructor4");
122: }
123:
124: public void testCreate1() {
125: LOG.debug("Entering testCreate1");
126: ImplFactory f = new ImplFactory() {
127: public Object createImplObject() {
128: return "a string";
129: }
130: };
131: InterfaceTestCase itc = new MyInterfaceTestCase("test",
132: Runnable.class, f);
133: try {
134: itc.createImplObject();
135: fail("Did not fail.");
136: } catch (AssertionFailedError e) {
137: // test error?
138: }
139: LOG.debug("Leaving testCreate1");
140: }
141:
142: public void testCreate2() {
143: LOG.debug("Entering testCreate2");
144: ImplFactory f = new ImplFactory() {
145: public Object createImplObject() {
146: return null;
147: }
148: };
149: InterfaceTestCase itc = new MyInterfaceTestCase("test",
150: Runnable.class, f);
151: try {
152: itc.createImplObject();
153: fail("Did not fail.");
154: } catch (AssertionFailedError e) {
155: // test error?
156: }
157: LOG.debug("Leaving testCreate2");
158: }
159:
160: public void testCreate3() {
161: LOG.debug("Entering testCreate3");
162: ImplFactory f = new ImplFactory() {
163: public Object createImplObject() {
164: throw new IllegalArgumentException("IAE");
165: }
166: };
167: InterfaceTestCase itc = new MyInterfaceTestCase("test",
168: Runnable.class, f);
169: try {
170: itc.createImplObject();
171: fail("Did not throw exception.");
172: } catch (AssertionFailedError e) {
173: // test error
174: assertTrue(
175: "Does not contain the correct exception text.",
176: e
177: .getMessage()
178: .indexOf(
179: " threw exception "
180: + "java.lang.IllegalArgumentException: IAE during "
181: + "creation:") > 0);
182:
183: }
184: }
185:
186: final String teststring = "a string";
187:
188: public void testCreate4() {
189: LOG.debug("Entering testCreate4");
190: ImplFactory f = new ImplFactory() {
191: public Object createImplObject() {
192: return teststring;
193: }
194: };
195: InterfaceTestCase itc = new MyInterfaceTestCase("test",
196: String.class, f);
197: Object o = itc.createImplObject();
198: assertSame(
199: "Did not return the exact object we thought we returned.",
200: teststring, o);
201: }
202:
203: final static String factoryname = "TestFactoryName";
204:
205: public void testToString1() {
206: LOG.debug("Entering testToString1");
207: ImplFactory f = new ImplFactory() {
208: public Object createImplObject() {
209: return "";
210: }
211:
212: public String toString() {
213: return factoryname;
214: }
215: };
216: InterfaceTestCase itc = new MyInterfaceTestCase("test",
217: String.class, f);
218: itc.setUseClassInName(false);
219: assertEquals("Did not return the correct text.", "test["
220: + factoryname + "](" + itc.getClass().getName() + ")",
221: itc.toString());
222: }
223:
224: public void testGetName1() {
225: LOG.debug("Entering testGetName1");
226: ImplFactory f = new ImplFactory() {
227: public Object createImplObject() {
228: return "";
229: }
230:
231: public String toString() {
232: return factoryname;
233: }
234: };
235: InterfaceTestCase itc = new MyInterfaceTestCase("test",
236: String.class, f);
237: itc.setUseClassInName(false);
238: assertEquals("Did not return the correct text.", "test["
239: + factoryname + "]", itc.getName());
240: }
241:
242: public void testGetName2() {
243: LOG.debug("Entering testGetName1");
244: ImplFactory f = new ImplFactory() {
245: public Object createImplObject() {
246: return "";
247: }
248:
249: public String toString() {
250: return factoryname;
251: }
252: };
253: InterfaceTestCase itc = new MyInterfaceTestCase("test",
254: String.class, f);
255: itc.setUseClassInName(true);
256: assertEquals("Did not return the correct text.",
257: "InterfaceTestCaseUTest$MyInterfaceTestCase.test["
258: + factoryname + "]", itc.getName());
259: }
260:
261: public void testName1() {
262: LOG.debug("Entering testName1");
263: ImplFactory f = new ImplFactory() {
264: public Object createImplObject() {
265: return "";
266: }
267:
268: public String toString() {
269: return factoryname;
270: }
271: };
272: InterfaceTestCase itc = new MyInterfaceTestCase("test",
273: String.class, f);
274: itc.setUseClassInName(false);
275: assertEquals("Did not return the correct text.", itc.getName(),
276: itc.name());
277: }
278:
279: //---------
280:
281: private static class MyCxFactory implements ICxFactory {
282: public int createCount = 0;
283: public int tearDownCount = 0;
284:
285: public Object createImplObject() {
286: ++this .createCount;
287: return new Integer(this .createCount);
288: }
289:
290: public void tearDown(Object o) {
291: int i = ((Integer) o).intValue();
292: assertEquals("Did not tear down in the right order.",
293: createCount - tearDownCount, i);
294: ++tearDownCount;
295: }
296: }
297:
298: public void testTearDown1() throws Exception {
299: LOG.debug("Entering testTearDown1()");
300: MyCxFactory f = new MyCxFactory();
301: InterfaceTestCase itc = new MyInterfaceTestCase("test",
302: Integer.class, f);
303: int instantCount = 100;
304: for (int i = 0; i < instantCount; ++i) {
305: itc.createImplObject();
306: }
307: itc.tearDown();
308: assertEquals(
309: "Did not tear down all expected instantiated objects.",
310: instantCount, f.tearDownCount);
311: }
312:
313: //---------
314:
315: private static class MyCxFactory2 implements ICxFactory {
316: public Object createImplObject() {
317: return new Integer(0);
318: }
319:
320: public void tearDown(Object o) throws Exception {
321: throw new IllegalStateException();
322: }
323: }
324:
325: public void testTearDown2() throws Exception {
326: LOG.debug("Entering testTearDown2()");
327: MyCxFactory2 f = new MyCxFactory2();
328: InterfaceTestCase itc = new MyInterfaceTestCase("test",
329: Integer.class, f);
330: int instantCount = 100;
331: for (int i = 0; i < instantCount; ++i) {
332: itc.createImplObject();
333: }
334: try {
335: itc.tearDown();
336: fail("tearDown did not propigate any exceptions to the top.");
337: } catch (AssertionFailedError ex) {
338: String s = ex.toString();
339: int count = -1;
340: int pos = 0;
341: while (pos >= 0) {
342: ++count;
343: pos = s.indexOf(IllegalStateException.class.getName(),
344: pos + 1);
345: }
346: assertEquals("Did not catch or report all exceptions.",
347: instantCount, count);
348: }
349: }
350:
351: //-------------------------------------------------------------------------
352: // Standard JUnit declarations
353:
354: public static Test suite() {
355: TestSuite suite = new TestSuite(THIS_CLASS);
356:
357: return suite;
358: }
359:
360: public static void main(String[] args) {
361: String[] name = { THIS_CLASS.getName() };
362:
363: // junit.textui.TestRunner.main( name );
364: // junit.swingui.TestRunner.main( name );
365:
366: junit.textui.TestRunner.main(name);
367: }
368:
369: /**
370: *
371: * @exception Exception thrown under any exceptional condition.
372: */
373: protected void setUp() throws Exception {
374: super .setUp();
375:
376: // set ourself up
377: }
378:
379: /**
380: *
381: * @exception Exception thrown under any exceptional condition.
382: */
383: protected void tearDown() throws Exception {
384: // tear ourself down
385:
386: super.tearDown();
387: }
388: }
|