/*
 * 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.activemq.artemis.quorum.file;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Map;

import org.apache.activemq.artemis.quorum.DistributedLockTest;
import org.apache.activemq.artemis.quorum.DistributedPrimitiveManager;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class FileDistributedLockTest extends DistributedLockTest {

   @Rule
   public TemporaryFolder tmpFolder = new TemporaryFolder();

   private File locksFolder;

   @Before
   @Override
   public void setupEnv() throws Throwable {
      locksFolder = tmpFolder.newFolder("locks-folder");
      super.setupEnv();
   }

   @Override
   protected void configureManager(Map<String, String> config) {
      config.put("locks-folder", locksFolder.toString());
   }

   @Override
   protected String managerClassName() {
      return FileBasedPrimitiveManager.class.getName();
   }

   @Test
   public void reflectiveManagerCreation() throws Exception {
      DistributedPrimitiveManager.newInstanceOf(managerClassName(), Collections.singletonMap("locks-folder", locksFolder.toString()));
   }

   @Test(expected = InvocationTargetException.class)
   public void reflectiveManagerCreationFailWithoutLocksFolder() throws Exception {
      DistributedPrimitiveManager.newInstanceOf(managerClassName(), Collections.emptyMap());
   }

   @Test(expected = InvocationTargetException.class)
   public void reflectiveManagerCreationFailIfLocksFolderIsNotFolder() throws Exception {
      DistributedPrimitiveManager.newInstanceOf(managerClassName(), Collections.singletonMap("locks-folder", tmpFolder.newFile().toString()));
   }

}
