001: /*
002: * @(#)DelegateTestCreator.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 java.lang.reflect.Method;
030: import java.lang.reflect.InvocationTargetException;
031:
032: import junit.framework.Test;
033:
034: import org.apache.log4j.Logger;
035:
036: /**
037: * Allows for an ordered set of TestCreator instances to be queried for
038: * generating instances.
039: *
040: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
041: * @version $Date: 2003/02/10 22:52:20 $
042: * @since November 4, 2002
043: */
044: public class DelegateTestCreator implements ITestCreator {
045: private static final Logger LOG = Logger
046: .getLogger(DelegateTestCreator.class);
047:
048: private ITestCreator[] creators;
049:
050: /**
051: * Create the delegation with an ordered array of creators. The
052: * creators are searched from index 0 to the last index for a valid
053: * creator.
054: */
055: public DelegateTestCreator(ITestCreator[] tc) {
056: if (tc == null || tc.length <= 0) {
057: throw new IllegalArgumentException("no null args");
058: }
059:
060: int len = tc.length;
061: this .creators = new ITestCreator[len];
062: for (int i = len; --i >= 0;) {
063: if (tc[i] == null) {
064: throw new IllegalArgumentException("no null args");
065: }
066: this .creators[i] = tc[i];
067: }
068: }
069:
070: /**
071: * Checks if the creator can be used on the given class.
072: *
073: * @param theClass the class to check if parsing is acceptable.
074: * @return whether the creator can generate a test based on
075: * <tt>theClass</tt>.
076: */
077: public boolean canCreate(Class theClass) {
078: // order doesn't matter at this point
079: for (int i = this .creators.length; --i >= 0;) {
080: if (this .creators[i].canCreate(theClass)) {
081: return true;
082: }
083: }
084: return false;
085: }
086:
087: /**
088: * Creates a new test, based on the given class and method of the
089: * class.
090: *
091: * @param theClass the class to parse for testing.
092: * @param m the method that will be tested with the new class instance.
093: * @exception InstantiationException if there was a problem creating
094: * the class.
095: * @exception NoSuchMethodException if the method does not exist in the
096: * class.
097: */
098: public Test createTest(Class theClass, Method method)
099: throws InstantiationException, NoSuchMethodException,
100: InvocationTargetException, IllegalAccessException,
101: ClassCastException {
102: // order matters here.
103: for (int i = 0; i < this .creators.length; ++i) {
104: ITestCreator tc = this .creators[i];
105: try {
106: if (tc.canCreate(theClass)) {
107: Test t = tc.createTest(theClass, method);
108: if (t != null) {
109: return t;
110: }
111: }
112: } catch (InstantiationException e) {
113: LOG.info("Failed to create test with creator " + tc
114: + ".", e);
115: } catch (NoSuchMethodException e) {
116: LOG.info("Failed to create test with creator " + tc
117: + ".", e);
118: } catch (InvocationTargetException e) {
119: LOG.info("Failed to create test with creator " + tc
120: + ".", e);
121: } catch (IllegalAccessException e) {
122: LOG.info("Failed to create test with creator " + tc
123: + ".", e);
124: } catch (ClassCastException e) {
125: LOG.info("Failed to create test with creator " + tc
126: + ".", e);
127: }
128: }
129:
130: // did not find a valid test creator.
131: return null;
132: }
133: }
|