onami-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sclas...@apache.org
Subject svn commit: r1604035 - in /onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits: BaseMultiplePuTest.java SimpleMultiplePuTest.java TransactionalMultiplePuTest.java
Date Thu, 19 Jun 2014 21:24:04 GMT
Author: sclassen
Date: Thu Jun 19 21:24:04 2014
New Revision: 1604035

URL: http://svn.apache.org/r1604035
Log:
onami-persist: added extracted BaseMultiplePuTest and added TransactionalMultiplePuTest

Added:
    onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
  (with props)
    onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
  (with props)
Modified:
    onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/SimpleMultiplePuTest.java

Added: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
URL: http://svn.apache.org/viewvc/onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java?rev=1604035&view=auto
==============================================================================
--- onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
(added)
+++ onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
Thu Jun 19 21:24:04 2014
@@ -0,0 +1,80 @@
+package org.apache.onami.persist.test.multipersistenceunits;
+
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+import com.google.inject.Key;
+import org.apache.onami.persist.EntityManagerProvider;
+import org.apache.onami.persist.PersistenceModule;
+import org.apache.onami.persist.PersistenceService;
+import org.apache.onami.persist.UnitOfWork;
+import org.junit.After;
+import org.junit.Before;
+
+import java.lang.annotation.Annotation;
+
+public abstract class BaseMultiplePuTest
+{
+    protected EntityManagerProvider firstEmp;
+
+    protected EntityManagerProvider secondEmp;
+
+    private Injector injector;
+
+    @Before
+    public void setUp()
+    {
+        final PersistenceModule pm = createPersistenceModuleForTest();
+        injector = Guice.createInjector( pm );
+
+        //startup persistence
+        injector.getInstance( Key.get( PersistenceService.class, FirstPU.class ) ).start();
+        injector.getInstance( Key.get( PersistenceService.class, SecondPU.class ) ).start();
+
+        firstEmp = injector.getInstance( Key.get( EntityManagerProvider.class, FirstPU.class
) );
+        secondEmp = injector.getInstance( Key.get( EntityManagerProvider.class, SecondPU.class
) );
+    }
+
+    private PersistenceModule createPersistenceModuleForTest()
+    {
+        return new PersistenceModule()
+        {
+
+            @Override
+            protected void configurePersistence()
+            {
+                bindApplicationManagedPersistenceUnit( "firstUnit" ).annotatedWith( FirstPU.class
);
+                bindApplicationManagedPersistenceUnit( "secondUnit" ).annotatedWith( SecondPU.class
);
+            }
+        };
+    }
+
+    @After
+    public void tearDown()
+        throws Exception
+    {
+        injector.getInstance( Key.get( PersistenceService.class, FirstPU.class ) ).stop();
+        injector.getInstance( Key.get( PersistenceService.class, SecondPU.class ) ).stop();
+    }
+
+    protected void beginUnitOfWork()
+    {
+        getInstance( UnitOfWork.class, FirstPU.class ).begin();
+        getInstance( UnitOfWork.class, SecondPU.class ).begin();
+    }
+
+    protected void endUnitOfWork()
+    {
+        getInstance( UnitOfWork.class, FirstPU.class ).end();
+        getInstance( UnitOfWork.class, SecondPU.class ).end();
+    }
+
+    protected <T> T getInstance(Class<T> type)
+    {
+        return injector.getInstance( type );
+    }
+
+    protected <T> T getInstance(Class<T> type, Class<? extends Annotation>
anno)
+    {
+        return injector.getInstance( Key.get( type, anno ) );
+    }
+}

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/BaseMultiplePuTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/SimpleMultiplePuTest.java
URL: http://svn.apache.org/viewvc/onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/SimpleMultiplePuTest.java?rev=1604035&r1=1604034&r2=1604035&view=diff
==============================================================================
--- onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/SimpleMultiplePuTest.java
(original)
+++ onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/SimpleMultiplePuTest.java
Thu Jun 19 21:24:04 2014
@@ -19,13 +19,6 @@ package org.apache.onami.persist.test.mu
  * under the License.
  */
 
-import com.google.inject.Guice;
-import com.google.inject.Injector;
-import com.google.inject.Key;
-import org.apache.onami.persist.EntityManagerProvider;
-import org.apache.onami.persist.PersistenceModule;
-import org.apache.onami.persist.PersistenceService;
-import org.apache.onami.persist.UnitOfWork;
 import org.apache.onami.persist.test.TestEntity;
 import org.junit.After;
 import org.junit.Before;
