]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/org/argeo/jcr/unit/AbstractJcrTestCase.java
Publish Jackrabbit repositories as such.
[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
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 protected abstract void clearRepository(Repository repository)
42 throws Exception;
43
44 @Override
45 protected void setUp() throws Exception {
46 File homeDir = getHomeDir();
47 FileUtils.deleteDirectory(homeDir);
48 repository = createRepository();
49 }
50
51 @Override
52 protected void tearDown() throws Exception {
53 if (session != null) {
54 session.logout();
55 if (log.isTraceEnabled())
56 log.trace("Logout session");
57 }
58 clearRepository(repository);
59 }
60
61 protected Session session() {
62 if (session == null || !session.isLive()) {
63 try {
64 if (log.isTraceEnabled())
65 log.trace("Login session");
66 session = getRepository().login(
67 new SimpleCredentials("demo", "demo".toCharArray()));
68 } catch (Exception e) {
69 throw new ArgeoException("Cannot login to repository", e);
70 }
71 }
72 return session;
73 }
74
75 protected Repository getRepository() {
76 return repository;
77 }
78
79 /**
80 * enables children class to set an existing repository in case it is not
81 * deleted on startup, to test migration by instance
82 */
83 public void setRepository(Repository repository) {
84 this.repository = repository;
85 }
86
87 // public void logout() {
88 // if (session != null && session.isLive())
89 // JcrUtils.logoutQuietly(session);
90 // }
91 //
92 // protected static TestSuite defaultTestSuite(Class<? extends TestCase>
93 // clss) {
94 // String testSuiteClassName =
95 // "org.argeo.jackrabbit.unit.JackrabbitTestSuite";
96 // try {
97 // Class<?> testSuiteClass = AbstractJcrTestCase.class
98 // .getClassLoader().loadClass(testSuiteClassName);
99 // if (clss == null) {
100 // return (TestSuite) testSuiteClass.newInstance();
101 // } else {
102 // return (TestSuite) testSuiteClass.getConstructor(Class.class)
103 // .newInstance(clss);
104 // }
105 // } catch (Exception e) {
106 // throw new ArgeoException("Cannot find default test suite "
107 // + testSuiteClassName, e);
108 // }
109 // }
110
111 protected File getHomeDir() {
112 File homeDir = new File(System.getProperty("java.io.tmpdir"),
113 AbstractJcrTestCase.class.getSimpleName() + "-"
114 + System.getProperty("user.name"));
115 return homeDir;
116 }
117
118 }