/*-
 * Copyright (c) 2017-2018 Red Hat, Inc.
 *
 * 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.fedoraproject.xmvn.tools.install.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

import org.junit.Test;

import org.fedoraproject.xmvn.tools.install.ArtifactInstaller;

/**
 * @author Mikolaj Izdebski
 */
public class ArtifactInstallerFactoryTest
{
    @Test
    public void testNoPluginsAvailable()
    {
        Path pluginDir = Paths.get( "src/test/resources/plugins-not-found" ).toAbsolutePath();
        ArtifactInstallerFactory aif = new ArtifactInstallerFactory( null, pluginDir );
        Properties props = new Properties();
        props.setProperty( "type", "myplugin-missing" );
        ArtifactInstaller inst = aif.getInstallerFor( null, props );
        assertTrue( DefaultArtifactInstaller.class.isAssignableFrom( inst.getClass() ) );
    }

    @Test
    public void testMissingPlugin()
    {
        Path pluginDir = Paths.get( "src/test/resources/plugins" ).toAbsolutePath();
        ArtifactInstallerFactory aif = new ArtifactInstallerFactory( null, pluginDir );
        Properties props = new Properties();
        props.setProperty( "type", "myplugin-missing" );
        ArtifactInstaller inst = aif.getInstallerFor( null, props );
        assertTrue( DefaultArtifactInstaller.class.isAssignableFrom( inst.getClass() ) );
    }

    @Test
    public void testValidPlugin()
    {
        Path pluginDir = Paths.get( "src/test/resources/plugins" ).toAbsolutePath();
        ArtifactInstallerFactory aif = new ArtifactInstallerFactory( null, pluginDir );
        Properties props = new Properties();
        props.setProperty( "type", "myplugin1" );
        ArtifactInstaller inst = aif.getInstallerFor( null, props );
        assertEquals( "foo.bar.MyPlugin", inst.getClass().getCanonicalName() );
    }

    @Test
    public void testBrokenPlugin()
    {
        Path pluginDir = Paths.get( "src/test/resources/plugins" ).toAbsolutePath();
        ArtifactInstallerFactory aif = new ArtifactInstallerFactory( null, pluginDir );
        Properties props = new Properties();
        props.setProperty( "type", "myplugin2" );
        try
        {
            aif.getInstallerFor( null, props );
            fail();
        }
        catch ( RuntimeException e )
        {
            assertEquals( "Unable to load XMvn Installer plugin for packaging type myplugin2", e.getMessage() );
            assertNotNull( e.getCause() );
            assertTrue( ReflectiveOperationException.class.isAssignableFrom( e.getCause().getClass() ) );
        }
    }

}

@@ -2114,38 +2114,63 @@
 stValidPlugin()%0A
+        throws Exception%0A
     %7B%0A
-
         Path plu
@@ -2565,24 +2565,1437 @@
 calName() );
+%0A%0A        try%0A        %7B%0A            // Deprecated call without repoId specified%0A            inst.install( null, null, null, null );%0A            fail( %22Expected UnsupportedOperationException%22 );%0A        %7D%0A        catch ( UnsupportedOperationException e )%0A        %7B%0A            // %22Not implemented exception%22 is thrown by the plugin%0A            assertEquals( %22Not implemented%22, e.getMessage() );%0A            assertEquals( %22foo.bar.MyPlugin.install(MyPlugin.java:16)%22, e.getStackTrace()%5B0%5D.toString() );%0A        %7D%0A%0A        try%0A        %7B%0A            // Modern call with default repoId%0A            inst.install( null, null, null, null, %22install%22 );%0A            fail( %22Expected UnsupportedOperationException%22 );%0A        %7D%0A        catch ( UnsupportedOperationException e )%0A        %7B%0A            // %22Not implemented exception%22 is thrown by the plugin%0A            assertEquals( %22Not implemented%22, e.getMessage() );%0A            assertEquals( %22foo.bar.MyPlugin.install(MyPlugin.java:16)%22, e.getStackTrace()%5B0%5D.toString() );%0A        %7D%0A%0A        try%0A        %7B%0A            // Modern call with non-default repoId%0A            inst.install( null, null, null, null, %22my-repo%22 );%0A            fail( %22Expected UnsupportedOperationException%22 );%0A        %7D%0A        catch ( UnsupportedOperationException e )%0A        %7B%0A            assertEquals( %22This artifact installer does not support non-default repository.%22, e.getMessage() );%0A        %7D
 %0A    %7D%0A%0A    
@@ -4728,17 +4728,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

Test if new installer API does not break plugins