1   
2   
3   package org.sourceforge.vlibrary;
4   
5   import java.util.Collection;
6   import java.util.Iterator;
7   import java.util.Map.Entry;
8   import java.util.Map;
9   
10  import org.apache.commons.beanutils.PropertyUtils;
11  import org.apache.commons.lang.StringUtils;
12  
13  import org.springframework.beans.factory.BeanFactory;
14  import org.springframework.context.support.ClassPathXmlApplicationContext;
15  import org.springframework.context.support.ResourceBundleMessageSource;
16  import org.springframework.jdbc.core.JdbcTemplate;
17  
18  import servletunit.struts.MockStrutsTestCase;
19  
20  /**
21   * Base class for vlibrary action tests
22   *
23   * @version $Revision$ $Date$
24   *
25   */
26  
27  public class VlibraryActionTestCase extends MockStrutsTestCase {
28      
29      protected BeanFactory springContext = null;
30      protected JdbcTemplate jdbcTemplate = null;
31      protected ResourceBundleMessageSource messageSource = null;
32      
33      public VlibraryActionTestCase(String testName) {
34          super(testName);
35      }
36      
37      /**
38       * Set web app and spring contexts and create database
39       */
40      protected void setUp() throws Exception {
41          super.setUp();
42          
43          // Use the web resources in /main
44          setContextDirectory(new java.io.File("src/main/webapp/"));
45          
46          // Need to use the /test version to pull in the /test
47          // spring context that struts loads
48          setConfigFile("struts-config.xml");
49          
50          springContext =
51           new ClassPathXmlApplicationContext("/lightweightcontainer.xml");
52          
53          jdbcTemplate = (JdbcTemplate) springContext.getBean(
54           "JdbcTemplate", JdbcTemplate.class);
55          
56          messageSource = (ResourceBundleMessageSource) springContext.getBean(
57           "messageSource", ResourceBundleMessageSource.class);
58          
59          try {
60              TestUtils.createTestDatabase();
61          } catch(Exception t) {
62              System.out.println(t);
63              tearDown();
64              TestUtils.shutDownDatabase();
65              throw t;
66          }
67      }
68      
69      protected void tearDown() throws Exception {
70          super.tearDown();
71          
72          try {
73              TestUtils.destroyDatabase();
74          } catch (Exception ex) {
75              System.out.println(ex);
76          } finally {
77              TestUtils.shutDownDatabase();
78          }
79      }
80      
81      /**
82       * Verifies that the errors collection maintained by the LibraryAction
83       * in the request contains an error with a message string containing
84       * the given message
85       *
86       * @param c the collection to compare against
87       */
88      protected void verifyErrorsContainsMessage(String message) {
89          Collection errors = (Collection) getRequest().getAttribute(
90           Constants.ERRORS);
91          assertNotNull("Errors collection expected not null", errors);
92          assertTrue("Errors collection expected not empty",errors.size() > 0);
93          Iterator iterator = errors.iterator();
94          boolean found = false;
95          while (iterator.hasNext() && !found) {
96              Exception ex = (Exception) iterator.next();
97              String errString = ex.getMessage();
98              found = StringUtils.contains(errString, message);
99          }
100         assertTrue("Error message not found", found);
101     }
102     
103     /**
104      * Dumps messages from exceptions in errors collection.
105      *
106      */
107     protected void dumpErrors() {
108         Collection errors = (Collection) getRequest().getAttribute(
109                 Constants.ERRORS);
110         Iterator iterator = errors.iterator();
111         while (iterator.hasNext()) {
112             Exception ex = (Exception) iterator.next();
113             System.out.println(ex.getMessage());  
114         }    
115     }
116     
117     /**
118      * Adds the non-null properties of the bean to the request using
119      * addRequestParameter.
120      * 
121      * Uses toString() to convert bean property values to strings.
122      * 
123      * @param bean the bean with properties to put into the request
124      */
125     protected void insertBeanAsParameters(Object bean) throws Exception {
126         Map beanMap = PropertyUtils.describe(bean);
127         Iterator iterator = beanMap.entrySet().iterator();
128         while (iterator.hasNext()) {
129             Entry entry = (Entry) iterator.next();
130             String keyString = (String) entry.getKey();
131             Object value = entry.getValue();
132             if (value != null && !keyString.equals("class")) {
133                 addRequestParameter(keyString, value.toString());
134             }
135         } 
136     }  
137 }