Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/test/WebappsTestCase.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/test/WebappsTestCase.java,v 1.18 2003/09/30 15:13:19 joe Exp $
2    * $Revision: 1.18 $
3    * $Date: 2003/09/30 15:13:19 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.test;
31  
32  import com.RuntimeCollective.webapps.EntityBeanStore;
33  import com.RuntimeCollective.webapps.RuntimeParameters;
34  import com.RuntimeCollective.webapps.RuntimeDataSource;
35  import com.RuntimeCollective.webapps.EntityBeanStoreHandler;
36  import com.RuntimeCollective.webapps.RuntimeTestCase;
37  import com.RuntimeCollective.webapps.UserGroups;
38  
39  import java.sql.SQLException;
40  import java.sql.Connection;
41  import java.util.*;
42  import java.io.*;
43  import javax.servlet.ServletException;
44  import javax.mail.Session;
45  
46  import junit.framework.*;
47  import junit.textui.TestRunner;
48  
49  import org.apache.log4j.*;
50  import org.apache.log4j.xml.*;
51  import org.apache.log4j.net.*;
52  
53  /**
54   * Test Case for basic webapps functionality, mostly EBS-related.
55   */
56  public class WebappsTestCase extends RuntimeTestCase {
57  
58      private static final String ENTITY_BEAN_CLASS_NAME = "com.RuntimeCollective.webapps.bean.EntityBean";
59  
60      protected String DEFAULT_STORE_CLASS = "com.RuntimeCollective.webapps.IndexedEntityBeanStore";
61  
62      private Vector ivBeanNames;  
63      private HashMap ivBeanStoreNames;  
64  
65      public WebappsTestCase(String name) {
66    super(name);
67      }
68  
69      /**
70       * A value that can be appended to a name in order that a bean can
71       * be identified aand pulled out of the database in other tests.
72       */
73      public String getNameId() {
74    Calendar calendar = Calendar.getInstance();
75    return String.valueOf(calendar.get(Calendar.DAY_OF_YEAR) + calendar.get(Calendar.MILLISECOND));
76      }
77      
78      public void resetBeanRegister() {
79    ivBeanNames = new Vector();
80    ivBeanStoreNames = new HashMap();
81    registerBeanClass(ENTITY_BEAN_CLASS_NAME);
82      }
83      
84      public void registerBeanClass(String name) {
85    ivBeanNames.addElement(name);
86      }
87      
88      public void registerBeanClass(String name, String storeClass) {
89    ivBeanNames.addElement(name);
90    ivBeanStoreNames.put(name, storeClass);
91      }
92      
93      public Enumeration getBeanNames() {
94    return ivBeanNames.elements();
95      }
96  
97      public String getStoreClassForBeanName(String name) {
98    Object storeClass = ivBeanStoreNames.get(name);
99    if (storeClass == null) {
100       storeClass = DEFAULT_STORE_CLASS;
101   }
102   return (String)storeClass;
103     }
104 
105     private void configureRuntimeDataSource() {
106   Properties properties = new Properties(System.getProperties());
107 
108   if(properties.getProperty("database.alias") == null) {
109       String propsFileName = System.getProperty("test.properties");
110       try {
111     FileInputStream propFile = new FileInputStream(propsFileName);
112     properties.load(propFile);
113       } catch (IOException ex) {
114     fail("Couldn't load database properties from : " + propsFileName);
115       }   
116   }
117   
118   RuntimeDataSource.addDb(0,
119         properties.getProperty("database.alias"),
120         properties.getProperty("database.driver"),
121         3,
122         1,
123         properties.getProperty("database.user"), 
124         properties.getProperty("database.password"), 
125         properties.getProperty("database.url"),
126         properties.getProperty("database.type"));
127   RuntimeParameters.set("db.alias", properties.getProperty("database.alias"));
128     }
129     
130     /**
131      * This has been copied over from the theseus test cases, and will
132      * be updated to use the RuntimeTestCase init routines shortly */
133     protected void setUp() {
134 
135   // configure the logging
136   RuntimeParameters.setLog4jLogging(false);
137   RuntimeParameters.setLogLevel(RuntimeParameters.LOG_LEVEL_DEBUG);
138   ConsoleAppender appender = new ConsoleAppender();
139   BasicConfigurator.configure(appender);
140 
141   // configure the data source
142   configureRuntimeDataSource();
143 
144   // configure the store
145   EntityBeanStoreHandler handler = initialiseBeanStore();
146   RuntimeParameters.setStore(handler);
147 
148   // init
149   initUserGroups();
150   initMailSession();
151     }
152     
153     public EntityBeanStoreHandler initialiseBeanStore() {
154   // Make an EntityBeanStore handler, and register the beans
155   EntityBeanStoreHandler ebs = new EntityBeanStoreHandler();
156   
157   String cacheClass = "com.RuntimeCollective.webapps.GarbageCollectedCache";
158   
159   Enumeration e = getBeanNames();
160   while(e.hasMoreElements()) {
161       String beanName = (String) e.nextElement();
162       try {
163     registerBean(beanName, ebs, cacheClass, getStoreClassForBeanName(beanName));
164       } catch (RuntimeException rx) {
165     fail("[WebappsTestCase:getBeanStore] Caught a runtime exception : " + rx);
166       } catch (SQLException sx) {
167     fail("[WebappsTestCase:getBeanStore] Caught a SQLException : " + sx);
168       }
169   }
170   
171   ebs.init();
172   
173   return ebs;
174     }
175     
176     private void registerBean(String beanName, EntityBeanStoreHandler handler, String cacheClass, String storeClass) throws SQLException {
177   try {
178       handler.registerBean(beanName, cacheClass, storeClass);
179   } catch (RuntimeException ex) {
180       fail("[WebappsTestCase:getBeanStore] Caught a runtime exception : " + ex);
181   }
182     }
183 
184     public void testCheckEntityBeanStoreValid() {
185   assertTrue("Bean store not properly initialised", checkBeanStore());
186     }
187     
188     public boolean checkBeanStore() {
189   Vector namesInStore = new Vector();
190   Iterator iterator = null;
191   try {
192       iterator = RuntimeParameters.getStore().getAllClasses();
193   } catch(SQLException ex) {
194       fail("Error getting all class names from EntityBeanStore");
195   }
196 
197   // Check the bean store actually contains some classes
198   Assert.assertNotNull("EntityBeanStore doesn't contain any clases", iterator);
199   Assert.assertTrue("EntityBeanStore is not configured correctly", iterator.hasNext());
200   
201   while(iterator.hasNext()) {
202       namesInStore.addElement((String) iterator.next());
203   }
204   
205   Object[] registeredNames = ivBeanNames.toArray();
206   Object[] inStoreNames = namesInStore.toArray();
207   Arrays.sort(registeredNames);
208   Arrays.sort(inStoreNames);
209 
210   return Arrays.equals(registeredNames, inStoreNames);
211     }
212 
213     public void printBeanStoreClasses() {
214   Vector namesInStore = new Vector();
215   Iterator iterator = null;
216   try {
217       iterator = RuntimeParameters.getStore().getAllClasses();
218   } catch(SQLException ex) {
219       fail("Error getting all class names from EntityBeanStore");
220   }
221   
222   // Check the bean store actually contains some classes
223   Assert.assertNotNull("EntityBeanStore doesn't contain any clases", iterator);
224   Assert.assertTrue("EntityBeanStore is not configured correctly", iterator.hasNext());
225   
226   while(iterator.hasNext()) {
227       namesInStore.addElement((String) iterator.next());
228   }
229   
230   Object[] registeredNames = ivBeanNames.toArray();
231   Object[] inStoreNames = namesInStore.toArray();
232   Arrays.sort(registeredNames);
233   Arrays.sort(inStoreNames);
234   
235   for(int i = 0; i < registeredNames.length; i++) {
236       RuntimeParameters.logDebug(this, "Registered name : " + registeredNames[i]);
237   }
238   
239   for(int i = 0; i < inStoreNames.length; i++) {
240       RuntimeParameters.logDebug(this, "InStoreNames name : " + inStoreNames[i]);
241   }
242     }
243 
244     /** Initialise the UserGroups object. */
245     public void initUserGroups() {
246   RuntimeParameters.setUserGroups(new UserGroups());
247     }
248     
249     /** Initialise the JavaMail Session object. */
250     public void initMailSession() {
251   try {
252       Properties mailProps = new Properties();
253       mailProps.put("mail.transport.protocol", "smtp");
254       mailProps.put("mail.host", "mail.runtime-collective.com");
255       mailProps.put("mail.from", "testsupport@runtime-collective.com");
256 
257       RuntimeParameters.setMailSession(Session.getInstance(mailProps, null));
258   } catch (RuntimeException e) {
259       RuntimeParameters.logError(this, "initMailSession could not initialise a mail session; web.xml must specify \"smtpHost\" and \"systemEmailAddress\" properties");
260   }
261     }
262 
263     protected void tearDown() {    
264   ivBeanNames = new Vector();
265   RuntimeParameters.getStore().clear();
266     }
267    
268 }
269