mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-10-10 09:09:04 +00:00
Moved tests from testsuite to orm
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@212 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
+1789
File diff suppressed because it is too large
Load Diff
+588
@@ -0,0 +1,588 @@
|
||||
/*
|
||||
* Copyright 2002-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.orm.hibernate3;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.easymock.MockControl;
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Interceptor;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cache.CacheProvider;
|
||||
import org.hibernate.cache.NoCacheProvider;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.cfg.ImprovedNamingStrategy;
|
||||
import org.hibernate.cfg.Mappings;
|
||||
import org.hibernate.cfg.NamingStrategy;
|
||||
import org.hibernate.connection.UserSuppliedConnectionProvider;
|
||||
import org.hibernate.engine.FilterDefinition;
|
||||
import org.hibernate.event.MergeEvent;
|
||||
import org.hibernate.event.MergeEventListener;
|
||||
import org.hibernate.mapping.TypeDef;
|
||||
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @since 05.03.2005
|
||||
*/
|
||||
public class LocalSessionFactoryBeanTests extends TestCase {
|
||||
|
||||
public void testLocalSessionFactoryBeanWithDataSource() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalDataSourceConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setDataSource(ds);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertEquals("newSessionFactory", invocations.get(0));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithCacheProvider() throws Exception {
|
||||
final CacheProvider cacheProvider = new NoCacheProvider();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalCacheProviderProxy.class.getName(),
|
||||
config.getProperty(Environment.CACHE_PROVIDER));
|
||||
assertSame(cacheProvider, LocalSessionFactoryBean.getConfigTimeCacheProvider());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setCacheProvider(cacheProvider);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertEquals("newSessionFactory", invocations.get(0));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithTransactionAwareDataSource() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(TransactionAwareDataSourceConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setDataSource(ds);
|
||||
sfb.setUseTransactionAwareDataSource(true);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertEquals("newSessionFactory", invocations.get(0));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndMappingResources() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
MockControl tmControl = MockControl.createControl(TransactionManager.class);
|
||||
final TransactionManager tm = (TransactionManager) tmControl.getMock();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalJtaDataSourceConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
|
||||
assertEquals(LocalTransactionManagerLookup.class.getName(),
|
||||
config.getProperty(Environment.TRANSACTION_MANAGER_STRATEGY));
|
||||
assertEquals(tm, LocalSessionFactoryBean.getConfigTimeTransactionManager());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[]{
|
||||
"/org/springframework/beans/factory/xml/test.xml",
|
||||
"/org/springframework/beans/factory/xml/child.xml"});
|
||||
sfb.setDataSource(ds);
|
||||
sfb.setJtaTransactionManager(tm);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertEquals("addResource", invocations.get(0));
|
||||
assertEquals("addResource", invocations.get(1));
|
||||
assertEquals("newSessionFactory", invocations.get(2));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndMappingJarLocations() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addJar(File file) {
|
||||
invocations.add("addResource " + file.getPath());
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalDataSourceConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setMappingJarLocations(new Resource[]{
|
||||
new FileSystemResource("mapping.hbm.jar"), new FileSystemResource("mapping2.hbm.jar")});
|
||||
sfb.setDataSource(ds);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertTrue(invocations.contains("addResource mapping.hbm.jar"));
|
||||
assertTrue(invocations.contains("addResource mapping2.hbm.jar"));
|
||||
assertTrue(invocations.contains("newSessionFactory"));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalDataSourceConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals(ds, LocalSessionFactoryBean.getConfigTimeDataSource());
|
||||
assertEquals("myValue", config.getProperty("myProperty"));
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setMappingLocations(new Resource[]{
|
||||
new ClassPathResource("/org/springframework/beans/factory/xml/test.xml")});
|
||||
sfb.setDataSource(ds);
|
||||
Properties prop = new Properties();
|
||||
prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
|
||||
prop.setProperty("myProperty", "myValue");
|
||||
sfb.setHibernateProperties(prop);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertTrue(invocations.contains("addResource"));
|
||||
assertTrue(invocations.contains("newSessionFactory"));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithValidProperties() throws Exception {
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(UserSuppliedConnectionProvider.class.getName(),
|
||||
config.getProperty(Environment.CONNECTION_PROVIDER));
|
||||
assertEquals("myValue", config.getProperty("myProperty"));
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
Properties prop = new Properties();
|
||||
prop.setProperty(Environment.CONNECTION_PROVIDER, UserSuppliedConnectionProvider.class.getName());
|
||||
prop.setProperty("myProperty", "myValue");
|
||||
sfb.setHibernateProperties(prop);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertTrue(invocations.contains("newSessionFactory"));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithInvalidProperties() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
|
||||
sfb.setMappingResources(new String[0]);
|
||||
Properties prop = new Properties();
|
||||
prop.setProperty(Environment.CONNECTION_PROVIDER, "myClass");
|
||||
sfb.setHibernateProperties(prop);
|
||||
try {
|
||||
sfb.afterPropertiesSet();
|
||||
}
|
||||
catch (HibernateException ex) {
|
||||
// expected, provider class not found
|
||||
}
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithInvalidMappings() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
|
||||
sfb.setMappingResources(new String[]{"mapping.hbm.xml"});
|
||||
try {
|
||||
sfb.afterPropertiesSet();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
// expected, mapping resource not found
|
||||
}
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception {
|
||||
MockControl factoryControl = MockControl.createControl(SessionFactory.class);
|
||||
final SessionFactory sessionFactory = (SessionFactory) factoryControl.getMock();
|
||||
sessionFactory.close();
|
||||
factoryControl.setVoidCallable(1);
|
||||
factoryControl.replay();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return sessionFactory;
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
sfb.setExposeTransactionAwareSessionFactory(false);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sessionFactory == sfb.getObject());
|
||||
sfb.destroy();
|
||||
factoryControl.verify();
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration setInterceptor(Interceptor interceptor) {
|
||||
throw new IllegalArgumentException(interceptor.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
MockControl interceptorControl = MockControl.createControl(Interceptor.class);
|
||||
Interceptor entityInterceptor = (Interceptor) interceptorControl.getMock();
|
||||
interceptorControl.replay();
|
||||
sfb.setEntityInterceptor(entityInterceptor);
|
||||
try {
|
||||
sfb.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration setNamingStrategy(NamingStrategy namingStrategy) {
|
||||
throw new IllegalArgumentException(namingStrategy.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
sfb.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
|
||||
try {
|
||||
sfb.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
assertTrue("Correct exception", ex.getMessage().equals(ImprovedNamingStrategy.INSTANCE.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithCacheStrategies() throws Exception {
|
||||
final Properties registeredClassCache = new Properties();
|
||||
final Properties registeredCollectionCache = new Properties();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public Configuration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy) {
|
||||
registeredClassCache.setProperty(clazz, concurrencyStrategy);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) {
|
||||
registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy);
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Properties classCache = new Properties();
|
||||
classCache.setProperty("org.springframework.beans.TestBean", "read-write");
|
||||
sfb.setEntityCacheStrategies(classCache);
|
||||
Properties collectionCache = new Properties();
|
||||
collectionCache.setProperty("org.springframework.beans.TestBean.friends", "read-only");
|
||||
sfb.setCollectionCacheStrategies(collectionCache);
|
||||
sfb.afterPropertiesSet();
|
||||
|
||||
assertEquals(classCache, registeredClassCache);
|
||||
assertEquals(collectionCache, registeredCollectionCache);
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithCacheStrategiesAndRegions() throws Exception {
|
||||
final Properties registeredClassCache = new Properties();
|
||||
final Properties registeredCollectionCache = new Properties();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public void setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String regionName) {
|
||||
registeredClassCache.setProperty(clazz, concurrencyStrategy + "," + regionName);
|
||||
}
|
||||
|
||||
public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String regionName) {
|
||||
registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy + "," + regionName);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Properties classCache = new Properties();
|
||||
classCache.setProperty("org.springframework.beans.TestBean", "read-write,myRegion");
|
||||
sfb.setEntityCacheStrategies(classCache);
|
||||
Properties collectionCache = new Properties();
|
||||
collectionCache.setProperty("org.springframework.beans.TestBean.friends", "read-only,myRegion");
|
||||
sfb.setCollectionCacheStrategies(collectionCache);
|
||||
sfb.afterPropertiesSet();
|
||||
|
||||
assertEquals(classCache, registeredClassCache);
|
||||
assertEquals(collectionCache, registeredCollectionCache);
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithEventListeners() throws Exception {
|
||||
final Map registeredListeners = new HashMap();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public void setListener(String type, Object listener) {
|
||||
registeredListeners.put(type, listener);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Map listeners = new HashMap();
|
||||
listeners.put("flush", "myListener");
|
||||
listeners.put("create", "yourListener");
|
||||
sfb.setEventListeners(listeners);
|
||||
sfb.afterPropertiesSet();
|
||||
assertEquals(listeners, registeredListeners);
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithEventListenerSet() throws Exception {
|
||||
final Map registeredListeners = new HashMap();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
public void setListeners(String type, Object[] listeners) {
|
||||
assertTrue(listeners instanceof MergeEventListener[]);
|
||||
registeredListeners.put(type, new HashSet(Arrays.asList(listeners)));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Map listeners = new HashMap();
|
||||
Set mergeSet = new HashSet();
|
||||
mergeSet.add(new DummyMergeEventListener());
|
||||
mergeSet.add(new DummyMergeEventListener());
|
||||
listeners.put("merge", mergeSet);
|
||||
sfb.setEventListeners(listeners);
|
||||
sfb.afterPropertiesSet();
|
||||
assertEquals(listeners, registeredListeners);
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithFilterDefinitions() throws Exception {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("filterDefinitions.xml", getClass()));
|
||||
FilterTestLocalSessionFactoryBean sf = (FilterTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory");
|
||||
assertEquals(2, sf.registeredFilterDefinitions.size());
|
||||
FilterDefinition filter1 = (FilterDefinition) sf.registeredFilterDefinitions.get(0);
|
||||
FilterDefinition filter2 = (FilterDefinition) sf.registeredFilterDefinitions.get(1);
|
||||
|
||||
assertEquals("filter1", filter1.getFilterName());
|
||||
assertEquals(2, filter1.getParameterNames().size());
|
||||
assertEquals(Hibernate.STRING, filter1.getParameterType("param1"));
|
||||
assertEquals(Hibernate.LONG, filter1.getParameterType("otherParam"));
|
||||
assertEquals("someCondition", filter1.getDefaultFilterCondition());
|
||||
|
||||
assertEquals("filter2", filter2.getFilterName());
|
||||
assertEquals(1, filter2.getParameterNames().size());
|
||||
assertEquals(Hibernate.INTEGER, filter2.getParameterType("myParam"));
|
||||
}
|
||||
|
||||
public void testLocalSessionFactoryBeanWithTypeDefinitions() throws Exception {
|
||||
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("typeDefinitions.xml", getClass()));
|
||||
TypeTestLocalSessionFactoryBean sf = (TypeTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory");
|
||||
TypeDef type1 = (TypeDef) sf.mappings.getTypeDef("type1");
|
||||
TypeDef type2 = (TypeDef) sf.mappings.getTypeDef("type2");
|
||||
|
||||
assertEquals("mypackage.MyTypeClass", type1.getTypeClass());
|
||||
assertEquals(2, type1.getParameters().size());
|
||||
assertEquals("value1", type1.getParameters().getProperty("param1"));
|
||||
assertEquals("othervalue", type1.getParameters().getProperty("otherParam"));
|
||||
|
||||
assertEquals("mypackage.MyOtherTypeClass", type2.getTypeClass());
|
||||
assertEquals(1, type2.getParameters().size());
|
||||
assertEquals("myvalue", type2.getParameters().getProperty("myParam"));
|
||||
}
|
||||
|
||||
|
||||
public static class FilterTestLocalSessionFactoryBean extends LocalSessionFactoryBean {
|
||||
|
||||
public List registeredFilterDefinitions = new LinkedList();
|
||||
|
||||
protected Configuration newConfiguration() throws HibernateException {
|
||||
return new Configuration() {
|
||||
public void addFilterDefinition(FilterDefinition definition) {
|
||||
registeredFilterDefinitions.add(definition);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TypeTestLocalSessionFactoryBean extends LocalSessionFactoryBean {
|
||||
|
||||
public Mappings mappings;
|
||||
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
this.mappings = config.createMappings();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class DummyMergeEventListener implements MergeEventListener {
|
||||
|
||||
public void onMerge(MergeEvent event) throws HibernateException {
|
||||
}
|
||||
|
||||
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="inheritsFromParentFactory" parent="inheritedTestBean">
|
||||
<property name="name"><value>override</value></property>
|
||||
<!-- age should inherit value of 1 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="inheritsWithDifferentClass" class="org.springframework.beans.DerivedTestBean"
|
||||
parent="inheritedTestBean" init-method="initialize">
|
||||
<property name="name"><value>override</value></property>
|
||||
<!-- age should inherit value of 1 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean"
|
||||
parent="inheritedTestBeanWithoutClass" init-method="initialize">
|
||||
<property name="name"><value>override</value></property>
|
||||
<!-- age should inherit value of 1 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="protoypeInheritsFromParentFactorySingleton" parent="inheritedTestBean" scope="prototype">
|
||||
<property name="name"><value>prototypeOverridesInheritedSingleton</value></property>
|
||||
<!-- age should inherit value of 1 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="prototypeInheritsFromParentFactoryPrototype" parent="inheritedTestBeanPrototype" scope="prototype">
|
||||
<property name="name"><value>prototype-override</value></property>
|
||||
<!-- age should inherit value of 2 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="singletonInheritsFromParentFactoryPrototype" parent="inheritedTestBeanPrototype" scope="singleton">
|
||||
<property name="name"><value>prototype-override</value></property>
|
||||
<!-- age should inherit value of 2 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="inheritedTestBean" parent="inheritedTestBean">
|
||||
<property name="name"><value>overrideParentBean</value></property>
|
||||
<!-- age should inherit value of 1 from parent -->
|
||||
</bean>
|
||||
|
||||
<bean id="bogusParent" parent="bogus" scope="prototype"/>
|
||||
|
||||
<bean id="indexedTestBean" class="org.springframework.beans.IndexedTestBean">
|
||||
<property name="array[0].name"><value>myname</value></property>
|
||||
</bean>
|
||||
|
||||
<bean parent="inheritedTestBean"/>
|
||||
|
||||
</beans>
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="validEmptyWithDescription" class="org.springframework.beans.TestBean">
|
||||
<description>
|
||||
I have no properties and I'm happy without them.
|
||||
</description>
|
||||
</bean>
|
||||
|
||||
<!--
|
||||
Check automatic creation of alias, to allow for names that are illegal as XML ids.
|
||||
-->
|
||||
<bean id="aliased" class=" org.springframework.beans.TestBean " name="myalias">
|
||||
<property name="name"><value>aliased</value></property>
|
||||
</bean>
|
||||
|
||||
<alias name="aliased" alias="youralias"/>
|
||||
|
||||
<alias name="multiAliased" alias="alias3"/>
|
||||
|
||||
<bean id="multiAliased" class="org.springframework.beans.TestBean" name="alias1,alias2">
|
||||
<property name="name"><value>aliased</value></property>
|
||||
</bean>
|
||||
|
||||
<alias name="multiAliased" alias="alias4"/>
|
||||
|
||||
<bean class="org.springframework.beans.TestBean" name="aliasWithoutId1,aliasWithoutId2,aliasWithoutId3">
|
||||
<property name="name"><value>aliased</value></property>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.TestBean">
|
||||
<property name="name"><null/></property>
|
||||
</bean>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.DummyReferencer"/>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.DummyReferencer"/>
|
||||
|
||||
<bean class="org.springframework.beans.factory.xml.DummyReferencer"/>
|
||||
|
||||
<bean id="rod" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value><!-- a comment -->Rod</value></property>
|
||||
<property name="age"><value>31</value></property>
|
||||
<property name="spouse"><ref bean="father"/></property>
|
||||
<property name="touchy"><value/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="roderick" parent="rod">
|
||||
<property name="name"><value>Roderick<!-- a comment --></value></property>
|
||||
<!-- Should inherit age -->
|
||||
</bean>
|
||||
|
||||
<bean id="kerry" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>Ker<!-- a comment -->ry</value></property>
|
||||
<property name="age"><value>34</value></property>
|
||||
<property name="spouse"><ref local="rod"/></property>
|
||||
<property name="touchy"><value></value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="kathy" class="org.springframework.beans.TestBean" scope="prototype">
|
||||
<property name="name"><value>Kathy</value></property>
|
||||
<property name="age"><value>28</value></property>
|
||||
<property name="spouse"><ref bean="father"/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="typeMismatch" class="org.springframework.beans.TestBean" scope="prototype">
|
||||
<property name="name"><value>typeMismatch</value></property>
|
||||
<property name="age"><value>34x</value></property>
|
||||
<property name="spouse"><ref local="rod"/></property>
|
||||
</bean>
|
||||
|
||||
<!-- Test of lifecycle callbacks -->
|
||||
<bean id="mustBeInitialized" class="org.springframework.beans.factory.MustBeInitialized"/>
|
||||
|
||||
<bean id="lifecycle" class="org.springframework.beans.factory.LifecycleBean"
|
||||
init-method="declaredInitMethod">
|
||||
<property name="initMethodDeclared"><value>true</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="protectedLifecycle" class="org.springframework.beans.factory.xml.ProtectedLifecycleBean"
|
||||
init-method="declaredInitMethod">
|
||||
<property name="initMethodDeclared"><value>true</value></property>
|
||||
</bean>
|
||||
|
||||
<!-- Factory beans are automatically treated differently -->
|
||||
<bean id="singletonFactory" class="org.springframework.beans.factory.DummyFactory">
|
||||
</bean>
|
||||
|
||||
<bean id="prototypeFactory" class="org.springframework.beans.factory.DummyFactory">
|
||||
<property name="singleton"><value>false</value></property>
|
||||
</bean>
|
||||
|
||||
<!-- Check that the circular reference resolution mechanism doesn't break
|
||||
repeated references to the same FactoryBean -->
|
||||
<bean id="factoryReferencer" class="org.springframework.beans.factory.xml.DummyReferencer">
|
||||
<property name="testBean1"><ref bean="singletonFactory"/></property>
|
||||
<property name="testBean2"><ref local="singletonFactory"/></property>
|
||||
<property name="dummyFactory"><ref bean="&singletonFactory"/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="factoryReferencerWithConstructor" class="org.springframework.beans.factory.xml.DummyReferencer">
|
||||
<constructor-arg><ref bean="&singletonFactory"/></constructor-arg>
|
||||
<property name="testBean1"><ref bean="singletonFactory"/></property>
|
||||
<property name="testBean2"><ref local="singletonFactory"/></property>
|
||||
</bean>
|
||||
|
||||
<!-- Check that the circular reference resolution mechanism doesn't break
|
||||
prototype instantiation -->
|
||||
<bean id="prototypeReferencer" class="org.springframework.beans.factory.xml.DummyReferencer" scope="prototype">
|
||||
<property name="testBean1"><ref local="kathy"/></property>
|
||||
<property name="testBean2"><ref bean="kathy"/></property>
|
||||
</bean>
|
||||
|
||||
<bean id="listenerVeto" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>listenerVeto</value></property>
|
||||
<property name="age"><value>66</value></property>
|
||||
</bean>
|
||||
|
||||
<bean id="validEmpty" class="org.springframework.beans.TestBean"/>
|
||||
|
||||
<bean id="commentsInValue" class="org.springframework.beans.TestBean">
|
||||
<property name="name"><value>this is<!-- don't mind me --> a <![CDATA[<!--comment-->]]></value></property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBeanTests$FilterTestLocalSessionFactoryBean">
|
||||
<property name="filterDefinitions">
|
||||
<list>
|
||||
<bean class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean">
|
||||
<property name="filterName" value="filter1"/>
|
||||
<property name="parameterTypes">
|
||||
<props>
|
||||
<prop key="param1">string</prop>
|
||||
<prop key="otherParam">long</prop>
|
||||
</props>
|
||||
</property>
|
||||
<property name="defaultFilterCondition" value="someCondition"/>
|
||||
</bean>
|
||||
<bean id="filter2" class="org.springframework.orm.hibernate3.FilterDefinitionFactoryBean">
|
||||
<property name="parameterTypes">
|
||||
<props>
|
||||
<prop key="myParam">integer</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBeanTests$TypeTestLocalSessionFactoryBean">
|
||||
<property name="typeDefinitions">
|
||||
<list>
|
||||
<bean class="org.springframework.orm.hibernate3.TypeDefinitionBean">
|
||||
<property name="typeName" value="type1"/>
|
||||
<property name="typeClass" value="mypackage.MyTypeClass"/>
|
||||
<property name="parameters">
|
||||
<props>
|
||||
<prop key="param1">value1</prop>
|
||||
<prop key="otherParam">othervalue</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
<bean id="type2" class="org.springframework.orm.hibernate3.TypeDefinitionBean">
|
||||
<property name="typeName" value="type2"/>
|
||||
<property name="typeClass" value="mypackage.MyOtherTypeClass"/>
|
||||
<property name="parameters">
|
||||
<props>
|
||||
<prop key="myParam">myvalue</prop>
|
||||
</props>
|
||||
</property>
|
||||
</bean>
|
||||
</list>
|
||||
</property>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user