PageRenderTime 3685ms CodeModel.GetById 3ms RepoModel.GetById 4ms app.codeStats 0ms

/warp-persist/test/com/wideplay/warp/persist/dao/JpaDynamicFindersTest.java

http://warp-persist.googlecode.com/
Java | 238 lines | 131 code | 66 blank | 41 comment | 12 complexity | da29ba7ee60153e79eec1174dabb91b8 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (C) 2008 Wideplay Interactive.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.wideplay.warp.persist.dao;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Inject;
  20. import com.google.inject.Injector;
  21. import com.wideplay.warp.persist.PersistenceService;
  22. import com.wideplay.warp.persist.Transactional;
  23. import com.wideplay.warp.persist.UnitOfWork;
  24. import com.wideplay.warp.persist.jpa.JpaTestEntity;
  25. import com.wideplay.warp.persist.jpa.JpaUnit;
  26. import org.testng.annotations.AfterTest;
  27. import org.testng.annotations.BeforeClass;
  28. import org.testng.annotations.Test;
  29. import javax.persistence.EntityManager;
  30. import javax.persistence.EntityManagerFactory;
  31. import java.util.ArrayList;
  32. import java.util.Date;
  33. import java.util.List;
  34. import java.util.Set;
  35. /**
  36. * Created by IntelliJ IDEA.
  37. * User: Dhanji R. Prasanna (dhanji@gmail.com)
  38. * Date: 4/06/2007
  39. * Time: 15:57:24
  40. * <p/>
  41. *
  42. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  43. * @since 1.0
  44. */
  45. public class JpaDynamicFindersTest {
  46. private Injector injector;
  47. private static final String TEXT_1 = "unique text1" + new Date();
  48. private static final String TEXT_2 = "unique text2" + new Date();
  49. @BeforeClass
  50. public void pre() {
  51. injector = Guice.createInjector(PersistenceService.usingJpa()
  52. .across(UnitOfWork.TRANSACTION)
  53. .addAccessor(JpaTestAccessor.class)
  54. .buildModule(),
  55. new AbstractModule() {
  56. protected void configure() {
  57. bindConstant().annotatedWith(JpaUnit.class).to("testUnit");
  58. }
  59. });
  60. injector.getInstance(PersistenceService.class).start();
  61. }
  62. @AfterTest
  63. public void post() {
  64. injector.getInstance(EntityManagerFactory.class).close();
  65. injector = null;
  66. }
  67. @Test
  68. public void testListAll() {
  69. //set up some test data
  70. injector.getInstance(FinderDao.class).store();
  71. //now attempt to query it out
  72. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  73. List<JpaTestEntity> results = accessor.listAll();
  74. assert results.size() >= 2 : "all results not returned!";
  75. assert results.get(0).getText().equals(TEXT_1) || results.get(0).getText().equals(TEXT_2) : "attribs not persisted correctly";
  76. assert results.get(1).getText().equals(TEXT_1) || results.get(1).getText().equals(TEXT_2) : "attribs not persisted correctly";
  77. }
  78. @Test
  79. public void testListAllDDD() {
  80. //set up some test data
  81. injector.getInstance(FinderDao.class).store();
  82. //now attempt to query it out
  83. List<JpaTestEntity> results = injector.getInstance(JpaTestEntity.class).listAll();
  84. assert null != results : "Finder did not intercept annotated DDD method!";
  85. assert results.size() >= 2 : "all results not returned!";
  86. assert results.get(0).getText().equals(TEXT_1) || results.get(0).getText().equals(TEXT_2) : "attribs not persisted correctly";
  87. assert results.get(1).getText().equals(TEXT_1) || results.get(1).getText().equals(TEXT_2) : "attribs not persisted correctly";
  88. }
  89. @Test
  90. public void testReturnAsSet() {
  91. //set up some test data
  92. injector.getInstance(FinderDao.class).store();
  93. //now attempt to query it out
  94. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  95. Set<JpaTestEntity> results = accessor.set();
  96. assert results.size() >= 2 : "all results not returned!";
  97. for (JpaTestEntity result : results) {
  98. assert result.getText().equals(TEXT_1) || result.getText().equals(TEXT_2) : "attribs not persisted correctly";
  99. }
  100. }
  101. @Test
  102. public void testSingleResultFetchWithNamedParameter() {
  103. //set up some test data
  104. Long id = injector.getInstance(FinderDao.class).store().getId();
  105. //now attempt to query it out
  106. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  107. JpaTestEntity result = accessor.fetch(id);
  108. assert null != result : "result not returned!";
  109. assert result.getId().equals(id) : "attribs not persisted correctly";
  110. }
  111. @Test
  112. public void testSingleResultFetchWithIndexedParameter() {
  113. //set up some test data
  114. Long id = injector.getInstance(FinderDao.class).store().getId();
  115. //now attempt to query it out
  116. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  117. JpaTestEntity result = accessor.fetchById(id);
  118. assert null != result : "result not returned!";
  119. assert result.getId().equals(id) : "attribs not persisted correctly";
  120. }
  121. @Test
  122. public void testListParamemter() {
  123. //set up some test data
  124. Long id = injector.getInstance(FinderDao.class).store().getId();
  125. //now attempt to query it out
  126. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  127. List<Long> list = new ArrayList<Long>();
  128. list.add(id);
  129. JpaTestEntity result = accessor.fetchByIdList(list);
  130. assert null != result : "result not returned!";
  131. assert result.getId().equals(id) : "attribs not persisted correctly";
  132. }
  133. @Test(expectedExceptions = ClassCastException.class)
  134. public void testUnnamedListParamemter() {
  135. //set up some test data
  136. Long id = injector.getInstance(FinderDao.class).store().getId();
  137. //now attempt to query it out
  138. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  139. List<Long> list = new ArrayList<Long>();
  140. list.add(id);
  141. JpaTestEntity result = accessor.fetchByIdUnnamedList(list);
  142. assert null != result : "result not returned!";
  143. assert result.getId().equals(id) : "attribs not persisted correctly";
  144. }
  145. @Test(expectedExceptions = ClassCastException.class)
  146. public void testArrayParamemter() {
  147. //set up some test data
  148. Long id = injector.getInstance(FinderDao.class).store().getId();
  149. //now attempt to query it out
  150. JpaTestAccessor accessor = injector.getInstance(JpaTestAccessor.class);
  151. JpaTestEntity result = accessor.fetchByIdArray(new Long[]{id});
  152. assert null != result : "result not returned!";
  153. assert result.getId().equals(id) : "attribs not persisted correctly";
  154. }
  155. public static class FinderDao {
  156. private final EntityManager em;
  157. @Inject
  158. public FinderDao(EntityManager em) {
  159. this.em = em;
  160. }
  161. @Transactional
  162. JpaTestEntity store() {
  163. JpaTestEntity entity = new JpaTestEntity();
  164. entity.setText(TEXT_1);
  165. em.persist(entity);
  166. entity = new JpaTestEntity();
  167. entity.setText(TEXT_2);
  168. em.persist(entity);
  169. return entity;
  170. }
  171. }
  172. }