]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/unit/AbstractJcrTestCase.java
Imporve JCR keyring and remote repository exploring
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / unit / AbstractJcrTestCase.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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
20 import javax.jcr.Repository;
21 import javax.jcr.Session;
22 import javax.jcr.SimpleCredentials;
23
24 import junit.framework.TestCase;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.argeo.ArgeoException;
30
31 public abstract class AbstractJcrTestCase extends TestCase {
32 private final static Log log = LogFactory.getLog(AbstractJcrTestCase.class);
33
34 private Repository repository;
35 private Session session = null;
36
37 protected abstract File getRepositoryFile() throws Exception;
38
39 protected abstract Repository createRepository() throws Exception;
40
41 @Override
42 protected void setUp() throws Exception {
43 File homeDir = getHomeDir();
44 FileUtils.deleteDirectory(homeDir);
45 repository = createRepository();
46 }
47
48 protected File getHomeDir() {
49 File homeDir = new File(System.getProperty("java.io.tmpdir"),
50 AbstractJcrTestCase.class.getSimpleName() + "-"
51 + System.getProperty("user.name"));
52 return homeDir;
53 }
54
55 @Override
56 protected void tearDown() throws Exception {
57 if (session != null) {
58 session.logout();
59 if (log.isDebugEnabled())
60 log.debug("Logout session");
61 }
62 }
63
64 protected Session session() {
65 if (session == null) {
66 try {
67 if (log.isDebugEnabled())
68 log.debug("Login session");
69 session = getRepository().login(
70 new SimpleCredentials("demo", "demo".toCharArray()));
71 } catch (Exception e) {
72 throw new ArgeoException("Cannot login to repository", e);
73 }
74 }
75 return session;
76 }
77
78 protected Repository getRepository() {
79 return repository;
80 }
81
82 /**
83 * enables children class to set an existing repository in case it is not
84 * deleted on startup, to test migration by instance
85 */
86 protected void setRepository(Repository repository) {
87 this.repository = repository;
88 }
89 }