<commit_before>package com.atlassian.plugin.parsers;

import java.io.*;
import java.net.URL;

import junit.framework.TestCase;

import com.atlassian.plugin.*;
import com.atlassian.plugin.mock.MockAnimalModuleDescriptor;
import com.atlassian.plugin.classloader.PluginClassLoader;
import com.atlassian.plugin.impl.DefaultDynamicPlugin;
import com.atlassian.plugin.util.ClassLoaderUtils;
import com.mockobjects.dynamic.Mock;
import com.mockobjects.dynamic.C;

@SuppressWarnings({"deprecation"}) //suppress deprecation warnings because we still need to test deprecated methods.
public class TestXmlDescriptorParser extends TestCase
{
    private static final String MISSING_INFO_TEST_FILE = "test-missing-plugin-info.xml";
    private static final String DUMMY_PLUGIN_FILE = "pooh-test-plugin.jar";

    public TestXmlDescriptorParser(String name)
    {
        super(name);
    }

    // CONF-12680 Test for missing plugin-info
    public void testMissingPluginInfo()
    {
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expect("getModuleDescriptorClass", "unknown-plugin");

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);

        try
        {
            XmlDescriptorParser parser = new XmlDescriptorParser(new FileInputStream(getTestFile(MISSING_INFO_TEST_FILE)), null);
            parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
            
            PluginInformation info = testPlugin.getPluginInformation();
            assertNotNull("Info should not be null", info);
        }
        catch (PluginParseException e)
        {
            e.printStackTrace();
            fail("Plugin information parsing should not fail.");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            // This shouldn't happen
            fail("Error setting up test");
        }
    }

    public void testPluginsApplicationVersionMinMax()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version min='3' max='4' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMinVersion());
        assertEquals(4, (int)parser.getPluginInformation().getMaxVersion());
    }

    public void testPluginsApplicationVersionMinMaxWithOnlyMin()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version min='3' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMinVersion());
        assertEquals(0, (int)parser.getPluginInformation().getMaxVersion());
    }

    public void testPluginsApplicationVersionMinMaxWithOnlyMax()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version max='3' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMaxVersion());
        assertEquals(0, (int)parser.getPluginInformation().getMinVersion());
    }

    // Also CONF-12680 test for missing "essential metadata"

    public void testPluginsVersion()
    {
        String xml = "<atlassian-plugin key=\"foo\" pluginsVersion=\"2\" />";
        XmlDescriptorParser parser = new XmlDescriptorParser(new ByteArrayInputStream(xml.getBytes()), null);
        assertEquals(2, parser.getPluginsVersion());
    }

    public void testPluginsVersionAfterConfigure()
    {
        XmlDescriptorParser parser = new XmlDescriptorParser(new ByteArrayInputStream("<atlassian-plugin key=\"foo\" plugins-version=\"2\" />".getBytes()), null);
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expect("getModuleDescriptorClass", "unknown-plugin");

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertEquals(2, testPlugin.getPluginsVersion());
    }

    public void testPluginWithModules()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' />",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNotNull(testPlugin.getModuleDescriptor("bear"));
    }

    public void testPluginWithModulesNoApplicationKey()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' application='foo'/>",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNull(testPlugin.getModuleDescriptor("bear"));
    }

    public void testPluginWithSomeNonApplicationModules()
    {
        XmlDescriptorParser parser = parse("myapp",
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' application='myapp'/>",
                "  <animal key='bear2' application='otherapp'/>",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNotNull(testPlugin.getModuleDescriptor("bear"));
        assertNull(testPlugin.getModuleDescriptor("bear2"));
    }

    public void testPluginWithSystemAttribute()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo' system='true'>",
                "</atlassian-plugin>");
        
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertEquals("This plugin should not be a system plugin - only WEB-INF/lib plugins can be system plugins.", false, testPlugin.isSystemPlugin());
    }

    public void testPluginWithoutSystemAttribute()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo' >",
                "</atlassian-plugin>");

        // mock up some supporting objects<commit_msg>PLUG-415 Plugins2 plugins now need to be able to be declared as system.<commit_after>package com.atlassian.plugin.parsers;

import java.io.*;
import java.net.URL;

import junit.framework.TestCase;

import com.atlassian.plugin.*;
import com.atlassian.plugin.mock.MockAnimalModuleDescriptor;
import com.atlassian.plugin.classloader.PluginClassLoader;
import com.atlassian.plugin.impl.DefaultDynamicPlugin;
import com.atlassian.plugin.util.ClassLoaderUtils;
import com.mockobjects.dynamic.Mock;
import com.mockobjects.dynamic.C;

@SuppressWarnings({"deprecation"}) //suppress deprecation warnings because we still need to test deprecated methods.
public class TestXmlDescriptorParser extends TestCase
{
    private static final String MISSING_INFO_TEST_FILE = "test-missing-plugin-info.xml";
    private static final String DUMMY_PLUGIN_FILE = "pooh-test-plugin.jar";

