Home | History | Annotate | Download | only in jpa
      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.Injector;
     21 import com.google.inject.persist.PersistService;
     22 import com.google.inject.persist.UnitOfWork;
     23 import javax.persistence.EntityManager;
     24 import javax.persistence.EntityManagerFactory;
     25 import junit.framework.TestCase;
     26 
     27 /** @author Dhanji R. Prasanna (dhanji (at) gmail.com) */
     28 
     29 public class EntityManagerFactoryProvisionTest extends TestCase {
     30   private Injector injector;
     31 
     32   @Override
     33   public void setUp() {
     34     injector = Guice.createInjector(new JpaPersistModule("testUnit"));
     35   }
     36 
     37   @Override
     38   public final void tearDown() {
     39     injector.getInstance(UnitOfWork.class).end();
     40     injector.getInstance(EntityManagerFactory.class).close();
     41   }
     42 
     43   public void testSessionCreateOnInjection() {
     44 
     45     assertEquals(
     46         "SINGLETON VIOLATION " + UnitOfWork.class.getName(),
     47         injector.getInstance(UnitOfWork.class),
     48         injector.getInstance(UnitOfWork.class));
     49 
     50     //startup persistence
     51     injector.getInstance(PersistService.class).start();
     52 
     53     //obtain em
     54     assertTrue(injector.getInstance(EntityManager.class).isOpen());
     55   }
     56 }
     57