/*
 * Copyright 2015 Decebal Suiu
 *
 * 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.Before;
import org.junit.Test;

import java.io.IOException;
import java.nio.file.Files;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class DefaultPluginManagerTest {

    private DefaultPluginDescriptor pd1 = null;
    private DefaultPluginManager pluginManager = new DefaultPluginManager();
    private PluginWrapper pw1;

    @Before
    public void init() throws IOException {
        pd1 = new DefaultPluginDescriptor();
        pd1.setPluginId("myPlugin");
        pd1.setPluginVersion("1.2.3");
        pd1.setPluginClass("foo");
        pd1.setPluginDescription("My plugin");
        pd1.setDependencies("bar, baz");
        pd1.setProvider("Me");
        pd1.setRequires("5.0.0");

        pw1 = new PluginWrapper(pluginManager, pd1, Files.createTempDirectory("test"), getClass().getClassLoader());
    }

    @Test
    public void validateOK() throws PluginException {
        pluginManager.validatePluginDescriptor(pd1);
    }

    @Test(expected = PluginException.class)
    public void validateFailsOnId() throws PluginException {
        pd1.setPluginId("");
        pluginManager.validatePluginDescriptor(pd1);
    }

    @Test(expected = PluginException.class)
    public void validateFailsOnVersion() throws PluginException {
        pd1.setPluginVersion(null);
        pluginManager.validatePluginDescriptor(pd1);
    }

    @Test(expected = PluginException.class)
    public void validateFailsOnClass() throws PluginException {
        pd1.setPluginClass(null);
        pluginManager.validatePluginDescriptor(pd1);
    }

    @Test
    public void isPluginValid() throws Exception {
        // By default accept all since system version not given
        assertTrue(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("1.0.0");
        assertFalse(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("5.0.0");
        assertTrue(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("6.0.0");
        assertTrue(pluginManager.isPluginValid(pw1));
    }

    @Test
    public void isPluginValidAllowExact() throws Exception {
        pluginManager.setExactVersionAllowed(true);

        // By default accept all since system version not given
        assertTrue(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("1.0.0");
        assertFalse(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("5.0.0");
        assertTrue(pluginManager.isPluginValid(pw1));

        pluginManager.setSystemVersion("6.0.0");
        assertFalse(pluginManager.isPluginValid(pw1));
    }

    @Test
    public void testDefaultExactVersionAllowed() throws Exception {
        assertEquals(false, pluginManager.isExactVersionAllowed());
    }

}

@@ -1226,43 +1226,8 @@
 %22);%0A
-        pd1.setPluginClass(%22foo%22);%0A
     
@@ -2037,42 +2037,8 @@
 Test
-(expected = PluginException.class)
 %0A   
@@ -2058,22 +2058,23 @@
 validate
-FailsO
+NoPlugi
 nClass()
@@ -2103,42 +2103,8 @@
 n %7B%0A
-        pd1.setPluginClass(null);%0A
     
@@ -2144,32 +2144,100 @@
 escriptor(pd1);%0A
+        assertEquals(Plugin.class.getName(), pd1.getPluginClass());%0A
     %7D%0A%0A    @Test
@@ -2268,33 +2268,16 @@
 nValid()
- throws Exception
  %7B%0A     
@@ -2760,33 +2760,16 @@
 wExact()
- throws Exception
  %7B%0A     
@@ -3317,25 +3317,8 @@
 ed()
- throws Exception
  %7B%0A 

Fix unit test