org.exist.test.ExistEmbeddedServer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of exist-core Show documentation
Show all versions of exist-core Show documentation
eXist-db NoSQL Database Core
/*
* eXist-db Open Source Native XML Database
* Copyright (C) 2001 The eXist-db Authors
*
* [email protected]
* http://www.exist-db.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.exist.test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.EXistException;
import org.exist.start.Classpath;
import org.exist.start.EXistClassLoader;
import org.exist.storage.BrokerPool;
import org.exist.storage.journal.Journal;
import org.exist.util.Configuration;
import org.exist.util.ConfigurationHelper;
import org.exist.util.DatabaseConfigurationException;
import org.exist.util.FileUtils;
import org.junit.rules.ExternalResource;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import static org.exist.repo.AutoDeploymentTrigger.AUTODEPLOY_PROPERTY;
/**
* Exist embedded Server Rule for JUnit.
*/
public class ExistEmbeddedServer extends ExternalResource {
private static final Logger LOG = LogManager.getLogger(ExistEmbeddedServer.class);
private final Optional instanceName;
private final Optional configFile;
private final Optional configProperties;
private final boolean useTemporaryStorage;
private final boolean disableAutoDeploy;
private Optional temporaryStorage = Optional.empty();
private String prevAutoDeploy = "off";
private BrokerPool pool = null;
public ExistEmbeddedServer() {
this(null, null, null, false, false);
}
public ExistEmbeddedServer(final boolean useTemporaryStorage) {
this(null, null, null, false, useTemporaryStorage);
}
public ExistEmbeddedServer(final boolean disableAutoDeploy, final boolean useTemporaryStorage) {
this(null, null, null, disableAutoDeploy, useTemporaryStorage);
}
public ExistEmbeddedServer(final Properties configProperties) {
this(null, null, configProperties, false, false);
}
public ExistEmbeddedServer(final Properties configProperties, final boolean disableAutoDeploy, final boolean useTemporaryStorage) {
this(null, null, configProperties, disableAutoDeploy, useTemporaryStorage);
}
public ExistEmbeddedServer(final String instanceName, final Path configFile) {
this(instanceName, configFile, null, false, false);
}
public ExistEmbeddedServer(final String instanceName, final Path configFile, final Properties configProperties) {
this(instanceName, configFile, configProperties, false, false);
}
public ExistEmbeddedServer(final String instanceName, final Path configFile, final Properties configProperties, final boolean disableAutoDeploy) {
this(instanceName, configFile, configProperties, false, false);
}
public ExistEmbeddedServer(@Nullable final String instanceName, @Nullable final Path configFile, @Nullable final Properties configProperties, @Nullable final boolean disableAutoDeploy, @Nullable final boolean useTemporaryStorage) {
this.instanceName = Optional.ofNullable(instanceName);
this.configFile = Optional.ofNullable(configFile);
this.configProperties = Optional.ofNullable(configProperties);
this.disableAutoDeploy = disableAutoDeploy;
this.useTemporaryStorage = useTemporaryStorage;
// setup classloader
final Classpath _classpath = new Classpath();
final EXistClassLoader cl = _classpath.getClassLoader(null);
Thread.currentThread().setContextClassLoader(cl);
}
@Override
protected void before() throws Throwable {
startDb();
super.before();
}
public void startDb() throws DatabaseConfigurationException, EXistException, IOException {
if(pool == null) {
if(disableAutoDeploy) {
this.prevAutoDeploy = System.getProperty(AUTODEPLOY_PROPERTY, "off");
System.setProperty(AUTODEPLOY_PROPERTY, "off");
}
final String name = instanceName.orElse(BrokerPool.DEFAULT_INSTANCE_NAME);
final Optional home = Optional.ofNullable(System.getProperty("exist.home", System.getProperty("user.dir"))).map(Paths::get);
final Path confFile = configFile.orElseGet(() -> ConfigurationHelper.lookup("conf.xml", home));
final Configuration config;
if(confFile.isAbsolute() && Files.exists(confFile)) {
//TODO(AR) is this correct?
config = new Configuration(confFile.toAbsolutePath().toString());
} else {
config = new Configuration(FileUtils.fileName(confFile), home);
}
// override any specified config properties
configProperties.ifPresent(properties -> {
for (final Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy