]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/unit/AbstractJcrTestCase.java
Remove DocBook from defaults CNDs
[lgpl/argeo-commons.git] / org.argeo.server.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 junit.framework.TestCase;
30
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;
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 ArgeoException("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 ArgeoException("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 // public void logout() {
127 // if (session != null && session.isLive())
128 // JcrUtils.logoutQuietly(session);
129 // }
130 //
131 // protected static TestSuite defaultTestSuite(Class<? extends TestCase>
132 // clss) {
133 // String testSuiteClassName =
134 // "org.argeo.jackrabbit.unit.JackrabbitTestSuite";
135 // try {
136 // Class<?> testSuiteClass = AbstractJcrTestCase.class
137 // .getClassLoader().loadClass(testSuiteClassName);
138 // if (clss == null) {
139 // return (TestSuite) testSuiteClass.newInstance();
140 // } else {
141 // return (TestSuite) testSuiteClass.getConstructor(Class.class)
142 // .newInstance(clss);
143 // }
144 // } catch (Exception e) {
145 // throw new ArgeoException("Cannot find default test suite "
146 // + testSuiteClassName, e);
147 // }
148 // }
149
150 protected File getHomeDir() {
151 File homeDir = new File(System.getProperty("java.io.tmpdir"),
152 AbstractJcrTestCase.class.getSimpleName() + "-"
153 + System.getProperty("user.name"));
154 return homeDir;
155 }
156
157 }