1 5 6 package org.springframework.orm.toplink; 7 8 import junit.framework.TestCase; 9 import oracle.toplink.sessions.Session; 10 import org.easymock.MockControl; 11 12 import org.springframework.transaction.support.TransactionSynchronizationManager; 13 14 19 public class SessionFactoryUtilsTests extends TestCase { 20 21 26 public void testNoSessionBound() { 27 MockControl sessionControl = MockControl.createControl(Session.class); 28 Session session = (Session) sessionControl.getMock(); 29 30 SessionFactory factory = new SingleSessionFactory(session); 31 32 session.hasExternalTransactionController(); 33 sessionControl.setReturnValue(false, 1); 34 35 sessionControl.replay(); 36 try { 37 Session boundSession = SessionFactoryUtils.getSession(factory, false); 38 fail(); 39 } 40 catch (Throwable t) { 41 assertTrue(t.getClass().equals(IllegalStateException .class)); 42 } 43 44 Session boundSession = SessionFactoryUtils.getSession(factory, true); 45 assertTrue(session == boundSession); 46 assertFalse(TransactionSynchronizationManager.hasResource(factory)); 47 assertFalse(TransactionSynchronizationManager.isSynchronizationActive()); 48 } 49 50 55 public void testNoSessionBoundAllowAndInit() { 56 MockControl sessionControl = MockControl.createControl(Session.class); 57 Session session = (Session) sessionControl.getMock(); 58 59 SessionFactory factory = new SingleSessionFactory(session); 60 61 session.hasExternalTransactionController(); 62 sessionControl.setReturnValue(false, 1); 63 64 sessionControl.replay(); 65 66 Session boundSession = SessionFactoryUtils.getSession(factory, true); 67 assertTrue(session == boundSession); 68 69 SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory); 70 assertTrue(holder == null); 71 72 TransactionSynchronizationManager.initSynchronization(); 73 74 boundSession = SessionFactoryUtils.getSession(factory, true); 75 assertTrue(session == boundSession); 76 assertTrue(TransactionSynchronizationManager.getSynchronizations().size() == 1); 77 assertTrue(TransactionSynchronizationManager.hasResource(factory)); 78 assertTrue(session == ((SessionHolder) TransactionSynchronizationManager.getResource(factory)).getSession()); 79 80 TransactionSynchronizationManager.clearSynchronization(); 81 TransactionSynchronizationManager.unbindResource(factory); 82 } 83 84 public void testNoSessionBoundAllowAndNoInit() { 85 MockControl sessionControl = MockControl.createControl(Session.class); 86 Session session = (Session) sessionControl.getMock(); 87 88 SessionFactory factory = new SingleSessionFactory(session); 89 90 session.hasExternalTransactionController(); 91 sessionControl.setReturnValue(false, 2); 92 93 sessionControl.replay(); 94 95 Session boundSession = SessionFactoryUtils.getSession(factory, true); 96 assertTrue(session == boundSession); 97 98 SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(factory); 99 assertTrue(holder == null); 100 101 boundSession = SessionFactoryUtils.getSession(factory, true); 102 assertTrue(session == boundSession); 103 assertFalse(TransactionSynchronizationManager.hasResource(factory)); 104 } 105 106 } 107 | Popular Tags |