PageRenderTime 5033ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/extensions/persist/test/com/google/inject/persist/jpa/DynamicFinderTest.java

https://gitlab.com/metamorphiccode/guice
Java | 109 lines | 64 code | 21 blank | 24 comment | 2 complexity | 089aa6f096a5c816aa08cece8105fa25 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 Google, Inc.
  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.google.inject.persist.jpa;
  17. import com.google.inject.Guice;
  18. import com.google.inject.Inject;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Provider;
  21. import com.google.inject.persist.PersistService;
  22. import com.google.inject.persist.Transactional;
  23. import com.google.inject.persist.finder.Finder;
  24. import junit.framework.TestCase;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.UUID;
  29. import javax.persistence.EntityManager;
  30. /**
  31. * A test around providing sessions (starting, closing etc.)
  32. *
  33. * @author Dhanji R. Prasanna (dhanji@gmail.com)
  34. */
  35. public class DynamicFinderTest extends TestCase {
  36. private Injector injector;
  37. public void setUp() {
  38. injector = Guice.createInjector(new JpaPersistModule("testUnit").addFinder(JpaFinder.class));
  39. //startup persistence
  40. injector.getInstance(PersistService.class).start();
  41. }
  42. public final void tearDown() {
  43. injector.getInstance(PersistService.class).stop();
  44. }
  45. public void testDynamicFinderListAll() {
  46. //obtain em
  47. JpaDao dao = injector.getInstance(JpaDao.class);
  48. //obtain same em again (bound to txn)
  49. JpaTestEntity te = new JpaTestEntity();
  50. te.setText("HIAjsOKAOSD" + new Date() + UUID.randomUUID());
  51. dao.persist(te);
  52. //im not sure this hack works...
  53. assertFalse("Duplicate entity managers crossing-scope",
  54. dao.lastEm.equals(injector.getInstance(EntityManager.class)));
  55. List<JpaTestEntity> list = injector.getInstance(JpaFinder.class).listAll();
  56. assertNotNull(list);
  57. assertFalse(list.isEmpty());
  58. assertEquals(1, list.size());
  59. assertEquals(te, list.get(0));
  60. }
  61. public static interface JpaFinder {
  62. @Finder(query = "from JpaTestEntity", returnAs = ArrayList.class)
  63. public List<JpaTestEntity> listAll();
  64. }
  65. public static class JpaDao {
  66. private final Provider<EntityManager> em;
  67. EntityManager lastEm;
  68. @Inject
  69. public JpaDao(Provider<EntityManager> em) {
  70. this.em = em;
  71. }
  72. @Transactional
  73. public <T> void persist(T t) {
  74. lastEm = em.get();
  75. assertTrue("em is not open!", lastEm.isOpen());
  76. assertTrue("no active txn!", lastEm.getTransaction().isActive());
  77. lastEm.persist(t);
  78. assertTrue("Persisting object failed", lastEm.contains(t));
  79. }
  80. @Transactional
  81. public <T> boolean contains(T t) {
  82. if (null == lastEm) {
  83. lastEm = em.get();
  84. }
  85. return lastEm.contains(t);
  86. }
  87. }
  88. }