Removed deprecated JdoTemplate and JdoInterceptor classes; formally requiring JDO 3.0+ now

This commit is contained in:
Juergen Hoeller
2013-03-29 13:17:13 +01:00
parent cc0ea4a824
commit 7ceb02257e
21 changed files with 94 additions and 2695 deletions

View File

@@ -1,150 +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.jdo;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Method;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.Invocation;
import org.aopalliance.intercept.MethodInvocation;
import org.junit.Test;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
*/
public class JdoInterceptorTests {
@Test
public void testInterceptor() {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
PersistenceManager pm = mock(PersistenceManager.class);
given(pmf.getPersistenceManager()).willReturn(pm);
JdoInterceptor interceptor = new JdoInterceptor();
interceptor.setPersistenceManagerFactory(pmf);
try {
interceptor.invoke(new TestInvocation(pmf));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
verify(pm).close();
}
@Test
public void testInterceptorWithPrebound() {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
PersistenceManager pm = mock(PersistenceManager.class);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
JdoInterceptor interceptor = new JdoInterceptor();
interceptor.setPersistenceManagerFactory(pmf);
try {
interceptor.invoke(new TestInvocation(pmf));
}
catch (Throwable t) {
fail("Should not have thrown Throwable: " + t.getMessage());
}
finally {
TransactionSynchronizationManager.unbindResource(pmf);
}
}
@SuppressWarnings("unused")
private static class TestInvocation implements MethodInvocation {
private PersistenceManagerFactory persistenceManagerFactory;
public TestInvocation(PersistenceManagerFactory persistenceManagerFactory) {
this.persistenceManagerFactory = persistenceManagerFactory;
}
@Override
public Object proceed() throws Throwable {
if (!TransactionSynchronizationManager.hasResource(this.persistenceManagerFactory)) {
throw new IllegalStateException("PersistenceManager not bound");
}
return null;
}
@Override
public Object[] getArguments() {
return null;
}
public int getCurrentInterceptorIndex() {
return 0;
}
public int getNumberOfInterceptors() {
return 0;
}
public Interceptor getInterceptor(int i) {
return null;
}
@Override
public Method getMethod() {
return null;
}
@Override
public AccessibleObject getStaticPart() {
return getMethod();
}
public Object getArgument(int i) {
return null;
}
public void setArgument(int i, Object handler) {
}
public int getArgumentCount() {
return 0;
}
@Override
public Object getThis() {
return null;
}
public Object getProxy() {
return null;
}
public Invocation cloneInstance() {
return null;
}
public void release() {
}
}
}

View File

@@ -1,534 +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.jdo;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import javax.jdo.JDODataStoreException;
import javax.jdo.JDOException;
import javax.jdo.JDOFatalDataStoreException;
import javax.jdo.JDOFatalUserException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.JDOOptimisticVerificationException;
import javax.jdo.JDOUserException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import org.junit.Before;
import org.junit.Test;
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 03.06.2003
*/
public class JdoTemplateTests {
private PersistenceManagerFactory pmf;
private PersistenceManager pm;
@Before
public void setUp() {
pmf = mock(PersistenceManagerFactory.class);
pm = mock(PersistenceManager.class);
}
@Test
public void testTemplateExecuteWithNotAllowCreate() {
JdoTemplate jt = new JdoTemplate();
jt.setPersistenceManagerFactory(pmf);
jt.setAllowCreate(false);
try {
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return null;
}
});
fail("Should have thrown IllegalStateException");
}
catch (IllegalStateException ex) {
// expected
}
}
@Test
public void testTemplateExecuteWithNotAllowCreateAndThreadBound() {
JdoTemplate jt = new JdoTemplate(pmf);
jt.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
TransactionSynchronizationManager.unbindResource(pmf);
}
@Test
public void testTemplateExecuteWithNewPersistenceManager() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
verify(pm).close();
}
@Test
public void testTemplateExecuteWithThreadBoundAndFlushEager() {
JdoTemplate jt = new JdoTemplate(pmf);
jt.setFlushEager(true);
jt.setAllowCreate(false);
TransactionSynchronizationManager.bindResource(pmf, new PersistenceManagerHolder(pm));
final List l = new ArrayList();
l.add("test");
List result = (List) jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
assertTrue("Correct result list", result == l);
TransactionSynchronizationManager.unbindResource(pmf);
verify(pm).flush();
}
@Test
public void testGetObjectById() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.getObjectById("0", true)).willReturn("A");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("A", jt.getObjectById("0"));
verify(pm).close();
}
@Test
public void testGetObjectByIdWithClassAndValue() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.getObjectById(String.class, "0")).willReturn("A");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("A", jt.getObjectById(String.class, "0"));
verify(pm).close();
}
@Test
public void testEvict() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evict("0");
verify(pm).evict("0");
verify(pm).close();
}
@Test
public void testEvictAllWithCollection() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evictAll(coll);
verify(pm).evictAll(coll);
verify(pm).close();
}
@Test
public void testEvictAll() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.evictAll();
verify(pm).evictAll();
verify(pm).close();
}
@Test
public void testRefresh() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refresh("0");
verify(pm).refresh("0");
verify(pm).close();
}
@Test
public void testRefreshAllWithCollection() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refreshAll(coll);
verify(pm).refreshAll(coll);
verify(pm).close();
}
@Test
public void testRefreshAll() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.refreshAll();
verify(pm).refreshAll();
verify(pm).close();
}
@Test
public void testMakePersistent() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.makePersistent("0");
verify(pm).makePersistent("0");
verify(pm).close();
}
@Test
public void testMakePersistentAll() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.makePersistentAll(coll);
verify(pm).makePersistentAll(coll);
verify(pm).close();
}
@Test
public void testDeletePersistent() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.deletePersistent("0");
verify(pm).deletePersistent("0");
verify(pm).close();
}
@Test
public void testDeletePersistentAll() {
Collection coll = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.deletePersistentAll(coll);
verify(pm).deletePersistentAll(coll);
verify(pm).close();
}
@Test
public void testDetachCopy() {
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.detachCopy("0")).willReturn("0x");
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals("0x", jt.detachCopy("0"));
verify(pm).close();
}
@Test
public void testDetachCopyAll() {
Collection attached = new HashSet();
Collection detached = new HashSet();
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.detachCopyAll(attached)).willReturn(detached);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(detached, jt.detachCopyAll(attached));
verify(pm).close();
}
@Test
public void testFlush() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.flush();
verify(pm).flush();
verify(pm).close();
}
@Test
public void testFlushWithDialect() {
given(pmf.getPersistenceManager()).willReturn(pm);
JdoTemplate jt = new JdoTemplate(pmf);
jt.flush();
verify(pm).flush();
verify(pm).close();
}
@Test
public void testFind() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class)).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class));
verify(pm).close();
}
@Test
public void testFindWithFilter() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b"));
verify(pm).close();
}
@Test
public void testFindWithFilterAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "c asc"));
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithParameterArray() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Object[] values = new Object[0];
Collection coll = new HashSet();
given(query.executeWithArray(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values));
verify(query).declareParameters("params");
verify(pm).close();
}
@Test
public void testFindWithParameterArrayAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Object[] values = new Object[0];
Collection coll = new HashSet();
given(query.executeWithArray(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc"));
verify(query).declareParameters("params");
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithParameterMap() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Map values = new HashMap();
Collection coll = new HashSet();
given(query.executeWithMap(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values));
verify(query).declareParameters("params");
verify(pm).close();
}
@Test
public void testFindWithParameterMapAndOrdering() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(String.class, "a == b")).willReturn(query);
Map values = new HashMap();
Collection coll = new HashSet();
given(query.executeWithMap(values)).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(String.class, "a == b", "params", values, "c asc"));
verify(query).declareParameters("params");
verify(query).setOrdering("c asc");
verify(pm).close();
}
@Test
public void testFindWithLanguageAndQueryObject() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery(Query.SQL, "some SQL")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find(Query.SQL, "some SQL"));
verify(pm).close();
}
@Test
public void testFindWithQueryString() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newQuery("single string query")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.find("single string query"));
verify(pm).close();
}
@Test
public void testFindByNamedQuery() {
Query query = mock(Query.class);
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.newNamedQuery(String.class, "some query name")).willReturn(query);
Collection coll = new HashSet();
given(query.execute()).willReturn(coll);
JdoTemplate jt = new JdoTemplate(pmf);
assertEquals(coll, jt.findByNamedQuery(String.class, "some query name"));
verify(pm).close();
}
@Test
public void testTemplateExceptions() {
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOObjectNotFoundException();
}
});
fail("Should have thrown JdoObjectRetrievalFailureException");
}
catch (JdoObjectRetrievalFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOOptimisticVerificationException();
}
});
fail("Should have thrown JdoOptimisticLockingFailureException");
}
catch (JdoOptimisticLockingFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDODataStoreException();
}
});
fail("Should have thrown JdoResourceFailureException");
}
catch (JdoResourceFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOFatalDataStoreException();
}
});
fail("Should have thrown JdoResourceFailureException");
}
catch (JdoResourceFailureException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOUserException();
}
});
fail("Should have thrown JdoUsageException");
}
catch (JdoUsageException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOFatalUserException();
}
});
fail("Should have thrown JdoUsageException");
}
catch (JdoUsageException ex) {
// expected
}
try {
createTemplate().execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new JDOException();
}
});
fail("Should have thrown JdoSystemException");
}
catch (JdoSystemException ex) {
// expected
}
}
@Test
public void testTranslateException() {
JdoDialect dialect = mock(JdoDialect.class);
final JDOException ex = new JDOException();
given(dialect.translateException(ex)).willReturn(new DataIntegrityViolationException("test", ex));
try {
JdoTemplate template = createTemplate();
template.setJdoDialect(dialect);
template.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw ex;
}
});
fail("Should have thrown DataIntegrityViolationException");
}
catch (DataIntegrityViolationException dive) {
// expected
}
}
private JdoTemplate createTemplate() {
given(pmf.getPersistenceManager()).willReturn(pm);
return new JdoTemplate(pmf);
}
}

