X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FTransactionProxyFactoryTestBase.java;fp=test%2Fenterprise%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsupport%2Fpersistence%2FTransactionProxyFactoryTestBase.java;h=286c8757bb81248a2f6622ac6ffd00dbb6854f24;hb=4c4b45259c4af24c2551c40df72ad776dd037cbe;hp=0000000000000000000000000000000000000000;hpb=dd174318e1f8b2afa8a17ffc7e3775d65d0401ca;p=utils diff --git a/test/enterprise/src/test/java/org/wamblee/support/persistence/TransactionProxyFactoryTestBase.java b/test/enterprise/src/test/java/org/wamblee/support/persistence/TransactionProxyFactoryTestBase.java new file mode 100644 index 00000000..286c8757 --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/support/persistence/TransactionProxyFactoryTestBase.java @@ -0,0 +1,102 @@ +/* + * Copyright 2005-2010 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.support.persistence; + +import javax.persistence.EntityManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.support.persistence.JpaBuilder.JpaUnitOfWork; + +import static junit.framework.TestCase.*; +import static org.mockito.Mockito.*; + +public class TransactionProxyFactoryTestBase { + + public static interface Service { + int execute(int aValue) throws Exception; + } + + private JpaTester jpaTester; + + @Before + public void setUp() throws Exception { + jpaTester = new JpaTester(new MyPersistenceUnit()); + jpaTester.start(); + } + + @After + public void tearDown() throws Exception { + jpaTester.stop(); + } + + @Test + public void testServiceIsInvoked() throws Exception { + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = mock(Service.class); + when(service.execute(10)).thenReturn(200); + Service proxy = factory.getProxy(service); + int res = proxy.execute(10); + assertEquals(200, res); + verify(service).execute(10); + } + + @Test + public void testServiceThrowsException() throws Exception { + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = mock(Service.class); + when(service.execute(10)).thenThrow(new RuntimeException("xyz")); + Service proxy = factory.getProxy(service); + try { + int res = proxy.execute(10); + fail(); + } catch (RuntimeException e) { + assertEquals("xyz", e.getMessage()); + } + } + + @Test + public void testEntityManagerIsPassed() throws Exception { + + + final TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = new Service() { + private EntityManager em = factory.getTransactionScopedEntityManager(); + + @Override + public int execute(int aValue) throws Exception { + assertNotNull(em); + assertTrue(em.isOpen()); + return 0; + } + }; + + final Service proxy = factory.getProxy(service); + jpaTester.getJpaBuilder().execute(new JpaUnitOfWork() { + @Override + public Void execute(EntityManager aEm) throws Exception { + assert(aEm != null); + proxy.execute(10); + return null; + } + }); + } + +}