View Javadoc

1   package org.appfuse.dao;
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.junit.runner.RunWith;
7   import org.springframework.beans.factory.annotation.Autowired;
8   import org.springframework.context.ApplicationContext;
9   import org.springframework.test.context.ContextConfiguration;
10  
11  import java.util.Enumeration;
12  import java.util.HashMap;
13  import java.util.Map;
14  import java.util.MissingResourceException;
15  import java.util.ResourceBundle;
16  import javax.persistence.EntityManagerFactory;
17  import javax.transaction.Transactional;
18  
19  import org.hibernate.search.jpa.FullTextEntityManager;
20  import org.hibernate.search.jpa.Search;
21  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
22  
23  /**
24   * Base class for running DAO tests.
25   * @author mraible
26   */
27  @RunWith(SpringJUnit4ClassRunner.class)
28  @ContextConfiguration(
29      locations={"classpath:/applicationContext-resources.xml",
30                 "classpath:/applicationContext-dao.xml",
31                 "classpath*:/applicationContext.xml",
32                 "classpath:**/applicationContext*.xml"})
33  @Transactional
34  public abstract class BaseDaoTestCase {
35      /**
36       * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
37       */
38      protected final Log log = LogFactory.getLog(getClass());
39      /**
40       * ResourceBundle loaded from src/test/resources/${package.name}/ClassName.properties (if exists)
41       */
42      protected ResourceBundle rb;
43  
44      /**
45       * Default constructor - populates "rb" variable if properties file exists for the class in
46       * src/test/resources.
47       */
48      public BaseDaoTestCase() {
49          // Since a ResourceBundle is not required for each class, just
50          // do a simple check to see if one exists
51          String className = this.getClass().getName();
52  
53          try {
54              rb = ResourceBundle.getBundle(className);
55          } catch (MissingResourceException mre) {
56              //log.warn("No resource bundle found for: " + className);
57          }
58      }
59  
60      /**
61       * Utility method to populate a javabean-style object with values
62       * from a Properties file
63       * @param obj the model object to populate
64       * @return Object populated object
65       * @throws Exception if BeanUtils fails to copy properly
66       */
67      protected Object populate(Object obj) throws Exception {
68          // loop through all the beans methods and set its properties from its .properties file
69          Map<String, String> map = new HashMap<String, String>();
70  
71          for (Enumeration<String> keys = rb.getKeys(); keys.hasMoreElements();) {
72              String key = keys.nextElement();
73              map.put(key, rb.getString(key));
74          }
75  
76          BeanUtils.copyProperties(obj, map);
77  
78          return obj;
79      }
80  
81      /**
82       * Flush search indexes, to be done after a reindex() or reindexAll() operation
83       */
84      @Autowired
85      public void flushSearchIndexes(ApplicationContext applicationContext) {
86          EntityManagerFactory entityManagerFactory = (EntityManagerFactory) applicationContext.getBean("entityManagerFactory");
87          FullTextEntityManager fullTextEntityMgr = Search.getFullTextEntityManager(entityManagerFactory.createEntityManager());
88          fullTextEntityMgr.flushToIndexes();
89      }
90  }