1 package org
.argeo
.jcr
.unit
;
4 import java
.security
.AccessController
;
5 import java
.security
.PrivilegedAction
;
7 import javax
.jcr
.Repository
;
8 import javax
.jcr
.Session
;
9 import javax
.jcr
.SimpleCredentials
;
10 import javax
.security
.auth
.Subject
;
11 import javax
.security
.auth
.login
.LoginContext
;
12 import javax
.security
.auth
.login
.LoginException
;
14 import org
.apache
.commons
.io
.FileUtils
;
15 import org
.apache
.commons
.logging
.Log
;
16 import org
.apache
.commons
.logging
.LogFactory
;
17 import org
.argeo
.jcr
.ArgeoJcrException
;
19 import junit
.framework
.TestCase
;
21 public abstract class AbstractJcrTestCase
extends TestCase
{
22 private final static Log log
= LogFactory
.getLog(AbstractJcrTestCase
.class);
24 private Repository repository
;
25 private Session session
= null;
27 public final static String LOGIN_CONTEXT_TEST_SYSTEM
= "TEST_JACKRABBIT_ADMIN";
29 // protected abstract File getRepositoryFile() throws Exception;
31 protected abstract Repository
createRepository() throws Exception
;
33 protected abstract void clearRepository(Repository repository
)
37 protected void setUp() throws Exception
{
38 File homeDir
= getHomeDir();
39 FileUtils
.deleteDirectory(homeDir
);
40 repository
= createRepository();
44 protected void tearDown() throws Exception
{
45 if (session
!= null) {
47 if (log
.isTraceEnabled())
48 log
.trace("Logout session");
50 clearRepository(repository
);
53 protected Session
session() {
54 if (session
!= null && session
.isLive())
57 if (getLoginContext() != null) {
60 lc
= new LoginContext(getLoginContext());
62 } catch (LoginException e
) {
63 throw new ArgeoJcrException("JAAS login failed", e
);
65 session
= Subject
.doAs(lc
.getSubject(),
66 new PrivilegedAction
<Session
>() {
69 public Session
run() {
76 this.session
= session
;
80 protected String
getLoginContext() {
84 protected Session
login() {
86 if (log
.isTraceEnabled())
87 log
.trace("Login session");
88 Subject subject
= Subject
.getSubject(AccessController
.getContext());
90 return getRepository().login();
92 return getRepository().login(
93 new SimpleCredentials("demo", "demo".toCharArray()));
94 } catch (Exception e
) {
95 throw new ArgeoJcrException("Cannot login to repository", e
);
99 protected Repository
getRepository() {
104 * enables children class to set an existing repository in case it is not
105 * deleted on startup, to test migration by instance
107 public void setRepository(Repository repository
) {
108 this.repository
= repository
;
111 protected File
getHomeDir() {
112 File homeDir
= new File(System
.getProperty("java.io.tmpdir"),
113 AbstractJcrTestCase
.class.getSimpleName() + "-"
114 + System
.getProperty("user.name"));