diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index 2dd1ba8f..196290aa 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -17,6 +17,8 @@ package org.springframework.data.gemfire; import java.io.File; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Properties; @@ -43,7 +45,6 @@ import com.gemstone.gemfire.GemFireException; import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.cache.CacheClosedException; import com.gemstone.gemfire.cache.CacheFactory; -import com.gemstone.gemfire.cache.CacheTransactionManager; import com.gemstone.gemfire.cache.DynamicRegionFactory; import com.gemstone.gemfire.cache.GemFireCache; import com.gemstone.gemfire.cache.TransactionListener; @@ -75,98 +76,55 @@ import com.gemstone.gemfire.pdx.PdxSerializer; */ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, InitializingBean, DisposableBean, FactoryBean, PersistenceExceptionTranslator { + + protected static final List VALID_JNDI_DATASOURCE_TYPE_NAMES = Collections.unmodifiableList( + Arrays.asList("ManagedDataSource", "PooledDataSource", "SimpleDataSource", "XAPooledDataSource")); + + protected boolean close = true; + protected boolean lazyInitialize = true; + protected boolean useBeanFactoryLocator = true; + protected final Log log = LogFactory.getLog(getClass()); - protected Cache cache; - - protected Resource cacheXml; - - protected Properties properties; - - protected ClassLoader beanClassLoader; - - protected GemfireBeanFactoryLocator factoryLocator; - protected BeanFactory beanFactory; - protected String beanName; - - protected boolean useBeanFactoryLocator = true; - - protected boolean close = true; - - // PDX options - protected Object pdxSerializer; - + protected Boolean copyOnRead; + protected Boolean pdxIgnoreUnreadFields; protected Boolean pdxPersistent; - protected Boolean pdxReadSerialized; - protected Boolean pdxIgnoreUnreadFields; + protected Cache cache; - protected String pdxDiskStoreName; - - protected Boolean copyOnRead; - - protected Integer lockTimeout; - - protected Integer lockLease; - - protected Integer messageSyncInterval; - - protected Integer searchTimeout; - - protected List transactionListeners; - - protected TransactionWriter transactionWriter; - - protected Float evictionHeapPercentage; - - protected Float criticalHeapPercentage; + protected ClassLoader beanClassLoader; protected DynamicRegionSupport dynamicRegionSupport; + protected Float criticalHeapPercentage; + protected Float evictionHeapPercentage; + + protected GemfireBeanFactoryLocator factoryLocator; + + protected Integer lockLease; + protected Integer lockTimeout; + protected Integer messageSyncInterval; + protected Integer searchTimeout; + protected List jndiDataSources; + protected List transactionListeners; + // Defined this way for backward compatibility protected Object gatewayConflictResolver; + protected Object pdxSerializer; - protected boolean lazyInitialize = true; + protected Properties properties; - /** - * Inner class to avoid a hard dependency on the GemFire 6.6 API. - * - * @author Costin Leau - */ + protected Resource cacheXml; - private class PdxOptions implements Runnable { + protected String beanName; + protected String pdxDiskStoreName; - private final CacheFactory factory; - - PdxOptions(CacheFactory factory) { - this.factory = factory; - } - - @Override - public void run() { - if (pdxSerializer != null) { - Assert.isAssignable(PdxSerializer.class, pdxSerializer.getClass(), "Invalid pdx serializer used"); - factory.setPdxSerializer((PdxSerializer) pdxSerializer); - } - if (pdxDiskStoreName != null) { - factory.setPdxDiskStore(pdxDiskStoreName); - } - if (pdxIgnoreUnreadFields != null) { - factory.setPdxIgnoreUnreadFields(pdxIgnoreUnreadFields); - } - if (pdxPersistent != null) { - factory.setPdxPersistent(pdxPersistent); - } - if (pdxReadSerialized != null) { - factory.setPdxReadSerialized(pdxReadSerialized); - } - } - } + protected TransactionWriter transactionWriter; public static class DynamicRegionSupport { private String diskDir; @@ -221,10 +179,11 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } public static class JndiDataSource { - private Map attributes; private List props; + private Map attributes; + public Map getAttributes() { return attributes; } @@ -242,47 +201,80 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } } - private void init() throws Exception { + /** + * Inner class to avoid a hard dependency on the GemFire 6.6 API. + * + * @author Costin Leau + */ + private class PdxOptions implements Runnable { + private final CacheFactory factory; + + PdxOptions(CacheFactory factory) { + this.factory = factory; + } + + @Override + public void run() { + if (pdxSerializer != null) { + Assert.isAssignable(PdxSerializer.class, pdxSerializer.getClass(), "Invalid pdx serializer used"); + factory.setPdxSerializer((PdxSerializer) pdxSerializer); + } + if (pdxDiskStoreName != null) { + factory.setPdxDiskStore(pdxDiskStoreName); + } + if (pdxIgnoreUnreadFields != null) { + factory.setPdxIgnoreUnreadFields(pdxIgnoreUnreadFields); + } + if (pdxPersistent != null) { + factory.setPdxPersistent(pdxPersistent); + } + if (pdxReadSerialized != null) { + factory.setPdxReadSerialized(pdxReadSerialized); + } + } + } + + private void init() throws Exception { if (useBeanFactoryLocator && factoryLocator == null) { factoryLocator = new GemfireBeanFactoryLocator(); factoryLocator.setBeanFactory(beanFactory); factoryLocator.setBeanName(beanName); factoryLocator.afterPropertiesSet(); } - // use the bean class loader to load Declarable classes - Thread th = Thread.currentThread(); - ClassLoader oldTCCL = th.getContextClassLoader(); + + final ClassLoader originalThreadContextClassLoader = Thread.currentThread().getContextClassLoader(); try { - th.setContextClassLoader(beanClassLoader); - // first look for open caches - String msg = null; + String messagePrefix; + + // use bean ClassLoader to load GemFire Declarable classes + Thread.currentThread().setContextClassLoader(beanClassLoader); + try { cache = (Cache) fetchCache(); - msg = "Retrieved existing"; - } catch (CacheClosedException ex) { - + messagePrefix = "Retrieved existing"; + } + catch (CacheClosedException ex) { initializeDynamicRegionFactory(); Object factory = createFactory(this.properties); // GemFire 6.6 specific options - if (pdxSerializer != null || pdxPersistent != null || pdxReadSerialized != null - || pdxIgnoreUnreadFields != null || pdxDiskStoreName != null) { + if (isPdxSettingsSpecified()) { Assert.isTrue(ClassUtils.isPresent("com.gemstone.gemfire.pdx.PdxSerializer", beanClassLoader), - "Cannot set PDX options since GemFire 6.6 not detected"); + "Cannot set PDX options since GemFire 6.6 not detected."); applyPdxOptions(factory); } // fall back to cache creation cache = (Cache) createCache(factory); - msg = "Created"; + messagePrefix = "Created new"; } + if (this.copyOnRead != null) { cache.setCopyOnRead(this.copyOnRead); } - if (lockLease != null) { cache.setLockLease(lockLease); } @@ -295,7 +287,6 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl if (messageSyncInterval != null) { cache.setMessageSyncInterval(messageSyncInterval); } - if (gatewayConflictResolver != null) { cache.setGatewayConflictResolver((GatewayConflictResolver) gatewayConflictResolver); } @@ -303,10 +294,12 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl DistributedSystem system = cache.getDistributedSystem(); DistributedMember member = system.getDistributedMember(); - log.info("Connected to Distributed System [" + system.getName() + "=" + member.getId() + "@" - + member.getHost() + "]"); - log.info(msg + " GemFire v." + CacheFactory.getVersion() + " Cache [" + cache.getName() + "]"); + log.info(String.format("Connected to Distributed System [%1$s] as Member [%2$s] on Host [%3$s].", + system.getName(), member.getId(), member.getHost())); + + log.info(String.format("%1$s GemFire v.%2$s Cache [%3$s].", messagePrefix, CacheFactory.getVersion(), + cache.getName())); // load/init cache.xml if (cacheXml != null) { @@ -321,16 +314,26 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl registerTransactionListeners(); registerTransactionWriter(); registerJndiDataSources(); - } finally { - th.setContextClassLoader(oldTCCL); + } + finally { + Thread.currentThread().setContextClassLoader(originalThreadContextClassLoader); } } - private void registerJndiDataSources() { - if (jndiDataSources != null) { - for (JndiDataSource jndiDataSource : jndiDataSources) { - JNDIInvoker.mapDatasource(jndiDataSource.getAttributes(), jndiDataSource.getProps()); - } + private boolean isPdxSettingsSpecified() { + return (pdxSerializer != null || pdxPersistent != null || pdxReadSerialized != null + || pdxIgnoreUnreadFields != null || pdxDiskStoreName != null); + } + + /** + * Sets the PDX properties for the given object. Note this is implementation + * specific as it depends on the type of the factory passed in. + * + * @param factory + */ + protected void applyPdxOptions(Object factory) { + if (factory instanceof CacheFactory) { + new PdxOptions((CacheFactory) factory).run(); } } @@ -344,22 +347,15 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } } - private void setHeapPercentages() { - if (criticalHeapPercentage != null) { - Assert.isTrue(criticalHeapPercentage > 0.0 && criticalHeapPercentage <= 100.0, - "invalid value specified for criticalHeapPercentage :" + criticalHeapPercentage - + ". Must be > 0.0 and <= 100.0"); - cache.getResourceManager().setCriticalHeapPercentage(criticalHeapPercentage); - + /** + * Register all declared transaction listeners + */ + protected void registerTransactionListeners() { + if (!CollectionUtils.isEmpty(transactionListeners)) { + for (TransactionListener transactionListener : transactionListeners) { + cache.getCacheTransactionManager().addListener(transactionListener); + } } - - if (evictionHeapPercentage != null) { - Assert.isTrue(evictionHeapPercentage > 0.0 && evictionHeapPercentage <= 100.0, - "invalid value specified for evictionHeapPercentage :" + evictionHeapPercentage - + ". Must be > 0.0 and <= 100.0"); - cache.getResourceManager().setEvictionHeapPercentage(evictionHeapPercentage); - } - } /** @@ -371,56 +367,64 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } } - /** - * Register all declared transaction listeners - */ - protected void registerTransactionListeners() { - if (!CollectionUtils.isEmpty(transactionListeners)) { - CacheTransactionManager txManager = cache.getCacheTransactionManager(); - for (TransactionListener transactionListener : transactionListeners) { - txManager.addListener(transactionListener); + private void registerJndiDataSources() { + if (jndiDataSources != null) { + for (JndiDataSource jndiDataSource : jndiDataSources) { + validate(jndiDataSource); + JNDIInvoker.mapDatasource(jndiDataSource.getAttributes(), jndiDataSource.getProps()); } } } - /** - * Sets the PDX properties for the given object. Note this is implementation - * specific as it depends on the type of the factory passed in. - * - * @param factory - */ - protected void applyPdxOptions(Object factory) { - if (factory instanceof CacheFactory) { - new PdxOptions((CacheFactory) factory).run(); + private void validate(final JndiDataSource jndiDataSource) { + Map attributes = jndiDataSource.getAttributes(); + String typeAttributeValue = attributes.get("type"); + Assert.isTrue(VALID_JNDI_DATASOURCE_TYPE_NAMES.contains(typeAttributeValue), + String.format("The 'jndi-binding', 'type' [%1$s] is invalid; the 'type' must be one of %2$s.", + typeAttributeValue, VALID_JNDI_DATASOURCE_TYPE_NAMES)); + } + + private void setHeapPercentages() { + if (criticalHeapPercentage != null) { + Assert.isTrue(criticalHeapPercentage > 0.0 && criticalHeapPercentage <= 100.0, + String.format("Invalid value specified for 'criticalHeapPercentage' (%1$s). Must be > 0.0 and <= 100.0.", + criticalHeapPercentage)); + cache.getResourceManager().setCriticalHeapPercentage(criticalHeapPercentage); } + + if (evictionHeapPercentage != null) { + Assert.isTrue(evictionHeapPercentage > 0.0 && evictionHeapPercentage <= 100.0, + String.format("Invalid value specified for 'evictionHeapPercentage' (%1$s). Must be > 0.0 and <= 100.0.", + evictionHeapPercentage)); + cache.getResourceManager().setEvictionHeapPercentage(evictionHeapPercentage); + } + } + + protected GemFireCache createCache(Object factory) { + return (cache != null ? cache : ((CacheFactory) factory).create()); + } + + protected GemFireCache fetchCache() { + return (cache != null ? cache : CacheFactory.getAnyInstance()); } protected Object createFactory(Properties props) { return new CacheFactory(props); } - protected GemFireCache fetchCache() { - return (cache == null) ? CacheFactory.getAnyInstance() : cache; - } - - protected GemFireCache createCache(Object factory) { - return (cache == null) ? ((CacheFactory) factory).create() : cache; - } - @Override public void destroy() throws Exception { - if (!close) { - return; - } - if (cache != null && !cache.isClosed()) { - cache.close(); - } + if (close) { + if (cache != null && !cache.isClosed()) { + cache.close(); + } - cache = null; + cache = null; - if (factoryLocator != null) { - factoryLocator.destroy(); - factoryLocator = null; + if (factoryLocator != null) { + factoryLocator.destroy(); + factoryLocator = null; + } } } @@ -665,13 +669,19 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl } /** - * - * @param jndiDataSources + * @param jndiDataSources the list of configured JndiDataSources to use with this Cache. */ public void setJndiDataSources(List jndiDataSources) { this.jndiDataSources = jndiDataSources; } + /** + * @return the list of configured JndiDataSources. + */ + public List getJndiDataSources() { + return jndiDataSources; + } + /** * @param lazyInitialize set to false to force cache initialization if no other bean references it */ @@ -813,13 +823,6 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl return dynamicRegionSupport; } - /** - * @return the jndiDataSources - */ - public List getJndiDataSources() { - return jndiDataSources; - } - /** * @return the gatewayConflictResolver */ diff --git a/src/main/java/org/springframework/data/gemfire/config/CacheParser.java b/src/main/java/org/springframework/data/gemfire/config/CacheParser.java index e52037ee..0e186183 100644 --- a/src/main/java/org/springframework/data/gemfire/config/CacheParser.java +++ b/src/main/java/org/springframework/data/gemfire/config/CacheParser.java @@ -170,33 +170,50 @@ class CacheParser extends AbstractSimpleBeanDefinitionParser { private void parseJndiBindings(Element element, BeanDefinitionBuilder builder) { List jndiBindings = DomUtils.getChildElementsByTagName(element, "jndi-binding"); + if (!CollectionUtils.isEmpty(jndiBindings)) { - ManagedList jndiDataSources = new ManagedList(); - ManagedMap jndiAttributes = new ManagedMap(); + ManagedList jndiDataSources = new ManagedList(jndiBindings.size()); + for (Element jndiBinding : jndiBindings) { - BeanDefinitionBuilder jndiDataSource = BeanDefinitionBuilder - .genericBeanDefinition(CacheFactoryBean.JndiDataSource.class); - NamedNodeMap nnm = jndiBinding.getAttributes(); - for (int i = 0; i < nnm.getLength(); i++) { - Attr attr = (Attr) nnm.item(i); - jndiAttributes.put(attr.getLocalName(), attr.getValue()); + BeanDefinitionBuilder jndiDataSource = BeanDefinitionBuilder.genericBeanDefinition( + CacheFactoryBean.JndiDataSource.class); + + // NOTE 'jndi-name' and 'type' are required by the XSD so we should have at least 2 attributes. + NamedNodeMap attributes = jndiBinding.getAttributes(); + ManagedMap jndiAttributes = new ManagedMap(attributes.getLength()); + + for (int index = 0, length = attributes.getLength(); index < length; index++) { + Attr attribute = (Attr) attributes.item(index); + jndiAttributes.put(attribute.getLocalName(), attribute.getValue()); } + jndiDataSource.addPropertyValue("attributes", jndiAttributes); - List jndiProps = DomUtils.getChildElementsByTagName(element, "jndi-prop"); + List jndiProps = DomUtils.getChildElementsByTagName(jndiBinding, "jndi-prop"); + if (!CollectionUtils.isEmpty(jndiProps)) { - ManagedList props = new ManagedList(); + ManagedList props = new ManagedList(jndiProps.size()); + for (Element jndiProp : jndiProps) { String key = jndiProp.getAttribute("key"); - String value = jndiProp.getNodeValue(); - String type = StringUtils.hasText(jndiProp.getAttribute("type")) ? jndiProp - .getAttribute("type") : String.class.getName(); - props.add(new ConfigProperty(key, value, type)); + String type = jndiProp.getAttribute("type"); + String value = jndiProp.getTextContent(); + + type = (StringUtils.hasText(type) ? type : String.class.getName()); + + props.add(BeanDefinitionBuilder.genericBeanDefinition(ConfigProperty.class) + .addConstructorArgValue(key) + .addConstructorArgValue(value) + .addConstructorArgValue(type) + .getBeanDefinition()); } + jndiDataSource.addPropertyValue("props", props); } + jndiDataSources.add(jndiDataSource.getBeanDefinition()); } + builder.addPropertyValue("jndiDataSources", jndiDataSources); } } diff --git a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd index fe746141..dcde2076 100644 --- a/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd +++ b/src/main/resources/org/springframework/data/gemfire/config/spring-gemfire-1.4.xsd @@ -2244,8 +2244,7 @@ The name of the pool used by the index. Used usually in client scenarios. - + - + - - - - - - - - @@ -2305,14 +2296,14 @@ Specifies the datasource implementation: ManagedDataSource,SimpleDataSource,Pool diff --git a/src/test/java/org/springframework/data/gemfire/config/JndiBindingsPropertyPlaceholderTest.java b/src/test/java/org/springframework/data/gemfire/config/JndiBindingsPropertyPlaceholderTest.java new file mode 100644 index 00000000..8fde976e --- /dev/null +++ b/src/test/java/org/springframework/data/gemfire/config/JndiBindingsPropertyPlaceholderTest.java @@ -0,0 +1,117 @@ +/* + * Copyright 2010-2013 the original author or authors. + * + * 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.springframework.data.gemfire.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.data.gemfire.CacheFactoryBean; +import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.gemstone.gemfire.internal.datasource.ConfigProperty; + +/** + * The JndiBindingsPropertyPlaceholderTest class is a test suite of test cases testing the configuration of a GemFire + * Cache JNDI DataSource using property placeholders. + *

+ * @author John Blum + * @see org.junit.Test + * @see org.springframework.context.ApplicationContext + * @see org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer + * @see org.springframework.test.context.ContextConfiguration + * @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner + * @since 1.4.0 + * @since 7.0.1 (GemFire) + */ +@ContextConfiguration(locations = "jndi-binding-with-property-placeholders-ns.xml", + initializers = GemfireTestApplicationContextInitializer.class) +@RunWith(SpringJUnit4ClassRunner.class) +public class JndiBindingsPropertyPlaceholderTest { + + @Autowired + @SuppressWarnings("unused") + private ApplicationContext context; + + protected void assertPropertyValueExists(final String expectedPropertyName, final String expectedPropertyValue, + final List properties) { + for (ConfigProperty property : properties) { + if (expectedPropertyName.equals(property.getName())) { + assertEquals(expectedPropertyValue, property.getValue()); + assertEquals(String.class.getName(), property.getType()); + return; + } + } + + fail(String.format("ConfigProperty with name [%1$s] was not found!", expectedPropertyName)); + } + + @Test + public void testCacheJndiDataSourceConfiguration() { + CacheFactoryBean factory = context.getBean("&gemfireCache", CacheFactoryBean.class); + + List jndiDataSources = factory.getJndiDataSources(); + + assertNotNull(jndiDataSources); + assertEquals(1, jndiDataSources.size()); + + CacheFactoryBean.JndiDataSource dataSource = jndiDataSources.get(0); + + assertNotNull(dataSource); + + Map attributes = dataSource.getAttributes(); + + assertNotNull(attributes); + assertFalse(attributes.isEmpty()); + assertEquals("testDataSource", attributes.get("jndi-name")); + assertEquals("XAPoolDataSource", attributes.get("type")); + assertEquals("60", attributes.get("blocking-timeout-seconds")); + assertEquals("org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource", attributes.get("conn-pooled-datasource-class")); + assertEquals("jdbc:derby:testDataStore;create=true", attributes.get("connection-url")); + assertEquals("180", attributes.get("idle-timeout-seconds")); + assertEquals("10", attributes.get("init-pool-size")); + assertEquals("org.apache.derby.jdbc.EmbeddedDriver", attributes.get("jdbc-driver-class")); + assertEquals("30", attributes.get("login-timeout-seconds")); + assertEquals("org.apache.derby.jdbc.NonExistingManagedConnectionFactoryClass", attributes.get("managed-connection-factory-class")); + assertEquals("50", attributes.get("max-pool-size")); + assertEquals("test123", attributes.get("password")); + assertEquals("XATransaction", attributes.get("transaction-type")); + assertEquals("masterdba", attributes.get("user-name")); + assertEquals("org.apache.derby.jdbc.EmbeddedXADataSource", attributes.get("xa-datasource-class")); + + List props = dataSource.getProps(); + + assertNotNull(props); + assertFalse(props.isEmpty()); + assertPropertyValueExists("schemaName", "testSchema", props); + assertPropertyValueExists("databaseName", "testDataStore", props); + assertPropertyValueExists("description", "test", props); + assertPropertyValueExists("email", "masterdba@xcompany.com", props); + assertPropertyValueExists("phone", "501-555-1234", props); + } + +} diff --git a/src/test/java/org/springframework/data/gemfire/config/JndiBindingsTest.java b/src/test/java/org/springframework/data/gemfire/config/JndiBindingsTest.java index 20ec8b34..db661d9e 100644 --- a/src/test/java/org/springframework/data/gemfire/config/JndiBindingsTest.java +++ b/src/test/java/org/springframework/data/gemfire/config/JndiBindingsTest.java @@ -13,10 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.springframework.data.gemfire.config; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,22 +30,29 @@ import com.gemstone.gemfire.cache.Cache; import com.gemstone.gemfire.internal.datasource.GemFireBasicDataSource; /** - * @author David Turanski - * * This test requires a real cache + *

+ * @author David Turanski + * @author John Blum */ +@ContextConfiguration("jndi-binding-ns.xml") @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("/org/springframework/data/gemfire/config/jndi-binding-ns.xml") +@SuppressWarnings("unused") public class JndiBindingsTest { - @Autowired ApplicationContext ctx; + @Autowired + private Cache cache; @Test public void testJndiBindings() throws Exception { - Cache cache = ctx.getBean("gemfireCache", Cache.class); - assertNotNull(cache.getJNDIContext().lookup("java:/SimpleDataSource")); - GemFireBasicDataSource ds = (GemFireBasicDataSource) cache.getJNDIContext().lookup("java:/SimpleDataSource"); - assertEquals("org.apache.derby.jdbc.EmbeddedDriver", ds.getJDBCDriver()); - assertEquals(60, ds.getLoginTimeout()); + Object dataSourceObject = cache.getJNDIContext().lookup("java:/SimpleDataSource"); + + assertTrue(dataSourceObject instanceof GemFireBasicDataSource); + + GemFireBasicDataSource dataSource = (GemFireBasicDataSource) dataSourceObject; + + assertEquals("org.apache.derby.jdbc.EmbeddedDriver", dataSource.getJDBCDriver()); + assertEquals(60, dataSource.getLoginTimeout()); } + } diff --git a/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-ns.xml index b40ba2cf..2e6786ef 100644 --- a/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-ns.xml +++ b/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-ns.xml @@ -1,23 +1,29 @@ + http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd +"> - + hi newDB - \ No newline at end of file + + diff --git a/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-with-property-placeholders-ns.xml b/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-with-property-placeholders-ns.xml new file mode 100644 index 00000000..cea728ac --- /dev/null +++ b/src/test/resources/org/springframework/data/gemfire/config/jndi-binding-with-property-placeholders-ns.xml @@ -0,0 +1,63 @@ + + + + + testDataSource + XAPoolDataSource + 60 + org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource + jdbc:derby:testDataStore;create=true + 180 + 10 + org.apache.derby.jdbc.EmbeddedDriver + 30 + org.apache.derby.jdbc.NonExistingManagedConnectionFactoryClass + 50 + test123 + XATransaction + masterdba + org.apache.derby.jdbc.EmbeddedXADataSource + testSchema + testDataStore + test + masterdba@xcompany.com + 501-555-1234 + + + + + + + ${jndi.prop.schema.name} + ${jndi.prop.database.name} + ${jndi.prop.description} + ${jndi.prop.email} + ${jndi.prop.phone} + + + +