package org.apache.solr.core;

/*
 * 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.
 */

import java.io.File;
import java.io.IOException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MockDirectoryWrapper;
import org.apache.lucene.util.LuceneTestCase;

/**
 * Opens a directory with {@link LuceneTestCase#newDirectory()}
 */
public class MockDirectoryFactory extends CachingDirectoryFactory {

  @Override
  protected Directory create(String path) throws IOException {
    Directory dir = LuceneTestCase.newDirectory();
    // we can't currently do this check because of how
    // Solr has to reboot a new Directory sometimes when replicating
    // or rolling back - the old directory is closed and the following
    // test assumes it can open an IndexWriter when that happens - we
    // have a new Directory for the same dir and still an open IW at 
    // this point
    if (dir instanceof MockDirectoryWrapper) {
      ((MockDirectoryWrapper)dir).setAssertNoUnrefencedFilesOnClose(false);
    }
    return dir;
  }
  
  @Override
  public boolean exists(String path) {
    String fullPath = new File(path).getAbsolutePath();
    synchronized (this) {
      CacheValue cacheValue = byPathCache.get(fullPath);
      Directory directory = null;
      if (cacheValue != null) {
        directory = cacheValue.directory;
      }
      if (directory == null) {
        return false;
      } else {
        return true;
      }
    }
  }
}

@@ -1282,24 +1282,152 @@
 irectory();%0A
+    if (dir instanceof MockDirectoryWrapper) %7B%0A      MockDirectoryWrapper mockDirWrapper = (MockDirectoryWrapper) dir;%0A      %0A  
     // we ca
@@ -1469,16 +1469,18 @@
  of how%0A
+  
     // S
@@ -1544,16 +1544,18 @@
 ing%0A    
+ 
+ 
 // or ro
@@ -1613,24 +1613,26 @@
 llowing%0A    
+  
 // test assu
@@ -1685,16 +1685,18 @@
 ns - we%0A
+  
     // h
@@ -1757,24 +1757,26 @@
  IW at %0A    
+  
 // this poin
@@ -1785,117 +1785,421 @@
     
+  
-if (
+mockDirWrapper.setAssertNoUnrefencedFilesOnClose(false);%0A      %0A      // ram 
 dir
+s
  in
-stanceof MockDirectoryWrapper) %7B%0A      ((MockDirectoryWrapper)dir).setAssertNoUnrefencedFilesOnClos
+ cores that are restarted end up empty%0A      // and check index fails%0A      mockDirWrapper.setCheckIndexOnClose(false);%0A      %0A      // if we enable this, TestReplicationHandler fails when it%0A      // tries to write to index.properties after the file has%0A      // already been created.%0A      mockDirWrapper.setPreventDoubleWrit
 e(fa
@@ -2206,24 +2206,26 @@
 lse);%0A    %7D%0A
+%09%0A
     return d

disable doubleWrite check in MockDirectoryFactory, or otherwise TestReplicationHandler fails