You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-dev@jackrabbit.apache.org by Galo Gimenez <ga...@gmail.com> on 2013/02/28 15:01:30 UTC
Jcr Session and Session.logout()
This is a test that is failing getting the session twice,
testAdminSessionTwice() fails, the first testAdminSession() works. The
problem can be solved by not logging out of the session (recycling the jcr
session on all tests), but I am not sure this is how the Session must be
handled.
8<----------------------------------------------------
package org.apache.jackrabbit.oak.jcr;
import com.mongodb.DB;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.mk.api.MicroKernel;
import org.apache.jackrabbit.mk.blobs.BlobStore;
import org.apache.jackrabbit.mongomk.impl.MongoConnection;
import org.apache.jackrabbit.mongomk.impl.MongoMicroKernel;
import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
import org.apache.jackrabbit.mongomk.impl.blob.MongoGridFSBlobStore;
import org.junit.After;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
public class JcrTest {
static Repository repository = null;
static MongoNodeStore nodeStore = null;
static BlobStore blobStore = null;
static MicroKernel mk;
static Jcr jcr;
@BeforeClass
public static void setup() {
MongoConnection mongoConnection = MongoUtils.getConnection();
DB db = mongoConnection.getDB();
nodeStore = new MongoNodeStore(db);
blobStore = new MongoGridFSBlobStore(db);
mk = new MongoMicroKernel(mongoConnection, nodeStore, blobStore);
jcr = new Jcr(mk);
repository = jcr.createRepository();
Session adminSession = null;
Session userSession = null;
try {
adminSession = repository.login(new SimpleCredentials("admin",
"admin".toCharArray()));
UserManager umgr = ((JackrabbitSession)
adminSession).getUserManager();
Assert.assertNotNull(umgr);
Authorizable au = umgr.getAuthorizable("author1");
if (au == null) {
// Create user
User usr = umgr.createUser("author1", "");
Assert.assertNotNull(usr);
adminSession.save();
//Add user to a Group
Authorizable auGroup = umgr.getAuthorizable("authors");
Assert.assertNull(auGroup);
if (auGroup == null) {
umgr.createGroup("authors");
auGroup = umgr.getAuthorizable("authors");
}
Group group = (Group) auGroup;
group.addMember(usr);
adminSession.save();
}
// Impersonate
userSession = adminSession.impersonate(new
SimpleCredentials("author1", "".toCharArray()));
Assert.assertNotNull(userSession);
userSession.save();
} catch (RepositoryException e) {
e.printStackTrace();
Assert.fail();
} finally {
if (adminSession != null) adminSession.logout();
if (userSession != null) adminSession.logout();
}
}
@Test
public void testAdminSession() {
Session adminSession = null;
try {
adminSession = repository.login(new SimpleCredentials("admin",
"admin".toCharArray()));
Assert.assertNotNull(adminSession);
Assert.assertTrue(adminSession.isLive());
} catch (RepositoryException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (adminSession != null) adminSession.logout();
}
}
@Test
public void testAdminSessionTwice() {
Session adminSession = null;
try {
adminSession = repository.login(new SimpleCredentials("admin",
"admin".toCharArray()));
Assert.assertNotNull(adminSession);
Assert.assertTrue(adminSession.isLive());
} catch (RepositoryException e) {
e.printStackTrace();
Assert.fail(e.getMessage());
} finally {
if (adminSession != null) adminSession.logout();
}
}
@After()
public void tearDown() {
MongoConnection conn = MongoUtils.getConnection();
MongoUtils.dropCollections(conn.getDB());
}
}
--
-- Galo
Re: Jcr Session and Session.logout()
Posted by Michael Dürig <md...@apache.org>.
Hi,
Your test setup shouldn't use static fields. This causes the repo to be
gone once the second test is executed. Make setup() non static, use
@Before instead of @BeforeClass and make the static class fields into
instance fields.
Michael
On 28.2.13 14:01, Galo Gimenez wrote:
> This is a test that is failing getting the session twice,
> testAdminSessionTwice() fails, the first testAdminSession() works. The
> problem can be solved by not logging out of the session (recycling the jcr
> session on all tests), but I am not sure this is how the Session must be
> handled.
>
> 8<----------------------------------------------------
>
> package org.apache.jackrabbit.oak.jcr;
>
> import com.mongodb.DB;
> import org.apache.jackrabbit.api.JackrabbitSession;
> import org.apache.jackrabbit.api.security.user.Authorizable;
> import org.apache.jackrabbit.api.security.user.Group;
> import org.apache.jackrabbit.api.security.user.User;
> import org.apache.jackrabbit.api.security.user.UserManager;
> import org.apache.jackrabbit.mk.api.MicroKernel;
> import org.apache.jackrabbit.mk.blobs.BlobStore;
> import org.apache.jackrabbit.mongomk.impl.MongoConnection;
> import org.apache.jackrabbit.mongomk.impl.MongoMicroKernel;
> import org.apache.jackrabbit.mongomk.impl.MongoNodeStore;
> import org.apache.jackrabbit.mongomk.impl.blob.MongoGridFSBlobStore;
> import org.junit.After;
> import org.junit.Assert;
> import org.junit.BeforeClass;
> import org.junit.Test;
>
> import javax.jcr.Repository;
> import javax.jcr.RepositoryException;
> import javax.jcr.Session;
> import javax.jcr.SimpleCredentials;
>
>
> public class JcrTest {
> static Repository repository = null;
> static MongoNodeStore nodeStore = null;
> static BlobStore blobStore = null;
> static MicroKernel mk;
> static Jcr jcr;
>
> @BeforeClass
> public static void setup() {
>
> MongoConnection mongoConnection = MongoUtils.getConnection();
> DB db = mongoConnection.getDB();
> nodeStore = new MongoNodeStore(db);
> blobStore = new MongoGridFSBlobStore(db);
>
> mk = new MongoMicroKernel(mongoConnection, nodeStore, blobStore);
> jcr = new Jcr(mk);
> repository = jcr.createRepository();
>
> Session adminSession = null;
> Session userSession = null;
> try {
> adminSession = repository.login(new SimpleCredentials("admin",
> "admin".toCharArray()));
>
> UserManager umgr = ((JackrabbitSession)
> adminSession).getUserManager();
> Assert.assertNotNull(umgr);
> Authorizable au = umgr.getAuthorizable("author1");
> if (au == null) {
> // Create user
> User usr = umgr.createUser("author1", "");
> Assert.assertNotNull(usr);
> adminSession.save();
>
> //Add user to a Group
> Authorizable auGroup = umgr.getAuthorizable("authors");
> Assert.assertNull(auGroup);
> if (auGroup == null) {
> umgr.createGroup("authors");
> auGroup = umgr.getAuthorizable("authors");
> }
> Group group = (Group) auGroup;
> group.addMember(usr);
> adminSession.save();
> }
>
> // Impersonate
> userSession = adminSession.impersonate(new
> SimpleCredentials("author1", "".toCharArray()));
> Assert.assertNotNull(userSession);
> userSession.save();
> } catch (RepositoryException e) {
> e.printStackTrace();
> Assert.fail();
> } finally {
> if (adminSession != null) adminSession.logout();
> if (userSession != null) adminSession.logout();
> }
>
> }
>
> @Test
> public void testAdminSession() {
> Session adminSession = null;
> try {
> adminSession = repository.login(new SimpleCredentials("admin",
> "admin".toCharArray()));
> Assert.assertNotNull(adminSession);
> Assert.assertTrue(adminSession.isLive());
>
>
> } catch (RepositoryException e) {
> e.printStackTrace();
> Assert.fail(e.getMessage());
> } finally {
> if (adminSession != null) adminSession.logout();
> }
> }
>
> @Test
> public void testAdminSessionTwice() {
> Session adminSession = null;
> try {
> adminSession = repository.login(new SimpleCredentials("admin",
> "admin".toCharArray()));
> Assert.assertNotNull(adminSession);
> Assert.assertTrue(adminSession.isLive());
>
>
> } catch (RepositoryException e) {
> e.printStackTrace();
> Assert.fail(e.getMessage());
> } finally {
> if (adminSession != null) adminSession.logout();
> }
> }
>
> @After()
> public void tearDown() {
> MongoConnection conn = MongoUtils.getConnection();
> MongoUtils.dropCollections(conn.getDB());
> }
> }
>
>