Author: drobiazko
Date: Thu Aug 18 12:41:53 2011
New Revision: 1159194

URL: http://svn.apache.org/viewvc?rev=1159194&view=rev
Log:
Throw an exception if multiple persistence units are configured to include 
unlisted classes

Added:
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
    
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/multiple-persistence-units-include-unlisted-classes.xml
      - copied, changed from r1159181, 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/single-persistence-unit.xml
Modified:
    
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java

Modified: 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java?rev=1159194&r1=1159193&r2=1159194&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
 Thu Aug 18 12:41:53 2011
@@ -28,6 +28,9 @@ import javax.persistence.spi.Persistence
 import javax.persistence.spi.PersistenceProviderResolverHolder;
 import javax.persistence.spi.PersistenceUnitInfo;
 
+import org.apache.tapestry5.func.F;
+import org.apache.tapestry5.func.Mapper;
+import org.apache.tapestry5.func.Predicate;
 import org.apache.tapestry5.ioc.Resource;
 import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
@@ -55,16 +58,50 @@ public class EntityManagerSourceImpl imp
 
         List<TapestryPersistenceUnitInfo> persistenceUnitInfos = 
parsePersistenceUnitInfos(persistenceDescriptor);
 
-         final Map<String, PersistenceUnitConfigurer> remainingConfigurations 
= configure(configuration, persistenceUnitInfos);
+        final Map<String, PersistenceUnitConfigurer> remainingConfigurations = 
configure(configuration, persistenceUnitInfos);
 
         configureRemaining(persistenceUnitInfos, remainingConfigurations);
 
-        if(persistenceUnitInfos.size() == 1)
+        if(persistenceUnitInfos.size() == 1) 
+        {
             
packageNamePersistenceUnitConfigurer.configure(persistenceUnitInfos.get(0));
+        }
+        else
+        {
+            validateUnitInfos(persistenceUnitInfos);
+        }
 
         this.persistenceUnitInfos = persistenceUnitInfos;
     }
 
+    private void validateUnitInfos(List<TapestryPersistenceUnitInfo> 
persistenceUnitInfos)
+    {
+        final List<String> affectedUnits = 
F.flow(persistenceUnitInfos).filter(new Predicate<TapestryPersistenceUnitInfo>()
+        {
+            public boolean accept(TapestryPersistenceUnitInfo info)
+            {
+                return !info.excludeUnlistedClasses();
+            }
+        }).map(new Mapper<TapestryPersistenceUnitInfo, String>()
+        {
+            public String map(TapestryPersistenceUnitInfo info)
+            {
+                return info.getPersistenceUnitName();
+            }
+        }).toList();
+
+        if(0 < affectedUnits.size())
+        {
+            throw new RuntimeException(
+                    String.format(
+                            "Persistence units '%s' are configured to include 
managed classes that have not been explicitly listed. " +
+                            "This is forbidden when multiple persistence units 
are used in the same application. " +
+                            "Please configure persistence units to exclude 
unlisted managed classes (e.g. by removing <exclude-unlisted-classes> element) 
" +
+                            "and include them explicitly.",
+                            InternalUtils.join(affectedUnits)));
+        }
+    }
+
     private List<TapestryPersistenceUnitInfo> 
