1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.test;
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Properties;
25  import java.util.Map.Entry;
26  
27  import junit.framework.Test;
28  import junit.framework.TestResult;
29  import junit.framework.TestSuite;
30  
31  import org.apache.commons.configuration.Configuration;
32  import org.apache.commons.configuration.PropertiesConfiguration;
33  import org.apache.jetspeed.Jetspeed;
34  import org.apache.jetspeed.PortalTestConstants;
35  import org.apache.jetspeed.engine.Engine;
36  import org.apache.jetspeed.engine.JetspeedEngineConstants;
37  import org.apache.jetspeed.exception.JetspeedException;
38  import org.apache.jetspeed.testhelpers.SpringEngineHelper;
39  
40  import com.mockrunner.mock.web.MockServletConfig;
41  import com.mockrunner.mock.web.MockServletContext;
42  
43  /***
44   * <p>
45   * JetspeedTestSuite
46   * </p>
47   * 
48   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
49   * @version $Id: JetspeedTestSuite.java 517719 2007-03-13 15:05:48Z ate $
50   *
51   */
52  public class JetspeedTestSuite extends TestSuite
53  {
54      protected static Engine engine = null;
55      private static SpringEngineHelper engineHelper;    
56  
57      /***
58       * 
59       */
60      public JetspeedTestSuite()
61      {
62          super();
63  		startEngine(getApplicationRoot(), getPropertiesFile());
64          
65      }
66  
67      /***
68       * @param arg0
69       * @param arg1
70       */
71      public JetspeedTestSuite(Class arg0, String arg1)
72      {
73          super(arg0, arg1);
74  		startEngine(getApplicationRoot(), getPropertiesFile());
75          
76      }
77  
78      /***
79       * @param arg0
80       */
81      public JetspeedTestSuite(Class arg0)
82      {
83          super(arg0);
84  		startEngine(getApplicationRoot(), getPropertiesFile());
85          
86      }
87  
88      /***
89       * @param arg0
90       */
91      public JetspeedTestSuite(String arg0)
92      {
93          super(arg0);
94  		startEngine(getApplicationRoot(), getPropertiesFile());
95          
96      }
97  
98      protected static void startEngine(String applicationRoot, String propertiesFilename)
99      {
100         try
101         {
102             if (engine != null)
103             {
104                 return;
105             }
106             
107             Configuration properties = new PropertiesConfiguration(propertiesFilename);
108 
109             properties.setProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, applicationRoot);
110             //properties.setProperty(WEBAPP_ROOT_KEY, null);
111             initializeConfiguration(properties, applicationRoot);
112             //Mock servletConfigMock = new Mock(ServletConfig.class);
113             MockServletConfig msc = new MockServletConfig();
114             msc.setServletContext(new MockServletContext());
115             HashMap context = new HashMap();
116             engineHelper = new SpringEngineHelper(context);
117             engineHelper.setUp();
118             engine = (Engine) context.get(SpringEngineHelper.ENGINE_ATTR);
119 
120         }
121         catch (Exception e)
122         {
123             e.printStackTrace();
124 
125         }
126     }
127     protected static void stopEngine()
128     {
129         try
130         {
131             if (engine != null)
132             {
133                 Jetspeed.shutdown();
134             }
135         }
136         catch (JetspeedException e)
137         {
138             e.printStackTrace();
139         }
140         finally
141         {
142             engine = null;
143         }
144     }
145 
146     /***
147      * Override to set your own application root
148      *
149      */
150     public String getApplicationRoot()
151     {
152         String applicationRoot = System.getProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, PortalTestConstants.PORTAL_WEBAPP_PATH);
153         return applicationRoot;
154     }
155 
156     /***
157      * Override to set your own properties file
158      *
159      */
160     public String getPropertiesFile()
161     {
162         String jetspeedProperties = System.getProperty(JetspeedEngineConstants.APPLICATION_ROOT_KEY, PortalTestConstants.PORTAL_WEBAPP_PATH) + "/WEB-INF/conf/jetspeed.properties";        
163         return jetspeedProperties;
164     }
165 
166     /*
167      * Implement this method to override any properties in your TestSuite.
168      * If you override this method in a derived class, call super.overrideProperties to get these settings
169      * 
170      * @param properties The base configuration properties for the Jetspeed system.
171      */
172     protected static void initializeConfiguration(Configuration properties, String appRoot)
173     {
174         String testPropsPath = appRoot + "/WEB-INF/conf/test/jetspeed.properties";
175         try
176         {
177             File testFile = new File(testPropsPath);
178             if (testFile.exists())
179             {
180                 FileInputStream is = new FileInputStream(testPropsPath);
181                 Properties props = new Properties();
182                 props.load(is);
183 
184                 Iterator it = props.entrySet().iterator();
185                 while (it.hasNext())
186                 {
187                     Entry entry = (Entry) it.next();
188                     //if (entry.getValue() != null && ((String)entry.getValue()).length() > 0)
189                     properties.setProperty((String) entry.getKey(), (String) entry.getValue());
190                 }
191             }
192         }
193         catch (IOException e)
194         {
195             e.printStackTrace();
196         }
197     }
198 
199     /***
200      * @see junit.framework.Test#run(junit.framework.TestResult)
201      */
202     public void run(TestResult arg0)
203     {
204         try
205         {            
206             super.run(arg0);
207         }
208         finally
209         {
210             stopEngine();
211         }
212     }
213 
214     /***
215      * @see junit.framework.TestSuite#runTest(junit.framework.Test, junit.framework.TestResult)
216      */
217     public void runTest(Test arg0, TestResult arg1)
218     {
219         if(arg0 instanceof JetspeedTest)
220         {
221         	JetspeedTest jtest = (JetspeedTest) arg0;
222         	jtest.engine = engine;
223         	jtest.jsuite = this;
224         }
225         super.runTest(arg0, arg1);
226     }
227 
228 }