001: /*
002: * Copyright 2004 Outerthought bvba and Schaubroeck nv
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.outerj.daisy.repository.testsupport;
017:
018: import junit.framework.TestCase;
019:
020: import java.io.*;
021: import java.util.Collections;
022: import java.util.Set;
023: import java.util.Map;
024:
025: import org.outerj.daisy.repository.RepositoryManager;
026: import org.outerj.daisy.repository.Credentials;
027: import org.outerj.daisy.launcher.remoteclient.RemoteClientLauncher;
028: import org.outerj.daisy.launcher.repository.RuntimeLauncher;
029: import org.outerj.daisy.launcher.repository.RuntimeHandle;
030: import org.outerj.daisy.jms.JmsClient;
031:
032: public abstract class AbstractDaisyTestCase extends TestCase {
033:
034: protected RuntimeHandle runtime;
035: private TestSupportConfig config;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039:
040: config = new TestSupportConfig();
041:
042: installRepoConfig(null);
043:
044: if (resetDataStores()) {
045: performResetDataStores();
046: }
047:
048: launchRuntime();
049: }
050:
051: private void installRepoConfig(String namespace) throws Exception {
052: if (namespace == null)
053: namespace = "DSYTEST";
054:
055: PrintWriter writer = new PrintWriter(new File(
056: "target/myconfig.xml"));
057: BufferedReader reader = new BufferedReader(new FileReader(
058: "myconfig.xml.template"));
059:
060: String line;
061: while ((line = reader.readLine()) != null) {
062: // This is not nice code
063: for (Map.Entry entry : config.entrySet()) {
064: String key = (String) entry.getKey();
065: String value = (String) entry.getValue();
066: line = line.replaceAll("@" + key + "@", value);
067: }
068:
069: line = line.replaceAll("@namespace@", namespace);
070:
071: writer.println(line);
072: }
073:
074: writer.close();
075: reader.close();
076: }
077:
078: private void performResetDataStores() throws Exception {
079: System.out.println("Reinstalling database...");
080: DatabaseHelper dbHelper = new DatabaseHelper(config);
081: dbHelper.resetDatabase("testuser", "testuser");
082:
083: String blobstore = config
084: .getRequiredProperty("testsupport.blobstore");
085: String indexstore = config
086: .getRequiredProperty("testsupport.fulltextindexstore");
087:
088: File blobstoreFile = new File(blobstore);
089: if (!blobstoreFile.exists() || !blobstoreFile.isDirectory())
090: throw new Exception(
091: "Blobstore directory does not exist or is not a directory: "
092: + blobstoreFile.getAbsolutePath());
093: System.out.println("Deleting contents of blobstore directory ("
094: + blobstoreFile.getAbsolutePath() + ")...");
095: emptyDir(blobstoreFile);
096:
097: File indexstoreFile = new File(indexstore);
098: if (!indexstoreFile.exists() || !indexstoreFile.isDirectory())
099: throw new Exception(
100: "Indexstore directory does not exist or is not a directory: "
101: + indexstoreFile.getAbsolutePath());
102: System.out
103: .println("Deleting contents of indexstore directory ("
104: + indexstoreFile.getAbsolutePath() + ")...");
105: emptyDir(indexstoreFile);
106: }
107:
108: private void launchRuntime() {
109: runtime = RuntimeLauncher.launch(
110: "../server/src/conf/runtime-config.xml",
111: getRepositoryLocation().getAbsolutePath(),
112: "target/myconfig.xml", getDisabledContainerIds());
113: }
114:
115: private void emptyDir(File dir) {
116: File[] files = dir.listFiles();
117: for (File file : files) {
118: if (file.isDirectory())
119: emptyDir(file);
120: file.delete();
121: }
122: }
123:
124: protected void tearDown() throws Exception {
125: super .tearDown();
126: runtime.shutdown();
127: }
128:
129: protected abstract boolean resetDataStores();
130:
131: protected Set<String> getDisabledContainerIds() {
132: // workflow takes long to load and is only need by a few tests, so disable it by default
133: return Collections.singleton("workflow");
134: }
135:
136: protected Object getComponent(Class type) throws Exception {
137: return runtime.getService(type);
138: }
139:
140: protected RepositoryManager getRemoteRepositoryManager()
141: throws Exception {
142: JmsClient jmsClient = (JmsClient) getComponent(JmsClient.class);
143: return (RepositoryManager) RemoteClientLauncher
144: .getRepositoryManager(
145: "http://localhost:9263",
146: new Credentials("testuser", "testuser"),
147: jmsClient,
148: config
149: .getRequiredProperty("testsupport.jmstopic"),
150: getRepositoryLocation());
151: }
152:
153: protected RepositoryManager getLocalRepositoryManager()
154: throws Exception {
155: return (RepositoryManager) getComponent(RepositoryManager.class);
156: }
157:
158: private File getRepositoryLocation() {
159: return new File(getMavenHome() + File.separator + "repository");
160: }
161:
162: private File getMavenHome() {
163: try {
164: String local = System.getProperty("maven.home.local",
165: System.getenv("MAVEN_HOME_LOCAL"));
166: if (local != null)
167: return new File(local).getCanonicalFile();
168: return new File(System.getProperty("user.home")
169: + File.separator + ".maven").getCanonicalFile();
170: } catch (IOException e) {
171: throw new RuntimeException(
172: "Error trying to determine maven home.", e);
173: }
174: }
175:
176: protected void restartRepository(boolean cleanData, String namespace)
177: throws Exception {
178: runtime.shutdown();
179:
180: if (cleanData) {
181: installRepoConfig(namespace);
182: performResetDataStores();
183: }
184:
185: launchRuntime();
186: }
187: }
|