001: /*
002: * Copyright 2004-2006 the original author or authors.
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:
017: package org.compass.core.test;
018:
019: import java.io.File;
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.Properties;
023:
024: import org.compass.core.Compass;
025: import org.compass.core.CompassSession;
026: import org.compass.core.ResourceFactory;
027: import org.compass.core.cache.first.NullFirstLevelCache;
028: import org.compass.core.config.CompassConfiguration;
029: import org.compass.core.config.CompassEnvironment;
030: import org.compass.core.config.CompassSettings;
031: import org.compass.core.util.FileHandlerMonitor;
032:
033: /**
034: * @author kimchy
035: */
036: public abstract class AbstractTestCase extends ExtendedTestCase {
037:
038: private static Compass compass;
039:
040: private static FileHandlerMonitor fileHandlerMonitor;
041:
042: protected abstract String[] getMappings();
043:
044: protected void beforeTestCase() throws Exception {
045: compass = buildCompass();
046: if (System.getProperty("compass.test.validateFileHandler",
047: "false").equals("true")) {
048: String connection = compass.getSettings().getSetting(
049: CompassEnvironment.CONNECTION);
050: if (connection.startsWith("file://")
051: || connection.indexOf("://") == -1) {
052: if (connection.startsWith("file://")) {
053: connection = connection.substring("file://"
054: .length());
055: }
056: fileHandlerMonitor = new FileHandlerMonitor(connection);
057: }
058: }
059: verifyNoHandlers();
060: }
061:
062: protected void setUp() throws Exception {
063: compass.getSearchEngineIndexManager().clearCache();
064: verifyNoHandlers();
065: try {
066: compass.getSearchEngineIndexManager().deleteIndex();
067: } catch (Exception e) {
068: e.printStackTrace();
069: }
070: if (compass.getSpellCheckManager() != null) {
071: try {
072: compass.getSpellCheckManager().deleteIndex();
073: } catch (Exception e) {
074: e.printStackTrace();
075: }
076: }
077: compass.getSearchEngineIndexManager().verifyIndex();
078: }
079:
080: protected void tearDown() throws Exception {
081: compass.getSearchEngineIndexManager().clearCache();
082: verifyNoHandlers();
083: try {
084: compass.getSearchEngineIndexManager().deleteIndex();
085: } catch (Exception e) {
086: e.printStackTrace();
087: }
088: if (compass.getSpellCheckManager() != null) {
089: try {
090: compass.getSpellCheckManager().deleteIndex();
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: }
095: }
096:
097: protected void afterTestCase() throws Exception {
098: compass.close();
099: verifyNoHandlers();
100: }
101:
102: protected Compass buildCompass() throws IOException {
103: CompassConfiguration conf = createConfiguration().configure(
104: "/org/compass/core/test/compass.cfg.xml");
105: File testPropsFile = new File("compass.test.properties");
106: if (testPropsFile.exists()) {
107: Properties testProps = new Properties();
108: testProps.load(new FileInputStream(testPropsFile));
109: conf.getSettings().addSettings(testProps);
110: }
111: for (String mapping : getMappings()) {
112: conf.addResource(getPackagePrefix() + mapping,
113: AbstractTestCase.class.getClassLoader());
114: }
115: conf.getSettings().setSetting(
116: CompassEnvironment.Cache.FirstLevel.TYPE,
117: NullFirstLevelCache.class.getName());
118: addSettings(conf.getSettings());
119: addExtraConf(conf);
120: return conf.buildCompass();
121: }
122:
123: protected String getPackagePrefix() {
124: return "org/compass/core/test/";
125: }
126:
127: protected CompassConfiguration createConfiguration() {
128: return new CompassConfiguration();
129: }
130:
131: protected void addExtraConf(CompassConfiguration conf) {
132: // do nothing
133: }
134:
135: protected void addSettings(CompassSettings settings) {
136:
137: }
138:
139: protected CompassSession openSession() {
140: return compass.openSession();
141: }
142:
143: public Compass getCompass() {
144: return compass;
145: }
146:
147: public ResourceFactory getResourceFactory() {
148: return getCompass().getResourceFactory();
149: }
150:
151: private void verifyNoHandlers() throws Exception {
152: if (fileHandlerMonitor == null) {
153: return;
154: }
155: FileHandlerMonitor.FileHandlers handlers = fileHandlerMonitor
156: .handlers();
157: if (handlers == null) {
158: return;
159: }
160: if (handlers.hasHandlers()) {
161: throw new Exception("File Handlers still exist \n"
162: + handlers.getRawOutput());
163: }
164: }
165: }
|