001: /*
002: * @(#)DelegateTestCreatorUTest.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.parser;
028:
029: import net.sourceforge.groboutils.junit.v1.iftc.*;
030: import java.lang.reflect.*;
031:
032: import org.easymock.EasyMock;
033: import org.easymock.MockControl;
034: import junit.framework.Test;
035: import junit.framework.TestCase;
036: import junit.framework.TestSuite;
037:
038: /**
039: * Tests the DelegateTestCreator class.
040: *
041: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
042: * @since November 4, 2002
043: * @version $Date: 2003/02/10 22:52:24 $
044: */
045: public class DelegateTestCreatorUTest extends TestCase {
046: //-------------------------------------------------------------------------
047: // Standard JUnit Class-specific declarations
048:
049: private static final Class THIS_CLASS = DelegateTestCreatorUTest.class;
050: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
051: .getLogger(THIS_CLASS);
052:
053: public DelegateTestCreatorUTest(String name) {
054: super (name);
055: }
056:
057: // mock object!
058: private static class MyTestCreator implements ITestCreator {
059: int canCreateCount = 0;
060: int createTestCount = 0;
061: boolean canCreate = false;
062: Test test = null;
063:
064: public Test createTest(Class theClass, Method method) {
065: ++this .createTestCount;
066: return this .test;
067: }
068:
069: public boolean canCreate(Class theClass) {
070: ++this .canCreateCount;
071: return this .canCreate;
072: }
073: }
074:
075: //-------------------------------------------------------------------------
076: // Tests
077:
078: public void testConstructor1() {
079: try {
080: new DelegateTestCreator(null);
081: fail("Did not throw IllegalArgumentException.");
082: } catch (IllegalArgumentException e) {
083: // test exception?
084: }
085: }
086:
087: public void testConstructor2() {
088: try {
089: new DelegateTestCreator(new ITestCreator[0]);
090: fail("Did not throw IllegalArgumentException.");
091: } catch (IllegalArgumentException e) {
092: // test exception?
093: }
094: }
095:
096: public void testCanCreate1() {
097: MyTestCreator tc1 = new MyTestCreator();
098: MyTestCreator tc2 = new MyTestCreator();
099: DelegateTestCreator dtc = new DelegateTestCreator(
100: new ITestCreator[] { tc1, tc2 });
101:
102: boolean res = dtc.canCreate(null);
103:
104: assertTrue("Did not return correct result.", !res);
105: assertEquals(
106: "Did not call canCreate correct number of times for first instance.",
107: 1, tc1.canCreateCount);
108: assertEquals(
109: "Did not call canCreate correct number of times for second instance.",
110: 1, tc2.canCreateCount);
111: }
112:
113: public void testCanCreate2() {
114: MyTestCreator tc1 = new MyTestCreator();
115: MyTestCreator tc2 = new MyTestCreator();
116: tc1.canCreate = true;
117: DelegateTestCreator dtc = new DelegateTestCreator(
118: new ITestCreator[] { tc1, tc2 });
119:
120: boolean res = dtc.canCreate(null);
121:
122: assertTrue("Did not return correct result.", res);
123:
124: // order of checks shouldn't be dictated here.
125: assertEquals(
126: "Did not call canCreate correct number of times for first instance.",
127: 1, tc1.canCreateCount);
128: assertEquals(
129: "Did not call canCreate correct number of times for second instance.",
130: 1, tc2.canCreateCount);
131: }
132:
133: public void testCanCreate3() {
134: MyTestCreator tc1 = new MyTestCreator();
135: MyTestCreator tc2 = new MyTestCreator();
136: tc2.canCreate = true;
137: DelegateTestCreator dtc = new DelegateTestCreator(
138: new ITestCreator[] { tc1, tc2 });
139:
140: boolean res = dtc.canCreate(null);
141:
142: assertTrue("Did not return correct result.", res);
143:
144: // order of checks shouldn't be dictated here.
145: assertEquals(
146: "Did not call canCreate correct number of times for first instance.",
147: 0, tc1.canCreateCount);
148: assertEquals(
149: "Did not call canCreate correct number of times for second instance.",
150: 1, tc2.canCreateCount);
151: }
152:
153: public void testCanCreate4() {
154: MyTestCreator tc1 = new MyTestCreator();
155: MyTestCreator tc2 = new MyTestCreator();
156: tc1.canCreate = true;
157: tc2.canCreate = true;
158: DelegateTestCreator dtc = new DelegateTestCreator(
159: new ITestCreator[] { tc1, tc2 });
160:
161: boolean res = dtc.canCreate(null);
162:
163: assertTrue("Did not return correct result.", res);
164:
165: // order of checks shouldn't be dictated here.
166: assertEquals(
167: "Did not call canCreate correct number of times for first instance.",
168: 0, tc1.canCreateCount);
169: assertEquals(
170: "Did not call canCreate correct number of times for second instance.",
171: 1, tc2.canCreateCount);
172: }
173:
174: public void testCanCreate5() {
175: MyTestCreator tc1 = new MyTestCreator();
176: DelegateTestCreator dtc = new DelegateTestCreator(
177: new ITestCreator[] { tc1 });
178:
179: boolean res = dtc.canCreate(null);
180:
181: assertTrue("Did not return correct result.", !res);
182: assertEquals(
183: "Did not call canCreate correct number of times for first instance.",
184: 1, tc1.canCreateCount);
185: }
186:
187: public void testCanCreate6() {
188: MyTestCreator tc1 = new MyTestCreator();
189: tc1.canCreate = true;
190: DelegateTestCreator dtc = new DelegateTestCreator(
191: new ITestCreator[] { tc1 });
192:
193: boolean res = dtc.canCreate(null);
194:
195: assertTrue("Did not return correct result.", res);
196: assertEquals(
197: "Did not call canCreate correct number of times for first instance.",
198: 1, tc1.canCreateCount);
199: }
200:
201: //-------------------------------------------------------------------------
202: // Standard JUnit declarations
203:
204: public static Test suite() {
205: InterfaceTestSuite suite = ITestCreatorUTestI.suite();
206:
207: // yes, this is an inner class inside an inner class!
208: // shudder - luckily, this is only for testing.
209: suite.addFactory(new CxFactory("A") {
210: public Object createImplObject() {
211: return new DelegateTestCreator(new ITestCreator[] {
212: new MyTestCreator(), new MyTestCreator() });
213: }
214: });
215: suite.addTestSuite(THIS_CLASS);
216:
217: return suite;
218: }
219:
220: public static void main(String[] args) {
221: String[] name = { THIS_CLASS.getName() };
222:
223: // junit.textui.TestRunner.main( name );
224: // junit.swingui.TestRunner.main( name );
225:
226: junit.textui.TestRunner.main(name);
227: }
228:
229: /**
230: *
231: * @exception Exception thrown under any exceptional condition.
232: */
233: protected void setUp() throws Exception {
234: super .setUp();
235:
236: // set ourself up
237: }
238:
239: /**
240: *
241: * @exception Exception thrown under any exceptional condition.
242: */
243: protected void tearDown() throws Exception {
244: // tear ourself down
245:
246: super.tearDown();
247: }
248: }
|