SOLR-11037: Improved unit test.

This commit is contained in:
Andrzej Bialecki 2017-07-19 15:44:13 +02:00
parent 541aa719c4
commit c1f146d2fc
3 changed files with 76 additions and 39 deletions

View File

@ -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;
}

View File

@ -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>

View File

@ -17,19 +17,47 @@
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 {
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
@Before
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);
@ -38,18 +66,11 @@ public class DirectoryFactoryTest extends LuceneTestCase {
assertEquals("hdfs", DirectoryFactory.LOCK_TYPE_HDFS);
}
@After
@Before
public void clean() {
System.clearProperty("solr.data.home");
System.clearProperty("solr.solr.home");
}
@Test
public 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 path
System.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);
}
}