    public TestXmlDescriptorParser(String name)
    {
        super(name);
    }

    // CONF-12680 Test for missing plugin-info
    public void testMissingPluginInfo()
    {
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expect("getModuleDescriptorClass", "unknown-plugin");

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);

        try
        {
            XmlDescriptorParser parser = new XmlDescriptorParser(new FileInputStream(getTestFile(MISSING_INFO_TEST_FILE)), null);
            parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
            
            PluginInformation info = testPlugin.getPluginInformation();
            assertNotNull("Info should not be null", info);
        }
        catch (PluginParseException e)
        {
            e.printStackTrace();
            fail("Plugin information parsing should not fail.");
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            // This shouldn't happen
            fail("Error setting up test");
        }
    }

    public void testPluginsApplicationVersionMinMax()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version min='3' max='4' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMinVersion());
        assertEquals(4, (int)parser.getPluginInformation().getMaxVersion());
    }

    public void testPluginsApplicationVersionMinMaxWithOnlyMin()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version min='3' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMinVersion());
        assertEquals(0, (int)parser.getPluginInformation().getMaxVersion());
    }

    public void testPluginsApplicationVersionMinMaxWithOnlyMax()
    {
        XmlDescriptorParser parser = parse(null,
                "<atlassian-plugin key='foo'>",
                "  <plugin-info>",
                "    <application-version max='3' />",
                "  </plugin-info>",
                "</atlassian-plugin>");
        assertEquals(3, (int)parser.getPluginInformation().getMaxVersion());
        assertEquals(0, (int)parser.getPluginInformation().getMinVersion());
    }

    // Also CONF-12680 test for missing "essential metadata"

    public void testPluginsVersion()
    {
        String xml = "<atlassian-plugin key=\"foo\" pluginsVersion=\"2\" />";
        XmlDescriptorParser parser = new XmlDescriptorParser(new ByteArrayInputStream(xml.getBytes()), null);
        assertEquals(2, parser.getPluginsVersion());
    }

    public void testPluginsVersionAfterConfigure()
    {
        XmlDescriptorParser parser = new XmlDescriptorParser(new ByteArrayInputStream("<atlassian-plugin key=\"foo\" plugins-version=\"2\" />".getBytes()), null);
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expect("getModuleDescriptorClass", "unknown-plugin");

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertEquals(2, testPlugin.getPluginsVersion());
    }

    public void testPluginWithModules()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' />",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNotNull(testPlugin.getModuleDescriptor("bear"));
    }

    public void testPluginWithModulesNoApplicationKey()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' application='foo'/>",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNull(testPlugin.getModuleDescriptor("bear"));
    }

    public void testPluginWithSomeNonApplicationModules()
    {
        XmlDescriptorParser parser = parse("myapp",
                 "<atlassian-plugin key='foo'>",
                "  <animal key='bear' application='myapp'/>",
                "  <animal key='bear2' application='otherapp'/>",
                "</atlassian-plugin>");
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        assertNotNull(testPlugin.getModuleDescriptor("bear"));
        assertNull(testPlugin.getModuleDescriptor("bear2"));
    }

    public void testPluginWithSystemAttribute()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo' system='true'>",
                "</atlassian-plugin>");
        
        // mock up some supporting objects
        PluginClassLoader classLoader = new PluginClassLoader(new File(getTestFile("ap-plugins") + "/" + DUMMY_PLUGIN_FILE));
        Mock mockFactory = new Mock(ModuleDescriptorFactory.class);
        mockFactory.expectAndReturn("getModuleDescriptorClass", C.args(C.eq("animal")), MockAnimalModuleDescriptor.class);

        // create a Plugin for testing
        Plugin testPlugin = new DefaultDynamicPlugin((PluginArtifact) new Mock(PluginArtifact.class).proxy(), classLoader);
        parser.configurePlugin((ModuleDescriptorFactory)mockFactory.proxy(), testPlugin);
        // PLUG-415 Plugins2 plugins now need to be able to be declared as system.
        assertEquals("This plugin should be a system plugin - bundled plugins2 plugins are system plugins.", true, testPlugin.isSystemPlugin());
    }

    public void testPluginWithoutSystemAttribute()
    {
        XmlDescriptorParser parser = parse(null,
                 "<atlassian-plugin key='foo' >",
                "</atlassian-plugin>");

        // mock up some supporting objects
{'fraction_non_alphanumeric': 0.11377738729339411, 'fraction_numerical': 0.0033496238537147876, 'mean_word_length': 3.0843238394258803, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6992396', 'n_tokens_mistral': 4752, 'n_tokens_neox': 4553, 'n_words': 946}