Upgrade to JPA 2.1+ and Bean Validation 1.1+; remove native support for Hibernate 3.6 and 4.x
Issue: SPR-13481 Issue: SPR-13827
This commit is contained in:
@@ -1,375 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.orm.hibernate3;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.aopalliance.intercept.MethodInvocation;
|
||||
import org.hibernate.FlushMode;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InOrder;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @since 05.03.2005
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class HibernateInterceptorTests {
|
||||
|
||||
private SessionFactory sessionFactory;
|
||||
private Session session;
|
||||
private MethodInvocation invocation;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Throwable {
|
||||
this.sessionFactory = mock(SessionFactory.class);
|
||||
this.session = mock(Session.class);
|
||||
this.invocation = mock(MethodInvocation.class);
|
||||
given(sessionFactory.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sessionFactory);
|
||||
given(invocation.proceed()).willAnswer(new Answer<Object>() {
|
||||
@Override
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
|
||||
throw new IllegalStateException("Session not bound");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
|
||||
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithNewSession() throws HibernateException {
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
|
||||
verify(session).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithNewSessionAndFlushNever() throws HibernateException {
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setFlushModeName("FLUSH_NEVER");
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session, never()).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithNewSessionAndFilter() throws HibernateException {
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setFilterName("myFilter");
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
|
||||
verify(session).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBound() {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
verify(session, never()).flush();
|
||||
verify(session, never()).close();
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFlushEager() throws HibernateException {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.AUTO);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER);
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
verify(session).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFlushEagerSwitch() throws HibernateException {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.NEVER);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER);
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
InOrder ordered = inOrder(session);
|
||||
ordered.verify(session).setFlushMode(FlushMode.AUTO);
|
||||
ordered.verify(session).flush();
|
||||
ordered.verify(session).setFlushMode(FlushMode.NEVER);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFlushCommit() {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.AUTO);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setFlushMode(HibernateInterceptor.FLUSH_COMMIT);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
InOrder ordered = inOrder(session);
|
||||
ordered.verify(session).setFlushMode(FlushMode.COMMIT);
|
||||
ordered.verify(session).setFlushMode(FlushMode.AUTO);
|
||||
verify(session, never()).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFlushAlways() {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.AUTO);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setFlushMode(HibernateInterceptor.FLUSH_ALWAYS);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
InOrder ordered = inOrder(session);
|
||||
ordered.verify(session).setFlushMode(FlushMode.ALWAYS);
|
||||
ordered.verify(session).setFlushMode(FlushMode.AUTO);
|
||||
verify(session, never()).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFilter() {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setFilterName("myFilter");
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
InOrder ordered = inOrder(session);
|
||||
ordered.verify(session).enableFilter("myFilter");
|
||||
ordered.verify(session).disableFilter("myFilter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundAndFilters() {
|
||||
given(session.isOpen()).willReturn(true);
|
||||
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setFilterNames(new String[] {"myFilter", "yourFilter"});
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.unbindResource(sessionFactory);
|
||||
}
|
||||
|
||||
InOrder ordered = inOrder(session);
|
||||
ordered.verify(session).enableFilter("myFilter");
|
||||
ordered.verify(session).enableFilter("yourFilter");
|
||||
ordered.verify(session).disableFilter("myFilter");
|
||||
ordered.verify(session).disableFilter("yourFilter");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithFlushFailure() throws Throwable {
|
||||
SQLException sqlEx = new SQLException("argh", "27");
|
||||
ConstraintViolationException jdbcEx = new ConstraintViolationException("", sqlEx, null);
|
||||
willThrow(jdbcEx).given(session).flush();
|
||||
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
fail("Should have thrown DataIntegrityViolationException");
|
||||
}
|
||||
catch (DataIntegrityViolationException ex) {
|
||||
// expected
|
||||
assertEquals(jdbcEx, ex.getCause());
|
||||
}
|
||||
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithThreadBoundEmptyHolder() {
|
||||
SessionHolder holder = new SessionHolder("key", session);
|
||||
holder.removeSession("key");
|
||||
TransactionSynchronizationManager.bindResource(sessionFactory, holder);
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
|
||||
verify(session).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithEntityInterceptor() throws HibernateException {
|
||||
org.hibernate.Interceptor entityInterceptor = mock(org.hibernate.Interceptor.class);
|
||||
given(sessionFactory.openSession(entityInterceptor)).willReturn(session);
|
||||
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setEntityInterceptor(entityInterceptor);
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
|
||||
verify(session).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInterceptorWithEntityInterceptorBeanName() throws HibernateException {
|
||||
org.hibernate.Interceptor entityInterceptor = mock(org.hibernate.Interceptor.class);
|
||||
org.hibernate.Interceptor entityInterceptor2 = mock(org.hibernate.Interceptor.class);
|
||||
|
||||
given(sessionFactory.openSession(entityInterceptor)).willReturn(session);
|
||||
given(sessionFactory.openSession(entityInterceptor2)).willReturn(session);
|
||||
|
||||
BeanFactory beanFactory = mock(BeanFactory.class);
|
||||
given(beanFactory.getBean("entityInterceptor", org.hibernate.Interceptor.class)).willReturn(
|
||||
entityInterceptor, entityInterceptor2);
|
||||
|
||||
HibernateInterceptor interceptor = new HibernateInterceptor();
|
||||
interceptor.setSessionFactory(sessionFactory);
|
||||
interceptor.setEntityInterceptorBeanName("entityInterceptor");
|
||||
interceptor.setBeanFactory(beanFactory);
|
||||
for (int i = 0; i < 2; i++) {
|
||||
try {
|
||||
interceptor.invoke(invocation);
|
||||
}
|
||||
catch (Throwable t) {
|
||||
fail("Should not have thrown Throwable: " + t.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
verify(session, times(2)).flush();
|
||||
verify(session, times(2)).close();
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,659 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.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 org.hibernate.HibernateException;
|
||||
import org.hibernate.Interceptor;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.cache.RegionFactory;
|
||||
import org.hibernate.cache.impl.NoCachingRegionFactory;
|
||||
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.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @since 05.03.2005
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class LocalSessionFactoryBeanTests {
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithDataSource() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithCacheRegionFactory() throws Exception {
|
||||
final RegionFactory regionFactory = new NoCachingRegionFactory(null);
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
assertEquals(LocalRegionFactoryProxy.class.getName(),
|
||||
config.getProperty(Environment.CACHE_REGION_FACTORY));
|
||||
assertSame(regionFactory, LocalSessionFactoryBean.getConfigTimeRegionFactory());
|
||||
invocations.add("newSessionFactory");
|
||||
return null;
|
||||
}
|
||||
};
|
||||
sfb.setCacheRegionFactory(regionFactory);
|
||||
sfb.afterPropertiesSet();
|
||||
assertTrue(sfb.getConfiguration() != null);
|
||||
assertEquals("newSessionFactory", invocations.get(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithTransactionAwareDataSource() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndMappingResources() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final TransactionManager tm = mock(TransactionManager.class);
|
||||
final List invocations = new ArrayList();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndMappingJarLocations() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addJar(File file) {
|
||||
invocations.add("addResource " + file.getPath());
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithDataSourceAndProperties() throws Exception {
|
||||
final DriverManagerDataSource ds = new DriverManagerDataSource();
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration addInputStream(InputStream is) {
|
||||
try {
|
||||
is.close();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
}
|
||||
invocations.add("addResource");
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalSessionFactoryBeanWithValidProperties() throws Exception {
|
||||
final Set invocations = new HashSet();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception {
|
||||
final SessionFactory sessionFactory = mock(SessionFactory.class);
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
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();
|
||||
verify(sessionFactory).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithEntityInterceptor() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration setInterceptor(Interceptor interceptor) {
|
||||
throw new IllegalArgumentException(interceptor.toString());
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Interceptor entityInterceptor = mock(Interceptor.class);
|
||||
sfb.setEntityInterceptor(entityInterceptor);
|
||||
try {
|
||||
sfb.afterPropertiesSet();
|
||||
fail("Should have thrown IllegalArgumentException");
|
||||
}
|
||||
catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
assertTrue("Correct exception", ex.getMessage().equals(entityInterceptor.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithNamingStrategy() throws Exception {
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
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()));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithCacheStrategies() throws Exception {
|
||||
final Properties registeredClassCache = new Properties();
|
||||
final Properties registeredCollectionCache = new Properties();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy) {
|
||||
registeredClassCache.setProperty(clazz, concurrencyStrategy);
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public Configuration setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy) {
|
||||
registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy);
|
||||
return this;
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Properties classCache = new Properties();
|
||||
classCache.setProperty("org.springframework.tests.sample.beans.TestBean", "read-write");
|
||||
sfb.setEntityCacheStrategies(classCache);
|
||||
Properties collectionCache = new Properties();
|
||||
collectionCache.setProperty("org.springframework.tests.sample.beans.TestBean.friends", "read-only");
|
||||
sfb.setCollectionCacheStrategies(collectionCache);
|
||||
sfb.afterPropertiesSet();
|
||||
|
||||
assertEquals(classCache, registeredClassCache);
|
||||
assertEquals(collectionCache, registeredCollectionCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithCacheStrategiesAndRegions() throws Exception {
|
||||
final Properties registeredClassCache = new Properties();
|
||||
final Properties registeredCollectionCache = new Properties();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public Configuration setCacheConcurrencyStrategy(String clazz, String concurrencyStrategy, String regionName) {
|
||||
registeredClassCache.setProperty(clazz, concurrencyStrategy + "," + regionName);
|
||||
return this;
|
||||
}
|
||||
@Override
|
||||
public void setCollectionCacheConcurrencyStrategy(String collectionRole, String concurrencyStrategy, String regionName) {
|
||||
registeredCollectionCache.setProperty(collectionRole, concurrencyStrategy + "," + regionName);
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
sfb.setMappingResources(new String[0]);
|
||||
sfb.setDataSource(new DriverManagerDataSource());
|
||||
Properties classCache = new Properties();
|
||||
classCache.setProperty("org.springframework.tests.sample.beans.TestBean", "read-write,myRegion");
|
||||
sfb.setEntityCacheStrategies(classCache);
|
||||
Properties collectionCache = new Properties();
|
||||
collectionCache.setProperty("org.springframework.tests.sample.beans.TestBean.friends", "read-only,myRegion");
|
||||
sfb.setCollectionCacheStrategies(collectionCache);
|
||||
sfb.afterPropertiesSet();
|
||||
|
||||
assertEquals(classCache, registeredClassCache);
|
||||
assertEquals(collectionCache, registeredCollectionCache);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithEventListeners() throws Exception {
|
||||
final Map registeredListeners = new HashMap();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public void setListener(String type, Object listener) {
|
||||
registeredListeners.put(type, listener);
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("serial")
|
||||
public void testLocalSessionFactoryBeanWithEventListenerSet() throws Exception {
|
||||
final Map registeredListeners = new HashMap();
|
||||
LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() {
|
||||
@Override
|
||||
protected Configuration newConfiguration() {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public void setListeners(String type, Object[] listeners) {
|
||||
assertTrue(listeners instanceof MergeEventListener[]);
|
||||
registeredListeners.put(type, new HashSet(Arrays.asList(listeners)));
|
||||
}
|
||||
};
|
||||
}
|
||||
@Override
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
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"));
|
||||
}
|
||||
*/
|
||||
|
||||
@Test
|
||||
public void testLocalSessionFactoryBeanWithTypeDefinitions() throws Exception {
|
||||
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
|
||||
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(new ClassPathResource("typeDefinitions.xml", getClass()));
|
||||
TypeTestLocalSessionFactoryBean sf = (TypeTestLocalSessionFactoryBean) xbf.getBean("&sessionFactory");
|
||||
// Requires re-compilation when switching to Hibernate 3.5/3.6
|
||||
// since Mappings changed from a class to an interface
|
||||
TypeDef type1 = sf.mappings.getTypeDef("type1");
|
||||
TypeDef type2 = 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"));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class FilterTestLocalSessionFactoryBean extends LocalSessionFactoryBean {
|
||||
|
||||
public List registeredFilterDefinitions = new LinkedList();
|
||||
|
||||
@Override
|
||||
protected Configuration newConfiguration() throws HibernateException {
|
||||
return new Configuration() {
|
||||
@Override
|
||||
public void addFilterDefinition(FilterDefinition definition) {
|
||||
registeredFilterDefinitions.add(definition);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class TypeTestLocalSessionFactoryBean extends LocalSessionFactoryBean {
|
||||
|
||||
public Mappings mappings;
|
||||
|
||||
@Override
|
||||
protected SessionFactory newSessionFactory(Configuration config) {
|
||||
this.mappings = config.createMappings();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public static class DummyMergeEventListener implements MergeEventListener {
|
||||
|
||||
@Override
|
||||
public void onMerge(MergeEvent event) throws HibernateException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMerge(MergeEvent event, Map copiedAlready) throws HibernateException {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.orm.hibernate3.support;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.orm.hibernate3.HibernateTemplate;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @since 05.03.2005
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class HibernateDaoSupportTests {
|
||||
|
||||
@Test
|
||||
public void testHibernateDaoSupportWithSessionFactory() throws Exception {
|
||||
SessionFactory sf = mock(SessionFactory.class);
|
||||
final List test = new ArrayList();
|
||||
HibernateDaoSupport dao = new HibernateDaoSupport() {
|
||||
@Override
|
||||
protected void initDao() {
|
||||
test.add("test");
|
||||
}
|
||||
};
|
||||
dao.setSessionFactory(sf);
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("Correct SessionFactory", sf, dao.getSessionFactory());
|
||||
assertEquals("Correct HibernateTemplate", sf, dao.getHibernateTemplate().getSessionFactory());
|
||||
assertEquals("initDao called", test.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHibernateDaoSupportWithHibernateTemplate() throws Exception {
|
||||
HibernateTemplate template = new HibernateTemplate();
|
||||
final List test = new ArrayList();
|
||||
HibernateDaoSupport dao = new HibernateDaoSupport() {
|
||||
@Override
|
||||
protected void initDao() {
|
||||
test.add("test");
|
||||
}
|
||||
};
|
||||
dao.setHibernateTemplate(template);
|
||||
dao.afterPropertiesSet();
|
||||
assertEquals("Correct HibernateTemplate", template, dao.getHibernateTemplate());
|
||||
assertEquals("initDao called", test.size(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,506 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-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.orm.hibernate3.support;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.transaction.Status;
|
||||
import javax.transaction.Synchronization;
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.jdbc.support.lob.LobCreator;
|
||||
import org.springframework.jdbc.support.lob.LobHandler;
|
||||
import org.springframework.orm.hibernate3.SessionFactoryUtils;
|
||||
import org.springframework.tests.transaction.MockJtaTransaction;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Phillip Webb
|
||||
* @since 05.03.2005
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class LobTypeTests {
|
||||
|
||||
private ResultSet rs = mock(ResultSet.class);
|
||||
private PreparedStatement ps = mock(PreparedStatement.class);
|
||||
private LobHandler lobHandler = mock(LobHandler.class);
|
||||
private LobCreator lobCreator = mock(LobCreator.class);
|
||||
|
||||
@Before
|
||||
public void setUp() throws SQLException {
|
||||
given(lobHandler.getLobCreator()).willReturn(lobCreator);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
|
||||
assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
|
||||
verify(lobCreator).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClobStringType() throws Exception {
|
||||
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
|
||||
|
||||
ClobStringType type = new ClobStringType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.CLOB, type.sqlTypes()[0]);
|
||||
assertEquals(String.class, type.returnedClass());
|
||||
assertTrue(type.equals("content", "content"));
|
||||
assertEquals("content", type.deepCopy("content"));
|
||||
assertFalse(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
assertTrue(synchs.get(0).getClass().getName().endsWith("SpringLobCreatorSynchronization"));
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setClobAsString(ps, 1, "content");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClobStringTypeWithSynchronizedSession() throws Exception {
|
||||
SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
|
||||
|
||||
ClobStringType type = new ClobStringType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.CLOB, type.sqlTypes()[0]);
|
||||
assertEquals(String.class, type.returnedClass());
|
||||
assertTrue(type.equals("content", "content"));
|
||||
assertEquals("content", type.deepCopy("content"));
|
||||
assertFalse(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
SessionFactoryUtils.getSession(sf, true);
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(2, synchs.size());
|
||||
assertTrue(synchs.get(0).getClass().getName().endsWith("SpringLobCreatorSynchronization"));
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
((TransactionSynchronization) synchs.get(1)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(1)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
|
||||
verify(session).close();
|
||||
verify(lobCreator).setClobAsString(ps, 1, "content");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClobStringTypeWithFlushOnCommit() throws Exception {
|
||||
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
|
||||
|
||||
ClobStringType type = new ClobStringType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.CLOB, type.sqlTypes()[0]);
|
||||
assertEquals(String.class, type.returnedClass());
|
||||
assertTrue(type.equals("content", "content"));
|
||||
assertEquals("content", type.deepCopy("content"));
|
||||
assertFalse(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setClobAsString(ps, 1, "content");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClobStringTypeWithJtaSynchronization() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
|
||||
|
||||
ClobStringType type = new ClobStringType(lobHandler, tm);
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.beforeCompletion();
|
||||
synch.afterCompletion(Status.STATUS_COMMITTED);
|
||||
verify(lobCreator).setClobAsString(ps, 1, "content");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testClobStringTypeWithJtaSynchronizationAndRollback() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
given(lobHandler.getClobAsString(rs, "column")).willReturn("content");
|
||||
|
||||
ClobStringType type = new ClobStringType(lobHandler, tm);
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.afterCompletion(Status.STATUS_ROLLEDBACK);
|
||||
|
||||
verify(lobCreator).setClobAsString(ps, 1, "content");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobStringType() throws Exception {
|
||||
String content = "content";
|
||||
byte[] contentBytes = content.getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes);
|
||||
|
||||
BlobStringType type = new BlobStringType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.BLOB, type.sqlTypes()[0]);
|
||||
assertEquals(String.class, type.returnedClass());
|
||||
assertTrue(type.equals("content", "content"));
|
||||
assertEquals("content", type.deepCopy("content"));
|
||||
assertFalse(type.isMutable());
|
||||
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobStringTypeWithNull() throws Exception {
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(null);
|
||||
|
||||
BlobStringType type = new BlobStringType(lobHandler, null);
|
||||
assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, null, 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobStringTypeWithJtaSynchronization() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
String content = "content";
|
||||
byte[] contentBytes = content.getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes);
|
||||
|
||||
BlobStringType type = new BlobStringType(lobHandler, tm);
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.beforeCompletion();
|
||||
synch.afterCompletion(Status.STATUS_COMMITTED);
|
||||
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobStringTypeWithJtaSynchronizationAndRollback() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
String content = "content";
|
||||
byte[] contentBytes = content.getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(contentBytes);
|
||||
|
||||
BlobStringType type = new BlobStringType(lobHandler, tm);
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.afterCompletion(Status.STATUS_ROLLEDBACK);
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, contentBytes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobByteArrayType() throws Exception {
|
||||
byte[] content = "content".getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);
|
||||
|
||||
BlobByteArrayType type = new BlobByteArrayType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.BLOB, type.sqlTypes()[0]);
|
||||
assertEquals(byte[].class, type.returnedClass());
|
||||
assertTrue(type.equals(new byte[] {(byte) 255}, new byte[] {(byte) 255}));
|
||||
assertTrue(Arrays.equals(new byte[] {(byte) 255}, (byte[]) type.deepCopy(new byte[] {(byte) 255})));
|
||||
assertTrue(type.isMutable());
|
||||
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobByteArrayTypeWithJtaSynchronization() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
byte[] content = "content".getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);
|
||||
|
||||
BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm);
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.beforeCompletion();
|
||||
synch.afterCompletion(Status.STATUS_COMMITTED);
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobByteArrayTypeWithJtaSynchronizationAndRollback() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
byte[] content = "content".getBytes();
|
||||
given(lobHandler.getBlobAsBytes(rs, "column")).willReturn(content);
|
||||
|
||||
BlobByteArrayType type = new BlobByteArrayType(lobHandler, tm);
|
||||
assertEquals(content, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, content, 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.afterCompletion(Status.STATUS_ROLLEDBACK);
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, content);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobSerializableType() throws Exception {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject("content");
|
||||
oos.close();
|
||||
|
||||
given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(new ByteArrayInputStream(baos.toByteArray()));
|
||||
|
||||
BlobSerializableType type = new BlobSerializableType(lobHandler, null);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.BLOB, type.sqlTypes()[0]);
|
||||
assertEquals(Serializable.class, type.returnedClass());
|
||||
assertTrue(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
((TransactionSynchronization) synchs.get(0)).afterCompletion(TransactionSynchronization.STATUS_COMMITTED);
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobSerializableTypeWithNull() throws Exception {
|
||||
given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(null);
|
||||
|
||||
BlobSerializableType type = new BlobSerializableType(lobHandler, null);
|
||||
assertEquals(null, type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
try {
|
||||
type.nullSafeSet(ps, null, 1);
|
||||
List synchs = TransactionSynchronizationManager.getSynchronizations();
|
||||
assertEquals(1, synchs.size());
|
||||
((TransactionSynchronization) synchs.get(0)).beforeCompletion();
|
||||
}
|
||||
finally {
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
}
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobSerializableTypeWithJtaSynchronization() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject("content");
|
||||
oos.close();
|
||||
|
||||
given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(
|
||||
new ByteArrayInputStream(baos.toByteArray()));
|
||||
|
||||
BlobSerializableType type = new BlobSerializableType(lobHandler, tm);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.BLOB, type.sqlTypes()[0]);
|
||||
assertEquals(Serializable.class, type.returnedClass());
|
||||
assertTrue(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.beforeCompletion();
|
||||
synch.afterCompletion(Status.STATUS_COMMITTED);
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBlobSerializableTypeWithJtaSynchronizationAndRollback() throws Exception {
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
MockJtaTransaction transaction = new MockJtaTransaction();
|
||||
given(tm.getStatus()).willReturn(Status.STATUS_ACTIVE);
|
||||
given(tm.getTransaction()).willReturn(transaction);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject("content");
|
||||
oos.close();
|
||||
|
||||
given(lobHandler.getBlobAsBinaryStream(rs, "column")).willReturn(
|
||||
new ByteArrayInputStream(baos.toByteArray()));
|
||||
|
||||
BlobSerializableType type = new BlobSerializableType(lobHandler, tm);
|
||||
assertEquals(1, type.sqlTypes().length);
|
||||
assertEquals(Types.BLOB, type.sqlTypes()[0]);
|
||||
assertEquals(Serializable.class, type.returnedClass());
|
||||
assertTrue(type.isMutable());
|
||||
|
||||
assertEquals("content", type.nullSafeGet(rs, new String[] {"column"}, null));
|
||||
type.nullSafeSet(ps, "content", 1);
|
||||
Synchronization synch = transaction.getSynchronization();
|
||||
assertNotNull(synch);
|
||||
synch.afterCompletion(Status.STATUS_ROLLEDBACK);
|
||||
verify(lobCreator).setBlobAsBytes(ps, 1, baos.toByteArray());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHbm2JavaStyleInitialization() throws Exception {
|
||||
ClobStringType cst = null;
|
||||
BlobByteArrayType bbat = null;
|
||||
BlobSerializableType bst = null;
|
||||
try {
|
||||
cst = new ClobStringType();
|
||||
bbat = new BlobByteArrayType();
|
||||
bst = new BlobSerializableType();
|
||||
}
|
||||
catch (Exception ex) {
|
||||
fail("Should not have thrown exception on initialization");
|
||||
}
|
||||
|
||||
try {
|
||||
cst.nullSafeGet(rs, new String[] {"column"}, null);
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
bbat.nullSafeGet(rs, new String[] {"column"}, null);
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
try {
|
||||
bst.nullSafeGet(rs, new String[] {"column"}, null);
|
||||
fail("Should have thrown IllegalStateException");
|
||||
}
|
||||
catch (IllegalStateException ex) {
|
||||
// expected
|
||||
}
|
||||
lobCreator.close();
|
||||
}
|
||||
}
|
||||
@@ -1,754 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.servlet.AsyncEvent;
|
||||
import javax.servlet.AsyncListener;
|
||||
import javax.servlet.FilterChain;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.transaction.TransactionManager;
|
||||
|
||||
import org.hibernate.FlushMode;
|
||||
import org.hibernate.SessionFactory;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.classic.Session;
|
||||
import org.hibernate.engine.SessionFactoryImplementor;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
import org.springframework.mock.web.test.MockAsyncContext;
|
||||
import org.springframework.mock.web.test.MockFilterConfig;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.test.MockServletContext;
|
||||
import org.springframework.mock.web.test.PassThroughFilterChain;
|
||||
import org.springframework.orm.hibernate3.HibernateAccessor;
|
||||
import org.springframework.orm.hibernate3.HibernateTransactionManager;
|
||||
import org.springframework.orm.hibernate3.SessionFactoryUtils;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
import org.springframework.web.context.request.async.AsyncWebRequest;
|
||||
import org.springframework.web.context.request.async.StandardServletAsyncWebRequest;
|
||||
import org.springframework.web.context.request.async.WebAsyncManager;
|
||||
import org.springframework.web.context.request.async.WebAsyncUtils;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.util.NestedServletException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* @author Juergen Hoeller
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Phillip Webb
|
||||
* @since 05.03.2005
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class OpenSessionInViewTests {
|
||||
|
||||
private MockServletContext sc;
|
||||
|
||||
private MockHttpServletRequest request;
|
||||
|
||||
private MockHttpServletResponse response;
|
||||
|
||||
private ServletWebRequest webRequest;
|
||||
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
this.sc = new MockServletContext();
|
||||
this.request = new MockHttpServletRequest(sc);
|
||||
this.request.setAsyncSupported(true);
|
||||
this.response = new MockHttpServletResponse();
|
||||
this.webRequest = new ServletWebRequest(this.request);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInterceptor() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
final Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInterceptor interceptor = new OpenSessionInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
Runnable tb = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertEquals(session, SessionFactoryUtils.getSession(sf, false));
|
||||
}
|
||||
};
|
||||
ProxyFactory pf = new ProxyFactory(tb);
|
||||
pf.addAdvice(interceptor);
|
||||
Runnable tbProxy = (Runnable) pf.getProxy();
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.isOpen()).willReturn(true);
|
||||
tbProxy.run();
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorWithSingleSession() throws Exception {
|
||||
SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(session.isOpen()).willReturn(true);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
// check that further invocations simply participate
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertEquals(session, SessionFactoryUtils.getSession(sf, false));
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorAsyncScenario() throws Exception {
|
||||
// Initial request thread
|
||||
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
|
||||
asyncManager.setTaskExecutor(new SyncTaskExecutor());
|
||||
asyncManager.setAsyncWebRequest(asyncWebRequest);
|
||||
asyncManager.startCallableProcessing(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "anything";
|
||||
}
|
||||
});
|
||||
|
||||
interceptor.afterConcurrentHandlingStarted(this.webRequest);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
// Async dispatch thread
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue("Session not bound to async thread", TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
verify(session, never()).close();
|
||||
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorAsyncTimeoutScenario() throws Exception {
|
||||
// Initial request thread
|
||||
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
|
||||
asyncManager.setTaskExecutor(new SyncTaskExecutor());
|
||||
asyncManager.setAsyncWebRequest(asyncWebRequest);
|
||||
asyncManager.startCallableProcessing(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "anything";
|
||||
}
|
||||
});
|
||||
|
||||
interceptor.afterConcurrentHandlingStarted(this.webRequest);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
verify(session, never()).close();
|
||||
|
||||
// Async request timeout
|
||||
|
||||
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
|
||||
for (AsyncListener listener : asyncContext.getListeners()) {
|
||||
listener.onTimeout(new AsyncEvent(asyncContext));
|
||||
}
|
||||
for (AsyncListener listener : asyncContext.getListeners()) {
|
||||
listener.onComplete(new AsyncEvent(asyncContext));
|
||||
}
|
||||
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorWithSingleSessionAndJtaTm() throws Exception {
|
||||
final SessionFactoryImplementor sf = mock(SessionFactoryImplementor.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
TransactionManager tm = mock(TransactionManager.class);
|
||||
given(tm.getTransaction()).willReturn(null);
|
||||
given(tm.getTransaction()).willReturn(null);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(sf.getTransactionManager()).willReturn(tm);
|
||||
given(sf.getTransactionManager()).willReturn(tm);
|
||||
given(session.isOpen()).willReturn(true);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
// Check that further invocations simply participate
|
||||
interceptor.preHandle(this.webRequest);
|
||||
|
||||
assertEquals(session, SessionFactoryUtils.getSession(sf, false));
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorWithSingleSessionAndFlush() throws Exception {
|
||||
SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
interceptor.setFlushMode(HibernateAccessor.FLUSH_AUTO);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
interceptor.preHandle(this.webRequest);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
verify(session).flush();
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewInterceptorAndDeferredClose() throws Exception {
|
||||
SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
interceptor.setSingleSession(false);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true);
|
||||
SessionFactoryUtils.releaseSession(sess, sf);
|
||||
|
||||
// check that further invocations simply participate
|
||||
interceptor.preHandle(this.webRequest);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterWithSingleSession() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(session.close()).willReturn(null);
|
||||
|
||||
final SessionFactory sf2 = mock(SessionFactory.class);
|
||||
Session session2 = mock(Session.class);
|
||||
|
||||
given(sf2.openSession()).willReturn(session2);
|
||||
given(session2.getSessionFactory()).willReturn(sf2);
|
||||
given(session2.close()).willReturn(null);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
|
||||
filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");
|
||||
filterConfig2.addInitParameter("flushMode", "AUTO");
|
||||
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter();
|
||||
filter2.init(filterConfig2);
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
servletRequest.setAttribute("invoked", Boolean.TRUE);
|
||||
}
|
||||
};
|
||||
|
||||
final FilterChain filterChain2 = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
|
||||
throws IOException, ServletException {
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf2));
|
||||
filter.doFilter(servletRequest, servletResponse, filterChain);
|
||||
}
|
||||
};
|
||||
|
||||
FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
|
||||
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf2));
|
||||
filter2.doFilter(this.request, this.response, filterChain3);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf2));
|
||||
assertNotNull(this.request.getAttribute("invoked"));
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session2).setFlushMode(FlushMode.AUTO);
|
||||
wac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterAsyncScenario() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
// Initial request during which concurrent handling starts..
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
|
||||
final AtomicInteger count = new AtomicInteger(0);
|
||||
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
count.incrementAndGet();
|
||||
}
|
||||
};
|
||||
|
||||
AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(this.request);
|
||||
asyncManager.setTaskExecutor(new SyncTaskExecutor());
|
||||
asyncManager.setAsyncWebRequest(asyncWebRequest);
|
||||
asyncManager.startCallableProcessing(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "anything";
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
filter.doFilter(this.request, this.response, filterChain);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertEquals(1, count.get());
|
||||
verify(session, never()).close();
|
||||
|
||||
// Async dispatch after concurrent handling produces result ...
|
||||
|
||||
this.request.setAsyncStarted(false);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
filter.doFilter(this.request, this.response, filterChain);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertEquals(2, count.get());
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
|
||||
wac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterAsyncTimeoutScenario() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
// Initial request during which concurrent handling starts..
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
|
||||
final AtomicInteger count = new AtomicInteger(0);
|
||||
final AsyncWebRequest asyncWebRequest = new StandardServletAsyncWebRequest(this.request, this.response);
|
||||
final MockHttpServletRequest request = this.request;
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
|
||||
throws NestedServletException {
|
||||
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
count.incrementAndGet();
|
||||
|
||||
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
|
||||
asyncManager.setTaskExecutor(new SyncTaskExecutor());
|
||||
asyncManager.setAsyncWebRequest(asyncWebRequest);
|
||||
try {
|
||||
asyncManager.startCallableProcessing(new Callable<String>() {
|
||||
@Override
|
||||
public String call() throws Exception {
|
||||
return "anything";
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new NestedServletException("", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
filter.doFilter(this.request, this.response, filterChain);
|
||||
assertFalse(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertEquals(1, count.get());
|
||||
verify(session, never()).close();
|
||||
|
||||
// Async request timeout ...
|
||||
|
||||
MockAsyncContext asyncContext = (MockAsyncContext) this.request.getAsyncContext();
|
||||
for (AsyncListener listener : asyncContext.getListeners()) {
|
||||
listener.onTimeout(new AsyncEvent(asyncContext));
|
||||
}
|
||||
for (AsyncListener listener : asyncContext.getListeners()) {
|
||||
listener.onComplete(new AsyncEvent(asyncContext));
|
||||
}
|
||||
|
||||
verify(session).close();
|
||||
|
||||
wac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterWithSingleSessionAndPreBoundSession() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
Session session = mock(Session.class);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
|
||||
filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
|
||||
interceptor.preHandle(this.webRequest);
|
||||
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
servletRequest.setAttribute("invoked", Boolean.TRUE);
|
||||
}
|
||||
};
|
||||
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
filter.doFilter(this.request, this.response, filterChain);
|
||||
assertTrue(TransactionSynchronizationManager.hasResource(sf));
|
||||
assertNotNull(this.request.getAttribute("invoked"));
|
||||
|
||||
interceptor.postHandle(this.webRequest, null);
|
||||
interceptor.afterCompletion(this.webRequest, null);
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
|
||||
wac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterWithDeferredClose() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
final Session session = mock(Session.class);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
|
||||
|
||||
final SessionFactory sf2 = mock(SessionFactory.class);
|
||||
final Session session2 = mock(Session.class);
|
||||
|
||||
Transaction tx = mock(Transaction.class);
|
||||
Connection con = mock(Connection.class);
|
||||
|
||||
given(sf2.openSession()).willReturn(session2);
|
||||
given(session2.connection()).willReturn(con);
|
||||
given(session2.beginTransaction()).willReturn(tx);
|
||||
given(session2.isConnected()).willReturn(true);
|
||||
given(session2.connection()).willReturn(con);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("mySessionFactory", sf2);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
|
||||
filterConfig.addInitParameter("singleSession", "false");
|
||||
filterConfig2.addInitParameter("singleSession", "false");
|
||||
filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");
|
||||
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter();
|
||||
filter2.init(filterConfig2);
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
|
||||
HibernateTransactionManager tm = new HibernateTransactionManager(sf);
|
||||
TransactionStatus ts = tm.getTransaction(
|
||||
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
|
||||
org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true);
|
||||
SessionFactoryUtils.releaseSession(sess, sf);
|
||||
tm.commit(ts);
|
||||
servletRequest.setAttribute("invoked", Boolean.TRUE);
|
||||
}
|
||||
};
|
||||
|
||||
final FilterChain filterChain2 = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
|
||||
throws IOException, ServletException {
|
||||
HibernateTransactionManager tm = new HibernateTransactionManager(sf2);
|
||||
TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
|
||||
tm.commit(ts);
|
||||
filter.doFilter(servletRequest, servletResponse, filterChain);
|
||||
}
|
||||
};
|
||||
|
||||
FilterChain filterChain3 = new PassThroughFilterChain(filter2, filterChain2);
|
||||
|
||||
filter2.doFilter(this.request, this.response, filterChain3);
|
||||
assertNotNull(this.request.getAttribute("invoked"));
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(tx).commit();
|
||||
verify(session2).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
verify(session2).close();
|
||||
|
||||
wac.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOpenSessionInViewFilterWithDeferredCloseAndAlreadyActiveDeferredClose() throws Exception {
|
||||
final SessionFactory sf = mock(SessionFactory.class);
|
||||
final Session session = mock(Session.class);
|
||||
|
||||
given(sf.openSession()).willReturn(session);
|
||||
given(session.getSessionFactory()).willReturn(sf);
|
||||
given(session.getFlushMode()).willReturn(FlushMode.MANUAL);
|
||||
|
||||
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||
wac.setServletContext(sc);
|
||||
wac.getDefaultListableBeanFactory().registerSingleton("sessionFactory", sf);
|
||||
wac.refresh();
|
||||
sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
|
||||
|
||||
MockFilterConfig filterConfig = new MockFilterConfig(wac.getServletContext(), "filter");
|
||||
MockFilterConfig filterConfig2 = new MockFilterConfig(wac.getServletContext(), "filter2");
|
||||
filterConfig.addInitParameter("singleSession", "false");
|
||||
filterConfig2.addInitParameter("singleSession", "false");
|
||||
filterConfig2.addInitParameter("sessionFactoryBeanName", "mySessionFactory");
|
||||
|
||||
OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor();
|
||||
interceptor.setSessionFactory(sf);
|
||||
interceptor.setSingleSession(false);
|
||||
|
||||
interceptor.preHandle(webRequest);
|
||||
|
||||
final OpenSessionInViewFilter filter = new OpenSessionInViewFilter();
|
||||
filter.init(filterConfig);
|
||||
final OpenSessionInViewFilter filter2 = new OpenSessionInViewFilter();
|
||||
filter2.init(filterConfig2);
|
||||
|
||||
final FilterChain filterChain = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) {
|
||||
HibernateTransactionManager tm = new HibernateTransactionManager(sf);
|
||||
TransactionStatus ts = tm.getTransaction(
|
||||
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS));
|
||||
org.hibernate.Session sess = SessionFactoryUtils.getSession(sf, true);
|
||||
SessionFactoryUtils.releaseSession(sess, sf);
|
||||
tm.commit(ts);
|
||||
servletRequest.setAttribute("invoked", Boolean.TRUE);
|
||||
}
|
||||
};
|
||||
|
||||
FilterChain filterChain2 = new FilterChain() {
|
||||
@Override
|
||||
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse)
|
||||
throws IOException, ServletException {
|
||||
filter.doFilter(servletRequest, servletResponse, filterChain);
|
||||
}
|
||||
};
|
||||
|
||||
filter.doFilter(this.request, this.response, filterChain2);
|
||||
assertNotNull(this.request.getAttribute("invoked"));
|
||||
|
||||
interceptor.postHandle(webRequest, null);
|
||||
interceptor.afterCompletion(webRequest, null);
|
||||
|
||||
verify(session).setFlushMode(FlushMode.MANUAL);
|
||||
verify(session).close();
|
||||
|
||||
wac.close();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static class SyncTaskExecutor extends SimpleAsyncTaskExecutor {
|
||||
|
||||
@Override
|
||||
public void execute(Runnable task, long startTimeout) {
|
||||
task.run();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2015 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.support;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.aop.framework.ProxyFactory;
|
||||
import org.springframework.aop.scope.ScopedObject;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ScopedBeanInterceptor}.
|
||||
*
|
||||
* @author Costin Leau
|
||||
* @deprecated as of Spring 4.3, in favor of Hibernate 4.x/5.x
|
||||
*/
|
||||
@Deprecated
|
||||
public class ScopedBeanInterceptorTests {
|
||||
|
||||
private final ScopedBeanInterceptor interceptor = new ScopedBeanInterceptor();
|
||||
|
||||
@Test
|
||||
public void interceptorWithPlainObject() throws Exception {
|
||||
final Object realObject = new Object();
|
||||
|
||||
ScopedObject scoped = new ScopedObject() {
|
||||
@Override
|
||||
public Object getTargetObject() {
|
||||
return realObject;
|
||||
}
|
||||
@Override
|
||||
public void removeFromScope() {
|
||||
// do nothing
|
||||
}
|
||||
};
|
||||
|
||||
// default contract is to return null for default behavior
|
||||
assertNull(interceptor.getEntityName(realObject));
|
||||
assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void interceptorWithCglibProxy() throws Exception {
|
||||
final Object realObject = new Object();
|
||||
ProxyFactory proxyFactory = new ProxyFactory();
|
||||
proxyFactory.setTarget(realObject);
|
||||
proxyFactory.setProxyTargetClass(true);
|
||||
final Object proxy = proxyFactory.getProxy();
|
||||
|
||||
ScopedObject scoped = new ScopedObject() {
|
||||
@Override
|
||||
public Object getTargetObject() {
|
||||
return proxy;
|
||||
}
|
||||
@Override
|
||||
public void removeFromScope() {
|
||||
// do nothing
|
||||
}
|
||||
};
|
||||
|
||||
assertEquals(realObject.getClass().getName(), interceptor.getEntityName(scoped));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -16,11 +16,12 @@
|
||||
|
||||
package org.springframework.orm.jpa.hibernate;
|
||||
|
||||
import org.hibernate.ejb.HibernateEntityManager;
|
||||
import org.hibernate.ejb.HibernateEntityManagerFactory;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.SessionFactory;
|
||||
|
||||
import org.springframework.orm.jpa.AbstractContainerEntityManagerFactoryIntegrationTests;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryInfo;
|
||||
import org.springframework.orm.jpa.EntityManagerProxy;
|
||||
|
||||
/**
|
||||
* Hibernate-specific JPA tests.
|
||||
@@ -39,13 +40,11 @@ public class HibernateEntityManagerFactoryIntegrationTests extends
|
||||
|
||||
public void testCanCastNativeEntityManagerFactoryToHibernateEntityManagerFactoryImpl() {
|
||||
EntityManagerFactoryInfo emfi = (EntityManagerFactoryInfo) entityManagerFactory;
|
||||
assertTrue(emfi.getNativeEntityManagerFactory() instanceof HibernateEntityManagerFactory);
|
||||
assertTrue(emfi.getNativeEntityManagerFactory() instanceof SessionFactory); // as of Hibernate 5.2
|
||||
}
|
||||
|
||||
public void testCanCastSharedEntityManagerProxyToHibernateEntityManager() {
|
||||
assertTrue(sharedEntityManager instanceof HibernateEntityManager);
|
||||
HibernateEntityManager hibernateEntityManager = (HibernateEntityManager) sharedEntityManager;
|
||||
assertNotNull(hibernateEntityManager.getSession());
|
||||
assertTrue(((EntityManagerProxy) sharedEntityManager).getTargetEntityManager() instanceof Session); // as of Hibernate 5.2
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2015 the original author or authors.
|
||||
* Copyright 2002-2016 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.
|
||||
@@ -21,6 +21,7 @@ import javax.persistence.EntityManagerFactory;
|
||||
import javax.persistence.EntityTransaction;
|
||||
import javax.persistence.PersistenceContext;
|
||||
import javax.persistence.PersistenceContextType;
|
||||
import javax.persistence.SynchronizationType;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
@@ -28,8 +29,6 @@ import org.junit.Test;
|
||||
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.TransactionStatus;
|
||||
import org.springframework.transaction.support.TransactionCallback;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@@ -40,7 +39,6 @@ import static org.mockito.BDDMockito.*;
|
||||
* @author Juergen Hoeller
|
||||
* @since 4.1.2
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public class PersistenceContextTransactionTests {
|
||||
|
||||
private EntityManagerFactory factory;
|
||||
@@ -94,12 +92,9 @@ public class PersistenceContextTransactionTests {
|
||||
public void testTransactionCommitWithSharedEntityManager() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(new TransactionCallback() {
|
||||
@Override
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
bean.sharedEntityManager.flush();
|
||||
return null;
|
||||
}
|
||||
tt.execute(status -> {
|
||||
bean.sharedEntityManager.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx).commit();
|
||||
@@ -113,12 +108,9 @@ public class PersistenceContextTransactionTests {
|
||||
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
tt.execute(new TransactionCallback() {
|
||||
@Override
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
bean.sharedEntityManager.clear();
|
||||
return null;
|
||||
}
|
||||
tt.execute(status -> {
|
||||
bean.sharedEntityManager.clear();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(manager).clear();
|
||||
@@ -129,12 +121,9 @@ public class PersistenceContextTransactionTests {
|
||||
public void testTransactionCommitWithExtendedEntityManager() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(new TransactionCallback() {
|
||||
@Override
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
bean.extendedEntityManager.flush();
|
||||
return null;
|
||||
}
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManager.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx, times(2)).commit();
|
||||
@@ -148,12 +137,111 @@ public class PersistenceContextTransactionTests {
|
||||
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
tt.execute(new TransactionCallback() {
|
||||
@Override
|
||||
public Object doInTransaction(TransactionStatus status) {
|
||||
bean.extendedEntityManager.flush();
|
||||
return null;
|
||||
}
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManager.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(manager).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithSharedEntityManagerUnsynchronized() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.sharedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx).commit();
|
||||
verify(manager).flush();
|
||||
verify(manager, times(2)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithSharedEntityManagerUnsynchronizedAndPropagationSupports() {
|
||||
given(manager.isOpen()).willReturn(true);
|
||||
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.sharedEntityManagerUnsynchronized.clear();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(manager).clear();
|
||||
verify(manager).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithExtendedEntityManagerUnsynchronized() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx).commit();
|
||||
verify(manager).flush();
|
||||
verify(manager).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedAndPropagationSupports() {
|
||||
given(manager.isOpen()).willReturn(true);
|
||||
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(manager).flush();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithSharedEntityManagerUnsynchronizedJoined() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.sharedEntityManagerUnsynchronized.joinTransaction();
|
||||
bean.sharedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx).commit();
|
||||
verify(manager).flush();
|
||||
verify(manager, times(2)).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedJoined() {
|
||||
given(manager.getTransaction()).willReturn(tx);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManagerUnsynchronized.joinTransaction();
|
||||
bean.extendedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(tx, times(2)).commit();
|
||||
verify(manager).flush();
|
||||
verify(manager).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTransactionCommitWithExtendedEntityManagerUnsynchronizedJoinedAndPropagationSupports() {
|
||||
given(manager.isOpen()).willReturn(true);
|
||||
|
||||
tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
|
||||
|
||||
tt.execute(status -> {
|
||||
bean.extendedEntityManagerUnsynchronized.joinTransaction();
|
||||
bean.extendedEntityManagerUnsynchronized.flush();
|
||||
return null;
|
||||
});
|
||||
|
||||
verify(manager).flush();
|
||||
@@ -167,6 +255,12 @@ public class PersistenceContextTransactionTests {
|
||||
|
||||
@PersistenceContext(type = PersistenceContextType.EXTENDED)
|
||||
public EntityManager extendedEntityManager;
|
||||
|
||||
@PersistenceContext(synchronization = SynchronizationType.UNSYNCHRONIZED)
|
||||
public EntityManager sharedEntityManagerUnsynchronized;
|
||||
|
||||
@PersistenceContext(type = PersistenceContextType.EXTENDED, synchronization = SynchronizationType.UNSYNCHRONIZED)
|
||||
public EntityManager extendedEntityManagerUnsynchronized;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user