KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > common > JacORBTestSuite


1 package org.jacorb.test.common;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 2005 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301, USA.
22  */

23
24 import java.lang.reflect.*;
25
26 import junit.framework.*;
27 import junit.extensions.*;
28
29 /**
30  * A special TestSuite that accepts only Tests that are applicable
31  * to a given client and server version.
32  *
33  * @author Andre Spiegel spiegel@gnu.org
34  * @version $Id: JacORBTestSuite.java,v 1.2 2005/05/16 11:39:27 andre.spiegel Exp $
35  */

36 public class JacORBTestSuite extends TestSuite implements JacORBTest
37 {
38     private TestAnnotations annotations = null;
39     
40     private String JavaDoc clientVersion = null;
41     private String JavaDoc serverVersion = null;
42
43     /**
44      * Constructs a JacORBTestSuite for a given name, using the
45      * annotatedClass to find test annotations. This constructor
46      * is for stand-alone use of this class, e.g. as part of a TestCase.
47      */

48     public JacORBTestSuite (String JavaDoc name,
49                             Class JavaDoc annotatedClass)
50     {
51         super (name);
52         clientVersion = System.getProperty ("jacorb.test.client.version", "cvs");
53         serverVersion = System.getProperty ("jacorb.test.server.version", "cvs");
54         if (!clientVersion.equals("cvs") || !serverVersion.equals("cvs"))
55         {
56             annotations = TestAnnotations.forClass (annotatedClass);
57         }
58     }
59                             
60     /**
61      * Constructs a JacORBTestSuite for the given name. This constructor
62      * may only be used by subclasses, because the test annotations will
63      * be parsed from the defining class.
64      */

65     protected JacORBTestSuite (String JavaDoc name)
66     {
67         super (name);
68         clientVersion = System.getProperty ("jacorb.test.client.version", "cvs");
69         serverVersion = System.getProperty ("jacorb.test.server.version", "cvs");
70         if (!clientVersion.equals("cvs") || !serverVersion.equals("cvs"))
71         {
72             annotations = TestAnnotations.forTestSuite (this);
73         }
74     }
75     
76     public boolean isApplicableTo (String JavaDoc clientVersion, String JavaDoc serverVersion)
77     {
78         if (annotations == null)
79             return true;
80         else
81         {
82             boolean result = annotations.isApplicableTo (clientVersion,
83                                                          serverVersion);
84             if (!result) System.out.println ("not applicable: " + getName());
85             return result;
86         }
87     }
88     
89     /**
90      * Adds a test to this suite, but if it's a JacORB test, it is
91      * only added if it is applicable to the currently tested client
92      * and server versions.
93      */

94     public void addTest (Test test)
95     {
96         if (clientVersion == null && serverVersion == null)
97         {
98             super.addTest (test);
99         }
100         else if (test instanceof JacORBTest)
101         {
102             if (((JacORBTest)test).isApplicableTo (clientVersion,
103                                                    serverVersion))
104                 super.addTest (test);
105         }
106         else if (test instanceof TestDecorator)
107         {
108             TestDecorator decorator = (TestDecorator)test;
109             Test t = decorator.getTest();
110             if (t instanceof JacORBTest)
111             {
112                 if (((JacORBTest)t).isApplicableTo (clientVersion,
113                                                     serverVersion))
114                     super.addTest (test);
115             }
116             else
117                 super.addTest (test);
118         }
119         else
120         {
121             super.addTest (test);
122         }
123     }
124     
125     /**
126      * Adds the TestSuite defined by class c to this JacORBTestSuite,
127      * but only if the TestSuite is applicable to the client and server
128      * version under test. The new TestSuite is computed by invoking the
129      * suite() method of class c -- this is different from what the superclass
130      * implementation of this method does, but it doesn't cause problems in
131      * practice. The advantage of invoking suite() here is that for
132      * non-applicable tests, the suite() method is never invoked and so the
133      * corresponding classes needn't be loaded.
134      */

135     public void addTestSuite (Class JavaDoc c)
136     {
137         if (JacORBTest.class.isAssignableFrom(c))
138         {
139             TestAnnotationsParser p = TestAnnotationsParser.getInstance(c);
140             TestAnnotations ta = p.getClassAnnotations();
141             if (ta == null || ta.isApplicableTo (clientVersion, serverVersion))
142             {
143                 TestSuite ts = invokeSuiteMethod (c);
144                 super.addTest (ts);
145             }
146         }
147         else
148         {
149             super.addTestSuite(c);
150         }
151     }
152
153     /**
154      * Invokes the static suite() method of the class c, and returns the
155      * result. If there is no suite() method, or the result of it is not
156      * a TestSuite, this method throws a RuntimeException.
157      */

158     private TestSuite invokeSuiteMethod (Class JavaDoc c)
159     {
160         try
161         {
162             Method suiteMethod = c.getDeclaredMethod ("suite",
163                                                       new Class JavaDoc[]{});
164             Object JavaDoc result = suiteMethod.invoke(null, new Object JavaDoc[]{});
165             return (TestSuite)result;
166         }
167         catch (Exception JavaDoc ex)
168         {
169             throw new RuntimeException JavaDoc (ex);
170         }
171     }
172     
173 }
174
Popular Tags