]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.jcr/src/org/argeo/jcr/unit/AbstractJcrTestCase.java
[maven-release-plugin] prepare for next development iteration
[lgpl/argeo-commons.git] / org.argeo.jcr / src / org / argeo / jcr / unit / AbstractJcrTestCase.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.jcr.unit;
17
18 import java.io.File;
19 import java.security.AccessController;
20 import java.security.PrivilegedAction;
21
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;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.argeo.jcr.ArgeoJcrException;
33
34 import junit.framework.TestCase;
35
36 public abstract class AbstractJcrTestCase extends TestCase {
37 private final static Log log = LogFactory.getLog(AbstractJcrTestCase.class);
38
39 private Repository repository;
40 private Session session = null;
41
42 public final static String LOGIN_CONTEXT_TEST_SYSTEM = "TEST_JACKRABBIT_ADMIN";
43
44 // protected abstract File getRepositoryFile() throws Exception;
45
46 protected abstract Repository createRepository() throws Exception;
47
48 protected abstract void clearRepository(Repository repository)
49 throws Exception;
50
51 @Override
52 protected void setUp() throws Exception {
53 File homeDir = getHomeDir();
54 FileUtils.deleteDirectory(homeDir);
55 repository = createRepository();
56 }
57
58 @Override
59 protected void tearDown() throws Exception {
60 if (session != null) {
61 session.logout();
62 if (log.isTraceEnabled())
63 log.trace("Logout session");
64 }
65 clearRepository(repository);
66 }
67
68 protected Session session() {
69 if (session != null && session.isLive())
70 return session;
71 Session session;
72 if (getLoginContext() != null) {
73 LoginContext lc;
74 try {
75 lc = new LoginContext(getLoginContext());
76 lc.login();
77 } catch (LoginException e) {
78 throw new ArgeoJcrException("JAAS login failed", e);
79 }
80 session = Subject.doAs(lc.getSubject(),
81 new PrivilegedAction<Session>() {
82
83 @Override
84 public Session run() {
85 return login();
86 }
87
88 });
89 } else
90 session = login();
91 this.session = session;
92 return this.session;
93 }
94
95 protected String getLoginContext() {
96 return null;
97 }
98
99 protected Session login() {
100 try {
101 if (log.isTraceEnabled())
102 log.trace("Login session");
103 Subject subject = Subject.getSubject(AccessController.getContext());
104 if (subject != null)
105 return getRepository().login();
106 else
107 return getRepository().login(
108 new SimpleCredentials("demo", "demo".toCharArray()));
109 } catch (Exception e) {
110 throw new ArgeoJcrException("Cannot login to repository", e);
111 }
112 }
113
114 protected Repository getRepository() {
115 return repository;
116 }
117
118 /**
119 * enables children class to set an existing repository in case it is not
120 * deleted on startup, to test migration by instance
121 */
122 public void setRepository(Repository repository) {
123 this.repository = repository;
124 }
125
126 protected File getHomeDir() {
127 File homeDir = new File(System.getProperty("java.io.tmpdir"),
128 AbstractJcrTestCase.class.getSimpleName() + "-"
129 + System.getProperty("user.name"));
130 return homeDir;
131 }
132
133 }