package kg.apc.emulators;

import kg.apc.jmeter.DirectoryAnchor;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.tree.JMeterTreeListener;
import org.apache.jmeter.gui.tree.JMeterTreeModel;
import org.apache.jmeter.threads.*;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import org.junit.Test;

import java.io.*;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class TestJMeterUtils {

    /**
     *
     */
    public static void createJmeterEnv() {
        File propsFile = null;
        try {
            propsFile = File.createTempFile("jmeter-plugins", ".properties");
            propsFile.deleteOnExit();
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }

        //propsFile=new File("/home/undera/NetBeansProjects/jmeter/trunk/bin/jmeter.properties");

        assert propsFile != null;
        JMeterUtils.loadJMeterProperties(propsFile.getAbsolutePath());
        JMeterUtils.setJMeterHome(new DirectoryAnchor().toString());
        JMeterUtils.setLocale(new Locale("ignoreResources"));

        JMeterTreeModel jMeterTreeModel = new JMeterTreeModel();
        JMeterTreeListener jMeterTreeListener = new JMeterTreeListener();
        jMeterTreeListener.setModel(jMeterTreeModel);
        GuiPackage.getInstance(jMeterTreeListener, jMeterTreeModel);
        JMeterContextService.getContext().setVariables(new JMeterVariables());
        StandardJMeterEngine engine = new EmulatorJmeterEngine();
        JMeterThreadMonitor monitor = new EmulatorThreadMonitor();
        JMeterContextService.getContext().setEngine(engine);
        HashTree hashtree = new HashTree();
        hashtree.add(new LoopController());
        JMeterThread thread = new JMeterThread(hashtree, monitor, null);
        thread.setThreadName("test thread");
        JMeterContextService.getContext().setThread(thread);
        ThreadGroup threadGroup = new org.apache.jmeter.threads.ThreadGroup();
        threadGroup.setName("test thread group");
        JMeterContextService.getContext().setThreadGroup(threadGroup);
        JMeterUtils.setProperty("sample_variables", "TEST1,TEST2,TEST3"); // for Flexible File Writer Test        
    }

    public static String getTempDir() {
        File f = null;
        try {
            f = File.createTempFile("jmeterplugins", ".tmp");
        } catch (IOException ex) {
            Logger.getLogger(TestJMeterUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        assert f != null;
        return f.getParent();
    }

    public static String getTestData(int i) {
        return RandomStringUtils.randomAlphanumeric(i);
    }

    /**
     *
     */
    @Test
    public void testEnv() {
        TestJMeterUtils.createJmeterEnv();
        GuiPackage.getInstance().updateCurrentNode();
    }

    public static String fixWinPath(String path) {
        String ret = path;
        //test if win os
        boolean isWinOs = System.getProperty("os.name").toLowerCase().contains("win");

        //fix only files in "document and settings", for computer with no admin / C: drive access...
        if (isWinOs) {
            ret = ret.replace("Documents%20and%20Settings", "DOCUME~1");
            ret = ret.replace("Local%20Settings", "LOCALS~1");
            ret = ret.replace("Application%20Data", "APPLIC~1");
        }

        return ret;
    }

    public static String convertStreamToString(InputStream is)
            throws IOException {
        /*
         * To convert the InputStream to String we use the Reader.read(char[]
         * buffer) method. We iterate until the Reader return -1 which means
         * there's no more data to read. We use the StringWriter class to
         * produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }
}

@@ -24,46 +24,8 @@
 s;%0A%0A
-import kg.apc.jmeter.DirectoryAnchor;%0A
 impo
@@ -316,24 +316,67 @@
 rTreeModel;%0A
+import org.apache.jmeter.save.SaveService;%0A
 import org.a
@@ -710,39 +710,16 @@
 tils %7B%0A%0A
-    /**%0A     *%0A     */%0A
     publ
@@ -1008,107 +1008,8 @@
  %7D%0A%0A
-        //propsFile=new File(%22/home/undera/NetBeansProjects/jmeter/trunk/bin/jmeter.properties%22);%0A%0A
     
@@ -1147,38 +1147,18 @@
 ome(
-new DirectoryAnchor().toString
+getTempDir
 ());
@@ -1418,77 +1418,8 @@
 l);%0A
-        GuiPackage.getInstance(jMeterTreeListener, jMeterTreeModel);%0A
     
@@ -2260,24 +2260,25 @@
 ter Test
+%0A
         
 %0A    %7D%0A%0A
@@ -2269,16 +2269,350 @@
         
+JMeterUtils.setProperty(%22saveservice_properties%22, %22/ss.props%22);%0A        File f = new File(JMeterUtils.getJMeterHome() + %22/ss.props%22);%0A        f.getParentFile().mkdirs();%0A%0A        try %7B%0A            f.createNewFile();%0A            SaveService.loadProperties();%0A        %7D catch (IOException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A    %7D%0A%0A
@@ -3062,31 +3062,8 @@
  %7D%0A%0A
-    /**%0A     *%0A     */%0A
     
@@ -3089,24 +3089,43 @@
 id testEnv()
+ throws IOException
  %7B%0A        T

Fix GUI package presence problems