[hibernate-commits] Hibernate SVN: r18636 - in core/trunk: entitymanager/src/main/java/org/hibernate/ejb and 1 other directories.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jan 26 13:36:59 EST 2010


Author: steve.ebersole at jboss.com
Date: 2010-01-26 13:36:59 -0500 (Tue, 26 Jan 2010)
New Revision: 18636

Added:
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java
   core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/PersistenceUnitInfoAdapter.java
Modified:
   core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
   core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java
Log:
HHH-4678 - Apply PersistenceUnitInfo#getSharedCacheMode and #getValidationMode


Modified: core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
--- core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-01-26 18:06:40 UTC (rev 18635)
+++ core/trunk/annotations/src/main/java/org/hibernate/cfg/AnnotationBinder.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -769,12 +769,7 @@
 		}
 
 		Cacheable cacheableAnn = clazzToProcess.getAnnotation( Cacheable.class );
-		SharedCacheMode mode = (SharedCacheMode) mappings.getConfigurationProperties().get(
-				"javax.persistence.sharedCache.mode"
-		);
-		if ( mode == null ) {
-			mode = SharedCacheMode.UNSPECIFIED;
-		}
+		SharedCacheMode mode = determineSharedCacheMode( mappings );
 		switch ( mode ) {
 			case ALL: {
 				cacheAnn = buildCacheMock( clazzToProcess.getName(), mappings );
@@ -800,6 +795,33 @@
 		return cacheAnn;
 	}
 
+	private static SharedCacheMode determineSharedCacheMode(ExtendedMappings mappings) {
+		SharedCacheMode mode;
+		final Object value = mappings.getConfigurationProperties().get( "javax.persistence.sharedCache.mode" );
+		if ( value == null ) {
+			log.debug( "no value specified for 'javax.persistence.sharedCache.mode'; using UNSPECIFIED" );
+			mode = SharedCacheMode.UNSPECIFIED;
+		}
+		else {
+			if ( SharedCacheMode.class.isInstance( value ) ) {
+				mode = ( SharedCacheMode ) value;
+			}
+			else {
+				try {
+					mode = SharedCacheMode.valueOf( value.toString() );
+				}
+				catch ( Exception e ) {
+					log.debug( 
+							"Unable to resolve given mode name [" + value.toString()
+									+ "]; using UNSPECIFIED : " + e.toString()
+					);
+					mode = SharedCacheMode.UNSPECIFIED;
+				}
+			}
+		}
+		return mode;
+	}
+
 	private static Cache buildCacheMock(String region, ExtendedMappings mappings) {
 		return new LocalCacheAnnotationImpl( region, determineCacheConcurrencyStrategy( mappings ) );
 	}

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java	2010-01-26 18:06:40 UTC (rev 18635)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/AvailableSettings.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -147,6 +147,11 @@
 	public static final String VALIDATION_MODE = "javax.persistence.validation.mode";
 
 	/**
+	 * Used to pass along any discovered validator factory.
+	 */
+	public static final String VALIDATION_FACTORY = "javax.persistence.validation.factory";
+
+	/**
 	 * Used to request (hint) a pessimistic lock scope.
 	 * <p/>
 	 * See JPA 2 sections 8.2.1.9 and 3.4.4.3

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java	2010-01-26 18:06:40 UTC (rev 18635)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/Ejb3Configuration.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -54,6 +54,7 @@
 import javax.persistence.EntityNotFoundException;
 import javax.persistence.MappedSuperclass;
 import javax.persistence.PersistenceException;
+import javax.persistence.SharedCacheMode;
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.PersistenceUnitTransactionType;
 import javax.sql.DataSource;
@@ -161,6 +162,7 @@
 	 * If used, be sure to <b>not override</b> the hibernate.connection.provider_class
 	 * property
 	 */
+	@SuppressWarnings({ "JavaDoc", "unchecked" })
 	public void setDataSource(DataSource ds) {
 		if ( ds != null ) {
 			Map cpInjection = new HashMap();
@@ -173,10 +175,23 @@
 	/**
 	 * create a factory from a parsed persistence.xml
 	 * Especially the scanning of classes and additional jars is done already at this point.
+	 * <p/>
+	 * NOTE: public only for unit testing purposes; not a public API!
+	 *
+	 * @param metadata The information parsed from the persistence.xml
+	 * @param overridesIn Any explicitly passed config settings
+	 *
+	 * @return this
 	 */
-	private Ejb3Configuration configure(PersistenceMetadata metadata, Map overrides) {
+	@SuppressWarnings({ "unchecked" })
+	public Ejb3Configuration configure(PersistenceMetadata metadata, Map overridesIn) {
 		log.debug( "Creating Factory: {}", metadata.getName() );
 
+		Map overrides = new HashMap();
+		if ( overridesIn != null ) {
+			overrides.putAll( overridesIn );
+		}
+
 		Map workingVars = new HashMap();
 		workingVars.put( AvailableSettings.PERSISTENCE_UNIT_NAME, metadata.getName() );
 		this.persistenceUnitName = metadata.getName();
@@ -218,20 +233,46 @@
 		if ( metadata.getHbmfiles().size() > 0 ) {
 			workingVars.put( AvailableSettings.HBXML_FILES, metadata.getHbmfiles() );
 		}
-		if ( metadata.getValidationMode() != null) {
-			workingVars.put( AvailableSettings.VALIDATION_MODE, metadata.getValidationMode() );
-		}
-		if ( metadata.getSharedCacheMode() != null) {
-			workingVars.put( AvailableSettings.SHARED_CACHE_MODE, metadata.getSharedCacheMode() );
-		}
+
 		Properties props = new Properties();
 		props.putAll( metadata.getProps() );
-		if ( overrides != null ) {
-			for ( Map.Entry entry : (Set<Map.Entry>) overrides.entrySet() ) {
-				Object value = entry.getValue();
-				props.put( entry.getKey(), value == null ? "" :  value ); //alter null, not allowed in properties
+
+		// validation factory
+		final Object validationFactory = overrides.get( AvailableSettings.VALIDATION_FACTORY );
+		if ( validationFactory != null ) {
+			props.put( AvailableSettings.VALIDATION_FACTORY, validationFactory );
+		}
+		overrides.remove( AvailableSettings.VALIDATION_FACTORY );
+
+		// validation-mode (overrides has precedence)
+		{
+			final Object integrationValue = overrides.get( AvailableSettings.VALIDATION_MODE );
+			if ( integrationValue != null ) {
+				props.put( AvailableSettings.VALIDATION_MODE, integrationValue.toString() );
 			}
+			else if ( metadata.getValidationMode() != null ) {
+				props.put( AvailableSettings.VALIDATION_MODE, metadata.getValidationMode() );
+			}
+			overrides.remove( AvailableSettings.VALIDATION_MODE );
 		}
+
+		// shared-cache-mode (overrides has precedence)
+		{
+			final Object integrationValue = overrides.get( AvailableSettings.SHARED_CACHE_MODE );
+			if ( integrationValue != null ) {
+				props.put( AvailableSettings.SHARED_CACHE_MODE, integrationValue.toString() );
+			}
+			else if ( metadata.getSharedCacheMode() != null ) {
+				props.put( AvailableSettings.SHARED_CACHE_MODE, metadata.getSharedCacheMode() );
+			}
+			overrides.remove( AvailableSettings.SHARED_CACHE_MODE );
+		}
+
+		for ( Map.Entry entry : (Set<Map.Entry>) overrides.entrySet() ) {
+			Object value = entry.getValue();
+			props.put( entry.getKey(), value == null ? "" :  value ); //alter null, not allowed in properties
+		}
+
 		configure( props, workingVars );
 		return this;
 	}
@@ -246,8 +287,12 @@
 	 *
 	 * @param persistenceUnitName persistence unit name
 	 * @param integration properties passed to the persistence provider
+	 *
 	 * @return configured Ejb3Configuration or null if no persistence unit match
+	 *
+	 * @see HibernatePersistence#createEntityManagerFactory(String, java.util.Map)
 	 */
+	@SuppressWarnings({ "unchecked" })
 	public Ejb3Configuration configure(String persistenceUnitName, Map integration) {
 		try {
 			log.debug( "Look up for persistence unit: {}", persistenceUnitName );
@@ -416,9 +461,18 @@
 	}
 
 	/**
-	 * Process configuration from a PersistenceUnitInfo object
-	 * Typically called by the container
+	 * Process configuration from a PersistenceUnitInfo object; typically called by the container
+	 * via {@link javax.persistence.spi.PersistenceProvider#createContainerEntityManagerFactory}.
+	 * In Hibernate EM, this correlates to {@link HibernatePersistence#createContainerEntityManagerFactory}
+	 *
+	 * @param info The persistence unit info passed in by the container (usually from processing a persistence.xml).
+	 * @param integration The map of integration properties from the container to configure the provider.
+	 *
+	 * @return The configured EJB3Configurartion object
+	 *
+	 * @see HibernatePersistence#createContainerEntityManagerFactory
 	 */
+	@SuppressWarnings({ "unchecked" })
 	public Ejb3Configuration configure(PersistenceUnitInfo info, Map integration) {
 		if ( log.isDebugEnabled() ) {
 			log.debug( "Processing {}", LogHelper.logPersistenceUnitInfo( info ) );
@@ -427,19 +481,26 @@
 			log.info( "Processing PersistenceUnitInfo [\n\tname: {}\n\t...]", info.getPersistenceUnitName() );
 		}
 
+		// Spec says the passed map may be null, so handle that to make further processing easier...
 		integration = integration != null ? Collections.unmodifiableMap( integration ) : CollectionHelper.EMPTY_MAP;
+
+		// See if we (Hibernate) are the persistence provider
 		String provider = (String) integration.get( AvailableSettings.PROVIDER );
-		if ( provider == null ) provider = info.getPersistenceProviderClassName();
+		if ( provider == null ) {
+			provider = info.getPersistenceProviderClassName();
+		}
 		if ( provider != null && ! provider.trim().startsWith( IMPLEMENTATION_NAME ) ) {
 			log.info( "Required a different provider: {}", provider );
 			return null;
 		}
+
+		// set the classloader, passed in by the container in info, to set as the TCCL so that
+		// Hibernate uses it to properly resolve class references.
 		if ( info.getClassLoader() == null ) {
 			throw new IllegalStateException(
 					"[PersistenceUnit: " + info.getPersistenceUnitName() == null ? "" : info.getPersistenceUnitName()
 							+ "] " + "PersistenceUnitInfo.getClassLoader() id null" );
 		}
-		//set the classloader
 		Thread thread = Thread.currentThread();
 		ClassLoader contextClassLoader = thread.getContextClassLoader();
 		boolean sameClassLoader = info.getClassLoader().equals( contextClassLoader );
@@ -451,6 +512,10 @@
 			overridenClassLoader = null;
 		}
 
+		// Best I can tell, 'workingVars' is some form of additional configuration contract.
+		// But it does not correlate 1-1 to EMF/SF settings.  It really is like a set of de-typed
+		// additional configuration info.  I think it makes better sense to define this as an actual
+		// contract if that was in fact the intent; the code here is pretty confusing.
 		try {
 			Map workingVars = new HashMap();
 			workingVars.put( AvailableSettings.PERSISTENCE_UNIT_NAME, info.getPersistenceUnitName() );
@@ -460,7 +525,9 @@
 			List<NamedInputStream> hbmFiles = new ArrayList<NamedInputStream>();
 			List<String> packages = new ArrayList<String>();
 			List<String> xmlFiles = new ArrayList<String>( 50 );
-			if ( info.getMappingFileNames() != null ) xmlFiles.addAll( info.getMappingFileNames() );
+			if ( info.getMappingFileNames() != null ) {
+				xmlFiles.addAll( info.getMappingFileNames() );
+			}
 			//Should always be true if the container is not dump
 			boolean searchForORMFiles = ! xmlFiles.contains( META_INF_ORM_XML );
 
@@ -481,9 +548,7 @@
 			setDetectedArtifactsOnScanningContext( context, info.getProperties(), null, info.excludeUnlistedClasses() );
 			scanForClasses( context, packages, entities, hbmFiles );
 
-			Properties properties = info.getProperties() != null ?
-					info.getProperties() :
-					new Properties();
+			Properties properties = info.getProperties() != null ? info.getProperties() : new Properties();
 			ConfigurationHelper.overrideProperties( properties, integration );
 
 			//FIXME entities is used to enhance classes and to collect annotated entities this should not be mixed
@@ -500,18 +565,34 @@
 			workingVars.put( AvailableSettings.XML_FILE_NAMES, xmlFiles );
 			if ( hbmFiles.size() > 0 ) workingVars.put( AvailableSettings.HBXML_FILES, hbmFiles );
 
-			//validation-mode
-			final Object validationMode = info.getValidationMode();
-			if ( validationMode != null) {
-				workingVars.put( AvailableSettings.VALIDATION_MODE, validationMode );
+			// validation factory
+			final Object validationFactory = integration.get( AvailableSettings.VALIDATION_FACTORY );
+			if ( validationFactory != null ) {
+				properties.put( AvailableSettings.VALIDATION_FACTORY, validationFactory );
 			}
 
-			//shared-cache-mode
-			final Object sharedCacheMode = info.getSharedCacheMode();
-			if ( sharedCacheMode != null) {
-				workingVars.put( AvailableSettings.SHARED_CACHE_MODE, sharedCacheMode );
+			// validation-mode (integration has precedence)
+			{
+				final Object integrationValue = integration.get( AvailableSettings.VALIDATION_MODE );
+				if ( integrationValue != null ) {
+					properties.put( AvailableSettings.VALIDATION_MODE, integrationValue.toString() );
+				}
+				else if ( info.getValidationMode() != null ) {
+					properties.put( AvailableSettings.VALIDATION_MODE, info.getValidationMode().name() );
+				}
 			}
 
+			// shared-cache-mode (integration has precedence)
+			{
+				final Object integrationValue = integration.get( AvailableSettings.SHARED_CACHE_MODE );
+				if ( integrationValue != null ) {
+					properties.put( AvailableSettings.SHARED_CACHE_MODE, integrationValue.toString() );
+				}
+				else if ( info.getSharedCacheMode() != null ) {
+					properties.put( AvailableSettings.SHARED_CACHE_MODE, info.getSharedCacheMode().name() );
+				}
+			}
+
 			//datasources
 			Boolean isJTA = null;
 			boolean overridenDatasource = false;
@@ -837,12 +918,20 @@
 	}
 
 	/**
-	 * create a factory from a canonical workingVars map and the overriden properties
+	 * Configures this configuration object from 2 distinctly different sources.
 	 *
+	 * @param properties These are the properties that came from the user, either via
+	 * a persistence.xml or explicitly passed in to one of our
+	 * {@link javax.persistence.spi.PersistenceProvider}/{@link HibernatePersistence} contracts.
+	 * @param workingVars Is collection of settings which need to be handled similarly
+	 * between the 2 main bootstrap methods, but where the values are determine very differently
+	 * by each bootstrap method.  todo eventually make this a contract (class/interface)
+	 *
+	 * @return The configured configuration
+	 *
+	 * @see HibernatePersistence
 	 */
-	private Ejb3Configuration configure(
-			Properties properties, Map workingVars
-	) {
+	private Ejb3Configuration configure(Properties properties, Map workingVars) {
 		//TODO check for people calling more than once this method (except buildEMF)
 		if (isConfigurationProcessed) return this;
 		isConfigurationProcessed = true;

Modified: core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java
===================================================================
--- core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java	2010-01-26 18:06:40 UTC (rev 18635)
+++ core/trunk/entitymanager/src/main/java/org/hibernate/ejb/HibernatePersistence.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -26,6 +26,7 @@
 import java.util.Map;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.spi.LoadState;
+import javax.persistence.spi.PersistenceProvider;
 import javax.persistence.spi.PersistenceUnitInfo;
 import javax.persistence.spi.ProviderUtil;
 
@@ -36,34 +37,50 @@
  *
  * @author Gavin King
  */
-public class HibernatePersistence implements javax.persistence.spi.PersistenceProvider {
+public class HibernatePersistence extends AvailableSettings implements PersistenceProvider {
 
-	//The following properties are for Internal use only
 	/**
-	 * link to the alternative Hibernate configuration file
-	 * Internal use only
+	 * Get an entity manager factory by its entity manager name, using the specified
+	 * properties (they override any found in the peristence.xml file).
+	 * <p/>
+	 * This is the form used in JSE environments.
+	 *
+	 * @param persistenceUnitName entity manager name
+	 * @param properties The explicit property values
+	 *
+	 * @return initialized EntityManagerFactory
 	 */
+	public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map properties) {
+		Ejb3Configuration cfg = new Ejb3Configuration();
+		Ejb3Configuration configured = cfg.configure( persistenceUnitName, properties );
+		return configured != null ? configured.buildEntityManagerFactory() : null;
+	}
 
-
 	/**
-	 * Get an entity manager factory by its entity manager name and given the
-	 * appropriate extra properties. Those proeprties override the one get through
-	 * the peristence.xml file.
+	 * Create an entity manager factory from the given persistence unit info, using the specified
+	 * properties (they override any on the PUI).
+	 * <p/>
+	 * This is the form used by the container in a JEE environment.
 	 *
-	 * @param persistenceUnitName entity manager name
-	 * @param overridenProperties properties passed to the persistence provider
+	 * @param info The persistence unit information
+	 * @param properties The explicit property values
+	 *
 	 * @return initialized EntityManagerFactory
 	 */
-	public EntityManagerFactory createEntityManagerFactory(String persistenceUnitName, Map overridenProperties) {
+	public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map properties) {
 		Ejb3Configuration cfg = new Ejb3Configuration();
-		Ejb3Configuration configured = cfg.configure( persistenceUnitName, overridenProperties );
+		Ejb3Configuration configured = cfg.configure( info, properties );
 		return configured != null ? configured.buildEntityManagerFactory() : null;
 	}
 
-	public EntityManagerFactory createContainerEntityManagerFactory(PersistenceUnitInfo info, Map map) {
+	/**
+	 * create a factory from a canonical version
+	 * @deprecated
+	 */
+	public EntityManagerFactory createEntityManagerFactory(Map properties) {
+		// This is used directly by JBoss so don't remove until further notice.  bill at jboss.org
 		Ejb3Configuration cfg = new Ejb3Configuration();
-		Ejb3Configuration configured = cfg.configure( info, map );
-		return configured != null ? configured.buildEntityManagerFactory() : null;
+		return cfg.createEntityManagerFactory( properties );
 	}
 
 	private final ProviderUtil providerUtil = new ProviderUtil() {
@@ -80,18 +97,11 @@
 		}
 	};
 
+	/**
+	 * {@inheritDoc}
+	 */
 	public ProviderUtil getProviderUtil() {
 		return providerUtil;
 	}
 
-	/**
-	 * create a factory from a canonical version
-	 * @deprecated
-	 */
-	public EntityManagerFactory createEntityManagerFactory(Map properties) {
-		// This is used directly by JBoss so don't remove until further notice.  bill at jboss.org
-		Ejb3Configuration cfg = new Ejb3Configuration();
-		return cfg.createEntityManagerFactory( properties );
-	}
-
 }
\ No newline at end of file

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/ConfigurationObjectSettingTest.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -0,0 +1,249 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.ejb.test.ejb3configuration;
+
+import java.util.Collections;
+import java.util.Map;
+import javax.persistence.SharedCacheMode;
+import javax.persistence.ValidationMode;
+
+import org.hibernate.HibernateException;
+import org.hibernate.ejb.AvailableSettings;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.ejb.packaging.PersistenceMetadata;
+
+/**
+ * Test passing along various config settings that take objects other than strings as values.
+ *
+ * @author Steve Ebersole
+ */
+public class ConfigurationObjectSettingTest extends junit.framework.TestCase {
+	public void testContainerBootstrapSharedCacheMode() {
+		// first, via the integration vars
+		PersistenceUnitInfoAdapter empty = new PersistenceUnitInfoAdapter();
+		{
+			// as object
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					empty,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+		{
+			// as string
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					empty,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE.name() )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+
+		// next, via the PUI
+		PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter() {
+			@Override
+			public SharedCacheMode getSharedCacheMode() {
+				return SharedCacheMode.ENABLE_SELECTIVE;
+			}
+		};
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure( adapter, null );
+			assertEquals( SharedCacheMode.ENABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+
+		// via both, integration vars should take precedence
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					adapter,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+	}
+
+	public void testContainerBootstrapValidationMode() {
+		// first, via the integration vars
+		PersistenceUnitInfoAdapter empty = new PersistenceUnitInfoAdapter();
+		{
+			// as object
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					empty,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK )
+			);
+			assertEquals( ValidationMode.CALLBACK.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+		{
+			// as string
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					empty,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK.name() )
+			);
+			assertEquals( ValidationMode.CALLBACK.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+
+		// next, via the PUI
+		PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter() {
+			@Override
+			public ValidationMode getValidationMode() {
+				return ValidationMode.CALLBACK;
+			}
+		};
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure( adapter, null );
+			assertEquals( ValidationMode.CALLBACK.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+
+		// via both, integration vars should take precedence
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					adapter,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.NONE )
+			);
+			assertEquals( ValidationMode.NONE.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+	}
+
+	public void testContainerBootstrapValidationFactory() {
+		final Object token = new Object();
+		PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
+		Ejb3Configuration cfg = new Ejb3Configuration();
+		try {
+			cfg.configure(
+					adapter,
+					Collections.singletonMap( AvailableSettings.VALIDATION_FACTORY, token )
+			);
+			fail( "Was expecting error as token did not implement ValidatorFactory" );
+		}
+		catch ( HibernateException e ) {
+			// probably the condition we want but unfortunately the exception is not specific
+			// and the pertinent info is in a cause
+		}
+	}
+
+	public void testStandaloneBootstrapSharedCacheMode() {
+		// first, via the integration vars
+		PersistenceMetadata metadata = new PersistenceMetadata();
+		{
+			// as object
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+		{
+			// as string
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE.name() )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+
+		// next, via the PM
+		metadata.setSharedCacheMode( SharedCacheMode.ENABLE_SELECTIVE.name() );
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure( metadata, null );
+			assertEquals( SharedCacheMode.ENABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+
+		// via both, integration vars should take precedence
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.DISABLE_SELECTIVE )
+			);
+			assertEquals( SharedCacheMode.DISABLE_SELECTIVE.name(), configured.getProperties().get( AvailableSettings.SHARED_CACHE_MODE ) );
+		}
+	}
+
+	public void testStandaloneBootstrapValidationMode() {
+		// first, via the integration vars
+		PersistenceMetadata metadata = new PersistenceMetadata();
+		{
+			// as object
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK )
+			);
+			assertEquals( ValidationMode.CALLBACK.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+		{
+			// as string
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.CALLBACK.name() )
+			);
+			assertEquals( ValidationMode.CALLBACK.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+
+		// next, via the PUI
+		metadata.setValidationMode( ValidationMode.AUTO.name() );
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure( metadata, null );
+			assertEquals( ValidationMode.AUTO.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+
+		// via both, integration vars should take precedence
+		{
+			Ejb3Configuration cfg = new Ejb3Configuration();
+			Ejb3Configuration configured = cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.VALIDATION_MODE, ValidationMode.NONE )
+			);
+			assertEquals( ValidationMode.NONE.name(), configured.getProperties().get( AvailableSettings.VALIDATION_MODE ) );
+		}
+	}
+
+	public void testStandaloneBootstrapValidationFactory() {
+		final Object token = new Object();
+		PersistenceMetadata metadata = new PersistenceMetadata();
+		Ejb3Configuration cfg = new Ejb3Configuration();
+		try {
+			cfg.configure(
+					metadata,
+					Collections.singletonMap( AvailableSettings.VALIDATION_FACTORY, token )
+			);
+			fail( "Was expecting error as token did not implement ValidatorFactory" );
+		}
+		catch ( HibernateException e ) {
+			// probably the condition we want but unfortunately the exception is not specific
+			// and the pertinent info is in a cause
+		}
+	}
+}

Added: core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/PersistenceUnitInfoAdapter.java
===================================================================
--- core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/PersistenceUnitInfoAdapter.java	                        (rev 0)
+++ core/trunk/entitymanager/src/test/java/org/hibernate/ejb/test/ejb3configuration/PersistenceUnitInfoAdapter.java	2010-01-26 18:36:59 UTC (rev 18636)
@@ -0,0 +1,116 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Inc.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ */
+package org.hibernate.ejb.test.ejb3configuration;
+
+import java.net.URL;
+import java.util.Collections;
+import java.util.List;
+import java.util.Properties;
+import javax.persistence.SharedCacheMode;
+import javax.persistence.ValidationMode;
+import javax.persistence.spi.ClassTransformer;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
+
+import org.hibernate.ejb.HibernatePersistence;
+
+/**
+ * TODO : javadoc
+ *
+ * @author Steve Ebersole
+ */
+public class PersistenceUnitInfoAdapter implements PersistenceUnitInfo {
+	private Properties properties;
+
+	public String getPersistenceUnitName() {
+		return "persistenceUnitAdapter";
+	}
+
+	public String getPersistenceProviderClassName() {
+		return HibernatePersistence.class.getName();
+	}
+
+	public PersistenceUnitTransactionType getTransactionType() {
+		return null;
+	}
+
+	public DataSource getJtaDataSource() {
+		return null;
+	}
+
+	public DataSource getNonJtaDataSource() {
+		return null;
+	}
+
+	public List<String> getMappingFileNames() {
+		return Collections.emptyList();
+	}
+
+	public List<URL> getJarFileUrls() {
+		return Collections.emptyList();
+	}
+
+	public URL getPersistenceUnitRootUrl() {
+		return null;
+	}
+
+	public List<String> getManagedClassNames() {
+		return Collections.emptyList();
+	}
+
+	public boolean excludeUnlistedClasses() {
+		return false;
+	}
+
+	public SharedCacheMode getSharedCacheMode() {
+		return null;
+	}
+
+	public ValidationMode getValidationMode() {
+		return null;
+	}
+
+	public Properties getProperties() {
+		if ( properties == null ) {
+			properties = new Properties();
+		}
+		return properties;
+	}
+
+	public String getPersistenceXMLSchemaVersion() {
+		return null;
+	}
+
+	public ClassLoader getClassLoader() {
+		return Thread.currentThread().getContextClassLoader();
+	}
+
+	public void addTransformer(ClassTransformer transformer) {
+	}
+
+	public ClassLoader getNewTempClassLoader() {
+		return Thread.currentThread().getContextClassLoader();
+	}
+}



More information about the hibernate-commits mailing list