You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ab...@apache.org on 2017/07/19 13:45:16 UTC

lucene-solr:branch_7x: SOLR-11037: Improved unit test.

Repository: lucene-solr
Updated Branches:
  refs/heads/branch_7x f297841b4 -> e9ae427f9


SOLR-11037: Improved unit test.


Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/e9ae427f
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/e9ae427f
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/e9ae427f

Branch: refs/heads/branch_7x
Commit: e9ae427f931d6b55fd3b9cf33d5f2ef9edf0fd23
Parents: f297841
Author: Andrzej Bialecki <ab...@apache.org>
Authored: Wed Jul 19 15:44:13 2017 +0200
Committer: Andrzej Bialecki <ab...@apache.org>
Committed: Wed Jul 19 15:45:08 2017 +0200

----------------------------------------------------------------------
 .../java/org/apache/solr/core/NodeConfig.java   |  5 +-
 .../src/test-files/solr/solr-solrDataHome.xml   | 24 ++++++
 .../apache/solr/core/DirectoryFactoryTest.java  | 84 +++++++++++---------
 3 files changed, 75 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e9ae427f/solr/core/src/java/org/apache/solr/core/NodeConfig.java
----------------------------------------------------------------------
diff --git a/solr/core/src/java/org/apache/solr/core/NodeConfig.java b/solr/core/src/java/org/apache/solr/core/NodeConfig.java
index fb52e4f..5b4debe 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;
     }
 

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e9ae427f/solr/core/src/test-files/solr/solr-solrDataHome.xml
----------------------------------------------------------------------
diff --git a/solr/core/src/test-files/solr/solr-solrDataHome.xml b/solr/core/src/test-files/solr/solr-solrDataHome.xml
new file mode 100644
index 0000000..5705f93
--- /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>

http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/e9ae427f/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java b/solr/core/src/test/org/apache/solr/core/DirectoryFactoryTest.java
index 18f0e87..869a4d2 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);
   }
 
   @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);
   }
-
 }