📄 localsessionfactorybeantests.java
字号:
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 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 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;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -