001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.test;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Properties;
025: import java.util.Map.Entry;
026:
027: import junit.framework.Test;
028: import junit.framework.TestResult;
029: import junit.framework.TestSuite;
030:
031: import org.apache.commons.configuration.Configuration;
032: import org.apache.commons.configuration.PropertiesConfiguration;
033: import org.apache.jetspeed.Jetspeed;
034: import org.apache.jetspeed.PortalTestConstants;
035: import org.apache.jetspeed.engine.Engine;
036: import org.apache.jetspeed.engine.JetspeedEngineConstants;
037: import org.apache.jetspeed.exception.JetspeedException;
038: import org.apache.jetspeed.testhelpers.SpringEngineHelper;
039:
040: import com.mockrunner.mock.web.MockServletConfig;
041: import com.mockrunner.mock.web.MockServletContext;
042:
043: /**
044: * <p>
045: * JetspeedTestSuite
046: * </p>
047: *
048: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
049: * @version $Id: JetspeedTestSuite.java 517719 2007-03-13 15:05:48Z ate $
050: *
051: */
052: public class JetspeedTestSuite extends TestSuite {
053: protected static Engine engine = null;
054: private static SpringEngineHelper engineHelper;
055:
056: /**
057: *
058: */
059: public JetspeedTestSuite() {
060: super ();
061: startEngine(getApplicationRoot(), getPropertiesFile());
062:
063: }
064:
065: /**
066: * @param arg0
067: * @param arg1
068: */
069: public JetspeedTestSuite(Class arg0, String arg1) {
070: super (arg0, arg1);
071: startEngine(getApplicationRoot(), getPropertiesFile());
072:
073: }
074:
075: /**
076: * @param arg0
077: */
078: public JetspeedTestSuite(Class arg0) {
079: super (arg0);
080: startEngine(getApplicationRoot(), getPropertiesFile());
081:
082: }
083:
084: /**
085: * @param arg0
086: */
087: public JetspeedTestSuite(String arg0) {
088: super (arg0);
089: startEngine(getApplicationRoot(), getPropertiesFile());
090:
091: }
092:
093: protected static void startEngine(String applicationRoot,
094: String propertiesFilename) {
095: try {
096: if (engine != null) {
097: return;
098: }
099:
100: Configuration properties = new PropertiesConfiguration(
101: propertiesFilename);
102:
103: properties.setProperty(
104: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
105: applicationRoot);
106: //properties.setProperty(WEBAPP_ROOT_KEY, null);
107: initializeConfiguration(properties, applicationRoot);
108: //Mock servletConfigMock = new Mock(ServletConfig.class);
109: MockServletConfig msc = new MockServletConfig();
110: msc.setServletContext(new MockServletContext());
111: HashMap context = new HashMap();
112: engineHelper = new SpringEngineHelper(context);
113: engineHelper.setUp();
114: engine = (Engine) context
115: .get(SpringEngineHelper.ENGINE_ATTR);
116:
117: } catch (Exception e) {
118: e.printStackTrace();
119:
120: }
121: }
122:
123: protected static void stopEngine() {
124: try {
125: if (engine != null) {
126: Jetspeed.shutdown();
127: }
128: } catch (JetspeedException e) {
129: e.printStackTrace();
130: } finally {
131: engine = null;
132: }
133: }
134:
135: /**
136: * Override to set your own application root
137: *
138: */
139: public String getApplicationRoot() {
140: String applicationRoot = System.getProperty(
141: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
142: PortalTestConstants.PORTAL_WEBAPP_PATH);
143: return applicationRoot;
144: }
145:
146: /**
147: * Override to set your own properties file
148: *
149: */
150: public String getPropertiesFile() {
151: String jetspeedProperties = System.getProperty(
152: JetspeedEngineConstants.APPLICATION_ROOT_KEY,
153: PortalTestConstants.PORTAL_WEBAPP_PATH)
154: + "/WEB-INF/conf/jetspeed.properties";
155: return jetspeedProperties;
156: }
157:
158: /*
159: * Implement this method to override any properties in your TestSuite.
160: * If you override this method in a derived class, call super.overrideProperties to get these settings
161: *
162: * @param properties The base configuration properties for the Jetspeed system.
163: */
164: protected static void initializeConfiguration(
165: Configuration properties, String appRoot) {
166: String testPropsPath = appRoot
167: + "/WEB-INF/conf/test/jetspeed.properties";
168: try {
169: File testFile = new File(testPropsPath);
170: if (testFile.exists()) {
171: FileInputStream is = new FileInputStream(testPropsPath);
172: Properties props = new Properties();
173: props.load(is);
174:
175: Iterator it = props.entrySet().iterator();
176: while (it.hasNext()) {
177: Entry entry = (Entry) it.next();
178: //if (entry.getValue() != null && ((String)entry.getValue()).length() > 0)
179: properties.setProperty((String) entry.getKey(),
180: (String) entry.getValue());
181: }
182: }
183: } catch (IOException e) {
184: e.printStackTrace();
185: }
186: }
187:
188: /**
189: * @see junit.framework.Test#run(junit.framework.TestResult)
190: */
191: public void run(TestResult arg0) {
192: try {
193: super .run(arg0);
194: } finally {
195: stopEngine();
196: }
197: }
198:
199: /**
200: * @see junit.framework.TestSuite#runTest(junit.framework.Test, junit.framework.TestResult)
201: */
202: public void runTest(Test arg0, TestResult arg1) {
203: if (arg0 instanceof JetspeedTest) {
204: JetspeedTest jtest = (JetspeedTest) arg0;
205: jtest.engine = engine;
206: jtest.jsuite = this;
207: }
208: super.runTest(arg0, arg1);
209: }
210:
211: }
|