Moved tests from testsuite to context and context support
This commit is contained in:
@@ -1,222 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2007 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.cache.ehcache;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import net.sf.ehcache.Cache;
|
||||
import net.sf.ehcache.CacheManager;
|
||||
import net.sf.ehcache.Ehcache;
|
||||
import net.sf.ehcache.constructs.blocking.BlockingCache;
|
||||
import net.sf.ehcache.constructs.blocking.CacheEntryFactory;
|
||||
import net.sf.ehcache.constructs.blocking.SelfPopulatingCache;
|
||||
import net.sf.ehcache.constructs.blocking.UpdatingCacheEntryFactory;
|
||||
import net.sf.ehcache.constructs.blocking.UpdatingSelfPopulatingCache;
|
||||
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
/**
|
||||
* @author Dmitriy Kopylenko
|
||||
* @author Juergen Hoeller
|
||||
* @since 27.09.2004
|
||||
*/
|
||||
public class EhCacheSupportTests extends TestCase {
|
||||
|
||||
public void testLoadingBlankCacheManager() throws Exception {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
assertEquals(CacheManager.class, cacheManagerFb.getObjectType());
|
||||
assertTrue("Singleton property", cacheManagerFb.isSingleton());
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
CacheManager cm = (CacheManager) cacheManagerFb.getObject();
|
||||
assertTrue("Loaded CacheManager with no caches", cm.getCacheNames().length == 0);
|
||||
Cache myCache1 = cm.getCache("myCache1");
|
||||
assertTrue("No myCache1 defined", myCache1 == null);
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void testLoadingCacheManagerFromConfigFile() throws Exception {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
|
||||
cacheManagerFb.setCacheManagerName("myCacheManager");
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
CacheManager cm = (CacheManager) cacheManagerFb.getObject();
|
||||
assertTrue("Correct number of caches loaded", cm.getCacheNames().length == 1);
|
||||
Cache myCache1 = cm.getCache("myCache1");
|
||||
assertFalse("myCache1 is not eternal", myCache1.isEternal());
|
||||
assertTrue("myCache1.maxElements == 300", myCache1.getMaxElementsInMemory() == 300);
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void testEhCacheFactoryBeanWithDefaultCacheManager() throws Exception {
|
||||
doTestEhCacheFactoryBean(false);
|
||||
}
|
||||
|
||||
public void testEhCacheFactoryBeanWithExplicitCacheManager() throws Exception {
|
||||
doTestEhCacheFactoryBean(true);
|
||||
}
|
||||
|
||||
private void doTestEhCacheFactoryBean(boolean useCacheManagerFb) throws Exception {
|
||||
Cache cache = null;
|
||||
EhCacheManagerFactoryBean cacheManagerFb = null;
|
||||
try {
|
||||
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
|
||||
assertEquals(Ehcache.class, cacheFb.getObjectType());
|
||||
assertTrue("Singleton property", cacheFb.isSingleton());
|
||||
if (useCacheManagerFb) {
|
||||
cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.setConfigLocation(new ClassPathResource("testEhcache.xml", getClass()));
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
|
||||
}
|
||||
|
||||
cacheFb.setCacheName("myCache1");
|
||||
cacheFb.afterPropertiesSet();
|
||||
cache = (Cache) cacheFb.getObject();
|
||||
assertEquals("myCache1", cache.getName());
|
||||
if (useCacheManagerFb){
|
||||
assertEquals("myCache1.maxElements", 300, cache.getMaxElementsInMemory());
|
||||
}
|
||||
else {
|
||||
assertEquals("myCache1.maxElements", 10000, cache.getMaxElementsInMemory());
|
||||
}
|
||||
|
||||
// Cache region is not defined. Should create one with default properties.
|
||||
cacheFb = new EhCacheFactoryBean();
|
||||
if (useCacheManagerFb) {
|
||||
cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
|
||||
}
|
||||
cacheFb.setCacheName("undefinedCache");
|
||||
cacheFb.afterPropertiesSet();
|
||||
cache = (Cache) cacheFb.getObject();
|
||||
assertEquals("undefinedCache", cache.getName());
|
||||
assertTrue("default maxElements is correct", cache.getMaxElementsInMemory() == 10000);
|
||||
assertTrue("default overflowToDisk is correct", cache.isOverflowToDisk());
|
||||
assertFalse("default eternal is correct", cache.isEternal());
|
||||
assertTrue("default timeToLive is correct", cache.getTimeToLiveSeconds() == 120);
|
||||
assertTrue("default timeToIdle is correct", cache.getTimeToIdleSeconds() == 120);
|
||||
assertTrue("default diskPersistent is correct", !cache.isDiskPersistent());
|
||||
assertTrue("default diskExpiryThreadIntervalSeconds is correct", cache.getDiskExpiryThreadIntervalSeconds() == 120);
|
||||
|
||||
// overriding the default properties
|
||||
cacheFb = new EhCacheFactoryBean();
|
||||
if (useCacheManagerFb) {
|
||||
cacheFb.setCacheManager((CacheManager) cacheManagerFb.getObject());
|
||||
}
|
||||
cacheFb.setBeanName("undefinedCache2");
|
||||
cacheFb.setMaxElementsInMemory(5);
|
||||
cacheFb.setOverflowToDisk(false);
|
||||
cacheFb.setEternal(true);
|
||||
cacheFb.setTimeToLive(8);
|
||||
cacheFb.setTimeToIdle(7);
|
||||
cacheFb.setDiskPersistent(true);
|
||||
cacheFb.setDiskExpiryThreadIntervalSeconds(10);
|
||||
cacheFb.afterPropertiesSet();
|
||||
cache = (Cache) cacheFb.getObject();
|
||||
|
||||
assertEquals("undefinedCache2", cache.getName());
|
||||
assertTrue("overridden maxElements is correct", cache.getMaxElementsInMemory() == 5);
|
||||
assertFalse("overridden overflowToDisk is correct", cache.isOverflowToDisk());
|
||||
assertTrue("overridden eternal is correct", cache.isEternal());
|
||||
assertTrue("default timeToLive is correct", cache.getTimeToLiveSeconds() == 8);
|
||||
assertTrue("default timeToIdle is correct", cache.getTimeToIdleSeconds() == 7);
|
||||
assertTrue("overridden diskPersistent is correct", cache.isDiskPersistent());
|
||||
assertTrue("overridden diskExpiryThreadIntervalSeconds is correct", cache.getDiskExpiryThreadIntervalSeconds() == 10);
|
||||
}
|
||||
finally {
|
||||
if (useCacheManagerFb) {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
else {
|
||||
CacheManager.getInstance().shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void testEhCacheFactoryBeanWithBlockingCache() throws Exception {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
CacheManager cm = (CacheManager) cacheManagerFb.getObject();
|
||||
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
|
||||
cacheFb.setCacheManager(cm);
|
||||
cacheFb.setCacheName("myCache1");
|
||||
cacheFb.setBlocking(true);
|
||||
cacheFb.afterPropertiesSet();
|
||||
Ehcache myCache1 = cm.getEhcache("myCache1");
|
||||
assertTrue(myCache1 instanceof BlockingCache);
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void testEhCacheFactoryBeanWithSelfPopulatingCache() throws Exception {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
CacheManager cm = (CacheManager) cacheManagerFb.getObject();
|
||||
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
|
||||
cacheFb.setCacheManager(cm);
|
||||
cacheFb.setCacheName("myCache1");
|
||||
cacheFb.setCacheEntryFactory(new CacheEntryFactory() {
|
||||
public Object createEntry(Object key) throws Exception {
|
||||
return key;
|
||||
}
|
||||
});
|
||||
cacheFb.afterPropertiesSet();
|
||||
Ehcache myCache1 = cm.getEhcache("myCache1");
|
||||
assertTrue(myCache1 instanceof SelfPopulatingCache);
|
||||
assertEquals("myKey1", myCache1.get("myKey1").getValue());
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
public void testEhCacheFactoryBeanWithUpdatingSelfPopulatingCache() throws Exception {
|
||||
EhCacheManagerFactoryBean cacheManagerFb = new EhCacheManagerFactoryBean();
|
||||
cacheManagerFb.afterPropertiesSet();
|
||||
try {
|
||||
CacheManager cm = (CacheManager) cacheManagerFb.getObject();
|
||||
EhCacheFactoryBean cacheFb = new EhCacheFactoryBean();
|
||||
cacheFb.setCacheManager(cm);
|
||||
cacheFb.setCacheName("myCache1");
|
||||
cacheFb.setCacheEntryFactory(new UpdatingCacheEntryFactory() {
|
||||
public Object createEntry(Object key) throws Exception {
|
||||
return key;
|
||||
}
|
||||
public void updateEntryValue(Object key, Object value) throws Exception {
|
||||
}
|
||||
});
|
||||
cacheFb.afterPropertiesSet();
|
||||
Ehcache myCache1 = cm.getEhcache("myCache1");
|
||||
assertTrue(myCache1 instanceof UpdatingSelfPopulatingCache);
|
||||
assertEquals("myKey1", myCache1.get("myKey1").getValue());
|
||||
}
|
||||
finally {
|
||||
cacheManagerFb.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<ehcache>
|
||||
|
||||
<!-- Sets the path to the directory where cache .data files are created.
|
||||
|
||||
If the path is a Java System Property it is replaced by
|
||||
its value in the running VM.
|
||||
|
||||
The following properties are translated:
|
||||
user.home - User's home directory
|
||||
user.dir - User's current working directory
|
||||
java.io.tmpdir - Default temp file path -->
|
||||
<diskStore path="java.io.tmpdir"/>
|
||||
|
||||
|
||||
<!--Default Cache configuration. These will applied to caches programmatically created through
|
||||
the CacheManager.
|
||||
|
||||
The following attributes are required for defaultCache:
|
||||
|
||||
maxInMemory - Sets the maximum number of objects that will be created in memory
|
||||
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
|
||||
is never expired.
|
||||
timeToIdleSeconds - Sets the time to idle for an element before it expires.
|
||||
i.e. The maximum amount of time between accesses before an element expires
|
||||
Is only used if the element is not eternal.
|
||||
Optional attribute. A value of 0 means that an Element can idle for infinity
|
||||
timeToLiveSeconds - Sets the time to live for an element before it expires.
|
||||
i.e. The maximum time between creation time and when an element expires.
|
||||
Is only used if the element is not eternal.
|
||||
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
|
||||
has reached the maxInMemory limit.
|
||||
|
||||
-->
|
||||
<defaultCache
|
||||
maxElementsInMemory="10000"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="120"
|
||||
timeToLiveSeconds="120"
|
||||
overflowToDisk="true"
|
||||
/>
|
||||
|
||||
<cache name="myCache1"
|
||||
maxElementsInMemory="300"
|
||||
eternal="false"
|
||||
timeToIdleSeconds="500"
|
||||
timeToLiveSeconds="500"
|
||||
overflowToDisk="true"
|
||||
/>
|
||||
|
||||
</ehcache>
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.context.access;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.easymock.MockControl;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
|
||||
/**
|
||||
* Unit test for ContextBeanFactoryReference
|
||||
*
|
||||
* @author Colin Sampaleanu
|
||||
*/
|
||||
public class ContextBeanFactoryReferenceTests extends TestCase {
|
||||
|
||||
public void testAllOperations() {
|
||||
MockControl control = MockControl.createControl(ConfigurableApplicationContext.class);
|
||||
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) control.getMock();
|
||||
|
||||
ctx.close();
|
||||
control.replay();
|
||||
|
||||
ContextBeanFactoryReference bfr = new ContextBeanFactoryReference(ctx);
|
||||
|
||||
assertNotNull(bfr.getFactory());
|
||||
bfr.release();
|
||||
|
||||
try {
|
||||
bfr.getFactory();
|
||||
}
|
||||
catch (IllegalStateException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
control.verify();
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2005 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.context.access;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.springframework.beans.factory.access.BeanFactoryLocator;
|
||||
|
||||
/**
|
||||
* @author Colin Sampaleanu
|
||||
*/
|
||||
public class DefaultLocatorFactoryTests extends TestCase {
|
||||
|
||||
/*
|
||||
* Class to test for BeanFactoryLocator getInstance()
|
||||
*/
|
||||
public void testGetInstance() {
|
||||
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance();
|
||||
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance();
|
||||
assertTrue(bf.equals(bf2));
|
||||
}
|
||||
|
||||
/*
|
||||
* Class to test for BeanFactoryLocator getInstance(String)
|
||||
*/
|
||||
public void testGetInstanceString() {
|
||||
BeanFactoryLocator bf = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
|
||||
BeanFactoryLocator bf2 = DefaultLocatorFactory.getInstance("my-bean-refs.xml");
|
||||
assertTrue(bf.equals(bf2));
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008 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.context.config;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
* @author Arjen Poutsma
|
||||
* @since 2.5.6
|
||||
*/
|
||||
public class ContextNamespaceHandlerTests extends TestCase {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
applicationContext = new ClassPathXmlApplicationContext("contextNamespaceHandlerTests.xml", getClass());
|
||||
}
|
||||
|
||||
public void testPropertyPlaceholder() throws Exception {
|
||||
Map beans = applicationContext.getBeansOfType(PropertyPlaceholderConfigurer.class);
|
||||
assertFalse("No PropertyPlaceHolderConfigurer found", beans.isEmpty());
|
||||
String s = (String) applicationContext.getBean("string");
|
||||
assertEquals("No properties replaced", "bar", s);
|
||||
}
|
||||
|
||||
public void testPropertyOverride() throws Exception {
|
||||
Map beans = applicationContext.getBeansOfType(PropertyOverrideConfigurer.class);
|
||||
assertFalse("No PropertyOverrideConfigurer found", beans.isEmpty());
|
||||
Date date = (Date) applicationContext.getBean("date");
|
||||
assertEquals("No properties overriden", 42, date.getMinutes());
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
|
||||
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
|
||||
|
||||
<util:properties id="placeholderProps">
|
||||
<prop key="foo">bar</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-placeholder properties-ref="placeholderProps"/>
|
||||
|
||||
<bean id="string" class="java.lang.String">
|
||||
<constructor-arg value="${foo}"/>
|
||||
</bean>
|
||||
|
||||
<util:properties id="overrideProps">
|
||||
<prop key="date.minutes">42</prop>
|
||||
</util:properties>
|
||||
|
||||
<context:property-override properties-ref="overrideProps"/>
|
||||
|
||||
<bean id="date" class="java.util.Date">
|
||||
<property name="minutes" value="10"/>
|
||||
</bean>
|
||||
</beans>
|
||||
@@ -1,56 +0,0 @@
|
||||
|
||||
/*
|
||||
* Copyright 2002-2005 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.jndi;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
*/
|
||||
public class JndiTemplateEditorTests extends TestCase {
|
||||
|
||||
public void testNullIsIllegalArgument() {
|
||||
try {
|
||||
new JndiTemplateEditor().setAsText(null);
|
||||
fail("Null is illegal");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// OK
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyStringMeansNullEnvironment() {
|
||||
JndiTemplateEditor je = new JndiTemplateEditor();
|
||||
je.setAsText("");
|
||||
JndiTemplate jt = (JndiTemplate) je.getValue();
|
||||
assertTrue(jt.getEnvironment() == null);
|
||||
}
|
||||
|
||||
public void testCustomEnvironment() {
|
||||
JndiTemplateEditor je = new JndiTemplateEditor();
|
||||
// These properties are meaningless for JNDI, but we don't worry about that:
|
||||
// the underlying JNDI implementation will throw exceptions when the user tries
|
||||
// to look anything up
|
||||
je.setAsText("jndiInitialSomethingOrOther=org.springframework.myjndi.CompleteRubbish\nfoo=bar");
|
||||
JndiTemplate jt = (JndiTemplate) je.getValue();
|
||||
assertTrue(jt.getEnvironment().size() == 2);
|
||||
assertTrue(jt.getEnvironment().getProperty("jndiInitialSomethingOrOther").equals("org.springframework.myjndi.CompleteRubbish"));
|
||||
assertTrue(jt.getEnvironment().getProperty("foo").equals("bar"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2006 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.jndi;
|
||||
|
||||
import javax.naming.Context;
|
||||
import javax.naming.NameNotFoundException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
|
||||
/**
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @since 08.07.2003
|
||||
*/
|
||||
public class JndiTemplateTests extends TestCase {
|
||||
|
||||
public void testLookupSucceeds() throws Exception {
|
||||
Object o = new Object();
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.lookup(name);
|
||||
mc.setReturnValue(o);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
Object o2 = jt.lookup(name);
|
||||
assertEquals(o, o2);
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testLookupFails() throws Exception {
|
||||
NameNotFoundException ne = new NameNotFoundException();
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.lookup(name);
|
||||
mc.setThrowable(ne);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
jt.lookup(name);
|
||||
fail("Should have thrown NamingException");
|
||||
}
|
||||
catch (NameNotFoundException ex) {
|
||||
// Ok
|
||||
}
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testLookupReturnsNull() throws Exception {
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.lookup(name);
|
||||
mc.setReturnValue(null);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
jt.lookup(name);
|
||||
fail("Should have thrown NamingException");
|
||||
}
|
||||
catch (NameNotFoundException ex) {
|
||||
// Ok
|
||||
}
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testLookupFailsWithTypeMismatch() throws Exception {
|
||||
Object o = new Object();
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.lookup(name);
|
||||
mc.setReturnValue(o);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
try {
|
||||
jt.lookup(name, String.class);
|
||||
fail("Should have thrown TypeMismatchNamingException");
|
||||
}
|
||||
catch (TypeMismatchNamingException ex) {
|
||||
// Ok
|
||||
}
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testBind() throws Exception {
|
||||
Object o = new Object();
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.bind(name, o);
|
||||
mc.setVoidCallable(1);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
jt.bind(name, o);
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testRebind() throws Exception {
|
||||
Object o = new Object();
|
||||
String name = "foo";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.rebind(name, o);
|
||||
mc.setVoidCallable(1);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
jt.rebind(name, o);
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
public void testUnbind() throws Exception {
|
||||
String name = "something";
|
||||
MockControl mc = MockControl.createControl(Context.class);
|
||||
final Context mock = (Context) mc.getMock();
|
||||
mock.unbind(name);
|
||||
mc.setVoidCallable(1);
|
||||
mock.close();
|
||||
mc.setVoidCallable(1);
|
||||
mc.replay();
|
||||
|
||||
JndiTemplate jt = new JndiTemplate() {
|
||||
protected Context createInitialContext() {
|
||||
return mock;
|
||||
}
|
||||
};
|
||||
|
||||
jt.unbind(name);
|
||||
mc.verify();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user