KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > junit > v1 > iftc > CxFactory


1 /*
2  * @(#)CxFactory.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.iftc;
28
29
30 /**
31  * Helper abstract class that aids in the setting of a unique and
32  * distinguishable name for a test case's factory.
33  * <P>
34  * As of 08-Dec-2002, the original constructor will NOT add the owning
35  * class's name to the factory <tt>toString()</tt> output. Since the
36  * Ant JUnit report is setup such that the concrete class's tests are
37  * organized under it, this is redundant information, and clutters the
38  * report.
39  *
40  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
41  * @version $Date: 2003/05/24 16:42:13 $
42  * @since October 30, 2002
43  */

44 public abstract class CxFactory implements ICxFactory
45 {
46     private String JavaDoc id;
47     
48     /**
49      * Specify a unique identifier for this specific factory within the
50      * context of the concrete test that is providing the factory. The
51      * <tt>CxFactory</tt> constructor will not add the test's class name to
52      * this string to create a distinguishable string to help debug the
53      * source of any errors caused by a factory's particular setup. If you
54      * want the owning class's name to appear in the id, use the alternate
55      * constructor.
56      *
57      * @param name the unique identifier within the context of the
58      * factory's owning test class. This cannot be <tt>null</tt>.
59      * @exception IllegalArgumentException thrown if <tt>name</tt>
60      * is <tt>null</tt>.
61      */

62     public CxFactory( String JavaDoc name )
63     {
64         this( name, false );
65     }
66     
67     
68     /**
69      * Specify a unique identifier for this specific factory within the
70      * context of the concrete test that is providing the factory. The
71      * <tt>CxFactory</tt> constructor may add the test's class name to
72      * this string to create a distinguishable string to help debug the
73      * source of any errors caused by a factory's particular setup,
74      * depending on the value of <tt>addClassName</tt>.
75      *
76      * @param name the unique identifier within the context of the
77      * factory's owning test class. This cannot be <tt>null</tt>.
78      * @param addClassName <tt>true</tt> if the owning class's name should
79      * be added to the id, and <tt>false</tt> if the name should be
80      * the ID itself.
81      * @exception IllegalArgumentException thrown if <tt>name</tt>
82      * is <tt>null</tt>.
83      * @since 07-Dec-2002
84      */

85     public CxFactory( String JavaDoc name, boolean addClassName )
86     {
87         if (name == null)
88         {
89             throw new IllegalArgumentException JavaDoc(
90                 "factory name cannot be null." );
91         }
92         
93         // If this factory is within another class (i.e. an inner class
94
// or an anonymous class), then the owning class's name will be
95
// used instead, which is normally the test class's name.
96
if (addClassName)
97         {
98             Class JavaDoc c = this.getClass();
99             String JavaDoc className = getOwningClassName( c );
100             this.id = className + "-" + name;
101         }
102         else
103         {
104             this.id = name;
105         }
106         
107         /* post condition - this can never happen.
108         Commented out for coverage numberes.
109         if (this.id == null)
110         {
111             throw new IllegalStateException(
112                 "internal coding error: generated ID was null." );
113         }
114         */

115     }
116     
117     
118     /**
119      * Override the Java-default toString, and provide our distinguishable
120      * name.
121      *
122      * @return the generated distinguishable name.
123      */

124     public String JavaDoc toString()
125     {
126         /* Pre-condition - this should never happen.
127         Commented out for coverage numberes.
128         if (this.id == null)
129         {
130             throw new IllegalStateException(
131                 "The internal id is null. "+
132                 "An internal API coding mistake was made." );
133         }
134         */

135         
136         return this.id;
137     }
138     
139     
140     /**
141      * Most factories have no need for a tearDown method, so a default
142      * (do-nothing) implementation has been provided here.
143      */

144     public void tearDown( Object JavaDoc implObject ) throws Exception JavaDoc
145     {
146         // do nothing
147
}
148     
149     
150     // inherited from ImplFactory
151
public abstract Object JavaDoc createImplObject() throws Exception JavaDoc;
152     
153     
154     /**
155      * Find the owning class name for the given class. For inner classes,
156      * this will return the parent class.
157      *
158      * @param c the class to find the owner
159      * @return the owning class' name.
160      */

161     private String JavaDoc getOwningClassName( Class JavaDoc c )
162     {
163         if (c == null)
164         {
165             throw new IllegalArgumentException JavaDoc( "No null args." );
166         }
167         
168         Class JavaDoc lastOwner = c;
169         /*
170         This part does not work as expected: see the corresponding enemy
171         unit test for reasons.
172         Class owner = c.getDeclaringClass();
173         while (owner != null)
174         {
175             lastOwner = owner;
176             owner = owner.getDeclaringClass();
177         }
178         */

179         String JavaDoc className = lastOwner.getName();
180         
181         int pos = className.lastIndexOf( '$' );
182         if (pos > 0)
183         {
184             className = className.substring( 0, pos );
185         }
186         pos = className.lastIndexOf( '.' );
187         if (pos > 0)
188         {
189             className = className.substring( pos+1 );
190         }
191         return className;
192     }
193 }
194
195
Popular Tags