@@ -35,57 +28,24 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertNull;
 
 public class SimpleMultiplePuTest
+    extends BaseMultiplePuTest
 {
 
-    private Injector injector;
-
-    private EntityManagerProvider firstEmp;
-
-    private EntityManagerProvider secondEmp;
-
     @Before
     public void setUp()
     {
-        final PersistenceModule pm = createPersistenceModuleForTest();
-        injector = Guice.createInjector( pm );
-
-        //startup persistence
-        injector.getInstance( Key.get( PersistenceService.class, FirstPU.class ) ).start();
-        injector.getInstance( Key.get( PersistenceService.class, SecondPU.class ) ).start();
-
-        injector.getInstance( Key.get( UnitOfWork.class, FirstPU.class ) ).begin();
-        injector.getInstance( Key.get( UnitOfWork.class, SecondPU.class ) ).begin();
-
-        firstEmp = injector.getInstance( Key.get( EntityManagerProvider.class, FirstPU.class
) );
-        secondEmp = injector.getInstance( Key.get( EntityManagerProvider.class, SecondPU.class
) );
+        super.setUp();
+        beginUnitOfWork();
     }
 
     @After
     public void tearDown()
         throws Exception
     {
-        injector.getInstance( Key.get( UnitOfWork.class, FirstPU.class ) ).end();
-        injector.getInstance( Key.get( UnitOfWork.class, SecondPU.class ) ).end();
-
-        injector.getInstance( Key.get( PersistenceService.class, FirstPU.class ) ).stop();
-        injector.getInstance( Key.get( PersistenceService.class, SecondPU.class ) ).stop();
-    }
-
-    private PersistenceModule createPersistenceModuleForTest()
-    {
-        return new PersistenceModule()
-        {
-
-            @Override
-            protected void configurePersistence()
-            {
-                bindApplicationManagedPersistenceUnit( "firstUnit" ).annotatedWith( FirstPU.class
);
-                bindApplicationManagedPersistenceUnit( "secondUnit" ).annotatedWith( SecondPU.class
);
-            }
-        };
+        endUnitOfWork();
+        super.tearDown();
     }
 
-
     @Test
     public void storeUnitsInTwoPersistenceUnits()
         throws Exception

Added: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
URL: http://svn.apache.org/viewvc/onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java?rev=1604035&view=auto
==============================================================================
--- onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
(added)
+++ onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
Thu Jun 19 21:24:04 2014
@@ -0,0 +1,263 @@
+package org.apache.onami.persist.test.multipersistenceunits;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import com.google.inject.Inject;
+import org.apache.onami.persist.EntityManagerProvider;
+import org.apache.onami.persist.Transactional;
+import org.apache.onami.persist.test.TestEntity;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class TransactionalMultiplePuTest
+    extends BaseMultiplePuTest
+{
+
+    private TestEntity firstEntity;
+
+    private TestEntity secondEntity;
+
+    @Before
+    public void setUp()
+    {
+        super.setUp();
+
+        firstEntity = new TestEntity();
+        secondEntity = new TestEntity();
+    }
+
+    @Test
+    public void storeUnitsInTwoPersistenceUnits()
+        throws Exception
+    {
+        // when
+        runServices( FirstServiceNotRollingBack.class, SecondServiceNotRollingBack.class
);
+
+        // then
+        beginUnitOfWork();
+        assertNotNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        assertNotNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        endUnitOfWork();
+    }
+
+    @Test
+    public void storeUnitsInTwoPersistenceUnitsAndRollBackBoth()
+        throws Exception
+    {
+        // when
+        runServices( FirstServiceRollingBack.class, SecondServiceRollingBack.class );
+
+        // then
+        beginUnitOfWork();
+        assertNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        endUnitOfWork();
+    }
+
+    @Test
+    public void storeUnitsInTwoPersistenceUnitsAndRollBackOnlyFirst()
+        throws Exception
+    {
+        // when
+        runServices( FirstServiceRollingBack.class, SecondServiceNotRollingBack.class );
+
+        // then
+        beginUnitOfWork();
+        assertNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        assertNotNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        endUnitOfWork();
+    }
+
+    @Test
+    public void storeUnitsInTwoPersistenceUnitsAndRollBackOnlySecond()
+        throws Exception
+    {
+        // when
+        runServices( FirstServiceNotRollingBack.class, SecondServiceRollingBack.class );
+
+        // then
+        beginUnitOfWork();
+        assertNotNull( firstEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( firstEmp.get().find( TestEntity.class, secondEntity.getId() ) );
+        assertNull( secondEmp.get().find( TestEntity.class, firstEntity.getId() ) );
+        endUnitOfWork();
+    }
+
+    private void runServices( Class<? extends FirstService> firstServiceClass,
+                              Class<? extends SecondService> secondServiceClass )
+    {
+        final FirstService fistService = getInstance( firstServiceClass );
+        final SecondService secondService = getInstance( secondServiceClass );
+
+        try {
+            fistService.setSecondService( secondService );
+            secondService.setException( new RuntimeException() );
+            fistService.run( firstEntity, secondEntity );
+        }
+        catch ( RuntimeException e ) {
+            // ignore
+        }
+    }
+
+    interface FirstService
+    {
+        void setSecondService(SecondService secondService);
+
+        void run(TestEntity firstEntity, TestEntity secondEntity);
+    }
+
+    static class FirstServiceRollingBack
+        implements FirstService
+    {
+
+        private final EntityManagerProvider emp;
+
+        private SecondService secondService;
+
+        @Inject
+        public FirstServiceRollingBack( @FirstPU EntityManagerProvider emp )
+        {
+            this.emp = emp;
+        }
+
+        // @Override
+        public void setSecondService( SecondService secondService )
+        {
+            this.secondService = secondService;
+        }
+
+        // @Override
+        @Transactional( onUnits = FirstPU.class )
+        public void run(TestEntity firstEntity, TestEntity secondEntity)
+        {
+            emp.get().persist( firstEntity );
+            secondService.run(secondEntity);
+        }
+    }
+
+    static class FirstServiceNotRollingBack
+        implements FirstService
+    {
+
+        private final EntityManagerProvider emp;
+
+        private SecondService secondService;
+
+        @Inject
+        public FirstServiceNotRollingBack( @FirstPU EntityManagerProvider emp )
+        {
+            this.emp = emp;
+        }
+
+        // @Override
+        public void setSecondService( SecondService secondService )
+        {
+            this.secondService = secondService;
+        }
+
+        // @Override
+        @Transactional( onUnits = FirstPU.class, ignore = RuntimeException.class)
+        public void run(TestEntity firstEntity, TestEntity secondEntity)
+        {
+            emp.get().persist( firstEntity );
+            secondService.run(secondEntity);
+        }
+    }
+
+    interface SecondService
+    {
+        void setException(RuntimeException exception);
+
+        void run(TestEntity secondEntity);
+    }
+
+    static class SecondServiceRollingBack
+        implements SecondService
+    {
+
+        private final EntityManagerProvider emp;
+
+        private RuntimeException ex;
+
+        @Inject
+        public SecondServiceRollingBack( @SecondPU EntityManagerProvider emp )
+        {
+            this.emp = emp;
+        }
+
+        // @Override
+        public void setException(RuntimeException ex)
+        {
+            this.ex = ex;
+        }
+
+        // @Override
+        @Transactional( onUnits = SecondPU.class )
+        public void run( TestEntity secondEntity )
+        {
+            emp.get().persist( secondEntity );
+            if (ex != null) {
+                throw ex;
+            }
+        }
+    }
+
+    static class SecondServiceNotRollingBack
+        implements SecondService
+    {
+
+        private final EntityManagerProvider emp;
+
+        private RuntimeException ex;
+
+        @Inject
+        public SecondServiceNotRollingBack( @SecondPU EntityManagerProvider emp )
+        {
+            this.emp = emp;
+        }
+
+        // @Override
+        public void setException(RuntimeException ex)
+        {
+            this.ex = ex;
+        }
+
+        // @Override
+        @Transactional( onUnits = SecondPU.class, ignore = RuntimeException.class )
+        public void run( TestEntity secondEntity )
+        {
+            emp.get().persist( secondEntity );
+            if (ex != null) {
+                throw ex;
+            }
+        }
+    }
+
+}

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: onami/trunk/persist/src/test/java/org/apache/onami/persist/test/multipersistenceunits/TransactionalMultiplePuTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message