diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index e22d9dbc36f..fc661c7f1d6 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -47,6 +47,8 @@ Other Changes * SOLR-4792: Stop shipping a .war. (Robert Muir) +* SOLR-4948: Tidied up CoreContainer construction logic. (Alan Woodward) + ================== 4.4.0 ================== Versions of Major Components diff --git a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java index 6f291062bd9..45bafb4a050 100644 --- a/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java +++ b/solr/contrib/dataimporthandler/src/test/org/apache/solr/handler/dataimport/TestZKPropertiesWriter.java @@ -60,7 +60,8 @@ public class TestZKPropertiesWriter extends AbstractDataImportHandlerTestCase { AbstractZkTestCase.buildZooKeeper(zkServer.getZkHost(), zkServer.getZkAddress(), getFile("dih/solr"), "dataimport-solrconfig.xml", "dataimport-schema.xml"); - initCore("dataimport-solrconfig.xml", "dataimport-schema.xml", getFile("dih/solr").getAbsolutePath()); + //initCore("solrconfig.xml", "schema.xml", getFile("dih/solr").getAbsolutePath()); + createDefaultCoreContainer(getFile("dih/solr").getAbsolutePath()); } @Before diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java index 7dafae3a148..dad2d831919 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java +++ b/solr/core/src/java/org/apache/solr/cloud/ZkCLI.java @@ -1,14 +1,5 @@ package org.apache.solr.cloud; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.concurrent.TimeoutException; - -import javax.xml.parsers.ParserConfigurationException; - import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.HelpFormatter; @@ -17,13 +8,9 @@ import org.apache.commons.cli.OptionBuilder; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.commons.cli.PosixParser; -import org.apache.commons.io.IOUtils; import org.apache.solr.common.cloud.OnReconnect; import org.apache.solr.common.cloud.SolrZkClient; -import org.apache.solr.core.Config; import org.apache.solr.core.ConfigSolr; -import org.apache.solr.core.ConfigSolrXml; -import org.apache.solr.core.ConfigSolrXmlOld; import org.apache.solr.core.SolrResourceLoader; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; @@ -32,6 +19,12 @@ import org.apache.zookeeper.data.ACL; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import javax.xml.parsers.ParserConfigurationException; +import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.concurrent.TimeoutException; + /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with @@ -183,25 +176,7 @@ public class ZkCLI { SolrResourceLoader loader = new SolrResourceLoader(solrHome); solrHome = loader.getInstanceDir(); - File configFile = new File(solrHome, SOLR_XML); - InputStream is = new FileInputStream(configFile); - - ConfigSolr cfg; - - try { - Config config = new Config(loader, null, new InputSource(is), null, false); - - boolean oldStyle = (config.getNode("solr/cores", false) != null); - - if (oldStyle) { - cfg = new ConfigSolrXmlOld(config, null); - } else { - cfg = new ConfigSolrXml(config, null); - } - } finally { - IOUtils.closeQuietly(is); - } - + ConfigSolr cfg = ConfigSolr.fromSolrHome(solrHome); if(!ZkController.checkChrootPath(zkServerAddress, true)) { System.out.println("A chroot was specified in zkHost but the znode doesn't exist. "); diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.java index d8b6c6b882e..b046e7d4018 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java +++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java @@ -17,28 +17,6 @@ package org.apache.solr.cloud; * limitations under the License. */ -import java.io.File; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.URLEncoder; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.Set; -import java.util.concurrent.Future; -import java.util.concurrent.TimeoutException; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import org.apache.commons.io.FileUtils; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.request.CoreAdminRequest.WaitForState; @@ -74,6 +52,28 @@ import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.URLEncoder; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.Future; +import java.util.concurrent.TimeoutException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + /** * Handle ZooKeeper interactions. * diff --git a/solr/core/src/java/org/apache/solr/core/Config.java b/solr/core/src/java/org/apache/solr/core/Config.java index 41fa3c26bda..af16b6638b9 100644 --- a/solr/core/src/java/org/apache/solr/core/Config.java +++ b/solr/core/src/java/org/apache/solr/core/Config.java @@ -68,6 +68,7 @@ public class Config { static final XPathFactory xpathFactory = XPathFactory.newInstance(); private final Document doc; + private final Document origDoc; // with unsubstituted properties private final String prefix; private final String name; private final SolrResourceLoader loader; @@ -131,6 +132,7 @@ public class Config { db.setErrorHandler(xmllog); try { doc = db.parse(is); + origDoc = copyDoc(doc); } finally { // some XML parsers are broken and don't close the byte stream (but they should according to spec) IOUtils.closeQuietly(is.getByteStream()); @@ -140,19 +142,24 @@ public class Config { } } catch (ParserConfigurationException e) { SolrException.log(log, "Exception during parsing file: " + name, e); - throw e; + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } catch (SAXException e) { SolrException.log(log, "Exception during parsing file: " + name, e); - throw e; - } catch( SolrException e ){ - SolrException.log(log,"Error in "+name,e); - throw e; + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } catch (TransformerException e) { + SolrException.log(log, "Exception during parsing file: " + name, e); + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); } } public Config(SolrResourceLoader loader, String name, Document doc) { this.prefix = null; this.doc = doc; + try { + this.origDoc = copyDoc(doc); + } catch (TransformerException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } this.name = name; this.loader = loader; } @@ -441,4 +448,9 @@ public class Config { return version; } + + public Config getOriginalConfig() { + return new Config(loader, null, origDoc); + } + } diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSolr.java b/solr/core/src/java/org/apache/solr/core/ConfigSolr.java index ef2ef84b2e1..edea722bbe5 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSolr.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSolr.java @@ -17,15 +17,8 @@ package org.apache.solr.core; * limitations under the License. */ -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Properties; - -import javax.xml.xpath.XPath; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; - +import com.google.common.base.Charsets; +import org.apache.commons.io.IOUtils; import org.apache.solr.common.SolrException; import org.apache.solr.util.DOMUtil; import org.apache.solr.util.PropertiesUtil; @@ -33,13 +26,77 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import org.xml.sax.InputSource; + +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; public abstract class ConfigSolr { protected static Logger log = LoggerFactory.getLogger(ConfigSolr.class); public final static String SOLR_XML_FILE = "solr.xml"; - + + public static ConfigSolr fromFile(File configFile) { + log.info("Loading container configuration from {}", configFile.getAbsolutePath()); + + String solrHome = configFile.getParent(); + SolrResourceLoader loader = new SolrResourceLoader(solrHome); + InputStream inputStream = null; + + try { + if (!configFile.exists()) { + log.info("{} does not exist, using default configuration", configFile.getAbsolutePath()); + inputStream = new ByteArrayInputStream(ConfigSolrXmlOld.DEF_SOLR_XML.getBytes(Charsets.UTF_8)); + } + else { + inputStream = new FileInputStream(configFile); + } + return fromInputStream(loader, inputStream); + } + catch (Exception e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, + "Could not load SOLR configuration", e); + } + finally { + IOUtils.closeQuietly(inputStream); + } + } + + public static ConfigSolr fromString(SolrResourceLoader loader, String xml) { + return fromInputStream(loader, new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8))); + } + + public static ConfigSolr fromInputStream(SolrResourceLoader loader, InputStream is) { + try { + Config config = new Config(loader, null, new InputSource(is), null, false); + //config.substituteProperties(); + return fromConfig(config); + } + catch (Exception e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } + } + + public static ConfigSolr fromSolrHome(String solrHome) { + return fromFile(new File(solrHome, SOLR_XML_FILE)); + } + + public static ConfigSolr fromConfig(Config config) { + boolean oldStyle = (config.getNode("solr/cores", false) != null); + return oldStyle ? new ConfigSolrXmlOld(config) + : new ConfigSolrXml(config, null); + } + // Ugly for now, but we'll at least be able to centralize all of the differences between 4x and 5x. public static enum CfgProp { SOLR_ADMINHANDLER, @@ -79,6 +136,11 @@ public abstract class ConfigSolr { public ConfigSolr(Config config) { this.config = config; } + + // for extension & testing. + protected ConfigSolr() { + + } public Config getConfig() { return config; @@ -125,7 +187,8 @@ public abstract class ConfigSolr { Properties properties = new Properties(); for (int i = 0; i < props.getLength(); i++) { Node prop = props.item(i); - properties.setProperty(DOMUtil.getAttr(prop, "name"), DOMUtil.getAttr(prop, "value")); + properties.setProperty(DOMUtil.getAttr(prop, "name"), + PropertiesUtil.substituteProperty(DOMUtil.getAttr(prop, "value"), null)); } return properties; } diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSolrXml.java b/solr/core/src/java/org/apache/solr/core/ConfigSolrXml.java index 81f4b0f4d0a..8199f75c514 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSolrXml.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSolrXml.java @@ -17,6 +17,12 @@ package org.apache.solr.core; * limitations under the License. */ +import org.apache.commons.io.IOUtils; +import org.apache.solr.common.SolrException; +import org.apache.solr.util.PropertiesUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.FileInputStream; import java.io.IOException; @@ -27,15 +33,6 @@ import java.util.List; import java.util.Map; import java.util.Properties; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.commons.io.IOUtils; -import org.apache.solr.common.SolrException; -import org.apache.solr.util.PropertiesUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; - /** * @@ -46,15 +43,18 @@ public class ConfigSolrXml extends ConfigSolr { private SolrCoreDiscoverer solrCoreDiscoverer = new SolrCoreDiscoverer(); private final Map coreDescriptorMap; - public ConfigSolrXml(Config config, CoreContainer container) - throws ParserConfigurationException, IOException, SAXException { + public ConfigSolrXml(Config config, CoreContainer container) { super(config); - checkForIllegalConfig(); - - fillPropMap(); - - String coreRoot = get(CfgProp.SOLR_COREROOTDIRECTORY, (container == null ? config.getResourceLoader().getInstanceDir() : container.getSolrHome())); - coreDescriptorMap = solrCoreDiscoverer.discover(container, new File(coreRoot)); + try { + checkForIllegalConfig(); + fillPropMap(); + config.substituteProperties(); + String coreRoot = get(CfgProp.SOLR_COREROOTDIRECTORY, (container == null ? config.getResourceLoader().getInstanceDir() : container.getSolrHome())); + coreDescriptorMap = solrCoreDiscoverer.discover(container, new File(coreRoot)); + } + catch (IOException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } } private void checkForIllegalConfig() throws IOException { diff --git a/solr/core/src/java/org/apache/solr/core/ConfigSolrXmlOld.java b/solr/core/src/java/org/apache/solr/core/ConfigSolrXmlOld.java index de4d1efdacf..40c1a87fe14 100644 --- a/solr/core/src/java/org/apache/solr/core/ConfigSolrXmlOld.java +++ b/solr/core/src/java/org/apache/solr/core/ConfigSolrXmlOld.java @@ -17,6 +17,17 @@ package org.apache.solr.core; * limitations under the License. */ +import org.apache.solr.common.SolrException; +import org.apache.solr.util.DOMUtil; +import org.apache.solr.util.PropertiesUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -28,20 +39,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; -import javax.xml.parsers.ParserConfigurationException; -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpressionException; - -import org.apache.solr.common.SolrException; -import org.apache.solr.util.DOMUtil; -import org.apache.solr.util.PropertiesUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.xml.sax.SAXException; - /** * @@ -51,17 +48,20 @@ public class ConfigSolrXmlOld extends ConfigSolr { private NodeList coreNodes = null; - public ConfigSolrXmlOld(Config config, CoreContainer container) - throws ParserConfigurationException, IOException, SAXException { - + public ConfigSolrXmlOld(Config config) { super(config); - checkForIllegalConfig(container); - - fillPropMap(); - initCoreList(container); + try { + checkForIllegalConfig(); + fillPropMap(); + config.substituteProperties(); + initCoreList(); + } + catch (IOException e) { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e); + } } - private void checkForIllegalConfig(CoreContainer container) throws IOException { + private void checkForIllegalConfig() throws IOException { // Do sanity checks - we don't want to find new style // config failIfFound("solr/str[@name='adminHandler']"); @@ -157,7 +157,7 @@ public class ConfigSolrXmlOld extends ConfigSolr { } - private void initCoreList(CoreContainer container) throws IOException { + private void initCoreList() throws IOException { coreNodes = (NodeList) config.evaluate("solr/cores/core", XPathConstants.NODESET); @@ -222,7 +222,7 @@ public class ConfigSolrXmlOld extends ConfigSolr { NamedNodeMap attributes = node.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Node attribute = attributes.item(i); - String val = attribute.getNodeValue(); + String val = PropertiesUtil.substituteProperty(attribute.getNodeValue(), null); if (CoreDescriptor.CORE_DATADIR.equals(attribute.getNodeName()) || CoreDescriptor.CORE_INSTDIR.equals(attribute.getNodeName())) { if (val.indexOf('$') == -1) { @@ -260,7 +260,8 @@ public class ConfigSolrXmlOld extends ConfigSolr { Node node = coreNodes.item(idx); if (coreName.equals(DOMUtil.getAttr(node, CoreDescriptor.CORE_NAME, null))) { - return DOMUtil.getAttr(node, property, defaultVal); + String propVal = DOMUtil.getAttr(node, property, defaultVal); + return PropertiesUtil.substituteProperty(propVal, null); } } } @@ -289,16 +290,16 @@ public class ConfigSolrXmlOld extends ConfigSolr { } public static final String DEF_SOLR_XML = "\n" - + "\n" - + " \n" - + " \n" - + " \n" + ""; + + "\n" + + " \n" + + " \n" + + " \n" + ""; @Override public void substituteProperties() { diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java index 8df62cf0a75..6c2fc559b39 100644 --- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java +++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java @@ -17,7 +17,6 @@ package org.apache.solr.core; -import org.apache.commons.io.IOUtils; import org.apache.solr.cloud.ZkController; import org.apache.solr.cloud.ZkSolrResourceLoader; import org.apache.solr.common.SolrException; @@ -39,21 +38,10 @@ import org.apache.solr.util.plugin.PluginInfoInitialized; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.w3c.dom.Document; import org.w3c.dom.Node; -import org.xml.sax.InputSource; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.dom.DOMResult; -import javax.xml.transform.dom.DOMSource; import javax.xml.xpath.XPathExpressionException; -import java.io.ByteArrayInputStream; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Collections; @@ -76,6 +64,8 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import static com.google.common.base.Preconditions.checkNotNull; + /** * @@ -104,15 +94,13 @@ public class CoreContainer protected CoreAdminHandler coreAdminHandler = null; protected CollectionsHandler collectionsHandler = null; - protected File configFile = null; protected String libDir = null; - protected SolrResourceLoader loader = null; + protected Properties containerProperties; protected Map indexSchemaCache; protected String adminHandler; protected boolean shareSchema; protected Integer zkClientTimeout; - protected String solrHome; protected String defaultCoreName = null; protected int distribUpdateConnTimeout = 0; protected int distribUpdateSoTimeout = 0; @@ -126,151 +114,90 @@ public class CoreContainer private int coreLoadThreads; private CloserThread backgroundCloser = null; - protected volatile ConfigSolr cfg; - private Config origCfg; + + protected final ConfigSolr cfg; + protected final SolrResourceLoader loader; + protected final String solrHome; { log.info("New CoreContainer " + System.identityHashCode(this)); } /** - * Deprecated - * @deprecated use the single arg constructor with locateSolrHome() - * @see SolrResourceLoader#locateSolrHome + * Create a new CoreContainer using system properties to detect the solr home + * directory. The container's cores are not loaded. + * @see #load() */ - @Deprecated public CoreContainer() { this(SolrResourceLoader.locateSolrHome()); } /** - * Initalize CoreContainer directly from the constructor + * Create a new CoreContainer using the given SolrResourceLoader. The container's + * cores are not loaded. + * @param loader the SolrResourceLoader + * @see #load() */ - public CoreContainer(String dir, File configFile) throws FileNotFoundException { - this(dir); - this.load(dir, configFile); + public CoreContainer(SolrResourceLoader loader) { + this(loader, ConfigSolr.fromSolrHome(loader.getInstanceDir())); } /** - * Minimal CoreContainer constructor. - * @param loader the CoreContainer resource loader + * Create a new CoreContainer using the given solr home directory. The container's + * cores are not loaded. + * @param solrHome a String containing the path to the solr home directory + * @see #load() */ - public CoreContainer(SolrResourceLoader loader) { - this(loader.getInstanceDir()); - this.loader = loader; + public CoreContainer(String solrHome) { + this(new SolrResourceLoader(solrHome), ConfigSolr.fromSolrHome(solrHome)); } - public CoreContainer(String solrHome) { - this.solrHome = solrHome; + /** + * Create a new CoreContainer using the given SolrResourceLoader and + * configuration. The container's cores are not loaded. + * @param loader the SolrResourceLoader + * @param config a ConfigSolr representation of this container's configuration + * @see #load() + */ + public CoreContainer(SolrResourceLoader loader, ConfigSolr config) { + this.loader = checkNotNull(loader); + this.solrHome = loader.getInstanceDir(); + this.cfg = checkNotNull(config); + } + + /** + * Create a new CoreContainer and load its cores + * @param solrHome the solr home directory + * @param configFile the file containing this container's configuration + * @return a loaded CoreContainer + */ + public static CoreContainer createAndLoad(String solrHome, File configFile) { + CoreContainer cc = new CoreContainer(new SolrResourceLoader(solrHome), ConfigSolr.fromFile(configFile)); + cc.load(); + return cc; } public Properties getContainerProperties() { return containerProperties; } - // Helper class to initialize the CoreContainer - public static class Initializer { - - // core container instantiation - public CoreContainer initialize() throws FileNotFoundException { - CoreContainer cores = null; - String solrHome = SolrResourceLoader.locateSolrHome(); - // ContainerConfigFilename could could be a properties file - File fconf = new File(solrHome, "solr.xml"); - - log.info("looking for solr config file: " + fconf.getAbsolutePath()); - cores = new CoreContainer(solrHome); - - // first we find zkhost, then we check for solr.xml in zk - // 1. look for zkhost from sys prop 2. TODO: look for zkhost in {solr.home}/solr.properties - - // Either we have a config file or not. - - if (fconf.exists()) { - cores.load(solrHome, fconf); - } else { - // Back compart support - log.info("no solr.xml found. using default old-style solr.xml"); - try { - cores.load(solrHome, new ByteArrayInputStream(ConfigSolrXmlOld.DEF_SOLR_XML.getBytes("UTF-8")), null); - } catch (Exception e) { - throw new SolrException(ErrorCode.SERVER_ERROR, - "CoreContainer.Initialize failed when trying to load default solr.xml file", e); - } - cores.configFile = fconf; - } - - return cores; - } - } - - //------------------------------------------------------------------- // Initialization / Cleanup //------------------------------------------------------------------- - - /** - * Load a config file listing the available solr cores. - * @param dir the home directory of all resources. - * @param configFile the configuration file - */ - public void load(String dir, File configFile) throws FileNotFoundException { - this.configFile = configFile; - InputStream in = new FileInputStream(configFile); - try { - this.load(dir, in, configFile.getName()); - } finally { - IOUtils.closeQuietly(in); - } - } /** - * Load a config file listing the available solr cores. - * - * @param dir the home directory of all resources. - * @param is the configuration file InputStream. May be a properties file or an xml file + * Load the cores defined for this CoreContainer */ + public void load() { + + log.info("Loading cores into CoreContainer [instanceDir={}]", loader.getInstanceDir()); - // Let's keep this ugly boolean out of public circulation. - protected void load(String dir, InputStream is, String fileName) { ThreadPoolExecutor coreLoadExecutor = null; - if (null == dir) { - // don't rely on SolrResourceLoader(), determine explicitly first - dir = SolrResourceLoader.locateSolrHome(); - } - log.info("Loading CoreContainer using Solr Home: '{}'", dir); - - this.loader = new SolrResourceLoader(dir); - solrHome = loader.getInstanceDir(); - - try { - Config config = new Config(loader, null, new InputSource(is), null, false); - - // old style defines cores in solr.xml, new style disovers them by - // directory structure - boolean oldStyle = (config.getNode("solr/cores", false) != null); - - if (oldStyle) { - // ConfigSolr handles keep orig values around for non solrcore level items, - // but this is still how original core lvl attributes are kept around - this.origCfg = new Config(loader, null, copyDoc(config.getDocument())); - - this.cfg = new ConfigSolrXmlOld(config, this); - } else { - this.cfg = new ConfigSolrXml(config, this); - - } - } catch (Exception e) { - throw new SolrException(ErrorCode.SERVER_ERROR, "", e); - } - // Since the cores var is now initialized to null, let's set it up right - // now. - cfg.substituteProperties(); // add the sharedLib to the shared resource loader before initializing cfg based plugins libDir = cfg.get(ConfigSolr.CfgProp.SOLR_SHAREDLIB , null); if (libDir != null) { - File f = FileUtils.resolvePath(new File(dir), libDir); + File f = FileUtils.resolvePath(new File(solrHome), libDir); log.info("loading shared library: " + f.getAbsolutePath()); loader.addToClassLoader(libDir, null, false); loader.reloadLuceneSPI(); @@ -1077,7 +1004,7 @@ public class CoreContainer } public File getConfigFile() { - return configFile; + return new File(solrHome, ConfigSolr.SOLR_XML_FILE); } /** @@ -1091,7 +1018,7 @@ public class CoreContainer /** Persists the cores config file in cores.xml. */ @Deprecated public void persist() { - persistFile(configFile); + persistFile(getConfigFile()); } /** @@ -1112,7 +1039,7 @@ public class CoreContainer // only the old solrxml persists if (cfg != null && !(cfg instanceof ConfigSolrXmlOld)) return; - log.info("Persisting cores config to " + (file == null ? configFile : file)); + log.info("Persisting cores config to " + (file == null ? getConfigFile() : file)); // Map rootSolrAttribs = new HashMap(); @@ -1186,7 +1113,7 @@ public class CoreContainer cfg.get(ConfigSolr.CfgProp.SOLR_SHARDHANDLERFACTORY_SOCKETTIMEOUT, null)); try { - solrCores.persistCores(origCfg, containerProperties, rootSolrAttribs,coresAttribs, + solrCores.persistCores(cfg.config.getOriginalConfig(), containerProperties, rootSolrAttribs,coresAttribs, loggingAttribs, watcherAttribs, shardHandlerAttrib, shardHandlerProps, file, loader); } catch (XPathExpressionException e) { throw new SolrException(ErrorCode.SERVER_ERROR, null, e); @@ -1262,14 +1189,7 @@ public class CoreContainer return solrCores.getCoreToOrigName(core); } - private Document copyDoc(Document document) throws TransformerException { - TransformerFactory tfactory = TransformerFactory.newInstance(); - Transformer tx = tfactory.newTransformer(); - DOMSource source = new DOMSource(document); - DOMResult result = new DOMResult(); - tx.transform(source,result); - return (Document)result.getNode(); - } + } class CloserThread extends Thread { diff --git a/solr/core/src/java/org/apache/solr/core/SolrCores.java b/solr/core/src/java/org/apache/solr/core/SolrCores.java index 6552fb9ae6d..5d397966541 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrCores.java +++ b/solr/core/src/java/org/apache/solr/core/SolrCores.java @@ -17,6 +17,15 @@ package org.apache.solr.core; * limitations under the License. */ +import org.apache.commons.lang.StringUtils; +import org.apache.solr.cloud.CloudDescriptor; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.params.CoreAdminParams; +import org.apache.solr.core.SolrXMLSerializer.SolrCoreXMLDef; +import org.apache.solr.util.DOMUtil; +import org.w3c.dom.Node; + +import javax.xml.xpath.XPathExpressionException; import java.io.File; import java.util.ArrayList; import java.util.Arrays; @@ -31,16 +40,6 @@ import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; -import javax.xml.xpath.XPathExpressionException; - -import org.apache.commons.lang.StringUtils; -import org.apache.solr.cloud.CloudDescriptor; -import org.apache.solr.common.SolrException; -import org.apache.solr.common.params.CoreAdminParams; -import org.apache.solr.core.SolrXMLSerializer.SolrCoreXMLDef; -import org.apache.solr.util.DOMUtil; -import org.w3c.dom.Node; - class SolrCores { private static SolrXMLSerializer SOLR_XML_SERIALIZER = new SolrXMLSerializer(); @@ -498,7 +497,7 @@ class SolrCores { // Insure instdir is persisted if it's the default since it's checked at startup even if not specified on the // create command. if (! dcore.getCreatedProperties().containsKey(CoreDescriptor.CORE_INSTDIR)) { - coreAttribs.put(CoreDescriptor.CORE_INSTDIR, dcore.getProperty(CoreDescriptor.CORE_INSTDIR)); + coreAttribs.put(CoreDescriptor.CORE_INSTDIR, dcore.getRawInstanceDir()); } } else { @@ -519,7 +518,7 @@ class SolrCores { } coreAttribs.put(CoreDescriptor.CORE_NAME, coreName); - + //coreAttribs.put(CoreDescriptor.CORE_INSTDIR, dcore.getRawInstanceDir()); addCoreProperty(coreAttribs, loader, node, CoreDescriptor.CORE_INSTDIR, dcore.getRawInstanceDir(), null); addCoreProperty(coreAttribs, loader, node, CoreDescriptor.CORE_COLLECTION, diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java index a6d9b2bbed1..a3dd5acbe1d 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java +++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java @@ -17,56 +17,58 @@ package org.apache.solr.core; +import org.apache.lucene.analysis.util.CharFilterFactory; +import org.apache.lucene.analysis.util.ResourceLoaderAware; +import org.apache.lucene.analysis.util.TokenFilterFactory; +import org.apache.lucene.analysis.util.TokenizerFactory; +import org.apache.lucene.analysis.util.WordlistLoader; +import org.apache.lucene.codecs.Codec; +import org.apache.lucene.codecs.DocValuesFormat; +import org.apache.lucene.codecs.PostingsFormat; +import org.apache.lucene.util.IOUtils; +import org.apache.solr.common.ResourceLoader; +import org.apache.solr.common.SolrException; +import org.apache.solr.handler.admin.CoreAdminHandler; +import org.apache.solr.handler.component.SearchComponent; +import org.apache.solr.handler.component.ShardHandlerFactory; +import org.apache.solr.request.SolrRequestHandler; +import org.apache.solr.response.QueryResponseWriter; +import org.apache.solr.schema.FieldType; +import org.apache.solr.schema.ManagedIndexSchemaFactory; +import org.apache.solr.schema.SimilarityFactory; +import org.apache.solr.search.QParserPlugin; +import org.apache.solr.update.processor.UpdateRequestProcessorFactory; +import org.apache.solr.util.FileUtils; +import org.apache.solr.util.plugin.SolrCoreAware; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.naming.NoInitialContextException; import java.io.Closeable; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Constructor; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.util.*; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.lucene.analysis.util.CharFilterFactory; -import org.apache.lucene.analysis.util.ResourceLoaderAware; -import org.apache.lucene.analysis.util.TokenFilterFactory; -import org.apache.lucene.analysis.util.TokenizerFactory; -import org.apache.lucene.codecs.Codec; -import org.apache.lucene.codecs.PostingsFormat; -import org.apache.lucene.codecs.DocValuesFormat; -import org.apache.lucene.util.IOUtils; -import org.apache.lucene.analysis.util.WordlistLoader; -import org.apache.solr.common.ResourceLoader; -import org.apache.solr.handler.admin.CoreAdminHandler; -import org.apache.solr.handler.component.ShardHandlerFactory; -import org.apache.solr.schema.ManagedIndexSchemaFactory; -import org.apache.solr.schema.SimilarityFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.lang.reflect.Constructor; - -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.naming.NoInitialContextException; - -import org.apache.solr.util.FileUtils; -import org.apache.solr.common.SolrException; -import org.apache.solr.handler.component.SearchComponent; -import org.apache.solr.request.SolrRequestHandler; -import org.apache.solr.response.QueryResponseWriter; -import org.apache.solr.schema.FieldType; -import org.apache.solr.update.processor.UpdateRequestProcessorFactory; -import org.apache.solr.util.plugin.SolrCoreAware; -import org.apache.solr.search.QParserPlugin; - /** * @since solr 1.3 */ diff --git a/solr/core/src/java/org/apache/solr/core/SolrXMLSerializer.java b/solr/core/src/java/org/apache/solr/core/SolrXMLSerializer.java index 853889b9d53..43760e07998 100644 --- a/solr/core/src/java/org/apache/solr/core/SolrXMLSerializer.java +++ b/solr/core/src/java/org/apache/solr/core/SolrXMLSerializer.java @@ -17,6 +17,11 @@ package org.apache.solr.core; * limitations under the License. */ +import org.apache.solr.common.SolrException; +import org.apache.solr.common.util.XML; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; @@ -30,11 +35,6 @@ import java.util.Map; import java.util.Properties; import java.util.Set; -import org.apache.solr.common.SolrException; -import org.apache.solr.common.util.XML; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - public class SolrXMLSerializer { protected static Logger log = LoggerFactory .getLogger(SolrXMLSerializer.class); @@ -151,7 +151,7 @@ public class SolrXMLSerializer { } void persistFile(File file, SolrXMLDef solrXMLDef) { - log.info("Persisting cores config to " + file); + log.info("Persisting cores config to " + file.getAbsolutePath()); File tmpFile = null; try { diff --git a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java index 0432a124541..3010d48c41a 100644 --- a/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java +++ b/solr/core/src/java/org/apache/solr/handler/admin/CoreAdminHandler.java @@ -17,19 +17,6 @@ package org.apache.solr.handler.admin; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.Future; - import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.lucene.index.DirectoryReader; @@ -57,8 +44,8 @@ import org.apache.solr.core.CloseHook; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.CoreDescriptor; import org.apache.solr.core.DirectoryFactory; -import org.apache.solr.core.SolrCore; import org.apache.solr.core.DirectoryFactory.DirContext; +import org.apache.solr.core.SolrCore; import org.apache.solr.handler.RequestHandlerBase; import org.apache.solr.request.LocalSolrQueryRequest; import org.apache.solr.request.SolrQueryRequest; @@ -75,6 +62,19 @@ import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Future; + /** * * @since solr 1.3 @@ -732,7 +732,9 @@ public class CoreAdminHandler extends RequestHandlerBase { boolean doPersist = false; String fileName = params.get(CoreAdminParams.FILE); if (fileName != null) { - File file = new File(coreContainer.getConfigFile().getParentFile(), fileName); + File file = new File(fileName); + if (!file.isAbsolute()) + file = new File(coreContainer.getConfigFile().getParentFile(), fileName); coreContainer.persistFile(file); rsp.add("saved", file.getAbsolutePath()); doPersist = false; diff --git a/solr/core/src/java/org/apache/solr/logging/LogWatcher.java b/solr/core/src/java/org/apache/solr/logging/LogWatcher.java index 16b5276b456..d2a23bdabc2 100644 --- a/solr/core/src/java/org/apache/solr/logging/LogWatcher.java +++ b/solr/core/src/java/org/apache/solr/logging/LogWatcher.java @@ -137,7 +137,7 @@ public abstract class LogWatcher { v.size = config.getInt(ConfigSolr.CfgProp.SOLR_LOGGING_WATCHER_SIZE, 50); v.threshold = config.get(ConfigSolr.CfgProp.SOLR_LOGGING_WATCHER_THRESHOLD, null); if (v.size > 0) { - log.info("Registering Log Listener"); + log.info("Registering Log Listener [{}]", logWatcher.getName()); logWatcher.registerListener(v); } } diff --git a/solr/core/src/java/org/apache/solr/schema/FieldType.java b/solr/core/src/java/org/apache/solr/schema/FieldType.java index 8acddaa4f89..5d03d383060 100644 --- a/solr/core/src/java/org/apache/solr/schema/FieldType.java +++ b/solr/core/src/java/org/apache/solr/schema/FieldType.java @@ -17,18 +17,6 @@ package org.apache.solr.schema; -import java.io.IOException; -import java.io.Reader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import static org.apache.lucene.analysis.util.AbstractAnalysisFactory.LUCENE_MATCH_VERSION_PARAM; - import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.Tokenizer; import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; @@ -67,6 +55,18 @@ import org.apache.solr.search.Sorting; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.io.Reader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.apache.lucene.analysis.util.AbstractAnalysisFactory.LUCENE_MATCH_VERSION_PARAM; + /** * Base class for all field types used by an index schema. * @@ -799,6 +799,15 @@ public abstract class FieldType extends FieldProperties { namedPropertyValues.add(getPropertyName(TOKENIZED), isTokenized()); // The BINARY property is always false // namedPropertyValues.add(getPropertyName(BINARY), hasProperty(BINARY)); + if (null != getSimilarityFactory()) { + namedPropertyValues.add(SIMILARITY, getSimilarityFactory().getNamedPropertyValues()); + } + if (null != getPostingsFormat()) { + namedPropertyValues.add(POSTINGS_FORMAT, getPostingsFormat()); + } + if (null != getDocValuesFormat()) { + namedPropertyValues.add(DOC_VALUES_FORMAT, getDocValuesFormat()); + } } else { // Don't show defaults Set fieldProperties = new HashSet(); for (String propertyName : FieldProperties.propertyNames) { @@ -826,15 +835,7 @@ public abstract class FieldType extends FieldProperties { namedPropertyValues.add(MULTI_TERM_ANALYZER, getAnalyzerProperties(((TextField) this).getMultiTermAnalyzer())); } } - if (null != getSimilarityFactory()) { - namedPropertyValues.add(SIMILARITY, getSimilarityFactory().getNamedPropertyValues()); - } - if (null != getPostingsFormat()) { - namedPropertyValues.add(POSTINGS_FORMAT, getPostingsFormat()); - } - if (null != getDocValuesFormat()) { - namedPropertyValues.add(DOC_VALUES_FORMAT, getDocValuesFormat()); - } + return namedPropertyValues; } diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java index 2be1fe2f1b3..72067fd79f4 100644 --- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java +++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java @@ -17,35 +17,6 @@ package org.apache.solr.servlet; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.net.HttpURLConnection; -import java.net.URL; -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.WeakHashMap; - -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - import org.apache.commons.io.IOUtils; import org.apache.solr.common.SolrException; import org.apache.solr.common.SolrException.ErrorCode; @@ -81,6 +52,34 @@ import org.apache.solr.util.FastWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; +import java.util.WeakHashMap; + /** * This filter looks at the incoming URL maps them to handlers defined in solrconfig.xml * @@ -116,12 +115,11 @@ public class SolrDispatchFilter implements Filter { log.info("SolrDispatchFilter.init()"); - CoreContainer.Initializer init = createInitializer(); try { // web.xml configuration this.pathPrefix = config.getInitParameter( "path-prefix" ); - this.cores = init.initialize(); + this.cores = createCoreContainer(); log.info("user.dir=" + System.getProperty("user.dir")); } catch( Throwable t ) { @@ -132,15 +130,20 @@ public class SolrDispatchFilter implements Filter log.info("SolrDispatchFilter.init() done"); } + + /** + * Override this to change CoreContainer initialization + * @return a CoreContainer to hold this server's cores + */ + protected CoreContainer createCoreContainer() { + CoreContainer cores = new CoreContainer(); + cores.load(); + return cores; + } public CoreContainer getCores() { return cores; } - - /** Method to override to change how CoreContainer initialization is performed. */ - protected CoreContainer.Initializer createInitializer() { - return new CoreContainer.Initializer(); - } @Override public void destroy() { diff --git a/solr/core/src/test/org/apache/solr/cloud/ClusterStateUpdateTest.java b/solr/core/src/test/org/apache/solr/cloud/ClusterStateUpdateTest.java index 433d5c39b21..7bc0789c617 100644 --- a/solr/core/src/test/org/apache/solr/cloud/ClusterStateUpdateTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/ClusterStateUpdateTest.java @@ -17,11 +17,6 @@ package org.apache.solr.cloud; * limitations under the License. */ -import java.io.File; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; - import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.cloud.ClusterState; @@ -31,7 +26,6 @@ import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.cloud.ZkNodeProps; import org.apache.solr.common.cloud.ZkStateReader; import org.apache.solr.core.CoreContainer; -import org.apache.solr.core.CoreContainer.Initializer; import org.apache.solr.core.CoreDescriptor; import org.apache.solr.core.SolrCore; import org.apache.zookeeper.CreateMode; @@ -41,6 +35,11 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + @Slow public class ClusterStateUpdateTest extends SolrTestCaseJ4 { protected static Logger log = LoggerFactory @@ -65,8 +64,6 @@ public class ClusterStateUpdateTest extends SolrTestCaseJ4 { private File dataDir3; private File dataDir4; - - private Initializer init2; @BeforeClass public static void beforeClass() { @@ -113,22 +110,21 @@ public class ClusterStateUpdateTest extends SolrTestCaseJ4 { System.setProperty("solr.solr.home", TEST_HOME()); System.setProperty("hostPort", "1661"); - CoreContainer.Initializer init1 = new CoreContainer.Initializer(); System.setProperty("solr.data.dir", ClusterStateUpdateTest.this.dataDir1.getAbsolutePath()); - container1 = init1.initialize(); + container1 = new CoreContainer(); + container1.load(); System.clearProperty("hostPort"); System.setProperty("hostPort", "1662"); - init2 = new CoreContainer.Initializer(); System.setProperty("solr.data.dir", ClusterStateUpdateTest.this.dataDir2.getAbsolutePath()); - container2 = init2.initialize(); + container2 = new CoreContainer(); + container2.load(); System.clearProperty("hostPort"); System.setProperty("hostPort", "1663"); - CoreContainer.Initializer init3 = new CoreContainer.Initializer(); - System.setProperty("solr.data.dir", ClusterStateUpdateTest.this.dataDir3.getAbsolutePath()); - container3 = init3.initialize(); + container3 = new CoreContainer(); + container3.load(); System.clearProperty("hostPort"); System.clearProperty("solr.solr.home"); @@ -226,7 +222,11 @@ public class ClusterStateUpdateTest extends SolrTestCaseJ4 { .disconnect(); container2.shutdown(); - container2 = init2.initialize(); + System.setProperty("hostPort", "1662"); + System.setProperty("solr.data.dir", ClusterStateUpdateTest.this.dataDir2.getAbsolutePath()); + container2 = new CoreContainer(); + container2.load(); + System.clearProperty("hostPort"); // pause for watch to trigger for(int i = 0; i < 200; i++) { diff --git a/solr/core/src/test/org/apache/solr/cloud/LeaderElectionIntegrationTest.java b/solr/core/src/test/org/apache/solr/cloud/LeaderElectionIntegrationTest.java index 4658b1dcb75..acaa0c7257e 100644 --- a/solr/core/src/test/org/apache/solr/cloud/LeaderElectionIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/LeaderElectionIntegrationTest.java @@ -17,6 +17,20 @@ package org.apache.solr.cloud; * limitations under the License. */ +import org.apache.lucene.util.LuceneTestCase.Slow; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.cloud.SolrZkClient; +import org.apache.solr.common.cloud.ZkNodeProps; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.core.CoreContainer; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -27,22 +41,6 @@ import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.lucene.util.LuceneTestCase.Slow; -import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.common.cloud.SolrZkClient; -import org.apache.solr.common.cloud.ZkNodeProps; -import org.apache.solr.common.cloud.ZkStateReader; -import org.apache.solr.core.CoreContainer; -import org.apache.solr.core.CoreContainer.Initializer; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; - @Slow public class LeaderElectionIntegrationTest extends SolrTestCaseJ4 { protected static Logger log = LoggerFactory @@ -140,7 +138,6 @@ public class LeaderElectionIntegrationTest extends SolrTestCaseJ4 { System.setProperty("hostPort", Integer.toString(port)); System.setProperty("shard", shard); - Initializer init = new CoreContainer.Initializer(); System.setProperty("solr.data.dir", data.getAbsolutePath()); System.setProperty("solr.solr.home", TEST_HOME()); Set ports = shardPorts.get(shard); @@ -149,7 +146,8 @@ public class LeaderElectionIntegrationTest extends SolrTestCaseJ4 { shardPorts.put(shard, ports); } ports.add(port); - CoreContainer container = init.initialize(); + CoreContainer container = new CoreContainer(); + container.load(); assertTrue("Container " + port + " has no cores!", container.getCores() .size() > 0); containerMap.put(port, container); diff --git a/solr/core/src/test/org/apache/solr/cloud/SliceStateUpdateTest.java b/solr/core/src/test/org/apache/solr/cloud/SliceStateUpdateTest.java index 1dfb4229620..67bd7f25a15 100644 --- a/solr/core/src/test/org/apache/solr/cloud/SliceStateUpdateTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/SliceStateUpdateTest.java @@ -28,7 +28,6 @@ import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.cloud.ZkNodeProps; import org.apache.solr.common.cloud.ZkStateReader; import org.apache.solr.core.CoreContainer; -import org.apache.solr.core.CoreContainer.Initializer; import org.apache.zookeeper.CreateMode; import org.junit.AfterClass; import org.junit.BeforeClass; @@ -62,8 +61,6 @@ public class SliceStateUpdateTest extends SolrTestCaseJ4 { private File dataDir3; - private Initializer init2; - @BeforeClass public static void beforeClass() { System.setProperty("solrcloud.skip.autorecovery", "true"); @@ -117,20 +114,21 @@ public class SliceStateUpdateTest extends SolrTestCaseJ4 { System.setProperty("solr.solr.home", TEST_HOME()); System.setProperty("hostPort", "1661"); - CoreContainer.Initializer init1 = new CoreContainer.Initializer(); System.setProperty("solr.data.dir", SliceStateUpdateTest.this.dataDir1.getAbsolutePath()); - container1 = init1.initialize(); + container1 = new CoreContainer(); System.clearProperty("hostPort"); System.setProperty("hostPort", "1662"); - init2 = new CoreContainer.Initializer(); System.setProperty("solr.data.dir", SliceStateUpdateTest.this.dataDir2.getAbsolutePath()); - container2 = init2.initialize(); + container2 = new CoreContainer(); System.clearProperty("hostPort"); System.clearProperty("solr.solr.home"); + container1.load(); + container2.load(); + log.info("####SETUP_END " + getTestName()); } diff --git a/solr/core/src/test/org/apache/solr/cloud/TestMultiCoreConfBootstrap.java b/solr/core/src/test/org/apache/solr/cloud/TestMultiCoreConfBootstrap.java index 0d9b467eca0..0e977317ee1 100644 --- a/solr/core/src/test/org/apache/solr/cloud/TestMultiCoreConfBootstrap.java +++ b/solr/core/src/test/org/apache/solr/cloud/TestMultiCoreConfBootstrap.java @@ -17,8 +17,6 @@ package org.apache.solr.cloud; -import java.io.File; - import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.core.CoreContainer; @@ -30,6 +28,8 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; + public class TestMultiCoreConfBootstrap extends SolrTestCaseJ4 { protected static Logger log = LoggerFactory.getLogger(TestMultiCoreConfBootstrap.class); protected CoreContainer cores = null; @@ -99,7 +99,7 @@ public class TestMultiCoreConfBootstrap extends SolrTestCaseJ4 { @Test public void testMultiCoreConfBootstrap() throws Exception { System.setProperty("bootstrap_conf", "true"); - cores = new CoreContainer(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); SolrZkClient zkclient = cores.getZkController().getZkClient(); // zkclient.printLayoutToStdOut(); diff --git a/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java b/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java index b28e3e2d928..b025d03e906 100644 --- a/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java +++ b/solr/core/src/test/org/apache/solr/cloud/TestZkChroot.java @@ -17,8 +17,6 @@ package org.apache.solr.cloud; * limitations under the License. */ -import java.io.File; - import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.cloud.SolrZkClient; import org.apache.solr.common.cloud.ZooKeeperException; @@ -31,6 +29,8 @@ import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.File; + public class TestZkChroot extends SolrTestCaseJ4 { protected static Logger log = LoggerFactory.getLogger(TestZkChroot.class); protected CoreContainer cores = null; @@ -91,7 +91,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { SolrZkClient zkClient2 = null; try { - cores = new CoreContainer(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); zkClient = cores.getZkController().getZkClient(); assertTrue(zkClient.exists("/clusterstate.json", true)); @@ -122,7 +122,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { AbstractZkTestCase.TIMEOUT); assertFalse("Path '" + chroot + "' should not exist before the test", zkClient.exists(chroot, true)); - cores = new CoreContainer(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); fail("There should be a zk exception, as the initial path doesn't exist"); } catch (ZooKeeperException e) { // expected @@ -150,7 +150,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { AbstractZkTestCase.TIMEOUT); assertFalse("Path '" + chroot + "' should not exist before the test", zkClient.exists(chroot, true)); - cores = new CoreContainer(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); assertTrue( "solrconfig.xml should have been uploaded to zk to the correct config directory", zkClient.exists(chroot + ZkController.CONFIGS_ZKNODE + "/" @@ -176,7 +176,7 @@ public class TestZkChroot extends SolrTestCaseJ4 { assertTrue(zkClient.exists(chroot, true)); assertFalse(zkClient.exists(chroot + "/clusterstate.json", true)); - cores = new CoreContainer(home, new File(home, "solr.xml")); + cores = CoreContainer.createAndLoad(home, new File(home, "solr.xml")); assertTrue(zkClient.exists(chroot + "/clusterstate.json", true)); } finally { if (cores != null) cores.shutdown(); diff --git a/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java b/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java index 530b4b55b31..3a31ea4e75a 100644 --- a/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java +++ b/solr/core/src/test/org/apache/solr/cloud/ZkControllerTest.java @@ -17,11 +17,6 @@ package org.apache.solr.cloud; * the License. */ -import java.io.File; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - import org.apache.lucene.util.LuceneTestCase.Slow; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.cloud.SolrZkClient; @@ -34,6 +29,11 @@ import org.apache.zookeeper.CreateMode; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + @Slow public class ZkControllerTest extends SolrTestCaseJ4 { @@ -240,12 +240,8 @@ public class ZkControllerTest extends SolrTestCaseJ4 { } private CoreContainer getCoreContainer() { - CoreContainer cc = new CoreContainer(TEMP_DIR.getAbsolutePath()) { - { - initShardHandler(); - } - }; - + CoreContainer cc = new CoreContainer(TEMP_DIR.getAbsolutePath()); + cc.load(); return cc; } diff --git a/solr/core/src/test/org/apache/solr/core/AbstractBadConfigTestBase.java b/solr/core/src/test/org/apache/solr/core/AbstractBadConfigTestBase.java index 97f294eb6b6..1ec2ff37132 100644 --- a/solr/core/src/test/org/apache/solr/core/AbstractBadConfigTestBase.java +++ b/solr/core/src/test/org/apache/solr/core/AbstractBadConfigTestBase.java @@ -18,9 +18,8 @@ package org.apache.solr.core; import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.common.SolrException; -import org.apache.solr.common.SolrException.ErrorCode; +import java.util.Map; import java.util.regex.Pattern; public abstract class AbstractBadConfigTestBase extends SolrTestCaseJ4 { @@ -50,26 +49,37 @@ public abstract class AbstractBadConfigTestBase extends SolrTestCaseJ4 { ignoreException(Pattern.quote(errString)); try { + if (null == solrHome) { initCore( solrconfigFile, schemaFile ); } else { initCore( solrconfigFile, schemaFile, solrHome ); } - } catch (Exception e) { - for (Throwable t = e; t != null; t = t.getCause()) { - // short circuit out if we found what we expected - if (t.getMessage() != null && -1 != t.getMessage().indexOf(errString)) return; - } - // otherwise, rethrow it, possibly completley unrelated - throw new SolrException - (ErrorCode.SERVER_ERROR, - "Unexpected error, expected error matching: " + errString, e); - } finally { + CoreContainer cc = h.getCoreContainer(); + for (Map.Entry entry : cc.getCoreInitFailures().entrySet()) { + if (matches(entry.getValue(), errString)) + return; + } + } + catch (Exception e) { + if (matches(e, errString)) + return; + throw e; + } + finally { deleteCore(); resetExceptionIgnores(); } fail("Did not encounter any exception from: " + solrconfigFile + " using " + schemaFile); } + private static boolean matches(Exception e, String errString) { + for (Throwable t = e; t != null; t = t.getCause()) { + if (t.getMessage() != null && -1 != t.getMessage().indexOf(errString)) + return true; + } + return false; + } + } diff --git a/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java b/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java index 3b7ebf0b121..622cf4c8a10 100644 --- a/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java +++ b/solr/core/src/test/org/apache/solr/core/CoreContainerCoreInitFailuresTest.java @@ -17,23 +17,17 @@ package org.apache.solr.core; -import java.util.Map; -import java.util.Collection; -import java.util.regex.Pattern; - -import java.io.File; - -import org.apache.solr.common.SolrException; -import org.apache.solr.SolrTestCaseJ4; - -import org.apache.lucene.util.IOUtils; - import org.apache.commons.io.FileUtils; - +import org.apache.lucene.util.IOUtils; +import org.apache.solr.SolrTestCaseJ4; +import org.apache.solr.common.SolrException; +import org.junit.After; import org.xml.sax.SAXParseException; -import org.junit.Before; -import org.junit.After; +import java.io.File; +import java.util.Collection; +import java.util.Map; +import java.util.regex.Pattern; public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { @@ -44,8 +38,7 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { // would be nice to do this in an @Before method, // but junit doesn't let @Before methods have test names solrHome = new File(TEMP_DIR, this.getClass().getName() + "_" + dirSuffix); - assertTrue("Failed to mkdirs solrhome", solrHome.mkdirs()); - cc = new CoreContainer(solrHome.getAbsolutePath()); + assertTrue("Failed to mkdirs solrhome [" + solrHome + "]", solrHome.mkdirs()); } @After @@ -68,7 +61,7 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { Map failures = null; Collection cores = null; Exception fail = null; - + init("empty_flow"); // solr.xml @@ -77,7 +70,8 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { // ---- // init the CoreContainer - cc.load(solrHome.getAbsolutePath(), solrXml); + cc = new CoreContainer(solrHome.getAbsolutePath()); + cc.load(); // check that we have the cores we expect cores = cc.getCoreNames(); @@ -154,18 +148,19 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { FileUtils.getFile(solrHome, "col_ok", "conf", "solrconfig.xml")); FileUtils.copyFile(getFile("solr/collection1/conf/schema-minimal.xml"), FileUtils.getFile(solrHome, "col_ok", "conf", "schema.xml")); - + // our "bad" collection ignoreException(Pattern.quote("DummyMergePolicy")); FileUtils.copyFile(getFile("solr/collection1/conf/bad-mp-solrconfig.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml")); FileUtils.copyFile(getFile("solr/collection1/conf/schema-minimal.xml"), FileUtils.getFile(solrHome, "col_bad", "conf", "schema.xml")); - - + + // ----- // init the CoreContainer with the mix of ok/bad cores - cc.load(solrHome.getAbsolutePath(), solrXml); + cc = new CoreContainer(solrHome.getAbsolutePath()); + cc.load(); // check that we have the cores we expect cores = cc.getCoreNames(); @@ -293,8 +288,8 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { fail("corrupt solrconfig.xml failed to trigger exception from reload"); } catch (SolrException e) { assertTrue("We're supposed to have a wrapped SAXParserException here, but we don't", - e.getCause() instanceof SAXParseException); - SAXParseException se = (SAXParseException)e.getCause(); + e.getCause().getCause() instanceof SAXParseException); + SAXParseException se = (SAXParseException)e.getCause().getCause(); assertTrue("reload exception doesn't refer to slrconfig.xml " + se.getSystemId(), 0 < se.getSystemId().indexOf("solrconfig.xml")); @@ -318,9 +313,9 @@ public class CoreContainerCoreInitFailuresTest extends SolrTestCaseJ4 { fail = failures.get("col_bad"); assertNotNull("null failure for test core", fail); assertTrue("init failure isn't SAXParseException", - fail instanceof SAXParseException); + fail.getCause() instanceof SAXParseException); assertTrue("init failure doesn't mention problem: " + fail.toString(), - 0 < ((SAXParseException)fail).getSystemId().indexOf("solrconfig.xml")); + 0 < ((SAXParseException)fail.getCause()).getSystemId().indexOf("solrconfig.xml")); // ---- // fix col_bad's config (again) and RELOAD to fix failure diff --git a/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java b/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java index e69ce2390dc..c1ec8f18040 100644 --- a/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java +++ b/solr/core/src/test/org/apache/solr/core/SolrCoreCheckLockOnStartupTest.java @@ -29,6 +29,7 @@ import org.junit.Before; import org.junit.Test; import java.io.File; +import java.util.Map; public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { @@ -62,13 +63,9 @@ public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { try { //opening a new core on the same index initCore("solrconfig-simplelock.xml", "schema.xml"); + if (checkForCoreInitException(LockObtainFailedException.class)) + return; fail("Expected " + LockObtainFailedException.class.getSimpleName()); - } catch (Throwable t) { - assertTrue(t instanceof RuntimeException); - assertNotNull(t.getCause()); - assertTrue(t.getCause() instanceof RuntimeException); - assertNotNull(t.getCause().getCause()); - assertTrue(t.getCause().getCause().toString(), t.getCause().getCause() instanceof LockObtainFailedException); } finally { indexWriter.close(); directory.close(); @@ -79,24 +76,33 @@ public class SolrCoreCheckLockOnStartupTest extends SolrTestCaseJ4 { @Test public void testNativeLockErrorOnStartup() throws Exception { - Directory directory = newFSDirectory(new File(dataDir, "index"), new NativeFSLockFactory()); + File indexDir = new File(dataDir, "index"); + log.info("Acquiring lock on {}", indexDir.getAbsolutePath()); + Directory directory = newFSDirectory(indexDir, new NativeFSLockFactory()); //creates a new IndexWriter without releasing the lock yet IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_40, null)); try { //opening a new core on the same index initCore("solrconfig-nativelock.xml", "schema.xml"); + CoreContainer cc = h.getCoreContainer(); + if (checkForCoreInitException(LockObtainFailedException.class)) + return; fail("Expected " + LockObtainFailedException.class.getSimpleName()); - } catch(Throwable t) { - assertTrue(t instanceof RuntimeException); - assertNotNull(t.getCause()); - assertTrue(t.getCause() instanceof RuntimeException); - assertNotNull(t.getCause().getCause()); - assertTrue(t.getCause().getCause() instanceof LockObtainFailedException); } finally { indexWriter.close(); directory.close(); deleteCore(); } } + + private boolean checkForCoreInitException(Class clazz) { + for (Map.Entry entry : h.getCoreContainer().getCoreInitFailures().entrySet()) { + for (Throwable t = entry.getValue(); t != null; t = t.getCause()) { + if (clazz.isInstance(t)) + return true; + } + } + return false; + } } diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java b/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java index a6f0d911b4b..259566e8ff2 100644 --- a/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java +++ b/solr/core/src/test/org/apache/solr/core/TestCoreContainer.java @@ -17,6 +17,16 @@ package org.apache.solr.core; +import org.apache.commons.io.FileUtils; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util._TestUtil; +import org.apache.solr.SolrTestCaseJ4; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; @@ -27,17 +37,6 @@ import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; -import javax.xml.parsers.ParserConfigurationException; - -import org.apache.commons.io.FileUtils; -import org.apache.lucene.util.IOUtils; -import org.apache.lucene.util._TestUtil; -import org.apache.solr.SolrTestCaseJ4; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; -import org.xml.sax.SAXException; - public class TestCoreContainer extends SolrTestCaseJ4 { private static String oldSolrHome; @@ -70,9 +69,10 @@ public class TestCoreContainer extends SolrTestCaseJ4 { assertTrue("Failed to mkdirs workDir", solrHomeDirectory.mkdirs()); FileUtils.copyDirectory(new File(SolrTestCaseJ4.TEST_HOME()), solrHomeDirectory); + System.out.println("Using solrconfig from " + new File(SolrTestCaseJ4.TEST_HOME()).getAbsolutePath()); CoreContainer ret = new CoreContainer(solrHomeDirectory.getAbsolutePath()); - ret.load(solrHomeDirectory.getAbsolutePath(), new File(solrHomeDirectory, "solr.xml")); + ret.load(); return ret; } @@ -161,13 +161,13 @@ public class TestCoreContainer extends SolrTestCaseJ4 { SolrCore template = null; try { template = cores.getCore("collection1"); - instDir = template.getCoreDescriptor().getInstanceDir(); + instDir = template.getCoreDescriptor().getRawInstanceDir(); } finally { if (null != template) template.close(); } } - final File instDirFile = new File(instDir); + final File instDirFile = new File(cores.getSolrHome(), instDir); assertTrue("instDir doesn't exist: " + instDir, instDirFile.exists()); // sanity check the basic persistence of the default init @@ -262,14 +262,8 @@ public class TestCoreContainer extends SolrTestCaseJ4 { File solrHomeDirectory = new File(TEMP_DIR, this.getClass().getName() + "_noCores"); SetUpHome(solrHomeDirectory, EMPTY_SOLR_XML); - CoreContainer.Initializer init = new CoreContainer.Initializer(); - CoreContainer cores = null; - try { - cores = init.initialize(); - } - catch(Exception e) { - fail("CoreContainer not created" + e.getMessage()); - } + CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath()); + cores.load(); try { //assert zero cores assertEquals("There should not be cores", 0, cores.getCores().size()); @@ -362,24 +356,21 @@ public class TestCoreContainer extends SolrTestCaseJ4 { FileUtils.writeStringToFile(new File(tmpRoot, "explicit-lib-solr.xml"), "", "UTF-8"); FileUtils.writeStringToFile(new File(tmpRoot, "custom-lib-solr.xml"), "", "UTF-8"); - final CoreContainer cc1 = new CoreContainer(tmpRoot.getAbsolutePath()); - cc1.load(tmpRoot.getAbsolutePath(), new File(tmpRoot, "default-lib-solr.xml")); + final CoreContainer cc1 = CoreContainer.createAndLoad(tmpRoot.getAbsolutePath(), new File(tmpRoot, "default-lib-solr.xml")); try { cc1.loader.openResource("defaultSharedLibFile").close(); } finally { cc1.shutdown(); } - final CoreContainer cc2 = new CoreContainer(tmpRoot.getAbsolutePath()); - cc2.load(tmpRoot.getAbsolutePath(), new File(tmpRoot, "explicit-lib-solr.xml")); + final CoreContainer cc2 = CoreContainer.createAndLoad(tmpRoot.getAbsolutePath(), new File(tmpRoot, "explicit-lib-solr.xml")); try { cc2.loader.openResource("defaultSharedLibFile").close(); } finally { cc2.shutdown(); } - final CoreContainer cc3 = new CoreContainer(tmpRoot.getAbsolutePath()); - cc3.load(tmpRoot.getAbsolutePath(), new File(tmpRoot, "custom-lib-solr.xml")); + final CoreContainer cc3 = CoreContainer.createAndLoad(tmpRoot.getAbsolutePath(), new File(tmpRoot, "custom-lib-solr.xml")); try { cc3.loader.openResource("customSharedLibFile").close(); } finally { diff --git a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java index 6e9a67651d7..a85630d042c 100644 --- a/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java +++ b/solr/core/src/test/org/apache/solr/core/TestCoreDiscovery.java @@ -17,10 +17,6 @@ package org.apache.solr.core; * limitations under the License. */ -import java.io.File; -import java.io.FileOutputStream; -import java.util.Properties; - import org.apache.commons.io.FileUtils; import org.apache.lucene.util.IOUtils; import org.apache.solr.SolrTestCaseJ4; @@ -29,6 +25,10 @@ import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; +import java.io.FileOutputStream; +import java.util.Properties; + public class TestCoreDiscovery extends SolrTestCaseJ4 { @BeforeClass @@ -106,11 +106,8 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 { } private CoreContainer init() throws Exception { - - CoreContainer.Initializer init = new CoreContainer.Initializer(); - - final CoreContainer cores = init.initialize(); - cores.setPersistent(false); + final CoreContainer cores = new CoreContainer(); + cores.load(); return cores; } @@ -183,10 +180,12 @@ public class TestCoreDiscovery extends SolrTestCaseJ4 { cc = init(); fail("Should have thrown exception in testDuplicateNames"); } catch (SolrException se) { + Throwable cause = se.getCause(); + String message = cause.getMessage(); assertTrue("Should have seen an exception because two cores had the same name", - "Core + desc.getName() + \" defined twice".indexOf(se.getMessage()) != -1); - assertTrue("/core1 should have been mentioned in the message", "/core1".indexOf(se.getMessage()) != -1); - assertTrue("/core2 should have been mentioned in the message", "/core2".indexOf(se.getMessage()) != -1); + message.indexOf("Core core1 defined more than once") != -1); + assertTrue("/core1 should have been mentioned in the message", message.indexOf("/core1") != -1); + assertTrue("/core2 should have been mentioned in the message", message.indexOf("/core2") != -1); } finally { if (cc != null) { cc.shutdown(); diff --git a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java index d4de5f13bb9..e4c2105d605 100644 --- a/solr/core/src/test/org/apache/solr/core/TestLazyCores.java +++ b/solr/core/src/test/org/apache/solr/core/TestLazyCores.java @@ -66,7 +66,7 @@ public class TestLazyCores extends SolrTestCaseJ4 { File solrXml = new File(solrHomeDirectory, "solr.xml"); FileUtils.write(solrXml, LOTS_SOLR_XML, IOUtils.CHARSET_UTF_8.toString()); final CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath()); - cores.load(solrHomeDirectory.getAbsolutePath(), solrXml); + cores.load(); // h.getCoreContainer().load(solrHomeDirectory.getAbsolutePath(), new File(solrHomeDirectory, "solr.xml")); cores.setPersistent(false); diff --git a/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java b/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java index d2e03329a5d..0f5fbca2312 100644 --- a/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java +++ b/solr/core/src/test/org/apache/solr/core/TestShardHandlerFactory.java @@ -29,8 +29,7 @@ import java.io.File; public class TestShardHandlerFactory extends SolrTestCaseJ4 { public void testXML() throws Exception { - CoreContainer cc = new CoreContainer(TEST_HOME()); - cc.load(TEST_HOME(), new File(TEST_HOME(), "solr-shardhandler.xml")); + CoreContainer cc = CoreContainer.createAndLoad(TEST_HOME(), new File(TEST_HOME(), "solr-shardhandler.xml")); ShardHandlerFactory factory = cc.getShardHandlerFactory(); assertTrue(factory instanceof MockShardHandlerFactory); NamedList args = ((MockShardHandlerFactory)factory).args; diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java index 09cac4f5503..aa51aa895d3 100644 --- a/solr/core/src/test/org/apache/solr/core/TestSolrXml.java +++ b/solr/core/src/test/org/apache/solr/core/TestSolrXml.java @@ -44,7 +44,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { boolean oldStyle = (config.getNode("solr/cores", false) != null); ConfigSolr cfg; if (oldStyle) { - cfg = new ConfigSolrXmlOld(config, cc); + cfg = new ConfigSolrXmlOld(config); } else { cfg = new ConfigSolrXml(config, cc); } @@ -103,7 +103,7 @@ public class TestSolrXml extends SolrTestCaseJ4 { boolean oldStyle = (config.getNode("solr/cores", false) != null); ConfigSolr cfg; if (oldStyle) { - cfg = new ConfigSolrXmlOld(config, cc); + cfg = new ConfigSolrXmlOld(config); } else { cfg = new ConfigSolrXml(config, cc); } diff --git a/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java b/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java index 8136c4bd008..1a7210ad811 100644 --- a/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java +++ b/solr/core/src/test/org/apache/solr/core/TestSolrXmlPersistence.java @@ -25,7 +25,6 @@ import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.params.CoreAdminParams; import org.apache.solr.handler.admin.CoreAdminHandler; import org.apache.solr.response.SolrQueryResponse; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.RuleChain; @@ -46,12 +45,14 @@ import java.util.List; public class TestSolrXmlPersistence extends SolrTestCaseJ4 { - private final File solrHomeDirectory = new File(TEMP_DIR, this.getClass().getName()); + private File solrHomeDirectory = new File(TEMP_DIR, this.getClass().getName()); + /* @BeforeClass public static void beforeClass() throws Exception { initCore("solrconfig-minimal.xml", "schema-tiny.xml"); } + */ @Rule public TestRule solrTestRules = @@ -59,9 +60,9 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 { private CoreContainer init(String solrXmlString, String... subDirs) throws Exception { - if (solrHomeDirectory.exists()) { - FileUtils.deleteDirectory(solrHomeDirectory); - } + + createTempDir(); + solrHomeDirectory = dataDir; for (String s : subDirs) { copyMinConf(new File(solrHomeDirectory, s)); @@ -69,10 +70,8 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 { File solrXml = new File(solrHomeDirectory, "solr.xml"); FileUtils.write(solrXml, solrXmlString, IOUtils.CHARSET_UTF_8.toString()); - final CoreContainer cores = new CoreContainer(solrHomeDirectory.getAbsolutePath()); - cores.load(solrHomeDirectory.getAbsolutePath(), solrXml); - cores.setPersistent(false); + final CoreContainer cores = createCoreContainer(solrHomeDirectory.getAbsolutePath(), solrXmlString); return cores; } @@ -304,7 +303,7 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 { } else if (persistList[idx].contains("@config='solrconfig.xml'")) { expressions[idx] = persistList[idx].replace("solrconfig.xml", "${solrconfig:solrconfig.xml}"); } else if (persistList[idx].contains("@instanceDir=")) { - expressions[idx] = persistList[idx].replaceFirst("instanceDir\\='.*?'", "instanceDir='" + which + "'"); + expressions[idx] = persistList[idx].replaceFirst("instanceDir\\='.*?'", "instanceDir='" + which + "/'"); } else { expressions[idx] = persistList[idx]; } @@ -611,14 +610,14 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 { " shareSchema=\"${shareSchema:false}\" distribUpdateConnTimeout=\"${distribUpdateConnTimeout:15000}\" \n" + " distribUpdateSoTimeout=\"${distribUpdateSoTimeout:120000}\" \n" + " leaderVoteWait=\"${leadVoteWait:32}\" managementPath=\"${manpath:/var/lib/path}\" transientCacheSize=\"${tranSize:128}\"> \n" + - " \n" + " \n" + - " \n" + " \n" + @@ -632,7 +631,7 @@ public class TestSolrXmlPersistence extends SolrTestCaseJ4 { private static String SOLR_XML_MINIMAL = "\n" + " \n" + - " \n" + + " \n" + " \n" + ""; diff --git a/solr/core/src/test/org/apache/solr/handler/admin/LoggingHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/LoggingHandlerTest.java index a81e0f11a4c..5eaf9ef820b 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/LoggingHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/LoggingHandlerTest.java @@ -17,15 +17,22 @@ package org.apache.solr.handler.admin; -import java.util.logging.Logger; - +import org.apache.log4j.Level; +import org.apache.log4j.Logger; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.common.params.CommonParams; -import org.apache.solr.logging.jul.JulInfo; +import org.apache.solr.logging.log4j.Log4jInfo; import org.junit.BeforeClass; import org.junit.Test; + public class LoggingHandlerTest extends SolrTestCaseJ4 { + + // TODO: This only tests Log4j at the moment, as that's what's defined + // through the CoreContainer. + + // TODO: Would be nice to throw an exception on trying to set a + // log level that doesn't exist @BeforeClass public static void beforeClass() throws Exception { @@ -35,7 +42,8 @@ public class LoggingHandlerTest extends SolrTestCaseJ4 { @Test public void testLogLevelHandlerOutput() throws Exception { Logger tst = Logger.getLogger("org.apache.solr.SolrTestCaseJ4"); - JulInfo wrap = new JulInfo(tst.getName(), tst); + tst.setLevel(Level.INFO); + Log4jInfo wrap = new Log4jInfo(tst.getName(), tst); assertQ("Show Log Levels OK", req(CommonParams.QT,"/admin/logging") @@ -46,8 +54,8 @@ public class LoggingHandlerTest extends SolrTestCaseJ4 { assertQ("Set and remove a level", req(CommonParams.QT,"/admin/logging", "set", "org.xxx.yyy.abc:null", - "set", "org.xxx.yyy.zzz:FINEST") - ,"//arr[@name='loggers']/lst/str[.='org.xxx.yyy.zzz']/../str[@name='level'][.='FINEST']" + "set", "org.xxx.yyy.zzz:TRACE") + ,"//arr[@name='loggers']/lst/str[.='org.xxx.yyy.zzz']/../str[@name='level'][.='TRACE']" ); } } diff --git a/solr/core/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java b/solr/core/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java index 5979e8418af..dcf73f660b6 100644 --- a/solr/core/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java +++ b/solr/core/src/test/org/apache/solr/handler/admin/LukeRequestHandlerTest.java @@ -17,17 +17,18 @@ package org.apache.solr.handler.admin; -import java.util.Arrays; -import java.util.EnumSet; - import org.apache.solr.common.luke.FieldFlag; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.schema.IndexSchema; import org.apache.solr.util.AbstractSolrTestCase; +import org.apache.solr.util.TestHarness; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import java.util.Arrays; +import java.util.EnumSet; + /** * :TODO: currently only tests some of the utilities in the LukeRequestHandler */ @@ -156,7 +157,7 @@ public class LukeRequestHandlerTest extends AbstractSolrTestCase { try { // First, determine that the two fields ARE there String response = h.query(req); - assertNull(h.validateXPath(response, + assertNull(TestHarness.validateXPath(response, getFieldXPathPrefix("solr_t") + "[@name='index']", getFieldXPathPrefix("solr_s") + "[@name='index']" )); @@ -165,7 +166,7 @@ public class LukeRequestHandlerTest extends AbstractSolrTestCase { for (String f : Arrays.asList("solr_ti", "solr_td", "solr_pl", "solr_dt", "solr_b")) { - assertNotNull(h.validateXPath(response, + assertNotNull(TestHarness.validateXPath(response, getFieldXPathPrefix(f) + "[@name='index']")); } @@ -175,7 +176,7 @@ public class LukeRequestHandlerTest extends AbstractSolrTestCase { for (String f : Arrays.asList("solr_t", "solr_s", "solr_ti", "solr_td", "solr_pl", "solr_dt", "solr_b")) { - assertNull(h.validateXPath(response, + assertNull(TestHarness.validateXPath(response, getFieldXPathPrefix(f) + "[@name='index']")); } } catch (Exception e) { @@ -187,7 +188,7 @@ public class LukeRequestHandlerTest extends AbstractSolrTestCase { SolrQueryRequest req = req("qt", "/admin/luke", "show", "schema"); String xml = h.query(req); - String r = h.validateXPath + String r = TestHarness.validateXPath (xml, field("text") + "/arr[@name='copySources']/str[.='title']", field("text") + "/arr[@name='copySources']/str[.='subject']", @@ -217,7 +218,7 @@ public class LukeRequestHandlerTest extends AbstractSolrTestCase { SolrQueryRequest req = req("qt", "/admin/luke", "show", "schema", "indent", "on"); String xml = h.query(req); - String result = h.validateXPath(xml, field("bday") + "/arr[@name='copyDests']/str[.='catchall_t']"); + String result = TestHarness.validateXPath(xml, field("bday") + "/arr[@name='copyDests']/str[.='catchall_t']"); assertNull(xml, result); // Put back the configuration expected by the rest of the tests in this suite diff --git a/solr/core/src/test/org/apache/solr/handler/component/BadComponentTest.java b/solr/core/src/test/org/apache/solr/handler/component/BadComponentTest.java index ae94d3cd6dc..17361e0eb2f 100644 --- a/solr/core/src/test/org/apache/solr/handler/component/BadComponentTest.java +++ b/solr/core/src/test/org/apache/solr/handler/component/BadComponentTest.java @@ -19,7 +19,6 @@ package org.apache.solr.handler.component; */ import org.apache.solr.SolrTestCaseJ4; -import org.apache.solr.common.SolrException; import org.junit.Test; /** @@ -34,14 +33,7 @@ public class BadComponentTest extends SolrTestCaseJ4{ ignoreException(".*QueryElevationComponent.*"); System.setProperty("elevate.file", "foo.xml"); initCore("solrconfig-elevate.xml", "schema12.xml"); - assertTrue(false); - } catch (RuntimeException e) { - //TODO: better way of checking this? - if (e.getCause() instanceof SolrException){ - assertTrue(true); - } else { - assertTrue(false); - } + assertTrue(hasInitException("QueryElevationComponent")); } finally { System.clearProperty("elevate.file"); resetExceptionIgnores(); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/MergeIndexesExampleTestBase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/MergeIndexesExampleTestBase.java index 55feeae18f1..c16f117ecf5 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/MergeIndexesExampleTestBase.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/MergeIndexesExampleTestBase.java @@ -17,10 +17,6 @@ package org.apache.solr.client.solrj; -import java.io.File; -import java.io.IOException; -import java.util.Arrays; - import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.client.solrj.request.AbstractUpdateRequest; import org.apache.solr.client.solrj.request.CoreAdminRequest; @@ -32,9 +28,12 @@ import org.apache.solr.common.params.SolrParams; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrCore; import org.apache.solr.util.ExternalPaths; -import org.junit.AfterClass; import org.junit.BeforeClass; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + /** * Abstract base class for testing merge indexes command * @@ -42,7 +41,8 @@ import org.junit.BeforeClass; * */ public abstract class MergeIndexesExampleTestBase extends SolrExampleTestBase { - protected static CoreContainer cores; + + protected CoreContainer cores; private String saveProp; private File dataDir2; @@ -56,13 +56,12 @@ public abstract class MergeIndexesExampleTestBase extends SolrExampleTestBase { if (dataDir == null) { createTempDir(); } - cores = new CoreContainer(); } - - @AfterClass - public static void afterClass() { - cores.shutdown(); - cores = null; + + protected void setupCoreContainer() { + cores = new CoreContainer(getSolrHome()); + cores.load(); + //cores = CoreContainer.createAndLoad(getSolrHome(), new File(TEMP_DIR, "solr.xml")); } @Override @@ -71,17 +70,19 @@ public abstract class MergeIndexesExampleTestBase extends SolrExampleTestBase { System.setProperty("solr.directoryFactory", "solr.StandardDirectoryFactory"); super.setUp(); - SolrCore.log.info("CORES=" + cores + " : " + cores.getCoreNames()); - cores.setPersistent(false); - // setup datadirs - System.setProperty( "solr.core0.data.dir", SolrTestCaseJ4.dataDir.getCanonicalPath() ); - + System.setProperty( "solr.core0.data.dir", SolrTestCaseJ4.dataDir.getCanonicalPath() ); + dataDir2 = new File(TEMP_DIR, getClass().getName() + "-" + System.currentTimeMillis()); dataDir2.mkdirs(); - - System.setProperty( "solr.core1.data.dir", this.dataDir2.getCanonicalPath() ); + + System.setProperty( "solr.core1.data.dir", this.dataDir2.getCanonicalPath() ); + + setupCoreContainer(); + SolrCore.log.info("CORES=" + cores + " : " + cores.getCoreNames()); + cores.setPersistent(false); + } @Override @@ -96,6 +97,8 @@ public abstract class MergeIndexesExampleTestBase extends SolrExampleTestBase { System.err.println("!!!! WARNING: best effort to remove " + dataDir2.getAbsolutePath() + " FAILED !!!!!"); } } + + cores.shutdown(); if (saveProp == null) System.clearProperty("solr.directoryFactory"); else System.setProperty("solr.directoryFactory", saveProp); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/MultiCoreExampleTestBase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/MultiCoreExampleTestBase.java index 48fe1d955e2..15f2128f3f6 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/MultiCoreExampleTestBase.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/MultiCoreExampleTestBase.java @@ -17,8 +17,6 @@ package org.apache.solr.client.solrj; -import java.io.File; - import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION; import org.apache.solr.client.solrj.request.CoreAdminRequest; import org.apache.solr.client.solrj.request.CoreAdminRequest.Unload; @@ -30,10 +28,10 @@ import org.apache.solr.common.util.NamedList; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrCore; import org.apache.solr.util.ExternalPaths; -import org.junit.AfterClass; -import org.junit.BeforeClass; import org.junit.Test; +import java.io.File; + /** * @@ -41,30 +39,21 @@ import org.junit.Test; */ public abstract class MultiCoreExampleTestBase extends SolrExampleTestBase { - protected static CoreContainer cores; + protected CoreContainer cores; private File dataDir2; private File dataDir1; @Override public String getSolrHome() { return ExternalPaths.EXAMPLE_MULTICORE_HOME; } - - @BeforeClass - public static void beforeThisClass2() throws Exception { + protected void setupCoreContainer() { cores = new CoreContainer(); - } - - @AfterClass - public static void afterClass() { - cores.shutdown(); + cores.load(); } @Override public void setUp() throws Exception { super.setUp(); - SolrCore.log.info("CORES=" + cores + " : " + cores.getCoreNames()); - cores.setPersistent(false); - dataDir1 = new File(TEMP_DIR, getClass().getName() + "-core0-" + System.currentTimeMillis()); dataDir1.mkdirs(); @@ -74,7 +63,14 @@ public abstract class MultiCoreExampleTestBase extends SolrExampleTestBase dataDir2.mkdirs(); System.setProperty( "solr.core0.data.dir", this.dataDir1.getCanonicalPath() ); - System.setProperty( "solr.core1.data.dir", this.dataDir2.getCanonicalPath() ); + System.setProperty( "solr.core1.data.dir", this.dataDir2.getCanonicalPath() ); + + setupCoreContainer(); + + SolrCore.log.info("CORES=" + cores + " : " + cores.getCoreNames()); + cores.setPersistent(false); + + } @Override @@ -89,6 +85,8 @@ public abstract class MultiCoreExampleTestBase extends SolrExampleTestBase System.err.println("!!!! WARNING: best effort to remove " + dataDir2.getAbsolutePath() + " FAILED !!!!!"); } } + + cores.shutdown(); } @Override diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java index 2b7a18b7cbf..dfa2e9aac8c 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/AbstractEmbeddedSolrServerTestCase.java @@ -51,6 +51,7 @@ public abstract class AbstractEmbeddedSolrServerTestCase extends LuceneTestCase super.setUp(); System.setProperty("solr.solr.home", SOLR_HOME.getAbsolutePath()); + System.out.println("Solr home: " + SOLR_HOME.getAbsolutePath()); //The index is always stored within a temporary directory createTempDir(); @@ -61,7 +62,8 @@ public abstract class AbstractEmbeddedSolrServerTestCase extends LuceneTestCase System.setProperty("dataDir2", dataDir2.getAbsolutePath()); System.setProperty("tempDir", tempDir.getAbsolutePath()); System.setProperty("tests.shardhandler.randomSeed", Long.toString(random().nextLong())); - cores = new CoreContainer(SOLR_HOME.getAbsolutePath(), getSolrXml()); + cores = CoreContainer.createAndLoad(SOLR_HOME.getAbsolutePath(), getSolrXml()); + cores.setPersistent(false); } protected abstract File getSolrXml() throws Exception; diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java index 52fc425fd8d..75239ab2ed1 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MergeIndexesEmbeddedTest.java @@ -17,8 +17,6 @@ package org.apache.solr.client.solrj.embedded; -import java.io.File; - import org.apache.solr.client.solrj.MergeIndexesExampleTestBase; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.core.SolrCore; @@ -36,10 +34,6 @@ public class MergeIndexesEmbeddedTest extends MergeIndexesExampleTestBase { // TODO: fix this test to use MockDirectoryFactory System.clearProperty("solr.directoryFactory"); super.setUp(); - - File home = new File(getSolrHome()); - File f = new File(home, "solr.xml"); - cores.load(getSolrHome(), f); } @Override diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MultiCoreEmbeddedTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MultiCoreEmbeddedTest.java index c3a1fb73090..427a10dfbcc 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MultiCoreEmbeddedTest.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/MultiCoreEmbeddedTest.java @@ -17,8 +17,6 @@ package org.apache.solr.client.solrj.embedded; -import java.io.File; - import org.apache.solr.client.solrj.MultiCoreExampleTestBase; import org.apache.solr.client.solrj.SolrServer; @@ -35,10 +33,6 @@ public class MultiCoreEmbeddedTest extends MultiCoreExampleTestBase { // TODO: fix this test to use MockDirectoryFactory System.clearProperty("solr.directoryFactory"); super.setUp(); - - File home = new File( getSolrHome() ); - File f = new File( home, "solr.xml" ); - cores.load( getSolrHome(), f ); } @Override diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java index 3d2282e669b..ede16607085 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/TestSolrProperties.java @@ -17,20 +17,16 @@ package org.apache.solr.client.solrj.embedded; -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStreamReader; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.xpath.*; - +import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; +import com.google.common.io.ByteStreams; import org.apache.commons.io.IOUtils; import org.apache.solr.SolrTestCaseJ4; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION; -import org.apache.solr.client.solrj.request.*; +import org.apache.solr.client.solrj.request.CoreAdminRequest; +import org.apache.solr.client.solrj.request.QueryRequest; +import org.apache.solr.client.solrj.request.UpdateRequest; import org.apache.solr.client.solrj.response.CoreAdminResponse; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.core.CoreContainer; @@ -46,7 +42,17 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Node; -import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; /** * @@ -75,6 +81,17 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { public void tearDown() throws Exception { super.tearDown(); } + + private static void dumpFile(File fileToDump) throws IOException { + System.out.println("Dumping " + fileToDump.getAbsolutePath()); + InputStream is = new FileInputStream(fileToDump); + try { + ByteStreams.copy(is, System.out); + } + finally { + IOUtils.closeQuietly(is); + } + } @Override protected File getSolrXml() throws Exception { @@ -107,6 +124,10 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { @Test public void testProperties() throws Exception { + + String persistedSolrXml = new File(tempDir, SOLR_PERSIST_XML).getAbsolutePath(); + log.info("persistedSolrXml: {}", persistedSolrXml); + UpdateRequest up = new UpdateRequest(); up.setAction(ACTION.COMMIT, true, true); up.deleteByQuery("*:*"); @@ -176,14 +197,14 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { long after = mcr.getStartTime(name).getTime(); assertTrue("should have more recent time: " + after + "," + before, after > before); - mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, coreadmin); + mcr = CoreAdminRequest.persist(persistedSolrXml, coreadmin); DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); - FileInputStream fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML)); + FileInputStream fis = new FileInputStream(new File(persistedSolrXml)); try { Document document = builder.parse(fis); fis.close(); - fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML)); + fis = new FileInputStream(new File(persistedSolrXml)); String solrPersistXml = IOUtils.toString(new InputStreamReader(fis, "UTF-8")); //System.out.println("xml:" + solrPersistXml); assertTrue("\"/solr/cores[@defaultCoreName='core0']\" doesn't match in:\n" + solrPersistXml, @@ -204,14 +225,14 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { CoreAdminRequest.renameCore(name, "renamed_core", coreadmin); - mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin()); + mcr = CoreAdminRequest.persist(persistedSolrXml, getRenamedSolrAdmin()); // fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML)); // String solrPersistXml = IOUtils.toString(fis); // System.out.println("xml:" + solrPersistXml); // fis.close(); - fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML)); + fis = new FileInputStream(new File(persistedSolrXml)); try { Document document = builder.parse(fis); assertTrue(exists("/solr/cores/core[@name='renamed_core']", document)); @@ -235,14 +256,14 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { // System.out.println("xml:" + solrPersistXml); // fis.close(); - mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin()); + mcr = CoreAdminRequest.persist(persistedSolrXml, getRenamedSolrAdmin()); // fis = new FileInputStream(new File(solrXml.getParent(), SOLR_PERSIST_XML)); // solrPersistXml = IOUtils.toString(fis); // System.out.println("xml:" + solrPersistXml); // fis.close(); - fis = new FileInputStream(new File(tempDir, SOLR_PERSIST_XML)); + fis = new FileInputStream(new File(persistedSolrXml)); try { Document document = builder.parse(fis); assertTrue(exists("/solr/cores/core[@name='collection1' and @instanceDir='.']", document)); @@ -259,9 +280,9 @@ public class TestSolrProperties extends AbstractEmbeddedSolrServerTestCase { // System.out.println("xml:" + solrPersistXml); // fis.close(); - cores = new CoreContainer(SOLR_HOME.getAbsolutePath(), new File(tempDir, SOLR_PERSIST_XML)); + cores = CoreContainer.createAndLoad(SOLR_HOME.getAbsolutePath(), new File(persistedSolrXml)); - mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin()); + //mcr = CoreAdminRequest.persist(SOLR_PERSIST_XML, getRenamedSolrAdmin()); // fis = new FileInputStream(new File(solrXml.getParent(), // SOLR_PERSIST_XML)); diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java index d1ae630dcff..681e5f9b754 100644 --- a/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java +++ b/solr/solrj/src/test/org/apache/solr/client/solrj/request/TestCoreAdmin.java @@ -17,8 +17,8 @@ package org.apache.solr.client.solrj.request; -import java.io.File; - +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; +import org.apache.commons.io.FileUtils; import org.apache.solr.SolrIgnoredThreadsFilter; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.embedded.AbstractEmbeddedSolrServerTestCase; @@ -27,14 +27,13 @@ import org.apache.solr.common.SolrException; import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.util.NamedList; import org.apache.solr.core.SolrCore; -import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.BeforeClass; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; +import java.io.File; @ThreadLeakFilters(defaultFilters = true, filters = {SolrIgnoredThreadsFilter.class}) public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase { @@ -67,6 +66,8 @@ public class TestCoreAdmin extends AbstractEmbeddedSolrServerTestCase { File tmp = new File(TEMP_DIR, "solrtest-" + getTestClass().getSimpleName() + "-" + System.currentTimeMillis()); tmp.mkdirs(); + + log.info("Creating cores underneath {}", tmp); File dataDir = new File(tmp, this.getTestName() + System.currentTimeMillis() + "-" + "data"); diff --git a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java index c2e1d35a0b4..3eda55f57cd 100755 --- a/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java +++ b/solr/test-framework/src/java/org/apache/solr/SolrTestCaseJ4.java @@ -17,26 +17,28 @@ package org.apache.solr; -import java.io.*; -import java.util.*; -import java.util.logging.*; - -import javax.xml.xpath.XPathExpressionException; - +import com.carrotsearch.randomizedtesting.RandomizedContext; +import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; +import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; import org.apache.commons.io.FileUtils; -import org.apache.lucene.util.Constants; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.QuickPatchThreadsFilter; import org.apache.solr.client.solrj.util.ClientUtils; -import org.apache.solr.common.*; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.SolrInputField; import org.apache.solr.common.cloud.SolrZkClient; -import org.apache.solr.common.params.*; +import org.apache.solr.common.params.CommonParams; +import org.apache.solr.common.params.ModifiableSolrParams; +import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.XML; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrCore; import org.apache.solr.handler.JsonUpdateRequestHandler; -import org.apache.solr.request.*; +import org.apache.solr.request.LocalSolrQueryRequest; +import org.apache.solr.request.SolrQueryRequest; +import org.apache.solr.request.SolrRequestHandler; import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.SchemaField; import org.apache.solr.search.SolrIndexSearcher; @@ -44,7 +46,10 @@ import org.apache.solr.servlet.DirectSolrConnection; import org.apache.solr.util.AbstractSolrTestCase; import org.apache.solr.util.RevertDefaultThreadHandlerRule; import org.apache.solr.util.TestHarness; -import org.junit.*; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Rule; import org.junit.rules.RuleChain; import org.junit.rules.TestRule; import org.noggit.CharArr; @@ -54,9 +59,22 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.SAXException; -import com.carrotsearch.randomizedtesting.RandomizedContext; -import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters; -import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule; +import javax.xml.xpath.XPathExpressionException; +import java.io.File; +import java.io.IOException; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; +import java.util.logging.Level; + +import static com.google.common.base.Preconditions.checkNotNull; /** * A junit4 Solr test harness that extends LuceneTestCaseJ4. To change which core is used when loading the schema and solrconfig.xml, simply @@ -405,6 +423,40 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { ("standard",0,20,CommonParams.VERSION,"2.2"); } + public static CoreContainer createCoreContainer(String solrHome, String solrXML) { + testSolrHome = checkNotNull(solrHome); + if (dataDir == null) + createTempDir(); + h = new TestHarness(solrHome, solrXML); + lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); + return h.getCoreContainer(); + } + + public static CoreContainer createDefaultCoreContainer(String solrHome) { + testSolrHome = checkNotNull(solrHome); + if (dataDir == null) + createTempDir(); + h = new TestHarness("collection1", dataDir.getAbsolutePath(), "solrconfig.xml", "schema.xml"); + lrf = h.getRequestFactory("standard", 0, 20, CommonParams.VERSION, "2.2"); + return h.getCoreContainer(); + } + + public static boolean hasInitException(String message) { + for (Map.Entry entry : h.getCoreContainer().getCoreInitFailures().entrySet()) { + if (entry.getValue().getMessage().indexOf(message) != -1) + return true; + } + return false; + } + + public static boolean hasInitException(Class exceptionType) { + for (Map.Entry entry : h.getCoreContainer().getCoreInitFailures().entrySet()) { + if (exceptionType.isAssignableFrom(entry.getValue().getClass())) + return true; + } + return false; + } + /** Subclasses that override setUp can optionally call this method * to log the fact that their setUp process has ended. */ @@ -1451,12 +1503,12 @@ public abstract class SolrTestCaseJ4 extends LuceneTestCase { return result; } - public void assertXmlFile(final File file, String... xpath) + public static void assertXmlFile(final File file, String... xpath) throws IOException, SAXException { try { String xml = FileUtils.readFileToString(file, "UTF-8"); - String results = h.validateXPath(xml, xpath); + String results = TestHarness.validateXPath(xml, xpath); if (null != results) { String msg = "File XPath failure: file=" + file.getPath() + " xpath=" + results + "\n\nxml was: " + xml; diff --git a/solr/test-framework/src/java/org/apache/solr/util/BaseTestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/BaseTestHarness.java index 0495c911161..6e478090b2e 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/BaseTestHarness.java +++ b/solr/test-framework/src/java/org/apache/solr/util/BaseTestHarness.java @@ -33,10 +33,10 @@ import java.io.StringWriter; import java.io.UnsupportedEncodingException; abstract public class BaseTestHarness { - private final ThreadLocal builderTL = new ThreadLocal(); - private final ThreadLocal xpathTL = new ThreadLocal(); + private static final ThreadLocal builderTL = new ThreadLocal(); + private static final ThreadLocal xpathTL = new ThreadLocal(); - public DocumentBuilder getXmlDocumentBuilder() { + public static DocumentBuilder getXmlDocumentBuilder() { try { DocumentBuilder builder = builderTL.get(); if (builder == null) { @@ -49,7 +49,7 @@ abstract public class BaseTestHarness { } } - public XPath getXpath() { + public static XPath getXpath() { try { XPath xpath = xpathTL.get(); if (xpath == null) { @@ -71,7 +71,7 @@ abstract public class BaseTestHarness { * @param tests Array of XPath strings to test (in boolean mode) on the xml * @return null if all good, otherwise the first test that fails. */ - public String validateXPath(String xml, String... tests) + public static String validateXPath(String xml, String... tests) throws XPathExpressionException, SAXException { if (tests==null || tests.length == 0) return null; diff --git a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java index 5a77b9f3acf..9041f0bfa57 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java +++ b/solr/test-framework/src/java/org/apache/solr/util/RestTestBase.java @@ -127,7 +127,7 @@ abstract public class RestTestBase extends SolrJettyTestBase { } */ - String results = restTestHarness.validateXPath(response, tests); + String results = TestHarness.validateXPath(response, tests); if (null != results) { String msg = "REQUEST FAILED: xpath=" + results diff --git a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java index e0c5433a9db..50cc5760fa7 100644 --- a/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java +++ b/solr/test-framework/src/java/org/apache/solr/util/TestHarness.java @@ -17,21 +17,17 @@ package org.apache.solr.util; +import com.google.common.base.Charsets; import org.apache.solr.common.SolrException; -import org.apache.solr.common.cloud.Slice; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.util.NamedList; -import org.apache.solr.core.Config; -import org.apache.solr.core.ConfigSolrXmlOld; +import org.apache.solr.common.util.NamedList.NamedListEntry; +import org.apache.solr.core.ConfigSolr; +import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrConfig; import org.apache.solr.core.SolrCore; -import org.apache.solr.core.CoreContainer; -import org.apache.solr.core.CoreDescriptor; import org.apache.solr.core.SolrResourceLoader; import org.apache.solr.handler.UpdateRequestHandler; -import org.apache.solr.logging.ListenerConfig; -import org.apache.solr.logging.LogWatcher; -import org.apache.solr.logging.jul.JulWatcher; import org.apache.solr.request.LocalSolrQueryRequest; import org.apache.solr.request.SolrQueryRequest; import org.apache.solr.request.SolrRequestHandler; @@ -41,9 +37,6 @@ import org.apache.solr.response.SolrQueryResponse; import org.apache.solr.schema.IndexSchema; import org.apache.solr.schema.IndexSchemaFactory; import org.apache.solr.servlet.DirectSolrConnection; -import org.apache.solr.common.util.NamedList.NamedListEntry; -import org.xml.sax.InputSource; -import org.xml.sax.SAXException; import java.io.ByteArrayInputStream; import java.io.File; @@ -52,8 +45,6 @@ import java.io.StringWriter; import java.util.HashMap; import java.util.Map; -import javax.xml.parsers.ParserConfigurationException; - /** * This class provides a simple harness that may be useful when @@ -107,19 +98,8 @@ public class TestHarness extends BaseTestHarness { SolrConfig solrConfig, String schemaFile) { this( coreName, dataDirectory, solrConfig, IndexSchemaFactory.buildIndexSchema(schemaFile, solrConfig)); - } - /** - * @param coreName to initialize - * @param dataDirectory path for index data, will not be cleaned up - * @param solrConfig solrconfig instance - * @param indexSchema schema instance - */ - public TestHarness( String coreName, - String dataDirectory, - SolrConfig solrConfig, - IndexSchema indexSchema) { - this(coreName, new Initializer(coreName, dataDirectory, solrConfig, indexSchema)); - } + } + /** * @param dataDirectory path for index data, will not be cleaned up * @param solrConfig solronfig instance @@ -138,18 +118,26 @@ public class TestHarness extends BaseTestHarness { public TestHarness( String dataDirectory, SolrConfig solrConfig, IndexSchema indexSchema) { - this(null, new Initializer(null, dataDirectory, solrConfig, indexSchema)); + this(CoreContainer.DEFAULT_DEFAULT_CORE_NAME, dataDirectory, solrConfig, indexSchema); } - - public TestHarness(String coreName, CoreContainer.Initializer init) { - try { - container = init.initialize(); + /** + * @param coreName to initialize + * @param dataDir path for index data, will not be cleaned up + * @param solrConfig solrconfig resource name + * @param indexSchema schema resource name + */ + public TestHarness(String coreName, String dataDir, String solrConfig, String indexSchema) { + try { if (coreName == null) coreName = CoreContainer.DEFAULT_DEFAULT_CORE_NAME; - this.coreName = coreName; + SolrResourceLoader loader = new SolrResourceLoader(SolrResourceLoader.locateSolrHome()); + ConfigSolr config = getTestHarnessConfig(coreName, dataDir, solrConfig, indexSchema); + container = new CoreContainer(loader, config); + container.load(); + updater = new UpdateRequestHandler(); updater.init( null ); } catch (Exception e) { @@ -157,81 +145,50 @@ public class TestHarness extends BaseTestHarness { } } - // Creates a container based on infos needed to create one core - static class Initializer extends CoreContainer.Initializer { - String coreName; - String dataDirectory; - SolrConfig solrConfig; - IndexSchema indexSchema; - public Initializer(String coreName, - String dataDirectory, - SolrConfig solrConfig, - IndexSchema indexSchema) { - if (coreName == null) - coreName = CoreContainer.DEFAULT_DEFAULT_CORE_NAME; - this.coreName = coreName; - this.dataDirectory = dataDirectory; - this.solrConfig = solrConfig; - this.indexSchema = indexSchema; - } - public String getCoreName() { - return coreName; - } - @Override - public CoreContainer initialize() { - CoreContainer container; - try { - String solrHome = SolrResourceLoader.locateSolrHome(); - container = new CoreContainer(new SolrResourceLoader(solrHome)) { - { - String hostPort = System.getProperty("hostPort", "8983"); - String hostContext = System.getProperty("hostContext", "solr"); - defaultCoreName = CoreContainer.DEFAULT_DEFAULT_CORE_NAME; - initShardHandler(); - zkSys.initZooKeeper(this, solrHome, System.getProperty("zkHost"), 30000, hostPort, hostContext, null, "30000", true, 30000, 30000); - ByteArrayInputStream is = new ByteArrayInputStream(ConfigSolrXmlOld.DEF_SOLR_XML.getBytes("UTF-8")); - Config config = new Config(loader, null, new InputSource(is), null, false); - cfg = new ConfigSolrXmlOld(config, this); - } - }; - } catch (ParserConfigurationException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (SAXException e) { - throw new RuntimeException(e); - } - LogWatcher logging = new JulWatcher("test"); - logging.registerListener(new ListenerConfig()); - container.setLogging(logging); - - CoreDescriptor dcore = new CoreDescriptor(container, coreName, solrConfig.getResourceLoader().getInstanceDir()); - if (container.isZooKeeperAware()) { - container.getZkController().preRegister(dcore); - } - dcore.setConfigName(solrConfig.getResourceName()); - dcore.setSchemaName(indexSchema.getResourceName()); - - if (container.getZkController() != null) { - container.preRegisterInZk(dcore); - } - - SolrCore core = new SolrCore(coreName, dataDirectory, solrConfig, indexSchema, dcore); - - if (container.isZooKeeperAware() && Slice.CONSTRUCTION.equals(dcore.getCloudDescriptor().getShardState())) { - // set update log to buffer before publishing the core - core.getUpdateHandler().getUpdateLog().bufferUpdates(); - } - - container.register(coreName, core, false); + public TestHarness(String coreName, String dataDir, SolrConfig solrConfig, IndexSchema indexSchema) { + this(coreName, dataDir, solrConfig.getResourceName(), indexSchema.getResourceName()); + } - // TODO: we should be exercising the *same* core container initialization code, not equivalent code! - if (container.getZkController() == null && core.getUpdateHandler().getUpdateLog() != null) { - // always kick off recovery if we are in standalone mode. - core.getUpdateHandler().getUpdateLog().recoverFromLog(); - } - return container; - } + /** + * Create a TestHarness using a specific solr home directory and solr xml + * @param solrHome the solr home directory + * @param solrXml a File pointing to a solr.xml configuration + */ + public TestHarness(String solrHome, String solrXml) { + this(new SolrResourceLoader(solrHome), + ConfigSolr.fromInputStream(null, new ByteArrayInputStream(solrXml.getBytes(Charsets.UTF_8)))); + } + + /** + * Create a TestHarness using a specific resource loader and config + * @param loader the SolrResourceLoader to use + * @param config the ConfigSolr to use + */ + public TestHarness(SolrResourceLoader loader, ConfigSolr config) { + container = new CoreContainer(loader, config); + container.load(); + updater = new UpdateRequestHandler(); + updater.init(null); + } + + private static ConfigSolr getTestHarnessConfig(String coreName, String dataDir, + String solrConfig, String schema) { + String solrxml = "\n" + + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + ""; + return ConfigSolr.fromString(new SolrResourceLoader(dataDir), solrxml); } public CoreContainer getCoreContainer() {