AbstractSpringEnabledTransactionalTest.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* ===================================================================
  2. * AbstractSpringEnabledTransactionalTest.java
  3. *
  4. * Created Oct 3, 2005 12:02:08 PM
  5. *
  6. * Copyright (c) 2005 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: AbstractSpringEnabledTransactionalTest.java 39 2009-05-06 03:30:47Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat;
  27. import java.net.URL;
  28. import java.util.ArrayList;
  29. import java.util.Calendar;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Locale;
  33. import java.util.Map;
  34. import magoffin.matt.ieat.biz.DomainObjectFactory;
  35. import magoffin.matt.ieat.dao.BaseDao;
  36. import magoffin.matt.ieat.dao.CourseDao;
  37. import magoffin.matt.ieat.dao.DifficultyDao;
  38. import magoffin.matt.ieat.dao.EthnicityDao;
  39. import magoffin.matt.ieat.dao.IngredientDao;
  40. import magoffin.matt.ieat.dao.PrepTimeDao;
  41. import magoffin.matt.ieat.dao.SystemDao;
  42. import magoffin.matt.ieat.dao.UnitDao;
  43. import magoffin.matt.ieat.domain.Base;
  44. import magoffin.matt.ieat.domain.Course;
  45. import magoffin.matt.ieat.domain.Difficulty;
  46. import magoffin.matt.ieat.domain.Ethnicity;
  47. import magoffin.matt.ieat.domain.Ingredient;
  48. import magoffin.matt.ieat.domain.PrepTime;
  49. import magoffin.matt.ieat.domain.Recipe;
  50. import magoffin.matt.ieat.domain.RecipeIngredient;
  51. import magoffin.matt.ieat.domain.RecipeStep;
  52. import magoffin.matt.ieat.domain.System;
  53. import magoffin.matt.ieat.domain.Unit;
  54. import magoffin.matt.ieat.domain.User;
  55. import org.apache.log4j.Logger;
  56. import org.springframework.context.ApplicationContext;
  57. import org.springframework.context.ConfigurableApplicationContext;
  58. import org.springframework.context.support.AbstractApplicationContext;
  59. import org.springframework.context.support.ClassPathXmlApplicationContext;
  60. import org.springframework.context.support.FileSystemXmlApplicationContext;
  61. import org.springframework.jdbc.core.JdbcTemplate;
  62. import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests;
  63. /**
  64. * Extension of Spring's AbstractTransactionalDataSourceSpringContextTests
  65. * with helper methods for the LMS project.
  66. *
  67. * @author matt.magoffin
  68. * @version $Revision: 39 $ $Date: 2009-05-06 15:30:47 +1200 (Wed, 06 May 2009) $
  69. */
  70. public abstract class AbstractSpringEnabledTransactionalTest extends
  71. AbstractTransactionalDataSourceSpringContextTests {
  72. /** The name of the JdbcTemplate bean in the Spring configuration file. */
  73. private static final String JDBC_TEMPLATE_BEAN_NAME = "myJdbcTemplate";
  74. /** The base Spring context. */
  75. private static AbstractApplicationContext baseContext = null;
  76. /** A test login. */
  77. public static final String TEST_USER_LOGIN = "_test_";
  78. /** A test password. */
  79. public static final String TEST_USER_PASS = "_foo_";
  80. /** A test ID. */
  81. public static final Integer DEFAULT_LOAD_ID = new Integer(1);
  82. /** Logger instance for extending classes to use. */
  83. protected final Logger log = Logger.getLogger(getClass());
  84. /** The DomainObjectFactory to test with. */
  85. protected DomainObjectFactory domainObjectFactory;
  86. /** The BaseDao. */
  87. protected BaseDao baseDao;
  88. /** The CourseDao. */
  89. protected CourseDao courseDao;
  90. /** The DifficultyDao. */
  91. protected DifficultyDao difficultyDao;
  92. /** The EthnicityDao. */
  93. protected EthnicityDao ethnicityDao;
  94. /** The IngredientDao. */
  95. protected IngredientDao ingredientDao;
  96. /** The PrepTimeDao. */
  97. protected PrepTimeDao prepTimeDao;
  98. /** The SystemDao. */
  99. protected SystemDao systemDao;
  100. /** The UnitDao. */
  101. protected UnitDao unitDao;
  102. /**
  103. * A cache of ApplicationContext objects created from individual URLs, used
  104. * to keep from having to recreate ApplicationContext objects between test
  105. * case invocations.
  106. */
  107. private static final Map<URL, ApplicationContext> APP_CONTEXT_CACHE
  108. = new HashMap<URL, ApplicationContext>();
  109. /** The paths to the default Spring context files. */
  110. private static final String[] DEFAULT_APP_CONTEXT_PATHS = {
  111. //"classpath:magoffin/matt/ieat/AbstractSpringEnabledTestContext.xml",
  112. "web/WEB-INF/applicationContext.xml",
  113. //"web/WEB-INF/dataAccessContext.xml",
  114. //"web/WEB-INF/ieat-servlet.xml",
  115. };
  116. /**
  117. * Default constructor.
  118. */
  119. public AbstractSpringEnabledTransactionalTest() {
  120. super();
  121. setPopulateProtectedVariables(true);
  122. if ( log.isInfoEnabled() ) {
  123. log.info("\n\n\n*** Constructing test class [" +getClass().getName() +"] ***\n");
  124. }
  125. }
  126. @Override
  127. protected final String[] getConfigLocations() {
  128. return DEFAULT_APP_CONTEXT_PATHS;
  129. }
  130. @Override
  131. protected Object contextKey() {
  132. return getClass();
  133. }
  134. @SuppressWarnings("unchecked")
  135. @Override
  136. protected ConfigurableApplicationContext loadContext(Object key) {
  137. ConfigurableApplicationContext context = getBaseContext(getConfigLocations());
  138. List classHierarchy = getClassHierarchy();
  139. for (int i = 0; i < classHierarchy.size(); i++) {
  140. Class clazz = (Class)classHierarchy.get(i);
  141. context = getApplicationContext(clazz,
  142. getClassName(clazz) + "Context.xml", context);
  143. }
  144. return context;
  145. }
  146. private List<Class<?>> getClassHierarchy() {
  147. List<Class<?>> result = new ArrayList<Class<?>>();
  148. Class<?> superclass = getClass();
  149. do {
  150. result.add(superclass);
  151. } while ((superclass = superclass.getSuperclass()) != null
  152. && superclass != AbstractSpringEnabledTransactionalTest.class);
  153. return result;
  154. }
  155. private String getClassName(Class<?> clazz) {
  156. String fullClassName = clazz.getName();
  157. String result = fullClassName
  158. .substring(fullClassName.lastIndexOf(".") + 1);
  159. return result;
  160. }
  161. private ConfigurableApplicationContext getApplicationContext(Class<?> clazz,
  162. String resourceName, ConfigurableApplicationContext parentContext) {
  163. log.debug("Attempting to locate " + resourceName);
  164. URL url = clazz.getResource(resourceName);
  165. if (url == null) {
  166. return parentContext;
  167. }
  168. if (!APP_CONTEXT_CACHE.containsKey(url)) {
  169. log.info("Loading " + url);
  170. APP_CONTEXT_CACHE.put(url,
  171. new ClassPathXmlApplicationContext(new String[] { url
  172. .toString() }, parentContext));
  173. }
  174. return (AbstractApplicationContext) APP_CONTEXT_CACHE.get(url);
  175. }
  176. private synchronized AbstractApplicationContext getBaseContext(String[] configLocations) {
  177. if ( baseContext == null ) {
  178. if ( configLocations != null && configLocations.length > 0 ) {
  179. // configure env location first
  180. ConfigurableApplicationContext envContext =
  181. getApplicationContext( AbstractSpringEnabledTransactionalTest.class,
  182. getClassName( AbstractSpringEnabledTransactionalTest.class) + "Context.xml",
  183. null);
  184. baseContext = new FileSystemXmlApplicationContext(configLocations,
  185. envContext);
  186. }
  187. jdbcTemplate = (JdbcTemplate)baseContext.getBean(JDBC_TEMPLATE_BEAN_NAME);
  188. } else {
  189. jdbcTemplate = (JdbcTemplate)baseContext.getBean(JDBC_TEMPLATE_BEAN_NAME);
  190. }
  191. return baseContext;
  192. }
  193. @Override
  194. protected void onTearDownInTransaction() throws Exception {
  195. super.onTearDownInTransaction();
  196. java.lang.System.gc();
  197. java.lang.System.runFinalization();
  198. }
  199. /**
  200. * @return Returns the baseContext.
  201. */
  202. public static AbstractApplicationContext getBaseContext() {
  203. return baseContext;
  204. }
  205. /**
  206. * Get a dummy user.
  207. * @return the dummy user.
  208. */
  209. protected User getDummyUser() {
  210. User u = domainObjectFactory.getUserInstance();
  211. u.setAccessLevel(new Integer(1));
  212. u.setCountry(Locale.getDefault().getCountry());
  213. u.setCreatedDate(Calendar.getInstance());
  214. u.setEmail("ieat.test@localhost");
  215. u.setLanguage(Locale.getDefault().getLanguage());
  216. u.setLogin(TEST_USER_LOGIN);
  217. u.setName("Test User");
  218. u.setPassword(TEST_USER_PASS);
  219. return u;
  220. }
  221. /**
  222. * Get a dummy recipe (non-persisted).
  223. * @return recipe
  224. */
  225. @SuppressWarnings("unchecked")
  226. protected Recipe getDummyRecipe() {
  227. Recipe r = domainObjectFactory.getRecipeInstance();
  228. r.setCreatedDate(Calendar.getInstance());
  229. r.setBase(getTestBase());
  230. r.setCourse(getTestCourse());
  231. r.setDifficulty(getTestDifficulty());
  232. r.setEthnicity(getTestEthnicity());
  233. r.setExcerpt("This is the test recipe description.");
  234. r.setName("My Test Recipe " +r.getCreatedDate().getTime() );
  235. r.setPrepTime(getTestPrepTime());
  236. r.setServingSize(new Integer(1));
  237. r.setSource("iEat Test Case");
  238. List<System> systems = systemDao.getSystems();
  239. for ( System system : systems ) {
  240. if ( "US".equals(system.getValue()) ) {
  241. r.setSystem(system);
  242. break;
  243. }
  244. }
  245. List units = unitDao.getUnits();
  246. List ingredients = ingredientDao.getIngredients();
  247. for ( int i = 0; i < 3 && i < ingredients.size(); i++ ) {
  248. RecipeIngredient ri = domainObjectFactory.getRecipeIngredientInstance();
  249. ri.setQuantity(1.5);
  250. ri.setIngredient((Ingredient)ingredients.get(i));
  251. ri.setUnit((Unit)units.get(0));
  252. r.getIngredient().add(ri);
  253. }
  254. for ( int i = 0; i < 3; i++ ) {
  255. RecipeStep step = domainObjectFactory.getRecipeStepInstance();
  256. step.setValue("Dummy step " +i);
  257. r.getStep().add(step);
  258. }
  259. return r;
  260. }
  261. /**
  262. * @return a test Base
  263. */
  264. protected Base getTestBase() {
  265. return baseDao.getBases().get(0);
  266. }
  267. /**
  268. * @return a test Course
  269. */
  270. protected Course getTestCourse() {
  271. return courseDao.getCourses().get(0);
  272. }
  273. /**
  274. * @return a test Difficulty
  275. */
  276. protected Difficulty getTestDifficulty() {
  277. return difficultyDao.getDifficulties().get(0);
  278. }
  279. /**
  280. * @return a test Ethnicity
  281. */
  282. protected Ethnicity getTestEthnicity() {
  283. return ethnicityDao.getEthnicities().get(0);
  284. }
  285. /**
  286. * @return a test PrepTime
  287. */
  288. protected PrepTime getTestPrepTime() {
  289. return prepTimeDao.getPrepTimes().get(0);
  290. }
  291. }