package kg.apc.jmeter.config;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import javax.swing.JTextArea;

public class TestCsvFileAction implements ActionListener {

    private final VariablesFromCSVGui variablesCsvUi;

    public TestCsvFileAction(VariablesFromCSVGui variablesCsvUi) {
        this.variablesCsvUi = variablesCsvUi;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
        infoArea.setText("");
        infoArea.setForeground(Color.black);

        VariablesFromCSV testElem = (VariablesFromCSV)variablesCsvUi.createTestElement();

        boolean noValues = true;
        String msgVars = "";
        int count = 0;

        try {
            Map<String, String> vars = testElem.getArgumentsAsMap();
            Iterator<String> iter = vars.keySet().iterator();

            while (iter.hasNext()) {
                String var = iter.next();
                String value = vars.get(var);
                if (!"".equals(value)) {
                    noValues = false;
                }
                msgVars = msgVars + "${" + var + "} = " + vars.get(var) + "\n";
                count++;
            }

            if (count == 0) {
                reportError("File parsed, but no variable found.");
            } else if (noValues) {
                reportOk("WARNING: File parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found, but no variable have value!");
                reportOk(msgVars);
            } else {
                reportOk("File successfuly parsed, " + count + " variable" + (count > 1 ? "s" : "") + " found:");
                reportOk(msgVars);
            }
        } catch (Exception ex) {
                reportError("Error processing file: " + ex.toString());
        }
    }

    private void reportError(String msg) {
        JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
        infoArea.setText(infoArea.getText() + "Problem detected: " + msg + "\n");
        infoArea.setForeground(Color.red);
    }

    private void reportOk(String string) {
        JTextArea infoArea = variablesCsvUi.getCheckInfoTextArea();
        infoArea.setText(infoArea.getText() + string + "\n");
    }
}

@@ -776,24 +776,31 @@
       String
+Builder
  msgVars = %22
@@ -798,18 +798,35 @@
 gVars = 
-%22%22
+new StringBuilder()
 ;%0A      
@@ -942,92 +942,64 @@
     
-Iterator%3CString%3E iter =
+for (Map.Entry%3CString, String%3E element :
  vars.
-k
 e
+ntr
 ySet()
-.iterator();%0A%0A            while (iter.hasNext()
 ) %7B%0A
@@ -1031,17 +1031,22 @@
 r = 
-iter.next
+element.getKey
 ();%0A
@@ -1076,28 +1076,33 @@
 value = 
-vars.get(var
+element.getValue(
 );%0A     
@@ -1220,63 +1220,96 @@
 Vars
- = msgVars + %22$%7B%22 + var + %22%7D = %22 + vars.get(var) + 
+.append(%22$%7B%22).append(var).append(%22%7D = %22)%0A                    .append(value).append(
 %22%5Cn%22
+)
 ;%0A  
@@ -1330,16 +1330,32 @@
 count++;
+                
 %0A       
@@ -1658,32 +1658,43 @@
 reportOk(msgVars
+.toString()
 );%0A            %7D
@@ -1847,16 +1847,27 @@
 (msgVars
+.toString()
 );%0A     

Fix inefficient iteration over map and String concat