001: /*
002: * @(#)AutoDocJDK13UTest.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.autodoc.v1;
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:
035: import net.sourceforge.groboutils.autodoc.v1.spi.*;
036: import net.sourceforge.groboutils.autodoc.v1.defimpl.*;
037: import java.util.*;
038:
039: /**
040: * Tests the AutoDoc class.
041: *
042: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
043: * @version $Date: 2003/02/10 22:52:14 $
044: * @since March 21, 2002
045: */
046: public class AutoDocJDK13UTest extends TestCase {
047: //-------------------------------------------------------------------------
048: // Standard JUnit Class-specific declarations
049:
050: private static final Class THIS_CLASS = AutoDocJDK13UTest.class;
051:
052: public AutoDocJDK13UTest(String name) {
053: super (name);
054: }
055:
056: //-------------------------------------------------------------------------
057: // setup
058:
059: private MockControl factoryControl;
060: private AutoDocFactory mockFactory;
061:
062: private MockControl logControl;
063: private AutoDocLog mockLog;
064:
065: private MockControl itControl;
066: private AutoDocIT mockIT;
067:
068: private MockControl tpControl;
069: private AutoDocTP mockTP;
070:
071: /**
072: *
073: * @exception Exception thrown under any exceptional condition.
074: */
075: protected void setUp() throws Exception {
076: super .setUp();
077:
078: // set ourself up
079: this .factoryControl = EasyMock.controlFor(AutoDocFactory.class);
080: this .mockFactory = (AutoDocFactory) this .factoryControl
081: .getMock();
082:
083: this .logControl = EasyMock.controlFor(AutoDocLog.class);
084: this .mockLog = (AutoDocLog) this .logControl.getMock();
085:
086: this .itControl = EasyMock.controlFor(AutoDocIT.class);
087: this .mockIT = (AutoDocIT) this .itControl.getMock();
088:
089: this .tpControl = EasyMock.controlFor(AutoDocTP.class);
090: this .mockTP = (AutoDocTP) this .tpControl.getMock();
091: }
092:
093: //-------------------------------------------------------------------------
094: // Tests
095:
096: public void testConstructor1() {
097: new AutoDoc(this .getClass());
098: }
099:
100: public void testConstructor2() {
101: try {
102: new AutoDoc(null);
103: fail("AutoDoc constructor did not throw IllegalArgumentException.");
104: } catch (IllegalArgumentException e) {
105: // test exception?
106: }
107: }
108:
109: /*
110: public void testFactoryUse1()
111: {
112: this.mockFactory.createLog( this.getClass() );
113: this.factoryControl.setReturnValue( this.mockLog, 1 );
114:
115: this.mockFactory.createIT( this.getClass() );
116: this.factoryControl.setReturnValue( this.mockIT, 1 );
117:
118: this.mockFactory.createTP( this.getClass() );
119: this.factoryControl.setReturnValue( this.mockTP, 1 );
120:
121: this.factoryControl.activate();
122: this.logControl.activate();
123: this.itControl.activate();
124: this.tpControl.activate();
125:
126: MockAutoDoc mad = new MockAutoDoc( this.getClass(),
127: this.mockFactory );
128:
129: assertIsOrContains(
130: "Did not use specified log factory.",
131: this.mockLog,
132: mad.getLog() );
133: assertIsOrContains(
134: "Did not use specified log factory.",
135: this.mockLog,
136: mad.getLog() );
137: assertIsOrContains(
138: "Did not use specified it factory.",
139: this.mockIT,
140: mad.getIT() );
141: assertIsOrContains(
142: "Did not use specified it factory.",
143: this.mockIT,
144: mad.getIT() );
145: assertIsOrContains(
146: "Did not use specified tp factory.",
147: this.mockTP,
148: mad.getTP() );
149: assertIsOrContains(
150: "Did not use specified tp factory.",
151: this.mockTP,
152: mad.getTP() );
153:
154:
155: this.factoryControl.verify();
156: this.logControl.verify();
157: this.itControl.verify();
158: this.tpControl.verify();
159: mad.mockVerify();
160: }
161: */
162:
163: public void testFactoryStore1() {
164: assertNotNull("SingletonStore cannot be null.", AutoDoc
165: .getFactoryStore());
166: }
167:
168: public void testGetFactory1() {
169: Enumeration origEnum = AutoDoc.getFactoryStore()
170: .getSingletons();
171: assertNotNull("Static set of factories returned null.",
172: origEnum);
173:
174: AutoDoc ad = new AutoDoc(THIS_CLASS);
175: AutoDocFactory[] adf = ad.getFactories();
176: assertNotNull("AutoDoc instance returned null factory.", adf);
177:
178: int foundCount = 0;
179: while (origEnum.hasMoreElements()) {
180: AutoDocFactory orig = (AutoDocFactory) origEnum
181: .nextElement();
182: assertNotNull("Static set of factories contains null.",
183: orig);
184: boolean found = false;
185: for (int i = 0; i < adf.length; ++i) {
186: if (orig == adf[i]) {
187: ++foundCount;
188: // null it out so we don't accidentily count it again
189: adf[i] = null;
190: found = true;
191: break;
192: }
193: }
194: assertTrue("Did not find static factory " + orig
195: + " in AutoDoc instance.", found);
196: }
197: assertEquals(
198: "Static set of factories contained fewer elements than "
199: + "AutDoc instance.", foundCount, adf.length);
200: }
201:
202: public void testAddFactory1() {
203: AutoDoc ad = new AutoDoc(THIS_CLASS);
204: ad.addFactory(this .mockFactory);
205: AutoDocFactory[] adf = ad.getFactories();
206: assertNotNull("AutoDoc instance returned null factory.", adf);
207: boolean found = false;
208: for (int i = 0; i < adf.length; ++i) {
209: if (adf[i] == this .mockFactory) {
210: found = true;
211: break;
212: }
213: }
214: assertTrue(
215: "Default autodoc must contain the exact factory added.",
216: found);
217: }
218:
219: public void testOwner1() {
220: AutoDoc ad = new AutoDoc(THIS_CLASS);
221: assertEquals("Did not store class correctly.", THIS_CLASS, ad
222: .getOwner());
223: }
224:
225: //-------------------------------------------------------------------------
226: // Helpers
227:
228: protected void assertIsOrContains( String msg, Object orig, Object ret )
229: {
230: String rmsg = msg+": original=<"+orig+">, found=<"+ret+">";
231: if (orig == null)
232: {
233: assertNull( rmsg, ret );
234: }
235: if (orig == ret
236: || orig.equals( ret ))
237: {
238: return;
239: }
240:
241: if (ret instanceof IAutoDocSet)
242: {
243: Enumeration enum = ((IAutoDocSet)ret).getSetContents();
244: while (enum.hasMoreElements())
245: {
246: Object next = enum.nextElement();
247: if (orig == next
248: || orig.equals( next ))
249: {
250: // found it - assert passed.
251: return;
252: }
253: }
254: // was not found
255: fail( rmsg );
256: }
257: else
258: {
259: fail( rmsg );
260: }
261: }
262:
263: //-------------------------------------------------------------------------
264: // Standard JUnit declarations
265:
266: public static Test suite() {
267: TestSuite suite = new TestSuite(THIS_CLASS);
268:
269: // Test the implementation's interface conformity.
270: /*
271: suite.addTest( IxUTestI.suite(
272: new ImplementationCreator[] {
273: new ImplementationCreator() {
274: public Object createImplementedObject() {
275: // XXXXXXXXXXXXXXXXXXXXXXXX
276: }
277: },
278: } ) );
279: */
280: return suite;
281: }
282:
283: public static void main(String[] args) {
284: String[] name = { THIS_CLASS.getName() };
285:
286: // junit.textui.TestRunner.main( name );
287: // junit.swingui.TestRunner.main( name );
288:
289: junit.textui.TestRunner.main(name);
290: }
291:
292: /**
293: *
294: * @exception Exception thrown under any exceptional condition.
295: */
296: protected void tearDown() throws Exception {
297: // tear ourself down
298:
299: super.tearDown();
300: }
301: }
|