ManualLocalTransactionsConfidenceTest.java revision 0391e964363bf98fdbe737723c4a7fdc05c3ffc3
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 17package com.google.inject.persist.jpa; 18 19import com.google.inject.Guice; 20import com.google.inject.Inject; 21import com.google.inject.Injector; 22import com.google.inject.persist.PersistModule; 23import com.google.inject.persist.PersistenceService; 24import com.google.inject.persist.Transactional; 25import com.google.inject.persist.UnitOfWork; 26import java.util.Date; 27import javax.persistence.EntityManager; 28import javax.persistence.PersistenceException; 29import junit.framework.TestCase; 30 31/** 32 * @author Dhanji R. Prasanna (dhanji@gmail.com) 33 */ 34public class ManualLocalTransactionsConfidenceTest extends TestCase { 35 private Injector injector; 36 private static final String UNIQUE_TEXT_3 = 37 ManualLocalTransactionsConfidenceTest.class.getSimpleName() 38 + "CONSTRAINT_VIOLATING some other unique text" + new Date(); 39 40 @Override 41 public void setUp() { 42 injector = Guice.createInjector(new PersistModule() { 43 44 protected void configurePersistence() { 45 //tell guice the name of the jpa persistence unit 46 workAcross(UnitOfWork.TRANSACTION).usingJpa("testUnit"); 47 } 48 }); 49 50 //startup persistence 51 injector.getInstance(PersistenceService.class).start(); 52 } 53 54 @Override 55 public final void tearDown() { 56 injector.getInstance(PersistenceService.class).shutdown(); 57 } 58 59 public void testThrowingCleanupInterceptorConfidence() { 60 Exception e = null; 61 try { 62 System.out.println( 63 "\n\n******************************* EXPECTED EXCEPTION NORMAL TEST BEHAVIOR **********"); 64 injector.getInstance(TransactionalObject.class).runOperationInTxn(); 65 fail(); 66 } catch (RuntimeException re) { 67 e = re; 68 System.out.println( 69 "\n\n******************************* EXPECTED EXCEPTION NORMAL TEST BEHAVIOR **********"); 70 re.printStackTrace(System.out); 71 System.out.println( 72 "\n\n**********************************************************************************"); 73 } 74 75 assertNotNull("No exception was thrown!", e); 76 assertTrue("Exception thrown was not what was expected (i.e. commit-time)", 77 e instanceof PersistenceException); 78 } 79 80 public static class TransactionalObject { 81 @Inject EntityManager em; 82 83 @Transactional 84 public void runOperationInTxn() { 85 JpaParentTestEntity entity = new JpaParentTestEntity(); 86 JpaTestEntity child = new JpaTestEntity(); 87 88 child.setText(UNIQUE_TEXT_3); 89 em.persist(child); 90 91 entity.getChildren().add(child); 92 em.persist(entity); 93 94 entity = new JpaParentTestEntity(); 95 entity.getChildren().add(child); 96 em.persist(entity); 97 } 98 } 99} 100