<commit_before>package org.jmeterplugins.repository;

import kg.apc.emulators.TestJMeterUtils;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.util.JMeterUtils;
import org.jmeterplugins.repository.exception.DownloadException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.*;

public class PluginManagerTest {
    @BeforeClass
    public static void setup() {
        TestJMeterUtils.createJmeterEnv();
        URL url = PluginManagerTest.class.getResource("/testVirtualPlugin.json");
        JMeterUtils.setProperty("jpgc.repo.address", url.getFile());
    }

    @AfterClass
    public static void tearDown() throws Exception {
        JMeterUtils.getJMeterProperties().remove("jpgc.repo.address");
    }

    @Test
    public void testResolve() throws IOException {
        Plugin[] init = new Plugin[]{};
        PluginManager obj = new PluginManagerEmul(init);
        obj.getChangesAsText();
    }

    @Test
    public void testStandardSet() throws Throwable {
        PluginManager pmgr = new PluginManager();
        pmgr.load();

        for (Plugin plugin : pmgr.allPlugins.keySet()) {
            if (plugin.getID().equals("jpgc-standard")) {
                assertTrue(pmgr.allPlugins.get(plugin));
            }
        }
    }

    @Test
    public void testStatus() throws IOException {
        String res = PluginManager.getAllPluginsStatus();
        String expected = "[jpgc-dep1=0.0.0-STOCK, jpgc-dep2=0.0.0-STOCK, jpgc-standard=2.0]";
        assertEquals(expected, res);
    }

    @Test
    public void testStatusSingle() throws IOException {
        assertEquals("0.0.0-STOCK", PluginManager.getPluginStatus("jpgc-dep2"));
        assertEquals(null, PluginManager.getPluginStatus("jmeter-nonexistent"));
    }

    @Test
    public void testReadOnly() throws Throwable {
        PluginManager mgr = new PluginManager();
        String jarPath = Plugin.getJARPath(JMeterEngine.class.getCanonicalName());
        assert jarPath != null;
        File ifile = new File(jarPath).getParentFile();
        ifile.setReadOnly();
        mgr.load();
        try {
            mgr.applyChanges(new LoggingCallback());
            fail();
        } catch (RuntimeException e) {
            String prefix = "Have no write access for JMeter directories, not possible to use Plugins Manager:";
            assertTrue(e.getMessage().contains(prefix));
        } finally {
            ifile.setWritable(true);
        }
    }

    @Test
    public void testApplyChanges() throws Exception {
        String imgPath = "file:///" + new File(".").getAbsolutePath() + "/target/classes/org/jmeterplugins/logo.png";
        String str = "{\"id\": 0,  \"markerClass\": \"" + PluginsListTest.class.getName() + "\"," +
                " \"screenshotUrl\": \"" + imgPath + "\", \"name\": 3, \"description\": 4, \"helpUrl\": 5, \"vendor\": 5, \"installerClass\": \"test\", " +
                "\"versions\" : { \"0.1\" : { \"changes\": \"fix verified exception1\" }," +
                "\"0.2\" : { \"changes\": \"fix verified exception1\", \"libs\": {\n" +
                "          \"jpgc-common\": \"http://httpstat.us/500\"\n" +
                "        }}," +
                "\"0.3\" : { \"changes\": \"fix verified exception1\", \"downloadUrl\": \"http://httpstat.us/500\" } }}";

        String addr = JMeterUtils.getPropDefault("jpgc.repo.address", "https://jmeter-plugins.org/repo/");
        try {
            JMeterUtils.setProperty("jpgc.repo.address", "http://httpstat.us/500");

            Plugin p = Plugin.fromJSON(JSONObject.fromObject(str, new JsonConfig()));
            PluginManager manager = new PluginManager();
            manager.allPlugins.put(p, true); // need to install
            p.setCandidateVersion("0.2");

            try {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(String progress) {
                    }
                });
            } catch (DownloadException ex) {
                assertTrue(ex.getMessage().contains("Failed to download library"));
            }