View File

@@ -16,19 +16,16 @@
package org.springframework.orm.jdo;
import java.lang.reflect.Proxy;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.Constants;
import javax.jdo.JDOFatalDataStoreException;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.Query;
import javax.jdo.Transaction;
import javax.sql.DataSource;
import javax.transaction.Status;
@@ -38,12 +35,12 @@ import javax.transaction.UserTransaction;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.jdbc.datasource.ConnectionHandle;
import org.springframework.jdbc.datasource.ConnectionHolder;
import org.springframework.jdbc.datasource.SimpleConnectionHandle;
import org.springframework.orm.jdo.support.SpringPersistenceManagerProxyBean;
import org.springframework.orm.jdo.support.StandardPersistenceManagerProxyBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.tests.transaction.MockJtaTransaction;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
@@ -124,14 +121,8 @@ public class JdoTransactionManagerTests {
PersistenceManager stdPmProxy = stdProxyBean.getObject();
stdPmProxy.flush();
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -161,13 +152,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
fail("Should have thrown RuntimeException");
@@ -199,13 +185,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
fail("Should have thrown RuntimeException");
@@ -235,14 +216,7 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
status.setRollbackOnly();
return null;
}
@@ -274,14 +248,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@@ -308,13 +276,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
throw new RuntimeException("application exception");
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
throw new RuntimeException("application exception");
}
});
}
@@ -350,14 +313,7 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
status.setRollbackOnly();
return null;
}
@@ -393,14 +349,8 @@ public class JdoTransactionManagerTests {
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@@ -430,25 +380,13 @@ public class JdoTransactionManagerTests {
Object result = tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@@ -483,25 +421,10 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("JTA synchronizations active", TransactionSynchronizationManager.isSynchronizationActive());
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
pm2.flush();
return l;
}
});
Object result = jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
return result;
return l;
}
});
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
@@ -545,25 +468,13 @@ public class JdoTransactionManagerTests {
catch (Exception ex) {
}
JdoTemplate jt = new JdoTemplate(pmf);
jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
return null;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
}
@@ -595,13 +506,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Is not new transaction", !status.isNewTransaction());
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -644,13 +550,8 @@ public class JdoTransactionManagerTests {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -689,13 +590,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -737,13 +633,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Has thread con", TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm) {
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true);
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -783,16 +674,8 @@ public class JdoTransactionManagerTests {
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
assertTrue("Hasn't thread con", !TransactionSynchronizationManager.hasResource(ds));
JdoTemplate jt = new JdoTemplate();
jt.setPersistenceManagerFactory(pmf);
jt.setJdoDialect(dialect);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -866,14 +749,8 @@ public class JdoTransactionManagerTests {
}
});
}
JdoTemplate jt = new JdoTemplate(pmf);
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
pm2.flush();
return l;
}
});
PersistenceManagerFactoryUtils.getPersistenceManager(pmf, true).flush();
return l;
}
});
assertTrue("Correct result list", result == l);
@@ -890,72 +767,6 @@ public class JdoTransactionManagerTests {
verify(tx).commit();
}
@Test
public void testTransactionTimeoutWithJdoDialect() throws SQLException {
doTestTransactionTimeoutWithJdoDialect(true);
}
@Test
public void testTransactionTimeoutWithJdoDialectAndPmProxy() throws SQLException {
doTestTransactionTimeoutWithJdoDialect(false);
}
private void doTestTransactionTimeoutWithJdoDialect(final boolean exposeNativePm) throws SQLException {
Query query = mock(Query.class);
final JdoDialect dialect = mock(JdoDialect.class);
TransactionTemplate tt = new TransactionTemplate();
given(pmf.getPersistenceManager()).willReturn(pm);
given(pm.currentTransaction()).willReturn(tx);
if (!exposeNativePm) {
dialect.applyQueryTimeout(query, 10);
}
given(pm.newQuery(TestBean.class)).willReturn(query);
JdoTransactionManager tm = new JdoTransactionManager(pmf);
tm.setJdoDialect(dialect);
tt.setTransactionManager(tm);
tt.setTimeout(10);
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
tt.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus status) {
assertTrue("Has thread pm", TransactionSynchronizationManager.hasResource(pmf));
JdoTemplate jt = new JdoTemplate(pmf);
jt.setJdoDialect(dialect);
if (exposeNativePm) {
jt.setExposeNativePersistenceManager(true);
}
return jt.execute(new JdoCallback() {
@Override
public Object doInJdo(PersistenceManager pm2) {
if (exposeNativePm) {
assertSame(pm, pm2);
}
else {
assertTrue(Proxy.isProxyClass(pm2.getClass()));
}
pm2.newQuery(TestBean.class);
return null;
}
});
}
});
assertTrue("Hasn't thread pm", !TransactionSynchronizationManager.hasResource(pmf));
assertTrue("JTA synchronizations not active", !TransactionSynchronizationManager.isSynchronizationActive());
verify(dialect).beginTransaction(tx, tt);
verify(dialect).cleanupTransaction(null);
verify(pm).close();
verify(tx).getRollbackOnly();
verify(tx).commit();
}
@Test
public void testTransactionFlush() {
given(pmf.getPersistenceManager()).willReturn(pm);

View File

@@ -1,71 +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.jdo.support;
import java.util.ArrayList;
import java.util.List;
import javax.jdo.PersistenceManagerFactory;
import org.junit.Test;
import org.springframework.orm.jdo.JdoTemplate;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.*;
/**
* @author Juergen Hoeller
* @author Phillip Webb
* @since 30.07.2003
*/
public class JdoDaoSupportTests {
@Test
public void testJdoDaoSupportWithPersistenceManagerFactory() throws Exception {
PersistenceManagerFactory pmf = mock(PersistenceManagerFactory.class);
pmf.getConnectionFactory();
final List test = new ArrayList();
JdoDaoSupport dao = new JdoDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setPersistenceManagerFactory(pmf);
dao.afterPropertiesSet();
assertEquals("Correct PersistenceManagerFactory", pmf, dao.getPersistenceManagerFactory());
assertEquals("Correct JdoTemplate", pmf, dao.getJdoTemplate().getPersistenceManagerFactory());
assertEquals("initDao called", test.size(), 1);
}
@Test
public void testJdoDaoSupportWithJdoTemplate() throws Exception {
JdoTemplate template = new JdoTemplate();
final List test = new ArrayList();
JdoDaoSupport dao = new JdoDaoSupport() {
@Override
protected void initDao() {
test.add("test");
}
};
dao.setJdoTemplate(template);
dao.afterPropertiesSet();
assertEquals("Correct JdoTemplate", template, dao.getJdoTemplate());
assertEquals("initDao called", test.size(), 1);
}
}