package com.dmdirc.util;

import com.dmdirc.plugins.PluginInfo;
import com.dmdirc.plugins.PluginManager;
import com.dmdirc.plugins.PluginMetaData;
import com.dmdirc.ui.themes.ThemeManager;

import java.net.MalformedURLException;
import java.net.URL;

import javax.inject.Provider;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class URLBuilderTest {

    @Mock private Provider<PluginManager> pluginManagerProvider;
    @Mock private Provider<ThemeManager> themeManagerProvider;
    @Mock private PluginManager pluginManager;
    @Mock private ThemeManager themeManager;
    @Mock private PluginInfo pluginInfo;
    @Mock private PluginMetaData pluginMetaData;

    @Before
    public void setup() throws MalformedURLException {
        when(pluginManagerProvider.get()).thenReturn(pluginManager);
        when(themeManagerProvider.get()).thenReturn(themeManager);
        when(pluginManager.getPluginInfoByName(Matchers.anyString())).thenReturn(pluginInfo);
        when(themeManager.getDirectory()).thenReturn("/themes/");
        when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
        when(pluginMetaData.getPluginUrl()).thenReturn(new URL("file://url/testPlugin"));
    }

    @Test
    public void testGetUrlForFileWithoutFilePrefix() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("file://test"), urlBuilder.getUrlForFile("test"));
    }

    @Test
    public void testGetUrlForFileWithFilePrefix() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("file://test"), urlBuilder.getUrlForFile("file://test"));
    }

    @Test
    public void testGetUrlForJarFile() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/jarFile!/test"), urlBuilder.getUrlForJarFile("jarFile", "test"));
    }

    @Test
    public void testGetUrlForDMDircResourceNonExistant() {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertNull(urlBuilder.getUrlForDMDircResource("test"));
    }

    @Test
    public void testGetUrlForDMDircResource() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        URL url = urlBuilder.getUrlForDMDircResource("com/dmdirc/Main.class");
        Assert.assertEquals("file", url.getProtocol());
        Assert.assertTrue(url.getFile().endsWith("com/dmdirc/Main.class"));
    }

    @Test
    public void testGetUrlForThemeResource() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/themes/testTheme.zip!/testFile"), urlBuilder.getUrlForThemeResource("testTheme", "testFile"));
    }

    @Test
    public void testGetUrlForPluginResource() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/testPlugin!/testFile"), urlBuilder.getUrlForPluginResource("testPlugin", "testFile"));
    }

    @Test
    public void testGetUrlDMDirc() {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        URL url = urlBuilder.getUrl("dmdirc://com/dmdirc/Main.class");
        Assert.assertEquals("file", url.getProtocol());
        Assert.assertTrue(url.getFile().endsWith("com/dmdirc/Main.class"));
    }

    @Test
    public void testGetUrlJar() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/jarFile!/testFile"), urlBuilder.getUrl("jar://jarFile:testFile"));
    }

    @Test
    public void testGetUrlZip() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/zipFile!/testFile"), urlBuilder.getUrl("zip://zipFile:testFile"));
    }

    @Test
    public void testGetUrlPlugin() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/testPlugin!/testFile"), urlBuilder.getUrl("plugin://pluginFile:testFile"));
    }

    @Test
    public void testGetUrlTheme() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("jar:file:/themes/themeFile.zip!/testFile"), urlBuilder.getUrl("theme://themeFile:testFile"));
    }

    @Test
    public void testGetUrlHTTP() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("http://testDomain"), urlBuilder.getUrl("http://testDomain"));
    }

    @Test
    public void testGetUrlFileWithPrefix() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("file://testFile"), urlBuilder.getUrl("file://testFile"));
    }

    @Test
    public void testGetUrlFileWithoutPrefix() throws MalformedURLException {
        URLBuilder urlBuilder = new URLBuilder(pluginManagerProvider, themeManagerProvider);
        Assert.assertEquals(new URL("file://testFile"), urlBuilder.getUrl("testFile"));
    }
}

@@ -1,8 +1,1136 @@
+/*%0A * Copyright (c) 2006-2013 DMDirc Developers%0A *%0A * Permission is hereby granted, free of charge, to any person obtaining a copy%0A * of this software and associated documentation files (the %22Software%22), to deal%0A * in the Software without restriction, including without limitation the rights%0A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell%0A * copies of the Software, and to permit persons to whom the Software is%0A * furnished to do so, subject to the following conditions:%0A *%0A * The above copyright notice and this permission notice shall be included in%0A * all copies or substantial portions of the Software.%0A *%0A * THE SOFTWARE IS PROVIDED %22AS IS%22, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR%0A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,%0A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE%0A * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER%0A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,%0A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE%0A * SOFTWARE.%0A */%0A%0A
 package 

Add missing license header