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 27 import junit.framework.TestCase; 28 29 import java.util.ArrayList; 30 import java.util.Date; 31 import java.util.List; 32 import java.util.UUID; 33 34 import javax.persistence.EntityManager; 35 36 /** 37 * A test around providing sessions (starting, closing etc.) 38 * 39 * @author Dhanji R. Prasanna (dhanji (at) gmail.com) 40 */ 41 42 public class DynamicFinderTest extends TestCase { 43 private Injector injector; 44 45 public void setUp() { 46 injector = Guice.createInjector(new JpaPersistModule("testUnit").addFinder(JpaFinder.class)); 47 48 //startup persistence 49 injector.getInstance(PersistService.class).start(); 50 } 51 52 public final void tearDown() { 53 injector.getInstance(PersistService.class).stop(); 54 } 55 56 public void testDynamicFinderListAll() { 57 //obtain em 58 JpaDao dao = injector.getInstance(JpaDao.class); 59 60 //obtain same em again (bound to txn) 61 JpaTestEntity te = new JpaTestEntity(); 62 te.setText("HIAjsOKAOSD" + new Date() + UUID.randomUUID()); 63 64 dao.persist(te); 65 66 //im not sure this hack works... 67 assertFalse("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) 79 public List<JpaTestEntity> listAll(); 80 } 81 82 public static class JpaDao { 83 private final Provider<EntityManager> em; 84 EntityManager lastEm; 85 86 @Inject 87 public JpaDao(Provider<EntityManager> em) { 88 this.em = em; 89 } 90 91 @Transactional 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 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