parsePersistenceUnitInfos(Resource persistenceDescriptor)
     {
         List<TapestryPersistenceUnitInfo> persistenceUnitInfos = 
CollectionFactory.newList();

Added: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java?rev=1159194&view=auto
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
 (added)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImplTest.java
 Thu Aug 18 12:41:53 2011
@@ -0,0 +1,51 @@
+// Copyright 2011 The Apache Software Foundation
+//
+// 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.apache.tapestry5.internal.jpa;
+
+import org.apache.tapestry5.ioc.internal.util.ClasspathResource;
+import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
+import org.apache.tapestry5.jpa.PersistenceUnitConfigurer;
+import org.apache.tapestry5.test.TapestryTestCase;
+import org.slf4j.LoggerFactory;
+import org.testng.annotations.Test;
+
+public class EntityManagerSourceImplTest extends TapestryTestCase
+{
+    @Test
+    public void multiple_persistence_units_include_unlisted_classes()
+    {
+        Exception exception = null;
+
+        try
+        {
+            new EntityManagerSourceImpl(
+                    LoggerFactory.getLogger(EntityManagerSourceImplTest.class),
+                    new 
ClasspathResource("multiple-persistence-units-include-unlisted-classes.xml"),
+                    null,
+                    CollectionFactory.<String, 
PersistenceUnitConfigurer>newMap());
+
+            fail("Exception expected");
+
+        } catch (Exception e)
+        {
+            exception = e;
+        }
+
+        assertNotNull(exception);
+
+        assertEquals(exception.getMessage(), "Persistence units 'TestUnit, 
TestUnit2' are configured to include managed classes that have not been 
explicitly listed. This is forbidden when multiple persistence units are used 
in the same application. Please configure persistence units to exclude unlisted 
managed classes (e.g. by removing <exclude-unlisted-classes> element) and 
include them explicitly.");
+
+    }
+}

Copied: 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/multiple-persistence-units-include-unlisted-classes.xml
 (from r1159181, 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/single-persistence-unit.xml)
URL: 
http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/multiple-persistence-units-include-unlisted-classes.xml?p2=tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/multiple-persistence-units-include-unlisted-classes.xml&p1=tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/single-persistence-unit.xml&r1=1159181&r2=1159194&rev=1159194&view=diff
==============================================================================
--- 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/single-persistence-unit.xml
 (original)
+++ 
tapestry/tapestry5/trunk/tapestry-jpa/src/test/resources/multiple-persistence-units-include-unlisted-classes.xml
 Thu Aug 18 12:41:53 2011
@@ -1,15 +1,40 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright 2011 The Apache Software Foundation -->
+<!-- -->
+<!-- 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. -->
+
 <persistence xmlns="http://java.sun.com/xml/ns/persistence";
-       version="2.0">
+             version="2.0">
+
+    <persistence-unit name="TestUnit" transaction-type="RESOURCE_LOCAL">
+        <exclude-unlisted-classes>false</exclude-unlisted-classes>
+        <properties>
+            <property name="javax.persistence.jdbc.driver" 
value="org.h2.Driver"/>
+            <property name="javax.persistence.jdbc.url" 
value="jdbc:h2:mem:test"/>
+            <property name="eclipselink.ddl-generation" value="create-tables"/>
+            <property name="eclipselink.logging.level" value="fine"/>
+        </properties>
+    </persistence-unit>
 
-       <persistence-unit name="App2PersistenceUnit" 
transaction-type="RESOURCE_LOCAL">
-               <properties>
-                       <property name="javax.persistence.jdbc.driver" 
value="org.h2.Driver" />
-                       <property name="javax.persistence.jdbc.url" 
value="jdbc:h2:mem:test" />
-                       <property name="javax.persistence.jdbc.username" 
value="sa" />
-                       <property name="eclipselink.ddl-generation" 
value="create-tables"/>
-                       <property name="eclipselink.logging.level" 
value="fine"/>
-               </properties>
-       </persistence-unit>
+    <persistence-unit name="TestUnit2" transaction-type="RESOURCE_LOCAL">
+        <exclude-unlisted-classes>false</exclude-unlisted-classes>
+        <properties>
+            <property name="javax.persistence.jdbc.driver" 
value="org.h2.Driver"/>
+            <property name="javax.persistence.jdbc.url" 
value="jdbc:h2:mem:test"/>
+            <property name="javax.persistence.jdbc.username" value="sa"/>
+            <property name="eclipselink.ddl-generation" value="create-tables"/>
+            <property name="eclipselink.logging.level" value="fine"/>
+        </properties>
+    </persistence-unit>
 
 </persistence>
\ No newline at end of file


Reply via email to