View Javadoc

1   package org.appfuse.service;
2   
3   import org.apache.commons.beanutils.BeanUtils;
4   import org.apache.commons.logging.Log;
5   import org.apache.commons.logging.LogFactory;
6   import org.appfuse.util.ConvertUtil;
7   import org.junit.runner.RunWith;
8   import org.springframework.test.context.ContextConfiguration;
9   import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
10  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
11  
12  import javax.transaction.Transactional;
13  import java.util.Map;
14  import java.util.MissingResourceException;
15  import java.util.ResourceBundle;
16  
17  @RunWith(SpringJUnit4ClassRunner.class)
18  @ContextConfiguration(locations = {
19          "classpath:/applicationContext-resources.xml", "classpath:/applicationContext-dao.xml",
20          "classpath:/applicationContext-service.xml", "classpath*:/**/applicationContext.xml"
21  })
22  /**
23   * Test classes can extend this manager based on a spring context.
24   * This test class can be moved to the test tree.
25   *
26   * @author mraible
27   */
28  @Transactional
29  public abstract class BaseManagerTestCase {
30  
31      /**
32       * A simple logger
33       */
34      protected final Log log = LogFactory.getLog(getClass());
35      /**
36       * The resourceBundle
37       */
38      protected ResourceBundle rb;
39  
40      /**
41       * Default constructor will set the ResourceBundle if needed.
42       */
43      public BaseManagerTestCase() {
44          // Since a ResourceBundle is not required for each class, just
45          // do a simple check to see if one exists
46          String className = this.getClass().getName();
47  
48          try {
49              rb = ResourceBundle.getBundle(className);
50          } catch (MissingResourceException mre) {
51              // log.warn("No resource bundle found for: " + className);
52          }
53      }
54  
55      /**
56       * Utility method to populate an object with values from a properties file
57       *
58       * @param obj the model object to populate
59       * @return Object populated object
60       * @throws Exception if BeanUtils fails to copy properly
61       */
62      protected Object populate(Object obj) throws Exception {
63          // loop through all the beans methods and set its properties from
64          // its .properties file
65          Map map = ConvertUtil.convertBundleToMap(rb);
66  
67          BeanUtils.copyProperties(obj, map);
68  
69          return obj;
70      }
71  }