]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/main/java/org/argeo/jcr/unit/AbstractJcrTestCase.java
Move JCR GIS from Commons to Connect
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / main / java / org / argeo / jcr / unit / AbstractJcrTestCase.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.jcr.unit;
18
19 import java.io.File;
20
21 import javax.jcr.Repository;
22 import javax.jcr.Session;
23 import javax.jcr.SimpleCredentials;
24
25 import junit.framework.TestCase;
26
27 import org.apache.commons.io.FileUtils;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.argeo.ArgeoException;
31
32 public abstract class AbstractJcrTestCase extends TestCase {
33 private final static Log log = LogFactory.getLog(AbstractJcrTestCase.class);
34
35 private Repository repository;
36 private Session session = null;
37
38 protected abstract File getRepositoryFile() throws Exception;
39
40 protected abstract Repository createRepository() throws Exception;
41
42 @Override
43 protected void setUp() throws Exception {
44 File homeDir = getHomeDir();
45 FileUtils.deleteDirectory(homeDir);
46 repository = createRepository();
47 }
48
49 protected File getHomeDir() {
50 File homeDir = new File(System.getProperty("java.io.tmpdir"),
51 AbstractJcrTestCase.class.getSimpleName() + "-"
52 + System.getProperty("user.name"));
53 return homeDir;
54 }
55
56 @Override
57 protected void tearDown() throws Exception {
58 if (session != null) {
59 session.logout();
60 if (log.isDebugEnabled())
61 log.debug("Logout session");
62 }
63 }
64
65 protected Session session() {
66 if (session == null) {
67 try {
68 if (log.isDebugEnabled())
69 log.debug("Login session");
70 session = getRepository().login(
71 new SimpleCredentials("demo", "demo".toCharArray()));
72 } catch (Exception e) {
73 throw new ArgeoException("Cannot login to repository", e);
74 }
75 }
76 return session;
77 }
78
79 protected Repository getRepository() {
80 return repository;
81 }
82 }