📄 hibernatetemplatetests.java
字号:
/*
* 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.orm.hibernate3;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.hibernate.Criteria;
import org.hibernate.Filter;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Interceptor;
import org.hibernate.JDBCException;
import org.hibernate.LockMode;
import org.hibernate.ObjectDeletedException;
import org.hibernate.ObjectNotFoundException;
import org.hibernate.PersistentObjectException;
import org.hibernate.Query;
import org.hibernate.QueryException;
import org.hibernate.ReplicationMode;
import org.hibernate.SessionFactory;
import org.hibernate.StaleObjectStateException;
import org.hibernate.StaleStateException;
import org.hibernate.TransientObjectException;
import org.hibernate.WrongClassException;
import org.hibernate.classic.Session;
import org.springframework.beans.TestBean;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.transaction.support.TransactionSynchronizationManager;
/**
* @author Juergen Hoeller
* @since 05.03.2005
*/
public class HibernateTemplateTests extends TestCase {
private MockControl sfControl;
private SessionFactory sf;
private MockControl sessionControl;
private Session session;
protected void setUp() {
sfControl = MockControl.createControl(SessionFactory.class);
sf = (SessionFactory) sfControl.getMock();
sessionControl = MockControl.createControl(Session.class);
session = (Session) sessionControl.getMock();
}
public void testExecuteWithNewSession() throws HibernateException {
sf.openSession();
sfControl.setReturnValue(session, 1);
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.flush();
sessionControl.setVoidCallable(1);
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
assertTrue("Correct allowCreate default", ht.isAllowCreate());
assertTrue("Correct flushMode default", ht.getFlushMode() == HibernateTemplate.FLUSH_AUTO);
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
public void testExecuteWithNewSessionAndFlushNever() throws HibernateException {
sf.openSession();
sfControl.setReturnValue(session, 1);
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.setFlushMode(FlushMode.NEVER);
sessionControl.setVoidCallable(1);
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setFlushMode(HibernateTemplate.FLUSH_NEVER);
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
public void testExecuteWithNewSessionAndFilter() throws HibernateException {
sf.openSession();
sfControl.setReturnValue(session, 1);
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.enableFilter("myFilter");
sessionControl.setReturnValue(null, 1);
session.flush();
sessionControl.setVoidCallable(1);
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setFilterName("myFilter");
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
public void testExecuteWithNewSessionAndFilters() throws HibernateException {
sf.openSession();
sfControl.setReturnValue(session, 1);
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.enableFilter("myFilter");
sessionControl.setReturnValue(null, 1);
session.enableFilter("yourFilter");
sessionControl.setReturnValue(null, 1);
session.flush();
sessionControl.setVoidCallable(1);
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setFilterNames(new String[] {"myFilter", "yourFilter"});
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
public void testExecuteWithNotAllowCreate() {
HibernateTemplate ht = new HibernateTemplate();
ht.setSessionFactory(sf);
ht.setAllowCreate(false);
try {
ht.execute(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException{
return null;
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
public void testExecuteWithNotAllowCreateAndThreadBound() {
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndFlushEager() throws HibernateException {
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.getFlushMode();
sessionControl.setReturnValue(FlushMode.AUTO);
session.flush();
sessionControl.setVoidCallable(1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setFlushModeName("FLUSH_EAGER");
ht.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndFilter() {
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.enableFilter("myFilter");
sessionControl.setReturnValue(null, 1);
session.disableFilter("myFilter");
sessionControl.setVoidCallable(1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAllowCreate(false);
ht.setFilterName("myFilter");
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndFilters() {
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.enableFilter("myFilter");
sessionControl.setReturnValue(null, 1);
session.enableFilter("yourFilter");
sessionControl.setReturnValue(null, 1);
session.disableFilter("myFilter");
sessionControl.setVoidCallable(1);
session.disableFilter("yourFilter");
sessionControl.setVoidCallable(1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAllowCreate(false);
ht.setFilterNames(new String[] {"myFilter", "yourFilter"});
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
List result = ht.executeFind(new HibernateCallback() {
public Object doInHibernate(org.hibernate.Session session) throws HibernateException {
return l;
}
});
assertTrue("Correct result list", result == l);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndParameterizedFilter() {
MockControl filterControl = MockControl.createControl(Filter.class);
Filter filter = (Filter) filterControl.getMock();
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.getEnabledFilter("myFilter");
sessionControl.setReturnValue(null, 1);
session.enableFilter("myFilter");
sessionControl.setReturnValue(filter, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAllowCreate(false);
ht.setFilterName("myFilter");
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
Filter f = ht.enableFilter("myFilter");
assertTrue("Correct filter", f == filter);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndParameterizedExistingFilter() {
MockControl filterControl = MockControl.createControl(Filter.class);
Filter filter = (Filter) filterControl.getMock();
session.getSessionFactory();
sessionControl.setReturnValue(sf, 1);
session.isOpen();
sessionControl.setReturnValue(true, 1);
session.getEnabledFilter("myFilter");
sessionControl.setReturnValue(filter, 1);
sfControl.replay();
sessionControl.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAllowCreate(false);
ht.setFilterName("myFilter");
TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
try {
final List l = new ArrayList();
l.add("test");
Filter f = ht.enableFilter("myFilter");
assertTrue("Correct filter", f == filter);
}
finally {
TransactionSynchronizationManager.unbindResource(sf);
}
}
public void testExecuteWithThreadBoundAndNewSession() throws HibernateException {
MockControl conControl = MockControl.createControl(Connection.class);
Connection con = (Connection) conControl.getMock();
MockControl session2Control = MockControl.createControl(Session.class);
Session session2 = (Session) session2Control.getMock();
session2.connection();
session2Control.setReturnValue(con, 1);
sf.openSession(con);
sfControl.setReturnValue(session, 1);
session.flush();
sessionControl.setVoidCallable(1);
session.close();
sessionControl.setReturnValue(null, 1);
sfControl.replay();
sessionControl.replay();
session2Control.replay();
HibernateTemplate ht = new HibernateTemplate(sf);
ht.setAlwaysUseNewSession(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -