alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Spring Framework example source code file (TopLinkTemplateTests.java)

This example Spring Framework source code file (TopLinkTemplateTests.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Spring Framework tags/keywords

mockcontrol, mockcontrol, object, object, session, session, sessionfactory, sessionholder, singlesessionfactory, testcase, toplinkcallback, toplinkexception, toplinktemplate, toplinktemplate

The Spring Framework TopLinkTemplateTests.java source code

/*
 * 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
 * @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);
	}
}

Other Spring Framework examples (source code examples)

Here is a short list of links related to this Spring Framework TopLinkTemplateTests.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.