2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
.unit
;
19 import java
.security
.AccessController
;
20 import java
.security
.PrivilegedAction
;
22 import javax
.jcr
.Repository
;
23 import javax
.jcr
.Session
;
24 import javax
.jcr
.SimpleCredentials
;
25 import javax
.security
.auth
.Subject
;
26 import javax
.security
.auth
.login
.LoginContext
;
27 import javax
.security
.auth
.login
.LoginException
;
29 import junit
.framework
.TestCase
;
31 import org
.apache
.commons
.io
.FileUtils
;
32 import org
.apache
.commons
.logging
.Log
;
33 import org
.apache
.commons
.logging
.LogFactory
;
34 import org
.argeo
.ArgeoException
;
36 public abstract class AbstractJcrTestCase
extends TestCase
{
37 private final static Log log
= LogFactory
.getLog(AbstractJcrTestCase
.class);
39 private Repository repository
;
40 private Session session
= null;
42 public final static String LOGIN_CONTEXT_TEST_SYSTEM
= "TEST_SYSTEM";
44 // protected abstract File getRepositoryFile() throws Exception;
46 protected abstract Repository
createRepository() throws Exception
;
48 protected abstract void clearRepository(Repository repository
)
52 protected void setUp() throws Exception
{
53 File homeDir
= getHomeDir();
54 FileUtils
.deleteDirectory(homeDir
);
55 repository
= createRepository();
59 protected void tearDown() throws Exception
{
60 if (session
!= null) {
62 if (log
.isTraceEnabled())
63 log
.trace("Logout session");
65 clearRepository(repository
);
68 protected Session
session() {
69 if (session
!= null && session
.isLive())
72 if (getLoginContext() != null) {
75 lc
= new LoginContext(getLoginContext());
77 } catch (LoginException e
) {
78 throw new ArgeoException("JAAS login failed", e
);
80 session
= Subject
.doAs(lc
.getSubject(),
81 new PrivilegedAction
<Session
>() {
84 public Session
run() {
91 this.session
= session
;
95 protected String
getLoginContext() {
99 protected Session
login() {
101 if (log
.isTraceEnabled())
102 log
.trace("Login session");
103 Subject subject
= Subject
.getSubject(AccessController
.getContext());
105 return getRepository().login();
107 return getRepository().login(
108 new SimpleCredentials("demo", "demo".toCharArray()));
109 } catch (Exception e
) {
110 throw new ArgeoException("Cannot login to repository", e
);
114 protected Repository
getRepository() {
119 * enables children class to set an existing repository in case it is not
120 * deleted on startup, to test migration by instance
122 public void setRepository(Repository repository
) {
123 this.repository
= repository
;
126 // public void logout() {
127 // if (session != null && session.isLive())
128 // JcrUtils.logoutQuietly(session);
131 // protected static TestSuite defaultTestSuite(Class<? extends TestCase>
133 // String testSuiteClassName =
134 // "org.argeo.jackrabbit.unit.JackrabbitTestSuite";
136 // Class<?> testSuiteClass = AbstractJcrTestCase.class
137 // .getClassLoader().loadClass(testSuiteClassName);
138 // if (clss == null) {
139 // return (TestSuite) testSuiteClass.newInstance();
141 // return (TestSuite) testSuiteClass.getConstructor(Class.class)
142 // .newInstance(clss);
144 // } catch (Exception e) {
145 // throw new ArgeoException("Cannot find default test suite "
146 // + testSuiteClassName, e);
150 protected File
getHomeDir() {
151 File homeDir
= new File(System
.getProperty("java.io.tmpdir"),
152 AbstractJcrTestCase
.class.getSimpleName() + "-"
153 + System
.getProperty("user.name"));