2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   package org.apache.cactus.internal.server;
2   /* 
3    * ========================================================================
4    * 
5    * Licensed to the Apache Software Foundation (ASF) under one or more
6    * contributor license agreements.  See the NOTICE file distributed with
7    * this work for additional information regarding copyright ownership.
8    * The ASF licenses this file to You under the Apache License, Version 2.0
9    * (the "License"); you may not use this file except in compliance with
10   * the License.  You may obtain a copy of the License at
11   * 
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   * 
20   * ========================================================================
21   */
22  import java.lang.reflect.Constructor;
23  import org.apache.cactus.internal.AbstractCactusTestCase;
24  import org.apache.cactus.internal.EJBTestResult;
25  import org.apache.cactus.internal.HttpServiceDefinition;
26  import org.apache.cactus.internal.util.ClassLoaderUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  
30  /**
31   * Prototype of EJBRedirector for Cactus.
32   * @author Siddhartha P. Chandurkar (siddhartha@visioncodified.com)
33   */
34  public abstract class AbstractEJBTestCaller 
35  {
36  
37      /**
38       * The logger.
39       */
40      private static final Log LOGGER = 
41          LogFactory.getLog(AbstractWebTestCaller.class);
42      
43      /**
44       * The ejb implicitObjects instance.
45       */
46      protected EJBImplicitObjects objects;
47  
48      /**
49       * A constructor for the class.
50       * @param theObjects
51       */
52      public AbstractEJBTestCaller(EJBImplicitObjects theObjects) 
53      {
54          this.objects = theObjects;
55      }
56      
57      /**
58       * Setter method to set the test-case fields.
59       * @param theTestCase
60       * @throws Exception in case an error occurs.
61       */
62      protected abstract void setTestCaseFields(AbstractCactusTestCase 
63              theTestCase) throws Exception;
64  
65      /**
66       * The "main" method of the test-case.
67       * @throws Exception in case an error occurs
68       */
69      public void doTest() throws Exception 
70      {
71          EJBTestResult result = null;
72  
73          try 
74          {
75  
76              // Create an instance of the test class
77              AbstractCactusTestCase testInstance =
78                  getTestClassInstance(getTestClassName(), getTestMethodName());
79              
80              LOGGER.debug("CLASS NAME " + getTestClassName());
81              LOGGER.debug("METHOD NAME " + getTestMethodName());
82              // Set its fields (implicit objects)
83              setTestCaseFields(testInstance);
84  
85              // Call it's method corresponding to the current test case
86              testInstance.runBareServer();
87  
88              // Return an instance of <code>WebTestResult</code> with a
89              // positive result.
90              result = new EJBTestResult();
91  
92          } 
93          catch (Throwable e) 
94          {
95              // An error occurred, return an instance of
96              // <code>WebTestResult</code> with an exception.
97              result = new EJBTestResult(e);
98          }
99          LOGGER.info("***********************************************");
100         LOGGER.info("Test result : [" + result + "]");
101         LOGGER.info("***********************************************");
102     }
103     
104     /**
105      * Getter method to return the name of the class being tested.
106      * @return
107      */
108     protected String getTestClassName() 
109     {
110         return this.objects.getEJBRequest().getClassName(
111             HttpServiceDefinition.CLASS_NAME_PARAM);
112     }
113 
114     /**
115      * Getter method to return the name of the test-method being executed.
116      * @return
117      * @throws Exception in case an error occurs
118      */
119     protected String getTestMethodName() throws Exception 
120     {
121         return this.objects.getEJBRequest().getMethodName(
122             HttpServiceDefinition.METHOD_NAME_PARAM);
123     }
124 
125     /**
126      * A method to return an instance of the test-class.
127      * @param theClassName
128      * @param theTestCaseName
129      * @return
130      * @throws Exception in case an error occurs
131      */
132     protected AbstractCactusTestCase getTestClassInstance(
133         String theClassName,
134         String theTestCaseName)
135         throws Exception 
136     {
137         // Get the class to call and build an instance of it.
138         Class testClass = getTestClassClass(theClassName);
139         AbstractCactusTestCase testInstance = null;
140         try 
141         {
142             Constructor constructor =
143                 testClass.getConstructor(new Class[] { String.class });
144             testInstance =
145                 (AbstractCactusTestCase) constructor.newInstance(
146                     new Object[] { theTestCaseName });
147         } 
148         catch (Exception e) 
149         {
150             throw new Exception(e);
151         }
152 
153         return testInstance;
154     }
155 
156     /**
157      * @param theClassName the name of the test class
158      * @return the class object the test class to call
159      * @exception Exception in case an error occurs
160      */
161     protected Class getTestClassClass(String theClassName) throws Exception 
162     {
163         // Get the class to call and build an instance of it.
164         Class testClass = null;
165         try 
166         {
167             testClass =
168                 ClassLoaderUtils.loadClass(theClassName, this.getClass());
169         } 
170         catch (Exception e) 
171         {
172             throw new Exception(e);
173         }
174 
175         return testClass;
176     }
177 }