PageRenderTime 4592ms CodeModel.GetById 24ms RepoModel.GetById 5ms app.codeStats 0ms

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

http://warp-persist.googlecode.com/
Java | 205 lines | 123 code | 49 blank | 33 comment | 5 complexity | e28fa63f71c374d2634e83486f5a5eb9 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.Injector;
  18. import com.google.inject.Guice;
  19. import com.google.inject.AbstractModule;
  20. import com.wideplay.warp.persist.PersistenceService;
  21. import com.wideplay.warp.persist.UnitOfWork;
  22. import com.wideplay.codemonkey.web.startup.Initializer;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Set;
  26. import org.testng.annotations.BeforeMethod;
  27. import org.testng.annotations.AfterMethod;
  28. import org.testng.annotations.Test;
  29. import org.hibernate.cfg.Configuration;
  30. import org.hibernate.cfg.AnnotationConfiguration;
  31. import org.hibernate.SessionFactory;
  32. import org.hibernate.Session;
  33. /**
  34. * Created by IntelliJ IDEA.
  35. * User: Dhanji R. Prasanna (dhanji@gmail.com)
  36. * Date: 4/06/2007
  37. * Time: 15:57:24
  38. * <p/>
  39. *
  40. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  41. * @since 1.0
  42. */
  43. public class HibernateDynamicFinderAbstractClassTest {
  44. private Injector injector;
  45. private static final String TEXT_1 = "unique text1" + new Date();
  46. private static final String TEXT_2 = "unique text2" + new Date();
  47. private static final String TEST_STRING_THAT_SHOULD_BE_PASSED_BACK_UNINTERCEPTED = "test string that should be passed back unintercepted";
  48. @BeforeMethod
  49. public void pre() {
  50. injector = Guice.createInjector(PersistenceService.usingHibernate()
  51. .across(UnitOfWork.TRANSACTION)
  52. .addAccessor(AbstractDF.class)
  53. .buildModule(),
  54. new AbstractModule() {
  55. protected void configure() {
  56. bind(Configuration.class).toInstance(new AnnotationConfiguration()
  57. .addAnnotatedClass(HibernateTestEntityTxnal.class)
  58. .setProperties(Initializer.loadProperties("spt-persistence.properties")));
  59. }
  60. });
  61. injector.getInstance(PersistenceService.class).start();
  62. }
  63. @AfterMethod
  64. public void post() {
  65. injector.getInstance(SessionFactory.class).close();
  66. injector = null;
  67. }
  68. @Test
  69. public void testDDDDynamicFinderListAll() {
  70. Session session = injector.getInstance(Session.class);
  71. session.beginTransaction();
  72. HibernateTestEntityTxnal entity = new HibernateTestEntityTxnal();
  73. entity.setText(TEXT_1);
  74. session.save(entity);
  75. entity = new HibernateTestEntityTxnal();
  76. entity.setText(TEXT_2);
  77. session.save(entity);
  78. session.getTransaction().commit();
  79. //now test our magic finders
  80. HibernateTestEntityTxnal dddAccessor = injector.getInstance(HibernateTestEntityTxnal.class);
  81. List<HibernateTestEntityTxnal> results = dddAccessor.listAll();
  82. //assert them
  83. assert results.size() >= 2 : "atleast 2 results expected! was: " + results.size();
  84. assert results.get(0).getText().equals(TEXT_1) || results.get(0).getText().equals(TEXT_2) : "attribs not persisted correctly";
  85. assert results.get(1).getText().equals(TEXT_1) || results.get(1).getText().equals(TEXT_2) : "attribs not persisted correctly";
  86. }
  87. @Test
  88. public void testAbstractDynamicFinderListAll() {
  89. Session session = injector.getInstance(Session.class);
  90. session.beginTransaction();
  91. HibernateTestEntityTxnal entity = new HibernateTestEntityTxnal();
  92. entity.setText(TEXT_1);
  93. session.save(entity);
  94. entity = new HibernateTestEntityTxnal();
  95. entity.setText(TEXT_2);
  96. session.save(entity);
  97. session.getTransaction().commit();
  98. //now test our magic finders
  99. session = injector.getInstance(Session.class);
  100. session.beginTransaction();
  101. List<HibernateTestEntityTxnal> results;
  102. AbstractDF abstractDF;
  103. try {
  104. abstractDF = injector.getInstance(AbstractDF.class);
  105. results = abstractDF.listAll();
  106. } finally {
  107. session.getTransaction().commit();
  108. }
  109. //assert that non-DFs are passed thru
  110. assert TEST_STRING_THAT_SHOULD_BE_PASSED_BACK_UNINTERCEPTED.equals(abstractDF.passThruMethod()) : "non-abstract method was intercepted!!";
  111. //assert them
  112. assert results.size() >= 2 : "atleast 2 results expected! was: " + results.size();
  113. assert abstractDF.someOtherMethod() : "non-abstract primitive method was intercepted!";
  114. assert results.get(0).getText().equals(TEXT_1) || results.get(0).getText().equals(TEXT_2) : "attribs not persisted correctly";
  115. assert results.get(1).getText().equals(TEXT_1) || results.get(1).getText().equals(TEXT_2) : "attribs not persisted correctly";
  116. }
  117. @Test(expectedExceptions = AbstractMethodError.class)
  118. public void testAbstractNonDynamicFinderFail() {
  119. Session session = injector.getInstance(Session.class);
  120. session.beginTransaction();
  121. HibernateTestEntityTxnal entity = new HibernateTestEntityTxnal();
  122. entity.setText(TEXT_1);
  123. session.save(entity);
  124. entity = new HibernateTestEntityTxnal();
  125. entity.setText(TEXT_2);
  126. session.save(entity);
  127. session.getTransaction().commit();
  128. //now test our magic finders
  129. session = injector.getInstance(Session.class);
  130. session.beginTransaction();
  131. List<HibernateTestEntityTxnal> results;
  132. AbstractDF abstractDF;
  133. try {
  134. abstractDF = injector.getInstance(AbstractDF.class);
  135. results = abstractDF.listAll();
  136. } finally {
  137. session.getTransaction().commit();
  138. }
  139. //assert that non-DFs are passed thru
  140. assert TEST_STRING_THAT_SHOULD_BE_PASSED_BACK_UNINTERCEPTED.equals(abstractDF.passThruMethod()) : "non-abstract method was intercepted!!";
  141. //assert them
  142. assert results.size() >= 2 : "atleast 2 results expected! was: " + results.size();
  143. assert abstractDF.listSome() != null;
  144. }
  145. public static abstract class AbstractDF {
  146. public String passThruMethod() {
  147. return TEST_STRING_THAT_SHOULD_BE_PASSED_BACK_UNINTERCEPTED;
  148. }
  149. public abstract List<HibernateTestEntityTxnal> listSome();
  150. @Finder(query = "from HibernateTestEntityTxnal")
  151. public abstract List<HibernateTestEntityTxnal> listAll();
  152. public boolean someOtherMethod() {
  153. return true;
  154. }
  155. }
  156. }