toplinktemplatetests.java

来自「spring,z几 塞积极 决撒 积极上经济歼击机就 将计就计经济年毫毫毫毫毫毫」· Java 代码 · 共 118 行

JAVA
118
字号
/*
 * Created on Mar 20, 2005
 *
 */

package org.springframework.orm.toplink;

import junit.framework.TestCase;
import oracle.toplink.exceptions.TopLinkException;
import oracle.toplink.sessions.Session;
import org.easymock.MockControl;

import org.springframework.transaction.support.TransactionSynchronizationManager;

/**
 * @author Juergen Hoeller
 * @author <a href="mailto:james.x.clark@oracle.com">James Clark</a>
 * @since 28.04.2005
 */
public class TopLinkTemplateTests extends TestCase {

	public void testTemplateNotAllowingCreate() {
		MockControl sessionControl = MockControl.createControl(Session.class);
		Session session = (Session) sessionControl.getMock();

		SessionFactory factory = new SingleSessionFactory(session);

		TopLinkTemplate template = new TopLinkTemplate();
		template.setAllowCreate(false);
		template.setSessionFactory(factory);
		try {
			template.execute(new TopLinkCallback() {
				public Object doInTopLink(Session session) throws TopLinkException {
					return null;
				}
			});
			fail();
		}
		catch (Exception e) {
		}
	}

	public void testTemplateWithCreate() {
		MockControl sessionControl = MockControl.createControl(Session.class);
		Session session = (Session) sessionControl.getMock();

		SessionFactory factory = new SingleSessionFactory(session);

		session.release();
		sessionControl.setVoidCallable(1);

		sessionControl.replay();

		TopLinkTemplate template = new TopLinkTemplate();
		template.setAllowCreate(true);
		template.setSessionFactory(factory);
		template.execute(new TopLinkCallback() {
			public Object doInTopLink(Session session) throws TopLinkException {
				assertTrue(session != null);
				return null;
			}
		});
		assertFalse(TransactionSynchronizationManager.hasResource(factory));

		sessionControl.verify();
	}

	public void testTemplateWithExistingSessionAndNoCreate() {
		MockControl sessionControl = MockControl.createControl(Session.class);
		Session session = (Session) sessionControl.getMock();

		SessionFactory factory = new SingleSessionFactory(session);

		sessionControl.replay();

		SessionHolder sessionHolder = new SessionHolder(factory.createSession());
		TransactionSynchronizationManager.bindResource(factory, sessionHolder);

		TopLinkTemplate template = new TopLinkTemplate();
		template.setAllowCreate(false);
		template.setSessionFactory(factory);
		template.execute(new TopLinkCallback() {
			public Object doInTopLink(Session session) throws TopLinkException {
				assertTrue(session != null);
				return null;
			}
		});
		assertTrue(TransactionSynchronizationManager.hasResource(factory));
		sessionControl.verify();
		TransactionSynchronizationManager.unbindResource(factory);
	}

	public void testTemplateWithExistingSessionAndCreateAllowed() {
		MockControl sessionControl = MockControl.createControl(Session.class);
		Session session = (Session) sessionControl.getMock();

		SessionFactory factory = new SingleSessionFactory(session);

		sessionControl.replay();

		SessionHolder sessionHolder = new SessionHolder(factory.createSession());
		TransactionSynchronizationManager.bindResource(factory, sessionHolder);

		TopLinkTemplate template = new TopLinkTemplate();
		template.setAllowCreate(true);
		template.setSessionFactory(factory);
		template.execute(new TopLinkCallback() {
			public Object doInTopLink(Session session) throws TopLinkException {
				assertTrue(session != null);
				return null;
			}
		});
		assertTrue(TransactionSynchronizationManager.hasResource(factory));
		sessionControl.verify();
		TransactionSynchronizationManager.unbindResource(factory);
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?