            manager = new PluginManager();
            manager.allPlugins.put(p, true); // need to install
            p.setCandidateVersion("0.3");
            try {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(String progress) {
                    }
                });
            } catch (DownloadException ex) {
                assertTrue(ex.getMessage().contains("Failed to download plugin"));
            }

        } finally {<commit_msg>test for illegal version plugin<commit_after>package org.jmeterplugins.repository;

import kg.apc.emulators.TestJMeterUtils;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.apache.jmeter.engine.JMeterEngine;
import org.apache.jmeter.util.JMeterUtils;
import org.jmeterplugins.repository.exception.DownloadException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.junit.Assert.*;

public class PluginManagerTest {
    @BeforeClass
    public static void setup() {
        TestJMeterUtils.createJmeterEnv();
        URL url = PluginManagerTest.class.getResource("/testVirtualPlugin.json");
        JMeterUtils.setProperty("jpgc.repo.address", url.getFile());
    }

    @AfterClass
    public static void tearDown() throws Exception {
        JMeterUtils.getJMeterProperties().remove("jpgc.repo.address");
    }

    @Test
    public void testResolve() throws IOException {
        Plugin[] init = new Plugin[]{};
        PluginManager obj = new PluginManagerEmul(init);
        obj.getChangesAsText();
    }

    @Test
    public void testStandardSet() throws Throwable {
        PluginManager pmgr = new PluginManager();
        pmgr.load();

        for (Plugin plugin : pmgr.allPlugins.keySet()) {
            if (plugin.getID().equals("jpgc-standard")) {
                assertTrue(pmgr.allPlugins.get(plugin));
            }
        }
    }

    @Test
    public void testStatus() throws IOException {
        String res = PluginManager.getAllPluginsStatus();
        String expected = "[jpgc-dep1=0.0.0-STOCK, jpgc-dep2=0.0.0-STOCK, jpgc-standard=2.0]";
        assertEquals(expected, res);
    }

    @Test
    public void testStatusSingle() throws IOException {
        assertEquals("0.0.0-STOCK", PluginManager.getPluginStatus("jpgc-dep2"));
        assertEquals(null, PluginManager.getPluginStatus("jmeter-nonexistent"));
    }

    @Test
    public void testReadOnly() throws Throwable {
        PluginManager mgr = new PluginManager();
        String jarPath = Plugin.getJARPath(JMeterEngine.class.getCanonicalName());
        assert jarPath != null;
        File ifile = new File(jarPath).getParentFile();
        ifile.setReadOnly();
        mgr.load();
        try {
            mgr.applyChanges(new LoggingCallback());
            fail();
        } catch (RuntimeException e) {
            String prefix = "Have no write access for JMeter directories, not possible to use Plugins Manager:";
            assertTrue(e.getMessage().contains(prefix));
        } finally {
            ifile.setWritable(true);
        }
    }

    @Test
    public void testApplyChanges() throws Exception {
        String imgPath = "file:///" + new File(".").getAbsolutePath() + "/target/classes/org/jmeterplugins/logo.png";
        String str = "{\"id\": 0,  \"markerClass\": \"" + PluginsListTest.class.getName() + "\"," +
                " \"screenshotUrl\": \"" + imgPath + "\", \"name\": 3, \"description\": 4, \"helpUrl\": 5, \"vendor\": 5, \"installerClass\": \"test\", " +
                "\"versions\" : { \"0.1\" : { \"changes\": \"fix verified exception1\" }," +
                "\"0.2\" : { \"changes\": \"fix verified exception1\", \"libs\": {\n" +
                "          \"jpgc-common\": \"http://httpstat.us/500\"\n" +
                "        }}," +
                "\"0.3\" : { \"changes\": \"fix verified exception1\", \"downloadUrl\": \"http://httpstat.us/500\" } }}";

        String addr = JMeterUtils.getPropDefault("jpgc.repo.address", "https://jmeter-plugins.org/repo/");
        try {
            JMeterUtils.setProperty("jpgc.repo.address", "http://httpstat.us/500");

            Plugin p = Plugin.fromJSON(JSONObject.fromObject(str, new JsonConfig()));
            PluginManager manager = new PluginManager();
            manager.allPlugins.put(p, true); // need to install
            p.setCandidateVersion("9999");

            try {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(String progress) {
                    }
                });
                fail();
            } catch (IllegalArgumentException ex) {
                assertTrue(ex.getMessage().contains("Version 9999 not found for plugin"));
            }

            manager.allPlugins.put(p, true); // need to install
            p.setCandidateVersion("0.2");

            try {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(String progress) {
                    }
                });
                fail();
            } catch (DownloadException ex) {
                assertTrue(ex.getMessage().contains("Failed to download library"));
            }

            manager = new PluginManager();
            manager.allPlugins.put(p, true); // need to install
            p.setCandidateVersion("0.3");
            try {
                manager.applyChanges(new GenericCallback<String>() {
                    @Override
                    public void notify(String progress) {
                    }
                });
                fail();
            } catch (DownloadException ex) {
                assertTrue(ex.getMessage().contains("Failed to download plugin"));
            }

        } finally {
{'fraction_non_alphanumeric': 0.137559460246578, 'fraction_numerical': 0.008737015823706435, 'mean_word_length': 2.1086300543150274, 'pattern_counts': {'":': 28, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6439965', 'n_tokens_mistral': 2908, 'n_tokens_neox': 2745, 'n_words': 572}