/*
 * 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.
 */
package org.apache.solr.core;

import java.io.IOException;
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.Before;
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);
  }

  @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");
    Properties cp = cc.getContainerProperties();
    boolean zkAware = cc.isZooKeeperAware();
    RAMDirectoryFactory rdf = new RAMDirectoryFactory();
    rdf.initCoreContainer(cc);
    rdf.init(new NamedList());

    // No solr.data.home property set. Absolute instanceDir
    assertEquals("/tmp/inst1/data", rdf.getDataHome(new CoreDescriptor("core_name", Paths.get("/tmp/inst1"), cp, zkAware)));

    // Simulate solr.data.home set in solrconfig.xml <directoryFactory> tag
    NamedList args = new NamedList();
    args.add("solr.data.home", "/solrdata/");
    rdf.init(args);
    assertEquals("/solrdata/inst_dir/data", rdf.getDataHome(new CoreDescriptor("core_name", Paths.get("inst_dir"), cp, zkAware)));

    // solr.data.home set with System property, and relative path
    System.setProperty("solr.data.home", "solrdata");
    rdf.init(new NamedList());
    assertEquals("/solr/home/solrdata/inst_dir/data", rdf.getDataHome(new CoreDescriptor("core_name", Paths.get("inst_dir"), cp, zkAware)));
    // Test parsing last component of instanceDir, and using custom dataDir
    assertEquals("/solr/home/solrdata/myinst/mydata", rdf.getDataHome(new CoreDescriptor("core_name", Paths.get("/path/to/myinst"), cp, zkAware, "dataDir", "mydata")));
  }


  private static class MockCoreContainer extends CoreContainer {

    private final String mockSolrHome;

    public MockCoreContainer(String solrHome) throws IOException {
      super(new Object());
      mockSolrHome = solrHome;
      this.shardHandlerFactory = new HttpShardHandlerFactory();
      this.coreAdminHandler = new CoreAdminHandler();
    }

    @Override
    public String getSolrHome() {
      return mockSolrHome;
    }
  }

}

@@ -912,16 +912,51 @@
 rties;%0A%0A
+import org.apache.commons.exec.OS;%0A
 import o
@@ -1160,16 +1160,63 @@
 actory;%0A
+import org.apache.solr.util.MockCoreContainer;%0A
 import o
@@ -1340,16 +1340,120 @@
 Case %7B%0A%0A
+  private static boolean IS_WINDOWS = (OS.isFamilyDOS() %7C%7C OS.isFamilyWin9x() %7C%7C OS.isFamilyWindows());%0A
   public
@@ -2357,22 +2357,24 @@
   assert
-Equals
+DataHome
 (%22/tmp/i
@@ -2389,66 +2389,8 @@
 a%22, 
-rdf.getDataHome(new CoreDescriptor(%22core_name%22, Paths.get(
 %22/tm
@@ -2397,32 +2397,25 @@
 p/inst1%22
-)
 , 
-cp, zkAware))
+rdf, cc
 );%0A%0A    
@@ -2596,30 +2596,32 @@
 ;%0A    assert
-Equals
+DataHome
 (%22/solrdata/
@@ -2640,66 +2640,8 @@
 a%22, 
-rdf.getDataHome(new CoreDescriptor(%22core_name%22, Paths.get(
 %22ins
@@ -2646,35 +2646,32 @@
 nst_dir%22
-)
 , 
-cp, zkAware)));%0A
+rdf, cc);%0A    
 %0A    // 
@@ -2816,38 +2816,40 @@
 t());%0A    assert
-Equals
+DataHome
 (%22/solr/home/sol
@@ -2874,66 +2874,8 @@
 a%22, 
-rdf.getDataHome(new CoreDescriptor(%22core_name%22, Paths.get(
 %22ins
@@ -2884,24 +2884,17 @@
 dir%22
-)
 , 
-cp, zkAware))
+rdf, cc
 );%0A 
@@ -2978,22 +2978,24 @@
   assert
-Equals
+DataHome
 (%22/solr/
@@ -3028,120 +3028,512 @@
 a%22, 
-rdf.getDataHome(new CoreDescriptor(%22core_name%22, Paths.get(%22/path/to/myinst%22), cp, zkAware, %22dataDir%22, %22mydata%22))
+%22/path/to/myinst%22, rdf, cc, %22dataDir%22, %22mydata%22);%0A  %7D%0A%0A  private void assertDataHome(String expected, String instanceDir, RAMDirectoryFactory rdf, MockCoreContainer cc, String... properties) throws IOException %7B%0A    String dataHome = rdf.getDataHome(new CoreDescriptor(%22core_name%22, Paths.get(instanceDir), cc.containerProperties, cc.isZooKeeperAware(), properties));%0A    if (IS_WINDOWS) %7B%0A      dataHome = dataHome.replaceFirst(%22%5EC:%22, %22%22).replaceAll(%22%5C%5C%5C%5C%22,%22/%22);%0A    %7D%0A    assertEquals(expected, dataHome
 );%0A 

Fix tests on Windows