]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.jcr/src/org/argeo/jcr/unit/AbstractJcrTestCase.java
Move time UUID nodeid back to the factory
[lgpl/argeo-commons.git] / org.argeo.cms.jcr / src / org / argeo / jcr / unit / AbstractJcrTestCase.java
1 package org.argeo.jcr.unit;
2
3 import java.io.File;
4 import java.security.AccessController;
5 import java.security.PrivilegedAction;
6
7 import javax.jcr.Repository;
8 import javax.jcr.RepositoryException;
9 import javax.jcr.Session;
10 import javax.jcr.SimpleCredentials;
11 import javax.security.auth.Subject;
12 import javax.security.auth.login.LoginContext;
13 import javax.security.auth.login.LoginException;
14
15 import org.apache.commons.io.FileUtils;
16 import org.argeo.api.cms.CmsLog;
17 import org.argeo.jcr.JcrException;
18
19 import junit.framework.TestCase;
20
21 /** Base for unit tests with a JCR repository. */
22 public abstract class AbstractJcrTestCase extends TestCase {
23 private final static CmsLog log = CmsLog.getLog(AbstractJcrTestCase.class);
24
25 private Repository repository;
26 private Session session = null;
27
28 public final static String LOGIN_CONTEXT_TEST_SYSTEM = "TEST_JACKRABBIT_ADMIN";
29
30 // protected abstract File getRepositoryFile() throws Exception;
31
32 protected abstract Repository createRepository() throws Exception;
33
34 protected abstract void clearRepository(Repository repository) throws Exception;
35
36 @Override
37 protected void setUp() throws Exception {
38 File homeDir = getHomeDir();
39 FileUtils.deleteDirectory(homeDir);
40 repository = createRepository();
41 }
42
43 @Override
44 protected void tearDown() throws Exception {
45 if (session != null) {
46 session.logout();
47 if (log.isTraceEnabled())
48 log.trace("Logout session");
49 }
50 clearRepository(repository);
51 }
52
53 protected Session session() {
54 if (session != null && session.isLive())
55 return session;
56 Session session;
57 if (getLoginContext() != null) {
58 LoginContext lc;
59 try {
60 lc = new LoginContext(getLoginContext());
61 lc.login();
62 } catch (LoginException e) {
63 throw new IllegalStateException("JAAS login failed", e);
64 }
65 session = Subject.doAs(lc.getSubject(), new PrivilegedAction<Session>() {
66
67 @Override
68 public Session run() {
69 return login();
70 }
71
72 });
73 } else
74 session = login();
75 this.session = session;
76 return this.session;
77 }
78
79 protected String getLoginContext() {
80 return null;
81 }
82
83 protected Session login() {
84 try {
85 if (log.isTraceEnabled())
86 log.trace("Login session");
87 Subject subject = Subject.getSubject(AccessController.getContext());
88 if (subject != null)
89 return getRepository().login();
90 else
91 return getRepository().login(new SimpleCredentials("demo", "demo".toCharArray()));
92 } catch (RepositoryException e) {
93 throw new JcrException("Cannot login to repository", e);
94 }
95 }
96
97 protected Repository getRepository() {
98 return repository;
99 }
100
101 /**
102 * enables children class to set an existing repository in case it is not
103 * deleted on startup, to test migration by instance
104 */
105 public void setRepository(Repository repository) {
106 this.repository = repository;
107 }
108
109 protected File getHomeDir() {
110 File homeDir = new File(System.getProperty("java.io.tmpdir"),
111 AbstractJcrTestCase.class.getSimpleName() + "-" + System.getProperty("user.name"));
112 return homeDir;
113 }
114
115 }