You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jackrabbit.apache.org by Christoph Kiehl <ch...@sulu3000.de> on 2007/07/19 22:52:48 UTC

Re: Junit testing with Jackrabbit using AbstractDependencyInjectionSpringContextTests

bilobag wrote:

> Ok, i think my issue is that I have the
> org.springmodules.jcr.support.OpenSessionInViewFilter configured in my
> web.xml which is supposed to keep my repository session open.  My dao mainly
> just uses the jcrTemplate methods to access the repository.  I believe the
> template opens and closes the session.  Does anyone know of a way to keep it
> open during my entire test case?   

I just copied some code from OpenSessionInViewFilter to put together a quick and 
dirty solution. I'm using transactions and therefore have a threadbound session 
available anyway. There might be a cleaner solution but this one should work for 
now.

Cheers,
Christoph

ps: Just post such questions to the user list next time. Most developers read 
both lists anyway.

=======

package com.subshell.sophora.server.persistence.jcr;

import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.jackrabbit.JcrConstants;
import org.springframework.test.AbstractDependencyInjectionSpringContextTests;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springmodules.jcr.JcrTemplate;
import org.springmodules.jcr.SessionFactory;
import org.springmodules.jcr.SessionFactoryUtils;

public class NodeServiceTests extends
		AbstractDependencyInjectionSpringContextTests {

	protected static final Log log = LogFactory.getLog(NodeServiceTests.class);

	protected JcrTemplate jcrTemplate;

	private SessionFactory sf;

	private Session session;

	public JcrTemplate getJcrTemplate() {
		return jcrTemplate;
	}

	public void setJcrTemplate(JcrTemplate jcrTemplate) {
		this.jcrTemplate = jcrTemplate;
	}

	public NodeServiceTests() {
		super();
		setAutowireMode(AUTOWIRE_BY_NAME);
	}

	@Override
	protected String[] getConfigLocations() {
		return new String[] { "classpath:WEB-INF/applicationContext.xml" };
	}

	@Override
	protected void onSetUp() throws Exception {
		super.onSetUp();
		sf = jcrTemplate.getSessionFactory();
		session = SessionFactoryUtils.getSession(sf, true);
		TransactionSynchronizationManager.bindResource(sf,
				sf.getSessionHolder(session));
	}

	@Override
	protected void onTearDown() throws Exception {
		TransactionSynchronizationManager.unbindResource(sf);
		SessionFactoryUtils.releaseSession(session, sf);
		super.onTearDown();
	}

	public void testGetAllFiles() throws RepositoryException {
		Node addNode = jcrTemplate.getRootNode().addNode("test");
		addNode.addMixin(JcrConstants.MIX_REFERENCEABLE);
		String uuid = addNode.getUUID();

		Node nodeByUUID = jcrTemplate.getNodeByUUID(uuid);
		assertEquals(JcrConstants.MIX_REFERENCEABLE,
				nodeByUUID.getMixinNodeTypes()[0].getName());
	}
}



Re: Junit testing with Jackrabbit using AbstractDependencyInjectionSpringContextTests

Posted by bilobag <bi...@hotmail.com>.
Thanks a lot for the help Christoph!  Your code worked.  This will really get
my unit tests going.  Thanks again.



Christoph Kiehl-3 wrote:
> 
> bilobag wrote:
> 
>> Ok, i think my issue is that I have the
>> org.springmodules.jcr.support.OpenSessionInViewFilter configured in my
>> web.xml which is supposed to keep my repository session open.  My dao
>> mainly
>> just uses the jcrTemplate methods to access the repository.  I believe
>> the
>> template opens and closes the session.  Does anyone know of a way to keep
>> it
>> open during my entire test case?   
> 
> I just copied some code from OpenSessionInViewFilter to put together a
> quick and 
> dirty solution. I'm using transactions and therefore have a threadbound
> session 
> available anyway. There might be a cleaner solution but this one should
> work for 
> now.
> 
> Cheers,
> Christoph
> 
> ps: Just post such questions to the user list next time. Most developers
> read 
> both lists anyway.
> 
> =======
> 
> package com.subshell.sophora.server.persistence.jcr;
> 
> import javax.jcr.Node;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> 
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.apache.jackrabbit.JcrConstants;
> import
> org.springframework.test.AbstractDependencyInjectionSpringContextTests;
> import
> org.springframework.transaction.support.TransactionSynchronizationManager;
> import org.springmodules.jcr.JcrTemplate;
> import org.springmodules.jcr.SessionFactory;
> import org.springmodules.jcr.SessionFactoryUtils;
> 
> public class NodeServiceTests extends
> 		AbstractDependencyInjectionSpringContextTests {
> 
> 	protected static final Log log =
> LogFactory.getLog(NodeServiceTests.class);
> 
> 	protected JcrTemplate jcrTemplate;
> 
> 	private SessionFactory sf;
> 
> 	private Session session;
> 
> 	public JcrTemplate getJcrTemplate() {
> 		return jcrTemplate;
> 	}
> 
> 	public void setJcrTemplate(JcrTemplate jcrTemplate) {
> 		this.jcrTemplate = jcrTemplate;
> 	}
> 
> 	public NodeServiceTests() {
> 		super();
> 		setAutowireMode(AUTOWIRE_BY_NAME);
> 	}
> 
> 	@Override
> 	protected String[] getConfigLocations() {
> 		return new String[] { "classpath:WEB-INF/applicationContext.xml" };
> 	}
> 
> 	@Override
> 	protected void onSetUp() throws Exception {
> 		super.onSetUp();
> 		sf = jcrTemplate.getSessionFactory();
> 		session = SessionFactoryUtils.getSession(sf, true);
> 		TransactionSynchronizationManager.bindResource(sf,
> 				sf.getSessionHolder(session));
> 	}
> 
> 	@Override
> 	protected void onTearDown() throws Exception {
> 		TransactionSynchronizationManager.unbindResource(sf);
> 		SessionFactoryUtils.releaseSession(session, sf);
> 		super.onTearDown();
> 	}
> 
> 	public void testGetAllFiles() throws RepositoryException {
> 		Node addNode = jcrTemplate.getRootNode().addNode("test");
> 		addNode.addMixin(JcrConstants.MIX_REFERENCEABLE);
> 		String uuid = addNode.getUUID();
> 
> 		Node nodeByUUID = jcrTemplate.getNodeByUUID(uuid);
> 		assertEquals(JcrConstants.MIX_REFERENCEABLE,
> 				nodeByUUID.getMixinNodeTypes()[0].getName());
> 	}
> }
> 
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Junit-testing-with-Jackrabbit-using-AbstractDependencyInjectionSpringContextTests-tf4111862.html#a11708893
Sent from the Jackrabbit - Dev mailing list archive at Nabble.com.