Apache Lucene open-source search software
SOLR-11037: Improved unit test.
Andrzej Bialeckic1f146d2fc19
diff --git a/solr/core/src/java/org/apache/solr/core/NodeConfig.java b/solr/core/src/java/org/apache/solr/core/NodeConfig.javaindex fb52e4fe4e4..5b4debe5e0f 100644--- a/solr/core/src/java/org/apache/solr/core/NodeConfig.java+++ b/solr/core/src/java/org/apache/solr/core/NodeConfig.java@@ -265,7 +265,10 @@ public class NodeConfig {}public NodeConfigBuilder setSolrDataHome(String solrDataHomeString) {- this.solrDataHome = loader.getInstancePath().resolve(solrDataHomeString);+ // keep it null unless explicitly set to non-empty value+ if (solrDataHomeString != null && !solrDataHomeString.isEmpty()) {+ this.solrDataHome = loader.getInstancePath().resolve(solrDataHomeString);+ }return this;}diff --git a/solr/core/src/test-files/solr/solr-solrDataHome.xml b/solr/core/src/test-files/solr/solr-solrDataHome.xmlnew file mode 100644index 00000000000..5705f933b91--- /dev/null+++ b/solr/core/src/test-files/solr/solr-solrDataHome.xml@@ -0,0 +1,24 @@+<?xml version="1.0" encoding="UTF-8" ?>+<!--+ Licensed to the Apache Software Foundation (ASF) under one or more+ contributor license agreements. See the NOTICE file distributed with+ this work for additional information regarding copyright ownership.+ The ASF licenses this file to You under the Apache License, Version 2.0+ (the "License"); you may not use this file except in compliance with+ the License. You may obtain a copy of the License at++ http://www.apache.org/licenses/LICENSE-2.0++ Unless required by applicable law or agreed to in writing, software+ distributed under the License is distributed on an "AS IS" BASIS,+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ See the License for the specific language governing permissions and+ limitations under the License.+-->++<!--+ solr.xml specifying configurable solr.data.home+-->+<solr>+ <str name="solrDataHome">${test.solr.data.home:}</str>+</solr>diff --git a/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java b/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.javaindex 18f0e870b03..869a4d2c2e2 100755--- a/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java+++ b/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java@@ -17,25 +17,36 @@package org.apache.solr.core;import java.io.IOException;+import java.io.InputStream;+import java.nio.file.Path;import java.nio.file.Paths;import java.util.Properties;import org.apache.lucene.util.LuceneTestCase;import org.apache.solr.common.util.NamedList;-import org.apache.solr.handler.admin.CoreAdminHandler;-import org.apache.solr.handler.component.HttpShardHandlerFactory;import org.junit.After;+import org.junit.AfterClass;import org.junit.Before;+import org.junit.BeforeClass;import org.junit.Test;public class DirectoryFactoryTest extends LuceneTestCase {- public void testLockTypesUnchanged() throws Exception {- assertEquals("simple", DirectoryFactory.LOCK_TYPE_SIMPLE);- assertEquals("native", DirectoryFactory.LOCK_TYPE_NATIVE);- assertEquals("single", DirectoryFactory.LOCK_TYPE_SINGLE);- assertEquals("none", DirectoryFactory.LOCK_TYPE_NONE);- assertEquals("hdfs", DirectoryFactory.LOCK_TYPE_HDFS);+ private static Path solrHome = null;+ private static SolrResourceLoader loader = null;++ @BeforeClass+ public static void setupLoader() throws Exception {+ solrHome = Paths.get(createTempDir().toAbsolutePath().toString());+ loader = new SolrResourceLoader(solrHome);+ }++ @AfterClass+ public static void cleanupLoader() throws Exception {+ if (loader != null) {+ loader.close();+ }+ loader = null;}@After@@ -43,13 +54,23 @@ public class DirectoryFactoryTest extends LuceneTestCase {public void clean() {System.clearProperty("solr.data.home");System.clearProperty("solr.solr.home");+ System.clearProperty("test.solr.data.home");+ }++ @Test+ public void testLockTypesUnchanged() throws Exception {+ assertEquals("simple", DirectoryFactory.LOCK_TYPE_SIMPLE);+ assertEquals("native", DirectoryFactory.LOCK_TYPE_NATIVE);+ assertEquals("single", DirectoryFactory.LOCK_TYPE_SINGLE);+ assertEquals("none", DirectoryFactory.LOCK_TYPE_NONE);+ assertEquals("hdfs", DirectoryFactory.LOCK_TYPE_HDFS);}@Testpublic void testGetDataHome() throws Exception {- MockCoreContainer cc = new MockCoreContainer("/solr/home");+ NodeConfig config = loadNodeConfig("/solr/solr-solrDataHome.xml");+ CoreContainer cc = new CoreContainer(config);Properties cp = cc.getContainerProperties();- boolean zkAware = cc.isZooKeeperAware();RAMDirectoryFactory rdf = new RAMDirectoryFactory();rdf.initCoreContainer(cc);rdf.init(new NamedList());@@ -65,43 +86,32 @@ public class DirectoryFactoryTest extends LuceneTestCase {// solr.data.home set with System property, and relative pathSystem.setProperty("solr.data.home", "solrdata");- cc = new MockCoreContainer("/solr/home");+ config = loadNodeConfig("/solr/solr-solrDataHome.xml");+ cc = new CoreContainer(config);rdf = new RAMDirectoryFactory();rdf.initCoreContainer(cc);rdf.init(new NamedList());- assertDataHome("/solr/home/solrdata/inst_dir/data", "inst_dir", rdf, cc);+ assertDataHome(solrHome.resolve("solrdata/inst_dir/data").toAbsolutePath().toString(), "inst_dir", rdf, cc);// Test parsing last component of instanceDir, and using custom dataDir- assertDataHome("/solr/home/solrdata/myinst/mydata", "/path/to/myinst", rdf, cc, "dataDir", "mydata");+ assertDataHome(solrHome.resolve("solrdata/myinst/mydata").toAbsolutePath().toString(), "/path/to/myinst", rdf, cc, "dataDir", "mydata");+ // solr.data.home set but also solrDataHome set in solr.xml, which should override the former+ System.setProperty("test.solr.data.home", "/foo");+ config = loadNodeConfig("/solr/solr-solrDataHome.xml");+ cc = new CoreContainer(config);+ rdf = new RAMDirectoryFactory();+ rdf.initCoreContainer(cc);+ rdf.init(new NamedList());+ assertDataHome("/foo/inst_dir/data", "inst_dir", rdf, cc);}- private void assertDataHome(String expected, String instanceDir, RAMDirectoryFactory rdf, MockCoreContainer cc, String... properties) throws IOException {+ private void assertDataHome(String expected, String instanceDir, RAMDirectoryFactory rdf, CoreContainer cc, String... properties) throws IOException {String dataHome = rdf.getDataHome(new CoreDescriptor("core_name", Paths.get(instanceDir), cc.containerProperties, cc.isZooKeeperAware(), properties));assertEquals(Paths.get(expected).toAbsolutePath(), Paths.get(dataHome).toAbsolutePath());}- private static class MockCoreContainer extends CoreContainer {-- private final String mockSolrHome;- private final NodeConfig mockConfig;-- public MockCoreContainer(String solrHome) throws IOException {- super(new Object());- mockConfig = new NodeConfig.NodeConfigBuilder("test", new SolrResourceLoader(Paths.get(solrHome))).build();- mockSolrHome = solrHome;- this.shardHandlerFactory = new HttpShardHandlerFactory();- this.coreAdminHandler = new CoreAdminHandler();- }-- @Override- public String getSolrHome() {- return mockSolrHome;- }-- @Override- public NodeConfig getConfig() {- return mockConfig;- }+ private NodeConfig loadNodeConfig(String config) throws Exception {+ InputStream is = DirectoryFactoryTest.class.getResourceAsStream(config);+ return SolrXmlConfig.fromInputStream(loader, is);}-}