Browse files

TAP5-1848: Persistence providers defined for a unit is not ignored an…

…ymore

git-svn-id: https://svn.apache.org/repos/asf/tapestry/tapestry5/trunk@1329111 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent 74fa797 commit 02450921bf1453238b99cb922f47d84208fe4ebd @drobiazko drobiazko committed Apr 23, 2012
View
50 tapestry-jpa/src/main/java/org/apache/tapestry5/internal/jpa/EntityManagerSourceImpl.java
@@ -192,7 +192,6 @@ public EntityManagerFactory getEntityManagerFactory(final String persistenceUnit
private EntityManagerFactory createEntityManagerFactory(final String persistenceUnitName)
{
- final PersistenceProvider persistenceProvider = getPersistenceProvider();
for (final TapestryPersistenceUnitInfo info : persistenceUnitInfos)
{
@@ -201,6 +200,10 @@ private EntityManagerFactory createEntityManagerFactory(final String persistence
final Map<String, String> properties = CollectionFactory.newCaseInsensitiveMap();
properties.put(JpaConstants.PERSISTENCE_UNIT_NAME, persistenceUnitName);
+ String providerClassName = info.getPersistenceProviderClassName();
+
+ final PersistenceProvider persistenceProvider = getPersistenceProvider(persistenceUnitName, providerClassName);
+
return persistenceProvider.createContainerEntityManagerFactory(info, properties);
}
}
@@ -210,20 +213,63 @@ private EntityManagerFactory createEntityManagerFactory(final String persistence
persistenceUnitName));
}
- private PersistenceProvider getPersistenceProvider()
+ private PersistenceProvider getPersistenceProvider(final String persistenceUnitName, final String providerClassName)
{
final PersistenceProviderResolver resolver = PersistenceProviderResolverHolder
.getPersistenceProviderResolver();
final List<PersistenceProvider> providers = resolver.getPersistenceProviders();
if (providers.isEmpty())
+ {
throw new IllegalStateException(
"No PersistenceProvider implementation available in the runtime environment.");
+ }
+
+ if(1 < providers.size() && providerClassName == null)
+ {
+ throw new IllegalStateException(
+ String.format("Persistence providers [%s] are available in the runtime environment " +
+ "but no provider class is defined for the persistence unit %s.", InternalUtils.join(toProviderClasses(providers)), persistenceUnitName));
+ }
+
+ if(providerClassName != null)
+ {
+ return findPersistenceProviderByName(providers, providerClassName);
+ }
return providers.get(0);
}
+ private PersistenceProvider findPersistenceProviderByName(final List<PersistenceProvider> providers, final String providerClassName)
+ {
+ PersistenceProvider provider = F.flow(providers).filter(new Predicate<PersistenceProvider>() {
+ @Override
+ public boolean accept(PersistenceProvider next) {
+ return next.getClass().getName().equals(providerClassName);
+ }
+ }).first();
+
+ if(provider == null)
+ {
+ throw new IllegalStateException(
+ String.format("No persistence provider of type %s found in the runtime environment. " +
+ "Following providers are available: [%s]", providerClassName, InternalUtils.join(toProviderClasses(providers))));
+ }
+
+ return provider;
+ }
+
+ private List<Class> toProviderClasses(final List<PersistenceProvider> providers)
+ {
+ return F.flow(providers).map(new Mapper<PersistenceProvider, Class>() {
+ @Override
+ public Class map(PersistenceProvider element) {
+ return element.getClass();
+ }
+ }).toList();
+ }
+
public EntityManager create(final String persistenceUnitName)
{
return getEntityManagerFactory(persistenceUnitName).createEntityManager();
View
7 tapestry-jpa/src/test/app5/Index.tml
@@ -0,0 +1,7 @@
+<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd">
+<body>
+<p>
+ EntityManager Class Name: <span>${entityManagerClassName}</span>
+</p>
+</body>
+</html>
View
33 tapestry-jpa/src/test/app5/WEB-INF/web.xml
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Copyright 2012 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. -->
+
+<!DOCTYPE web-app
+ PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
+ "http://java.sun.com/dtd/web-app_2_3.dtd">
+<web-app>
+ <display-name>Tapestry-JPA Integration Test Application</display-name>
+ <context-param>
+ <param-name>tapestry.app-package</param-name>
+ <param-value>org.example.app5</param-value>
+ </context-param>
+ <filter>
+ <filter-name>app</filter-name>
+ <filter-class>org.apache.tapestry5.TapestryFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>app</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+</web-app>
View
7 tapestry-jpa/src/test/conf/testng.xml
@@ -31,6 +31,13 @@
</packages>
</test>
+ <test name="Explicit Provider Class Name In Persistence Unit Integration Tests" enabled="true">
+ <parameter name="tapestry.web-app-folder" value="src/test/app5" />
+ <packages>
+ <package name="org.apache.tapestry5.jpa.integration.app5" />
+ </packages>
+ </test>
+
<test name="Tapestry JPA Unit Tests" enabled="true">
<packages>
<package name="org.apache.tapestry5.internal.jpa" />
View
240 tapestry-jpa/src/test/java/org/apache/tapestry5/jpa/integration/app5/DummyEntityManager.java
@@ -0,0 +1,240 @@
+// 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.jpa.integration.app5;
+
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.FlushModeType;
+import javax.persistence.LockModeType;
+import javax.persistence.Query;
+import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.metamodel.Metamodel;
+
+public class DummyEntityManager implements EntityManager
+{
+
+ public DummyEntityManager()
+ {
+
+ }
+
+ public void persist(Object entity)
+ {
+
+ }
+
+ public <T> T merge(T entity)
+ {
+ return null;
+ }
+
+ public void remove(Object entity)
+ {
+
+ }
+
+ public <T> T find(Class<T> entityClass, Object primaryKey)
+ {
+ return null;
+ }
+
+ public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties)
+ {
+ return null;
+ }
+
+ public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode)
+ {
+ return null;
+ }
+
+ public <T> T find(Class<T> entityClass, Object primaryKey, LockModeType lockMode,
+ Map<String, Object> properties)
+ {
+ return null;
+ }
+
+ public <T> T getReference(Class<T> entityClass, Object primaryKey)
+ {
+ return null;
+ }
+
+ public void flush()
+ {
+
+ }
+
+ public void setFlushMode(FlushModeType flushMode)
+ {
+
+ }
+
+ public FlushModeType getFlushMode()
+ {
+ return null;
+ }
+
+ public void lock(Object entity, LockModeType lockMode)
+ {
+
+ }
+
+ public void lock(Object entity, LockModeType lockMode, Map<String, Object> properties)
+ {
+
+ }
+
+ public void refresh(Object entity)
+ {
+
+ }
+
+ public void refresh(Object entity, Map<String, Object> properties)
+ {
+
+ }
+
+ public void refresh(Object entity, LockModeType lockMode)
+ {
+
+ }
+
+ public void refresh(Object entity, LockModeType lockMode, Map<String, Object> properties)
+ {
+
+ }
+
+ public void clear()
+ {
+
+ }
+
+ public void detach(Object entity)
+ {
+
+ }
+
+ public boolean contains(Object entity)
+ {
+ return false;
+ }
+
+ public LockModeType getLockMode(Object entity)
+ {
+ return null;
+ }
+
+ public void setProperty(String propertyName, Object value)
+ {
+
+ }
+
+ public Map<String, Object> getProperties()
+ {
+ return null;
+ }
+
+ public Query createQuery(String qlString)
+ {
+ return null;
+ }
+
+ public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery)
+ {
+ return null;
+ }
+
+ public <T> TypedQuery<T> createQuery(String qlString, Class<T> resultClass)
+ {
+ return null;
+ }
+
+ public Query createNamedQuery(String name)
+ {
+ return null;
+ }
+
+ public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass)
+ {
+ return null;
+ }
+
+ public Query createNativeQuery(String sqlString)
+ {
+ return null;
+ }
+
+ @SuppressWarnings("rawtypes")
+ public Query createNativeQuery(String sqlString, Class resultClass)
+ {
+ return null;
+ }
+
+ public Query createNativeQuery(String sqlString, String resultSetMapping)
+ {
+ return null;
+ }
+
+ public void joinTransaction()
+ {
+
+ }
+
+ public <T> T unwrap(Class<T> cls)
+ {
+ return null;
+ }
+
+ public Object getDelegate()
+ {
+ return null;
+ }
+
+ public void close()
+ {
+
+ }
+
+ public boolean isOpen()
+ {
+ return false;
+ }
+
+ public EntityTransaction getTransaction()
+ {
+ return null;
+ }
+
+ public EntityManagerFactory getEntityManagerFactory()
+ {
+ return null;
+ }
+
+ public CriteriaBuilder getCriteriaBuilder()
+ {
+ return null;
+ }
+
+ public Metamodel getMetamodel()
+ {
+ return null;
+ }
+
+}
View
72 ...pa/src/test/java/org/apache/tapestry5/jpa/integration/app5/DummyEntityManagerFactory.java
@@ -0,0 +1,72 @@
+// Copyright 2012 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.jpa.integration.app5;
+
+import javax.persistence.Cache;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.PersistenceUnitUtil;
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.metamodel.Metamodel;
+import java.util.Map;
+
+final class DummyEntityManagerFactory implements EntityManagerFactory
+{
+ public boolean isOpen()
+ {
+ return false;
+ }
+
+ public Map<String, Object> getProperties()
+ {
+ return null;
+ }
+
+ public PersistenceUnitUtil getPersistenceUnitUtil()
+ {
+ return null;
+ }
+
+ public Metamodel getMetamodel()
+ {
+ return null;
+ }
+
+ public CriteriaBuilder getCriteriaBuilder()
+ {
+ return null;
+ }
+
+ public Cache getCache()
+ {
+ return null;
+ }
+
+ @SuppressWarnings("rawtypes")
+ public EntityManager createEntityManager(Map map)
+ {
+ return new DummyEntityManager();
+ }
+
+ public EntityManager createEntityManager()
+ {
+ return new DummyEntityManager();
+ }
+
+ public void close()
+ {
+
+ }
+}
View
41 ...jpa/src/test/java/org/apache/tapestry5/jpa/integration/app5/DummyPersistenceProvider.java
@@ -0,0 +1,41 @@
+// Copyright 2012 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.jpa.integration.app5;
+
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.ProviderUtil;
+import java.util.Map;
+
+@SuppressWarnings("rawtypes")
+public class DummyPersistenceProvider implements PersistenceProvider
+{
+ public EntityManagerFactory createEntityManagerFactory(String emName, Map map)
+ {
+ return new DummyEntityManagerFactory();
+ }
+
+ public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map)
+ {
+ return new DummyEntityManagerFactory();
+ }
+
+ public ProviderUtil getProviderUtil()
+ {
+ return null;
+ }
+
+}
View
31 .../java/org/apache/tapestry5/jpa/integration/app5/ExplicitPersistenceProviderClassTest.java
@@ -0,0 +1,31 @@
+// Copyright 2012 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.jpa.integration.app5;
+
+import org.apache.tapestry5.test.SeleniumTestCase;
+import org.testng.annotations.Test;
+
+public class ExplicitPersistenceProviderClassTest extends SeleniumTestCase
+{
+
+ @Test
+ public void check_entitymanager_class_name() throws Exception
+ {
+
+ open("/");
+
+ assertEquals(getText("//span"), DummyEntityManager.class.getName());
+ }
+}
View
29 tapestry-jpa/src/test/java/org/example/app5/pages/Index.java
@@ -0,0 +1,29 @@
+// Copyright 2012 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.example.app5.pages;
+
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+
+public class Index
+{
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ public String getEntityManagerClassName() {
+ return entityManager.getClass().getName();
+ }
+}
View
58 tapestry-jpa/src/test/java/org/example/app5/services/AppModule.java
@@ -0,0 +1,58 @@
+// Copyright 2012 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.example.app5.services;
+
+import org.apache.tapestry5.ioc.MappedConfiguration;
+import org.apache.tapestry5.ioc.annotations.Contribute;
+import org.apache.tapestry5.ioc.annotations.SubModule;
+import org.apache.tapestry5.ioc.services.ApplicationDefaults;
+import org.apache.tapestry5.ioc.services.SymbolProvider;
+import org.apache.tapestry5.jpa.JpaModule;
+import org.apache.tapestry5.jpa.JpaSymbols;
+import org.apache.tapestry5.jpa.integration.app5.DummyPersistenceProvider;
+
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceProviderResolver;
+import javax.persistence.spi.PersistenceProviderResolverHolder;
+import java.util.Arrays;
+import java.util.List;
+
+@SubModule(JpaModule.class)
+public class AppModule {
+
+ static {
+
+ PersistenceProviderResolverHolder.setPersistenceProviderResolver(new PersistenceProviderResolver() {
+ @Override
+ public List<PersistenceProvider> getPersistenceProviders() {
+ return Arrays.<PersistenceProvider>asList(new DummyPersistenceProvider());
+ }
+
+ @Override
+ public void clearCachedProviders() {
+ }
+ }
+
+ );
+ }
+
+ @Contribute(SymbolProvider.class)
+ @ApplicationDefaults
+ public static void provideApplicationDefaults(
+ final MappedConfiguration<String, String> configuration) {
+ configuration.add(JpaSymbols.PROVIDE_ENTITY_VALUE_ENCODERS, "false");
+ configuration.add(JpaSymbols.PERSISTENCE_DESCRIPTOR, "/explicit-persistence-provider-class-persistence-unit.xml");
+ }
+}
View
16 tapestry-jpa/src/test/resources/explicit-persistence-provider-class-persistence-unit.xml
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<persistence xmlns="http://java.sun.com/xml/ns/persistence"
+ version="2.0">
+
+ <persistence-unit name="App2PersistenceUnit" transaction-type="RESOURCE_LOCAL">
+ <provider>org.apache.tapestry5.jpa.integration.app5.DummyPersistenceProvider</provider>
+ <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>

0 comments on commit 0245092

Please sign in to comment.