1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.jetspeed.test;
17  
18  import java.io.File;
19  import java.io.FileReader;
20  
21  import junit.framework.AssertionFailedError;
22  import junit.framework.TestResult;
23  
24  import org.apache.commons.configuration.Configuration;
25  import org.apache.commons.configuration.PropertiesConfiguration;
26  import org.apache.commons.lang.exception.NestableRuntimeException;
27  import org.apache.jetspeed.om.profile.PSMLDocument;
28  import org.apache.jetspeed.om.profile.Portlets;
29  import org.apache.jetspeed.services.PsmlManager;
30  import org.apache.turbine.util.StringUtils;
31  import org.apache.turbine.util.TurbineConfig;
32  import org.exolab.castor.mapping.Mapping;
33  
34  /***
35   * @author <a href="mailto:sweaver@rippe.com">Scott Weaver</a>
36   *
37   * Base abstract class for running Jetspeed (Turbine) in headles-mode
38   * i.e. without a servlet container.
39   *  
40   * @version $Id: HeadlessBaseTest.java,v 1.1 2004/04/07 22:02:41 jford Exp $
41   */
42  public abstract class HeadlessBaseTest extends JetspeedTestCase
43  {
44      
45      public static final String WEBAPP_PATH = "webapp.path";
46      public static final String TR_PROPS_PATH = "tr.props.path";
47      public static final String TEST_CONF_RES = "org/apache/jetspeed/test/jetspeed_testconfig.properties";
48      public static final String PSML_MAPPING = "psml.mapping"; 
49      public static final String DIVIDER = "+=============================================================================+";
50      //public static final String  TEST_CONF_RES = "jetspeed_testconfig.properties";
51      
52      
53      /*
54      Configuration object to run Turbine outside a servlet container
55      ( uses turbine.properties )
56      */
57      private static TurbineConfig config = null;
58      private static final PropertiesConfiguration testConfig = new PropertiesConfiguration();
59      
60      protected TestResult testResult;
61  
62  
63      
64      public HeadlessBaseTest( String name)
65      {
66          super(name);
67      }
68  
69   
70      /***
71       * Returns the Configuration object that holds testing values
72       * @return PropertiesConfiguration
73       */
74      public static Configuration getTestConfig()
75      {
76          return testConfig;
77      }
78      
79      
80          /***
81       * @see junit.framework.Test#run(TestResult)
82       */
83      public void run(TestResult result)
84      {
85          result.startTest(this);
86          try
87          {
88              setUp();
89          }
90          catch (Exception e)
91          {
92              throw new NestableRuntimeException("Error running TestCase setUp().", e);
93          }
94          try
95          {
96              runTest();
97          }
98          catch (AssertionFailedError e)
99          { //1
100             result.addFailure(this, e);
101         }
102 
103         catch (Throwable e)
104         { // 2
105             result.addError(this, e);
106         }
107         finally
108         {
109             try
110             {
111                 tearDown();
112             }
113             catch (Exception e)
114             {
115                 throw new NestableRuntimeException("Error running TestCase tearDown().", e);
116             }
117         }
118     }
119     
120 
121 
122 
123     /***
124      * @see junit.framework.TestCase#setUp()
125      *
126      * Sets up TurbineConfig using the system property:
127      *  <pre>turbine.properties</pre>
128      */
129     
130     protected void setUp() throws Exception
131     {
132         try
133         {
134             // Only set it up once per class loader
135             if(testConfig == null || config == null)
136             {
137                 ClassLoader cl = Thread.currentThread().getContextClassLoader();
138                 System.out.println(cl.getResource(TEST_CONF_RES));
139                 testConfig.load(cl.getResourceAsStream(TEST_CONF_RES));
140                 System.out.println(testConfig.getString(WEBAPP_PATH));
141                 File webappUrl = new File(testConfig.getString(WEBAPP_PATH));
142                 System.out.println(webappUrl);
143                 //System.out.println("webapp found "+webappUrl.exists());
144                 config = new TurbineConfig(webappUrl.getPath(), testConfig.getString(TR_PROPS_PATH));
145                 config.init();
146             }
147         }
148         catch (Throwable e)
149         {
150             fail(StringUtils.stackTrace(e));
151         }        
152         
153         super.setUp();
154     }
155     
156     public PSMLDocument getDocumentFromPath(String psmlFile)
157     {
158         print("Loading portlets from PSML file "+psmlFile);
159       
160         Portlets rootset = null;
161             
162         PSMLDocument doc = PsmlManager.getDocument(psmlFile);        
163         rootset = doc.getPortlets();
164         
165          return doc;
166     }
167     
168     public void saveDocument(PSMLDocument doc)
169     {
170         PsmlManager.saveDocument(doc);
171     }
172     
173  
174     
175     public void savePortletsToPSML(String psmlFile, Portlets portlets)
176     {
177         print("Storing portlets to PSML file "+psmlFile);
178       
179         Mapping mapping = null;
180         String mapFile = getProfileMappingFileName();
181         print("Locating PSML file "+mapFile+"...");
182         File map = new File(mapFile);
183         print("PSML file exists????: "+map.exists());
184         Portlets rootset = null;
185         FileReader reader = null;
186         if (map.exists() && map.isFile() && map.canRead())
187         {
188                 
189                 try
190                 {
191                 }
192                 catch(Exception e)
193                 {
194                 }
195         }
196     }
197     
198     protected void print(Object obj)
199     {
200         if(testConfig.getBoolean("verbose"))
201             System.out.println("+ "+obj);
202     }
203     
204     protected void printOk()
205     {
206     
207             print("OK");
208         
209     }
210     
211      protected void printOk(String linePrefix)
212     {
213     
214             print(linePrefix+"OK");
215     
216     }
217     
218     protected void printDivider()
219     {
220         if(testConfig.getBoolean("verbose"))
221           System.out.println(DIVIDER);        
222     }
223     
224     /***
225      * retreives the castor mapping file to use if
226      * we need to marshal/unmarshal profiles
227      */
228     protected String getProfileMappingFileName()
229     {
230         return testConfig.getString(PSML_MAPPING);
231     }
232 
233 }