/*
 * Copyright (C) 2012-present the original author or authors.
 *
 * Licensed 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.pf4j;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.pf4j.util.FileUtils;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * @author Mario Franco
 * @author Decebal Suiu
 */
public class DefaultPluginStatusProviderTest {

    @TempDir
    Path pluginsPath;

    @Test
    public void testIsPluginDisabled() throws IOException {
        createEnabledFile();
        createDisabledFile();

        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);

        assertFalse(statusProvider.isPluginDisabled("plugin-1"));
        assertTrue(statusProvider.isPluginDisabled("plugin-2"));
        assertTrue(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testIsPluginDisabledWithEnableEmpty() throws IOException {
        createDisabledFile();

        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);

        assertFalse(statusProvider.isPluginDisabled("plugin-1"));
        assertTrue(statusProvider.isPluginDisabled("plugin-2"));
        assertFalse(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testDisablePlugin() throws Exception {
        createEnabledFile();
        createDisabledFile();

        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
        statusProvider.disablePlugin("plugin-1");

        assertTrue(statusProvider.isPluginDisabled("plugin-1"));
        assertTrue(statusProvider.isPluginDisabled("plugin-2"));
        assertTrue(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testDisablePluginWithEnableEmpty() throws Exception {
        createDisabledFile();

        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
        statusProvider.disablePlugin("plugin-1");

        assertTrue(statusProvider.isPluginDisabled("plugin-1"));
        assertTrue(statusProvider.isPluginDisabled("plugin-2"));
        assertFalse(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testEnablePlugin() throws Exception {
        createEnabledFile();

        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
        statusProvider.enablePlugin("plugin-2");

        assertFalse(statusProvider.isPluginDisabled("plugin-1"));
        assertFalse(statusProvider.isPluginDisabled("plugin-2"));
        assertTrue(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testEnablePluginWithEnableEmpty() throws Exception{
        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);
        statusProvider.enablePlugin("plugin-2");

        assertFalse(statusProvider.isPluginDisabled("plugin-1"));
        assertFalse(statusProvider.isPluginDisabled("plugin-2"));
        assertFalse(statusProvider.isPluginDisabled("plugin-3"));
    }

    @Test
    public void testDisablePluginWithoutDisabledFile() throws Exception {
        PluginStatusProvider statusProvider = new DefaultPluginStatusProvider(pluginsPath);

        assertFalse(statusProvider.isPluginDisabled("plugin-1"));

        statusProvider.disablePlugin("plugin-1");
        assertTrue(statusProvider.isPluginDisabled("plugin-1"));
    }

    private void createDisabledFile() throws IOException {
        List<String> disabledPlugins = new ArrayList<>();
        disabledPlugins.add("plugin-2");

        Path disabledPath = pluginsPath.resolve("disabled.txt");
        FileUtils.writeLines(disabledPlugins, disabledPath.toFile());
    }

    private void createEnabledFile() throws IOException {
        List<String> enabledPlugins = new ArrayList<>();
        enabledPlugins.add("plugin-1");
        enabledPlugins.add("plugin-2");

        Path enabledPath = pluginsPath.resolve("enabled.txt");
        FileUtils.writeLines(enabledPlugins, enabledPath.toFile());
    }

}

@@ -2865,32 +2865,194 @@
 ed(%22plugin-3%22));
+%0A%0A        List%3CString%3E disabledPlugins = FileUtils.readLines(pluginsPath.resolve(%22disabled.txt%22), true);%0A        assertTrue(disabledPlugins.contains(%22plugin-1%22));
 %0A    %7D%0A%0A    @Tes
@@ -3467,32 +3467,195 @@
 ed(%22plugin-3%22));
+%0A%0A        List%3CString%3E disabledPlugins = FileUtils.readLines(pluginsPath.resolve(%22disabled.txt%22), true);%0A        assertFalse(disabledPlugins.contains(%22plugin-2%22));
 %0A    %7D%0A%0A    @Tes
@@ -3706,32 +3706,16 @@
 Empty() 
-throws Exception
 %7B%0A      
@@ -4119,33 +4119,16 @@
 edFile()
- throws Exception
  %7B%0A     
@@ -4567,73 +4567,8 @@
 );%0A%0A
-        Path disabledPath = pluginsPath.resolve(%22disabled.txt%22);%0A
     
@@ -4609,24 +4609,45 @@
 lugins, 
+pluginsPath.resolve(%22
 disabled
 Path.toF
@@ -4634,28 +4634,30 @@
 ve(%22disabled
-Path
+.txt%22)
 .toFile());%0A
@@ -4863,71 +4863,8 @@
 );%0A%0A
-        Path enabledPath = pluginsPath.resolve(%22enabled.txt%22);%0A
     
@@ -4908,19 +4908,42 @@
 ns, 
+pluginsPath.resolve(%22
 enabled
-Path
+.txt%22)
 .toF

Improve testing of #335