You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by cs...@apache.org on 2014/10/07 11:44:49 UTC

svn commit: r1629842 - in /aries/trunk/jpa: jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/ jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/ jpa-container-itest/src/test/java/org...

Author: cschneider
Date: Tue Oct  7 09:44:49 2014
New Revision: 1629842

URL: http://svn.apache.org/r1629842
Log:
Fix and optimize JPA tests

Added:
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/AbstractQuiesceJPATest.java
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPAInteractionTest.java
Modified:
    aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/JTAEntityManagerHandler.java
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/JPAInjectionTest.java
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/context/itest/JPAContextTest.java
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
    aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java

Modified: aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/JTAEntityManagerHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/JTAEntityManagerHandler.java?rev=1629842&r1=1629841&r2=1629842&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/JTAEntityManagerHandler.java (original)
+++ aries/trunk/jpa/jpa-container-context/src/main/java/org/apache/aries/jpa/container/context/transaction/impl/JTAEntityManagerHandler.java Tue Oct  7 09:44:49 2014
@@ -137,39 +137,44 @@ public class JTAEntityManagerHandler imp
 
     @Override
     public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
-        if ("close".equals(method.getName())) {
+        String methodName = method.getName();
+        if ("close".equals(methodName)) {
             throw new IllegalStateException(NLS.MESSAGES.getMessage("close.called.on.container.manged.em"));
         }
-        if ("getTransaction".equals(method.getName())) {
+        if ("getTransaction".equals(methodName)) {
             throw new IllegalStateException(NLS.MESSAGES.getMessage("getTransaction.called.on.container.managed.em"));
         }
-        if ("isOpen".equals(method.getName())) {
+        if ("isOpen".equals(methodName)) {
             return true;
         }
-        if ("joinTransaction".equals(method.getName())) {
+        if ("joinTransaction".equals(methodName)) {
             // This should be a no-op for a JTA entity manager
             return null;
         }
         
-        if ("postCall".equals(method.getName())) {
+        if ("postCall".equals(methodName)) {
             postCall();
             return null;
         }
         
-        if ("preCall".equals(method.getName())) {
+        if ("preCall".equals(methodName)) {
             preCall();
             return null;
         }
         
-        if ("internalClose".equals(method.getName())) {
+        if ("internalClose".equals(methodName)) {
             internalClose();
             return null;
         }
         
-        boolean forceTransaction = transactedMethods.contains(method.getName());
+        boolean forceTransaction = transactedMethods.contains(methodName);
         
         // TODO Check if this can be reached
-        if ("joinTransaction".equals(method.getName())) {
+        if ("joinTransaction".equals(methodName)) {
+            forceTransaction = args[2] != LockModeType.NONE;
+        }
+        
+        if ("find".equals(methodName) && args.length >= 3 && args[2].getClass() == LockModeType.class) {
             forceTransaction = args[2] != LockModeType.NONE;
         }
         

Modified: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/JPAInjectionTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/JPAInjectionTest.java?rev=1629842&r1=1629841&r2=1629842&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/JPAInjectionTest.java (original)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/blueprint/aries/itest/JPAInjectionTest.java Tue Oct  7 09:44:49 2014
@@ -16,7 +16,6 @@
 package org.apache.aries.jpa.blueprint.aries.itest;
 
 import static org.junit.Assert.assertTrue;
-import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
 import static org.ops4j.pax.exam.CoreOptions.options;
 
 import org.apache.aries.jpa.blueprint.itest.JPATestBean;

Modified: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/context/itest/JPAContextTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/context/itest/JPAContextTest.java?rev=1629842&r1=1629841&r2=1629842&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/context/itest/JPAContextTest.java (original)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/context/itest/JPAContextTest.java Tue Oct  7 09:44:49 2014
@@ -45,11 +45,7 @@ import javax.transaction.UserTransaction
 
 import org.apache.aries.jpa.container.itest.entities.Car;
 import org.apache.aries.jpa.itest.AbstractJPAItest;
-import org.junit.Ignore;
 import org.junit.Test;
-import org.ops4j.pax.exam.Configuration;
-import org.ops4j.pax.exam.CoreOptions;
-import org.ops4j.pax.exam.Option;
 
 public abstract class JPAContextTest extends AbstractJPAItest {
   
@@ -82,7 +78,6 @@ public abstract class JPAContextTest ext
     EntityManagerFactory emf = getProxyEMF(BP_TEST_UNIT);
     
     final EntityManager managedEm = emf.createEntityManager();
-    
     ensureTREBehaviour(false, managedEm, "contains", new Object());
     ensureTREBehaviour(false, managedEm, "createNamedQuery", "hi");
     ensureTREBehaviour(false, managedEm, "createNativeQuery", "hi");
@@ -116,8 +111,7 @@ public abstract class JPAContextTest ext
     ensureTREBehaviour(true, managedEm, "find", Object.class, new Object(), LockModeType.OPTIMISTIC);
     ensureTREBehaviour(false, managedEm, "find", Object.class, new Object(), LockModeType.NONE, 
         new HashMap());
-    ensureTREBehaviour(true, managedEm, "find", Object.class, new Object(), LockModeType.OPTIMISTIC, 
-        new HashMap());
+    ensureTREBehaviour(true, managedEm, "find", Object.class, new Object(), LockModeType.OPTIMISTIC, new HashMap());
     ensureTREBehaviour(false, managedEm, "getCriteriaBuilder");
     ensureTREBehaviour(true, managedEm, "getLockMode", new Object());
     ensureTREBehaviour(false, managedEm, "getMetamodel");
@@ -339,35 +333,35 @@ public abstract class JPAContextTest ext
     assertEquals("A1AAA", list.get(1).getNumberPlate());
   }
 
-  private void ensureTREBehaviour(boolean expectedToFail, EntityManager em, String methodName, Object... args) throws Exception {
-    List<Class<?>> argTypes = new ArrayList<Class<?>>();
-    for(Object o : args) {
-      if(o instanceof Map)
-        argTypes.add(Map.class);
-      else if (o instanceof CriteriaQuery)
-        argTypes.add(CriteriaQuery.class);
-      else
-        argTypes.add(o.getClass());
-    }
-    
-    Method m = EntityManager.class.getMethod(methodName, 
-        argTypes.toArray(new Class[args.length]));
-    
-    try {
-      m.invoke(em, args);
-      if(expectedToFail)
-        fail("A transaction is required");
-    } catch (InvocationTargetException ite) {
-      if(expectedToFail && 
-          !!!(ite.getCause() instanceof TransactionRequiredException))
-        fail("We got the wrong failure. Expected a TransactionRequiredException" +
-        		", got a " + ite.toString());
-      else if (!!!expectedToFail && 
-          ite.getCause() instanceof TransactionRequiredException)
-        fail("We got the wrong failure. Expected not to get a TransactionRequiredException" +
-            ", but we got one anyway!");
+    private void ensureTREBehaviour(boolean expectedToFail, EntityManager em, String methodName,
+                                    Object... args) throws Exception {
+        List<Class<?>> argTypes = new ArrayList<Class<?>>();
+        for (Object o : args) {
+            if (o instanceof Map)
+                argTypes.add(Map.class);
+            else if (o instanceof CriteriaQuery)
+                argTypes.add(CriteriaQuery.class);
+            else
+                argTypes.add(o.getClass());
+        }
+
+        Method m = EntityManager.class.getMethod(methodName, argTypes.toArray(new Class[args.length]));
+
+        try {
+            m.invoke(em, args);
+            if (expectedToFail) {
+                fail("Should have failed with TransactionRequiredException");
+            }
+        } catch (InvocationTargetException ite) {
+            if (expectedToFail && !(ite.getCause() instanceof TransactionRequiredException)) {
+                fail("We got the wrong failure. Expected a TransactionRequiredException" + ", got a "
+                     + ite.toString());
+            } else if (!expectedToFail && ite.getCause() instanceof TransactionRequiredException) {
+                fail("We got the wrong failure. Expected not to get a TransactionRequiredException"
+                     + ", but we got one anyway!");
+            }
+        }
     }
-  }
   
 
 }

Modified: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java?rev=1629842&r1=1629841&r2=1629842&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java (original)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java Tue Oct  7 09:44:49 2014
@@ -43,8 +43,8 @@ public abstract class AbstractJPAItest e
 	}
 	
 	protected EntityManagerFactory getProxyEMF(String name) {
-		return context().getService(EntityManagerFactory.class, "(&(osgi.unit.name=" + name + ")" 
-				+ FILTER_CONTAINER_MANAGED + FILTER_PROXY +")");
+		String filter = "(&(osgi.unit.name=" + name + ")" + FILTER_CONTAINER_MANAGED + FILTER_PROXY +")";
+        return context().getService(EntityManagerFactory.class, filter, 5000);
 	}
 	
 	protected EntityManagerFactory getEMF(String name) {

Added: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/AbstractQuiesceJPATest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/AbstractQuiesceJPATest.java?rev=1629842&view=auto
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/AbstractQuiesceJPATest.java (added)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/AbstractQuiesceJPATest.java Tue Oct  7 09:44:49 2014
@@ -0,0 +1,182 @@
+/*  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.
+ */
+package org.apache.aries.jpa.quiesce.itest;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.ops4j.pax.exam.CoreOptions.options;
+
+import java.util.Collections;
+import java.util.List;
+
+import javax.inject.Inject;
+import javax.sql.DataSource;
+import javax.transaction.UserTransaction;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.apache.aries.jpa.itest.AbstractJPAItest;
+import org.apache.aries.quiesce.manager.QuiesceCallback;
+import org.apache.aries.quiesce.participant.QuiesceParticipant;
+import org.junit.After;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+@ExamReactorStrategy(PerClass.class)
+public abstract class AbstractQuiesceJPATest extends AbstractJPAItest {
+    protected static final int WAIT_TIME = 200;
+    protected static final String JPA_CONTAINER = "org.apache.aries.jpa.container";
+    protected static final String JPA_CONTEXT = "org.apache.aries.jpa.container.context";
+    protected static final String TEST_BUNDLE = "org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle";
+    
+    protected static Logger LOG = LoggerFactory.getLogger(AbstractQuiesceJPATest.class);
+
+    @Inject
+    UserTransaction tm;
+
+    //This is load bearing. we have to wait to create the EntityManager until the DataSource is available
+    @Inject
+    DataSource ds;
+    
+
+    @Configuration
+    public Option[] configuration() {
+        return options(
+                baseOptions(),
+                ariesJpa(),
+                openJpa(),
+                testDs(),
+                testBundle()
+                );
+    }
+
+    @After
+    public void restartTestBundles() throws BundleException {
+        restartTestBundle();
+        restartBundle(JPA_CONTAINER);
+        restartBundle(JPA_CONTEXT);
+        try {
+            tm.rollback();
+        } catch (Exception e) {
+            // Ignore
+        }
+    }
+
+    protected List<Bundle> getListForTestBundle() {
+        return Collections.singletonList(
+                context().getBundleByName(TEST_BUNDLE));
+    }
+
+    protected void assertFinished(TestQuiesceCallback callback) {
+        assertTrue("Quiesce not finished", callback.bundleClearedUp());
+    }
+
+    protected void assertNotFinished(TestQuiesceCallback... callbacks)
+            throws InterruptedException {
+        Thread.sleep(WAIT_TIME);
+        for (TestQuiesceCallback callback : callbacks) {
+            assertFalse("Quiesce finished ", callback.bundleClearedUp());
+        }
+    }
+
+    protected void assertNoEMFForTestUnit() throws InvalidSyntaxException {
+        assertNull("No unit should exist", getEMFRefs(TEST_UNIT));
+    }
+
+    protected void assertNoProxyEMFForTestUnit() throws InvalidSyntaxException {
+        assertNull("No context should exist", getProxyEMFRefs(TEST_UNIT));
+    }
+
+    protected TestQuiesceCallback quiesce(String participantName, String bundleName) throws InvalidSyntaxException {
+        QuiesceParticipant participant = getParticipant(participantName);
+        TestQuiesceCallback callback = new TestQuiesceCallback();
+        participant.quiesce(callback, Collections.singletonList(context().getBundleByName(bundleName)));
+        return callback;
+    }
+
+    @SuppressWarnings({ "rawtypes", "unchecked" })
+    protected QuiesceParticipant getParticipant(String bundleName) throws InvalidSyntaxException {
+        ServiceReference[] refs = bundleContext.getServiceReferences(QuiesceParticipant.class.getName(), null);
+
+        if(refs != null) {
+            for(ServiceReference ref : refs) {
+                if(ref.getBundle().getSymbolicName().equals(bundleName))
+                    return (QuiesceParticipant) bundleContext.getService(ref);
+            }
+        }
+
+
+        return null;
+    }
+
+    protected void restartTestBundle() throws BundleException {
+        restartBundle(TEST_BUNDLE_NAME);
+    }
+
+    protected void restartBundle(String bundleName) throws BundleException {
+        Bundle b = context().getBundleByName(bundleName);
+        b.stop();
+        b.start();
+    }
+
+    protected class TestQuiesceCallback implements QuiesceCallback{
+        protected int calls = 0;
+
+        public void bundleQuiesced(Bundle... bundles) {
+            for (Bundle bundle : bundles) {
+                LOG.info("Bundle quiesced " + bundle.getSymbolicName());
+            }
+            calls++;
+        }
+
+        public boolean bundleClearedUp() {
+            return calls == 1;
+        }
+    }
+
+    protected class MultiQuiesceCallback extends TestQuiesceCallback implements QuiesceCallback{
+        private boolean contextFirst = true;
+
+        public void bundleQuiesced(Bundle... bundles) {
+            if (++calls == 1) {
+                try {
+                    getEMF(TEST_UNIT);
+                } catch (Throwable t){
+                    contextFirst = false;
+                    if(t instanceof RuntimeException)
+                        throw (RuntimeException) t;
+                    else if (t instanceof Error)
+                        throw (Error) t;
+                    else
+                        throw new RuntimeException(t);
+                }
+            }
+        }
+
+        public boolean bundleClearedUp()
+        {
+            return calls == 2 && contextFirst;
+        }
+    }
+
+}

Added: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPAInteractionTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPAInteractionTest.java?rev=1629842&view=auto
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPAInteractionTest.java (added)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPAInteractionTest.java Tue Oct  7 09:44:49 2014
@@ -0,0 +1,93 @@
+/*  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.
+ */
+package org.apache.aries.jpa.quiesce.itest;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.junit.Test;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+
+@ExamReactorStrategy(PerClass.class)
+public class QuiesceJPAInteractionTest extends AbstractQuiesceJPATest {
+    
+    /**
+     * TODO This test does not work together with the QuiesceJPATest tests.
+     * This might point to an error in the code
+     */
+	@Test
+	public void testComplexQuiesceInteraction() throws Exception {
+		registerClient(TEST_UNIT);
+
+		EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
+		tm.begin();
+		emf.createEntityManager().getProperties();
+
+		//Quiesce the Unit, nothing should happen
+		TestQuiesceCallback unitCallback = quiesce(JPA_CONTAINER, TEST_BUNDLE);
+		assertNotFinished(unitCallback);
+		getProxyEMF(TEST_UNIT);
+
+		//Quiesce the context, still nothing
+		TestQuiesceCallback contextCallback = quiesce(JPA_CONTEXT, TEST_BUNDLE);
+		assertNotFinished(unitCallback, contextCallback);
+		getProxyEMF(TEST_UNIT);
+
+		//Keep the unit alive
+		emf = getEMF(TEST_UNIT);
+
+		EntityManager em = emf.createEntityManager();
+		LOG.info("After commit quiesce should happen");
+		tm.commit();
+		
+		assertFinished(contextCallback);
+		assertNoProxyEMFForTestUnit();
+		getEMF(TEST_UNIT);
+		em.close();
+
+		assertFinished(unitCallback);
+		assertNoEMFForTestUnit();
+	}
+
+	@Test
+	public void testComplexQuiesceInteraction2() throws Exception {
+		registerClient(TEST_UNIT);
+
+		EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
+		tm.begin();
+		emf.createEntityManager().getProperties();
+
+		MultiQuiesceCallback callback = new MultiQuiesceCallback();
+
+		// Quiesce the Unit, nothing should happen
+		getParticipant(JPA_CONTAINER).quiesce(callback, getListForTestBundle());
+		assertNotFinished(callback);
+
+		// Quiesce the context, still nothing
+		getParticipant(JPA_CONTEXT).quiesce(callback, getListForTestBundle());
+		assertNotFinished(callback);
+
+		getProxyEMF(TEST_UNIT);
+		getEMF(TEST_UNIT);
+
+		tm.commit();
+
+		assertFinished(callback);
+		assertNoEMFForTestUnit();
+	}
+
+}

Modified: aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java?rev=1629842&r1=1629841&r2=1629842&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java (original)
+++ aries/trunk/jpa/jpa-container-itest/src/test/java/org/apache/aries/jpa/quiesce/itest/QuiesceJPATest.java Tue Oct  7 09:44:49 2014
@@ -15,114 +15,28 @@
  */
 package org.apache.aries.jpa.quiesce.itest;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertTrue;
-import static org.ops4j.pax.exam.CoreOptions.options;
-
-import java.util.Collections;
-
-import javax.inject.Inject;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
-import javax.sql.DataSource;
-import javax.transaction.UserTransaction;
 
-import org.apache.aries.jpa.itest.AbstractJPAItest;
-import org.apache.aries.quiesce.manager.QuiesceCallback;
-import org.apache.aries.quiesce.participant.QuiesceParticipant;
-import org.junit.After;
 import org.junit.Test;
-import org.ops4j.pax.exam.Configuration;
-import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
-import org.ops4j.pax.exam.spi.reactors.PerMethod;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleException;
-import org.osgi.framework.InvalidSyntaxException;
-import org.osgi.framework.ServiceReference;
-
-@ExamReactorStrategy(PerMethod.class)
-public class QuiesceJPATest extends AbstractJPAItest {
-	private static final int WAIT_TIME = 200;
-	private static final String JPA_CONTAINER = "org.apache.aries.jpa.container";
-	private static final String JPA_CONTEXT = "org.apache.aries.jpa.container.context";
-	private final String TEST_BUNDLE = "org.apache.aries.jpa.org.apache.aries.jpa.container.itest.bundle";
-
-	@Inject
-	UserTransaction tm;
-
-	//This is load bearing. we have to wait to create the EntityManager until the DataSource is available
-	@Inject
-	DataSource ds;
-
-	private class TestQuiesceCallback implements QuiesceCallback{
-
-		protected int calls = 0;
-
-		public void bundleQuiesced(Bundle... arg0) {
-			calls++;
-		}
-
-		public boolean bundleClearedUp()
-		{
-			return calls == 1;
-		}
-	}
-
-	private class MultiQuiesceCallback extends TestQuiesceCallback implements QuiesceCallback{
-
-		private boolean contextFirst = true;
-
-		public void bundleQuiesced(Bundle... arg0) {
-			if(++calls == 1)
-				try {
-					getEMF(TEST_UNIT);
-				} catch (Throwable t){
-					contextFirst = false;
-					if(t instanceof RuntimeException)
-						throw (RuntimeException) t;
-					else if (t instanceof Error)
-						throw (Error) t;
-					else
-						throw new RuntimeException(t);
-				}
-
-		}
-
-		public boolean bundleClearedUp()
-		{
-			return calls == 2 && contextFirst;
-		}
-	}
-
-
-	@After
-	public void restartTestBundles() throws BundleException {
-		restartTestBundle();
-		restartBundle(JPA_CONTAINER);
-		restartBundle(JPA_CONTEXT);
-		try {
-			tm.rollback();
-		} catch (Exception e) {
-			// Ignore
-		}
-	}
+import org.ops4j.pax.exam.spi.reactors.PerClass;
 
+@ExamReactorStrategy(PerClass.class)
+public class QuiesceJPATest extends AbstractQuiesceJPATest {
 	@Test
 	public void testSimpleContextQuiesce() throws Exception {
 		registerClient(TEST_UNIT);
 		getProxyEMF(TEST_UNIT);
 
-		//Quiesce it
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTEXT, TEST_BUNDLE);
+		// Quiesce should work
+		TestQuiesceCallback callback = quiesce(JPA_CONTEXT, TEST_BUNDLE);
 		Thread.sleep(WAIT_TIME);
 		assertFinished(callback);
-
 		assertNoProxyEMFForTestUnit();
 
+		// After restart emf should be there again
 		restartTestBundle();
-
 		getProxyEMF(TEST_UNIT);
 	}
 
@@ -130,41 +44,27 @@ public class QuiesceJPATest extends Abst
 	public void testComplexContextQuiesce() throws Exception {
 		registerClient(TEST_UNIT);
 
-		EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
-		tm.begin();
-		emf.createEntityManager().getProperties();
-
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTEXT, TEST_BUNDLE);
-		assertNotFinished(callback);
-
-		emf = getProxyEMF(TEST_UNIT);
-		tm.commit();
-		assertTrue("Quiesce not finished", callback.bundleClearedUp());
-		assertNoProxyEMFForTestUnit();
+        testQuiesceContext();
 
 		restartTestBundle();
 
-		emf = getProxyEMF(TEST_UNIT);
-		tm.begin();
-		emf.createEntityManager().getProperties();
-		tm.commit();
-
-		Thread.sleep(WAIT_TIME);
-
 		//Test again to make sure we don't hold state over
-		emf = getProxyEMF(TEST_UNIT);
+		testQuiesceContext();
+	}
+
+    private void testQuiesceContext() throws Exception {
+        EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
 		tm.begin();
 		emf.createEntityManager().getProperties();
 
-		callback = getQuiesceCallback(JPA_CONTEXT, TEST_BUNDLE);
+		TestQuiesceCallback callback = quiesce(JPA_CONTEXT, TEST_BUNDLE);
 		assertNotFinished(callback);
 
-		emf = getProxyEMF(TEST_UNIT);
+		getProxyEMF(TEST_UNIT);
 		tm.commit();
-
 		assertFinished(callback);
 		assertNoProxyEMFForTestUnit();
-	}
+    }
 
 	@Test
 	public void testContextRuntimeQuiesce() throws Exception {
@@ -174,7 +74,7 @@ public class QuiesceJPATest extends Abst
 		tm.begin();
 		emf.createEntityManager().getProperties();
 
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTEXT, JPA_CONTEXT);
+		TestQuiesceCallback callback = quiesce(JPA_CONTEXT, JPA_CONTEXT);
 		assertNotFinished(callback);
 
 		emf = getProxyEMF(TEST_UNIT);
@@ -186,35 +86,34 @@ public class QuiesceJPATest extends Abst
 
 	@Test
 	public void testSimpleUnitQuiesce() throws Exception {
-		assertEMFForTestUnit();
+		getEMF(TEST_UNIT);
 
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTAINER, TEST_BUNDLE);
+		TestQuiesceCallback callback = quiesce(JPA_CONTAINER, TEST_BUNDLE);
 		Thread.sleep(WAIT_TIME);
 		assertFinished(callback);
 		assertNoEMFForTestUnit();
 
 		restartTestBundle();
 
-		assertEMFForTestUnit();
+		getEMF(TEST_UNIT);
 	}
 
 	
 	@Test
 	public void testComplexUnitQuiesce() throws Exception {
-		quiesceUnit();
+		testQuiesceUnit();
 		restartTestBundle();
-	    getEMF(TEST_UNIT).createEntityManager().close();
 		//Test a second time to make sure state isn't held
-		quiesceUnit();
+		testQuiesceUnit();
 	}
 	
-	private void quiesceUnit() throws Exception {
+	private void testQuiesceUnit() throws Exception {
 		EntityManager em = getEMF(TEST_UNIT).createEntityManager();
 
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTAINER, TEST_BUNDLE);
+		TestQuiesceCallback callback = quiesce(JPA_CONTAINER, TEST_BUNDLE);
 		assertNotFinished(callback);
 
-		assertEMFForTestUnit();
+		getEMF(TEST_UNIT);
 		em.close();
 		assertFinished(callback);
 		assertNoEMFForTestUnit();
@@ -225,146 +124,13 @@ public class QuiesceJPATest extends Abst
 		EntityManagerFactory emf = getEMF(TEST_UNIT);
 		EntityManager em = emf.createEntityManager();
 
-		TestQuiesceCallback callback = getQuiesceCallback(JPA_CONTAINER, JPA_CONTAINER);
+		TestQuiesceCallback callback = quiesce(JPA_CONTAINER, JPA_CONTAINER);
 		assertNotFinished(callback);
 
-		assertEMFForTestUnit();
-		em.close();
-		assertFinished(callback);
-		assertNoEMFForTestUnit();
-	}
-
-	@Test
-	public void testComplexQuiesceInteraction() throws Exception {
-		registerClient(TEST_UNIT);
-
-		EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
-		tm.begin();
-		emf.createEntityManager().getProperties();
-
-		//Quiesce the Unit, nothing should happen
-		TestQuiesceCallback unitCallback = getQuiesceCallback(JPA_CONTAINER, TEST_BUNDLE);
-		assertNotFinished(unitCallback);
-
-		emf = getProxyEMF(TEST_UNIT);
-
-		//Quiesce the context, still nothing
-		TestQuiesceCallback contextCallback = getQuiesceCallback(JPA_CONTEXT, TEST_BUNDLE);
-		assertNotFinished(unitCallback, contextCallback);
-
-		emf = getProxyEMF(TEST_UNIT);
-
-		//Keep the unit alive
-		emf = getEMF(TEST_UNIT);
-
-		EntityManager em = emf.createEntityManager();
-		tm.commit();
-		assertFinished(contextCallback);
-		assertNoProxyEMFForTestUnit();
-		assertEMFForTestUnit();
+		getEMF(TEST_UNIT);
 		em.close();
-
-		assertFinished(unitCallback);
-		assertNoEMFForTestUnit();
-	}
-
-	@Test
-	public void testComplexQuiesceInteraction2() throws Exception {
-		registerClient(TEST_UNIT);
-
-		EntityManagerFactory emf = getProxyEMF(TEST_UNIT);
-
-		tm.begin();
-
-		emf.createEntityManager().getProperties();
-
-		MultiQuiesceCallback callback = new MultiQuiesceCallback();
-
-		//Quiesce the Unit, nothing should happen
-		QuiesceParticipant participant = getParticipant(JPA_CONTAINER);
-		participant.quiesce(callback, Collections.singletonList(context().getBundleByName(
-				TEST_BUNDLE)));
-
-		//Quiesce the context, still nothing
-		participant = getParticipant(JPA_CONTEXT);
-		participant.quiesce(callback, Collections.singletonList(
-				context().getBundleByName(TEST_BUNDLE)));
-		assertNotFinished(callback);
-
-		emf = getProxyEMF(TEST_UNIT);
-		assertEMFForTestUnit();
-
-		tm.commit();
-
 		assertFinished(callback);
 		assertNoEMFForTestUnit();
 	}
 
-	private void assertFinished(TestQuiesceCallback callback) {
-		assertTrue("Quiesce not finished", callback.bundleClearedUp());
-	}
-
-	private void assertNotFinished(TestQuiesceCallback... callbacks)
-			throws InterruptedException {
-		Thread.sleep(WAIT_TIME);
-		for (TestQuiesceCallback callback : callbacks) {
-			assertFalse("Quiesce finished", callback.bundleClearedUp());
-		}
-	}
-
-	private void assertNoEMFForTestUnit() throws InvalidSyntaxException {
-		assertNull("No unit should exist", getEMFRefs(TEST_UNIT));
-	}
-
-	private void assertEMFForTestUnit() {
-		getEMF(TEST_UNIT);
-	}
-
-	private void assertNoProxyEMFForTestUnit() throws InvalidSyntaxException {
-		assertNull("No context should exist", getProxyEMFRefs(TEST_UNIT));
-	}
-
-	private TestQuiesceCallback getQuiesceCallback(String participantName, String bundleName) throws InvalidSyntaxException {
-		QuiesceParticipant participant = getParticipant(participantName);
-		TestQuiesceCallback callback = new TestQuiesceCallback();
-		participant.quiesce(callback, Collections.singletonList(context().getBundleByName(bundleName)));
-		return callback;
-	}
-
-	@SuppressWarnings({ "rawtypes", "unchecked" })
-	private QuiesceParticipant getParticipant(String bundleName) throws InvalidSyntaxException {
-		ServiceReference[] refs = bundleContext.getServiceReferences(QuiesceParticipant.class.getName(), null);
-
-		if(refs != null) {
-			for(ServiceReference ref : refs) {
-				if(ref.getBundle().getSymbolicName().equals(bundleName))
-					return (QuiesceParticipant) bundleContext.getService(ref);
-			}
-		}
-
-
-		return null;
-	}
-
-	private void restartTestBundle() throws BundleException {
-		restartBundle(TEST_BUNDLE_NAME);
-	}
-
-	private void restartBundle(String bundleName) throws BundleException {
-		Bundle b = context().getBundleByName(bundleName);
-		b.stop();
-		b.start();
-	}
-
-	@Configuration
-	public Option[] configuration() {
-		return options(
-				baseOptions(),
-				ariesJpa(),
-				openJpa(),
-				testDs(),
-				testBundle()
-				);
-	}
-
 }