aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject svn commit: r1685771 - in /aries/trunk/jpa: itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/ itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/ jpa-container/src/main/java/org/apache/aries/jpa/container/i...
Date Tue, 16 Jun 2015 11:47:30 GMT
Author: cschneider
Date: Tue Jun 16 11:47:30 2015
New Revision: 1685771

URL: http://svn.apache.org/r1685771
Log:
[ARIES-1337] Avoid exception when closing an already closed EMF

Added:
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/TCKJPATestCase.java
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/persistence.xml
Modified:
    aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
    aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ManagedEMF.java
    aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleTracker.java

Added: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/TCKJPATestCase.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/TCKJPATestCase.java?rev=1685771&view=auto
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/TCKJPATestCase.java
(added)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/TCKJPATestCase.java
Tue Jun 16 11:47:30 2015
@@ -0,0 +1,62 @@
+/*  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.container.itest;
+
+import static org.ops4j.pax.exam.CoreOptions.streamBundle;
+
+import java.io.InputStream;
+
+import javax.persistence.EntityManagerFactory;
+
+import org.apache.aries.jpa.itest.AbstractJPAItest;
+import org.junit.Assert;
+import org.junit.Test;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.Constants;
+
+/**
+ * Simulates some tests from the tck
+ */
+public class TCKJPATestCase extends AbstractJPAItest {
+
+    @Test
+	public void testEntityManagerFactoryWithIncompletePersistenceUnit() throws Exception {
+		EntityManagerFactory emf = getService(EntityManagerFactory.class, "(osgi.unit.name=incompleteTestUnit)",
false); 
+		Assert.assertNull("There should be no EntityManagerFactory registered since this persistence
unit is incomplete", emf);
+		Bundle testBundle = getBundleByName("incompleteTestUnit");
+		testBundle.uninstall();
+	}
+
+    @Configuration
+    public Option[] configuration() {
+    	InputStream testBundle = TinyBundles.bundle()
+    			.set(Constants.BUNDLE_SYMBOLICNAME, "incompleteTestUnit")
+    			.set("Meta-Persistence", " ")
+    			.add("META-INF/persistence.xml", this.getClass().getResourceAsStream("persistence.xml"))
+    			.build(TinyBundles.withBnd());
+        return new Option[] {
+            baseOptions(), //
+            ariesJpa20(), //
+            derbyDSF(), //
+            hibernate(), //
+            streamBundle(testBundle),
+            debug()
+        };
+    }
+}

Added: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/persistence.xml
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/persistence.xml?rev=1685771&view=auto
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/persistence.xml
(added)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/container/itest/persistence.xml
Tue Jun 16 11:47:30 2015
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
+   version="1.0">
+	<persistence-unit name="incompleteTestUnit">
+		<description>Persistence unit for testing an incomplete persistence unit</description>
+	</persistence-unit>
+</persistence>
\ No newline at end of file

Modified: aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java?rev=1685771&r1=1685770&r2=1685771&view=diff
==============================================================================
--- aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
(original)
+++ aries/trunk/jpa/itests/jpa-container-itest/src/test/java/org/apache/aries/jpa/itest/AbstractJPAItest.java
Tue Jun 16 11:47:30 2015
@@ -48,27 +48,23 @@ public abstract class AbstractJPAItest {
     protected EntityManagerFactory getEMF(String name) {
         return getService(EntityManagerFactory.class, "osgi.unit.name=" + name);
     }
+    
+    public <T> T getService(Class<T> type, String filter) {
+    	return getService(type, filter, true);
+    }
 
     @SuppressWarnings({ "rawtypes", "unchecked" })
-	public <T> T getService(Class<T> type, String filter) {
+	public <T> T getService(Class<T> type, String filter, boolean mandatory) {
         ServiceTracker tracker = null;
         try {
-            String flt;
-            if (filter != null) {
-                if (filter.startsWith("(")) {
-                    flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")"
+ filter + ")";
-                } else {
-                    flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")("
+ filter + "))";
-                }
-            } else {
-                flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
-            }
+        	String objClassFilter = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
+            String flt = filter != null ? "(&" + objClassFilter + sanitizeFilter(filter)
+ ")" : objClassFilter;
             Filter osgiFilter = FrameworkUtil.createFilter(flt);
             tracker = new ServiceTracker(bundleContext, osgiFilter, null);
             tracker.open();
 
             Object svc = type.cast(tracker.waitForService(10000));
-            if (svc == null) {
+            if (svc == null && mandatory) {
                 throw new RuntimeException("Gave up waiting for service " + flt);
             }
             return type.cast(svc);
@@ -76,8 +72,15 @@ public abstract class AbstractJPAItest {
             throw new IllegalArgumentException("Invalid filter", e);
         } catch (InterruptedException e) {
             throw new RuntimeException(e);
+        } finally {
+        	tracker.close();
         }
     }
+
+	public String sanitizeFilter(String filter) {
+		return filter.startsWith("(") ? filter : "(" + filter + ")";
+	}	
+		
     
 	/**
 	 * Helps to diagnose bundles that are not resolved as it will throw a detailed exception

Modified: aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ManagedEMF.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ManagedEMF.java?rev=1685771&r1=1685770&r2=1685771&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ManagedEMF.java
(original)
+++ aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/ManagedEMF.java
Tue Jun 16 11:47:30 2015
@@ -83,11 +83,11 @@ public class ManagedEMF implements Close
         } catch (Exception e) {
             // Ignore. May happen if persistence unit bundle is unloaded/updated
         }
-        if (emf != null) {
+        if (emf != null && emf.isOpen()) {
             try {
                 emf.close();
             } catch (Exception e) {
-                LOGGER.warn("EntityManagerFactory for " + persistenceUnit.getPersistenceUnitName()
+ " already close", e);
+                LOGGER.warn("Error closing EntityManagerFactory for " + persistenceUnit.getPersistenceUnitName(),
e);
             }
         }
         reg = null;

Modified: aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleTracker.java
URL: http://svn.apache.org/viewvc/aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleTracker.java?rev=1685771&r1=1685770&r2=1685771&view=diff
==============================================================================
--- aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleTracker.java
(original)
+++ aries/trunk/jpa/jpa-container/src/main/java/org/apache/aries/jpa/container/impl/PersistenceBundleTracker.java
Tue Jun 16 11:47:30 2015
@@ -114,6 +114,7 @@ public class PersistenceBundleTracker im
         case BundleEvent.STOPPED:
             return "STOPPED";
         case BundleEvent.UNINSTALLED:
+        case 256: // Not sure why but this event is sent when a bundle is uninstalled
             return "UNINSTALLED";
         case BundleEvent.UNRESOLVED:
             return "UNRESOLVED";



Mime
View raw message