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