Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/phoenixst/junit/CloneableTestCase.java


1   /*
2    *  $Id: CloneableTestCase.java,v 1.1 2003/10/08 20:26:43 rconner Exp $
3    *
4    *  Copyright (c) 2003, Ray A. Conner
5    *  All rights reserved.
6    *
7    *  Redistribution and use in source and binary forms, with or without
8    *  modification, are permitted provided that the following conditions
9    *  are met:
10   *
11   *      * Redistributions of source code must retain the above
12   *        copyright notice, this list of conditions and the following
13   *        disclaimer.
14   *
15   *      * Redistributions in binary form must reproduce the above
16   *        copyright notice, this list of conditions and the following
17   *        disclaimer in the documentation and/or other materials
18   *        provided with the distribution.
19   *
20   *      * Neither the name "Melange" nor the names of its contributors
21   *        may be used to endorse or promote products derived from this
22   *        software without specific prior written permission.
23   *
24   *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25   *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26   *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27   *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28   *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
29   *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30   *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31   *  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32   *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
33   *  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34   *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
35   *  THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36   *  SUCH DAMAGE.
37   */
38  
39  package com.phoenixst.junit;
40  
41  import java.lang.reflect.*;
42  
43  import junit.framework.*;
44  
45  
46  /**
47   *  A <code>TestCase</code> for which instances are
48   *  <code>Cloneable</code>.  This class has been copied from the test
49   *  code in the Melange project (http://melange.sourceforge.net).
50   *
51   *  @version    $Revision: 1.1 $
52   *  @author     Ray A. Conner
53   *
54   *  @since      1.0
55   */
56  public abstract class CloneableTestCase extends TestCase
57      implements Cloneable
58  {
59  
60      /**
61       *  Used by toString(), so that output for different suite
62       *  instances can be distinct.  Class is not sufficiently
63       *  unambiguous.
64       */
65      private String suiteName;
66  
67  
68      ////////////////////////////////////////
69      // Constructors
70      ////////////////////////////////////////
71  
72  
73      /**
74       *  Create a new <code>CloneableTestCase</code>.
75       */
76      public CloneableTestCase()
77      {
78          super();
79      }
80  
81  
82      /**
83       *  Create a new <code>CloneableTestCase</code> with the specified
84       *  method name.
85       */
86      public CloneableTestCase( String name )
87      {
88          super( name );
89      }
90  
91  
92      ////////////////////////////////////////
93      // Clone operation
94      ////////////////////////////////////////
95  
96  
97      public Object clone()
98          throws CloneNotSupportedException
99      {
100         return super.clone();
101     }
102 
103 
104     ////////////////////////////////////////
105     // Test suite creation
106     ////////////////////////////////////////
107 
108 
109     /**
110      *  Returns whether or not the specified method is a test method.
111      */
112     private static final boolean isTestMethod( Method method )
113     {
114         return Modifier.isPublic( method.getModifiers() )
115             && method.getName().startsWith( "test" )
116             && method.getParameterTypes().length == 0
117             && method.getReturnType().equals( Void.TYPE );
118     }
119 
120 
121     /**
122      *  Create a new <code>TestSuite</code> by cloning a new instance
123      *  of this object for each test method.
124      */
125     public TestSuite getInstanceSuite()
126     {
127         return getInstanceSuite( null );
128     }
129 
130 
131     /**
132      *  Create a new <code>TestSuite</code> with the specified name by
133      *  cloning a new instance of this object for each test method.
134      */
135     public TestSuite getInstanceSuite( String name )
136     {
137         TestSuite suite = new TestSuite( name );
138         String suiteName = suite.toString();
139         Method[] methods = getClass().getMethods();
140         for( int i = 0; i < methods.length; i++ ) {
141             if( isTestMethod( methods[i] ) ) {
142                 try {
143                     CloneableTestCase test = (CloneableTestCase) clone();
144                     test.setName( methods[i].getName() );
145                     test.suiteName = suiteName;
146                     suite.addTest( test );
147                 } catch( CloneNotSupportedException e ) {
148                     // just return an empty suite
149                 }
150             }
151         }
152         return suite;
153     }
154 
155 
156     ////////////////////////////////////////
157     // toString()
158     ////////////////////////////////////////
159 
160 
161     public String toString()
162     {
163         if( suiteName != null ) {
164             return suiteName + ":" + super.toString();
165         } else {
166             return super.toString();
167         }
168     }
169 
170 }