KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > parser > DelegateTestCreator


1 /*
2  * @(#)DelegateTestCreator.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.junit.v1.parser;
28
29 import java.lang.reflect.Method JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31
32 import junit.framework.Test;
33
34 import org.apache.log4j.Logger;
35
36
37 /**
38  * Allows for an ordered set of TestCreator instances to be queried for
39  * generating instances.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2003/02/10 22:52:20 $
43  * @since November 4, 2002
44  */

45 public class DelegateTestCreator implements ITestCreator
46 {
47     private static final Logger LOG = Logger.getLogger(
48         DelegateTestCreator.class );
49     
50     private ITestCreator[] creators;
51     
52     
53     /**
54      * Create the delegation with an ordered array of creators. The
55      * creators are searched from index 0 to the last index for a valid
56      * creator.
57      */

58     public DelegateTestCreator( ITestCreator[] tc )
59     {
60         if (tc == null || tc.length <= 0)
61         {
62             throw new IllegalArgumentException JavaDoc("no null args");
63         }
64         
65         int len = tc.length;
66         this.creators = new ITestCreator[ len ];
67         for (int i = len; --i >= 0;)
68         {
69             if (tc[i] == null)
70             {
71                 throw new IllegalArgumentException JavaDoc("no null args");
72             }
73             this.creators[i] = tc[i];
74         }
75     }
76     
77     
78     /**
79      * Checks if the creator can be used on the given class.
80      *
81      * @param theClass the class to check if parsing is acceptable.
82      * @return whether the creator can generate a test based on
83      * <tt>theClass</tt>.
84      */

85     public boolean canCreate( Class JavaDoc theClass )
86     {
87         // order doesn't matter at this point
88
for (int i = this.creators.length; --i >= 0;)
89         {
90             if (this.creators[i].canCreate( theClass ))
91             {
92                 return true;
93             }
94         }
95         return false;
96     }
97     
98     
99     /**
100      * Creates a new test, based on the given class and method of the
101      * class.
102      *
103      * @param theClass the class to parse for testing.
104      * @param m the method that will be tested with the new class instance.
105      * @exception InstantiationException if there was a problem creating
106      * the class.
107      * @exception NoSuchMethodException if the method does not exist in the
108      * class.
109      */

110     public Test createTest( Class JavaDoc theClass, Method JavaDoc method )
111             throws InstantiationException JavaDoc, NoSuchMethodException JavaDoc,
112             InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
113             ClassCastException JavaDoc
114     {
115         // order matters here.
116
for (int i = 0; i < this.creators.length; ++i)
117         {
118             ITestCreator tc = this.creators[i];
119             try
120             {
121                 if (tc.canCreate( theClass ))
122                 {
123                     Test t = tc.createTest( theClass, method );
124                     if (t != null)
125                     {
126                         return t;
127                     }
128                 }
129             }
130             catch (InstantiationException JavaDoc e)
131             {
132                 LOG.info( "Failed to create test with creator "+tc+".", e );
133             }
134             catch (NoSuchMethodException JavaDoc e)
135             {
136                 LOG.info( "Failed to create test with creator "+tc+".", e );
137             }
138             catch (InvocationTargetException JavaDoc e)
139             {
140                 LOG.info( "Failed to create test with creator "+tc+".", e );
141             }
142             catch (IllegalAccessException JavaDoc e)
143             {
144                 LOG.info( "Failed to create test with creator "+tc+".", e );
145             }
146             catch (ClassCastException JavaDoc e)
147             {
148                 LOG.info( "Failed to create test with creator "+tc+".", e );
149             }
150         }
151         
152         // did not find a valid test creator.
153
return null;
154     }
155 }
156
157
Popular Tags