码迷,mamicode.com
首页 > 其他好文 > 详细

编写JMeter扩展(2)编写采样器代码对应的GUI

时间:2015-04-07 20:02:22      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:jmeter

请参考如下的代码及注释:

package kg.apc.jmeter.samplers;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import kg.apc.jmeter.JMeterPluginsUtils;
import kg.apc.jmeter.gui.BrowseAction;
import kg.apc.jmeter.gui.GuiBuilderHelper;

import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

/**
 *
 * @author apc
 */
public class TLSRawSamplerGui
        extends AbstractSamplerGui {

    private static final Logger log = LoggingManager.getLoggerForClass();
    private JTextField hostName;
    private JTextField port;
    private JTextField tlsVersion;
    private JTextField cipherList;
    private JTextField clientCert;
    private JTextField clientCertPassword;
    private JTextArea requestData;
    

    /**
     *
     */
    public TLSRawSamplerGui() {
        init();
        initFields();
    }

    @Override
    public String getStaticLabel() {
        return "TLS Raw Request";
    }
    
    /**
     * Mirror data from TestElement to GUI
     */

    @Override
    public void configure(TestElement element) {
        super.configure(element);
        log.info(element.toString());

        if (element instanceof TLSRawSampler) {
            TLSRawSampler rawSampler = (TLSRawSampler) element;
            hostName.setText(rawSampler.getHostName());
            port.setText(rawSampler.getPort());
            tlsVersion.setText(rawSampler.getTLSVersion());
            cipherList.setText(rawSampler.getCipherList());
            clientCert.setText(rawSampler.getClientCert());
            clientCertPassword.setText(rawSampler.getClientCertPassword());
            requestData.setText(rawSampler.getData());
        }
    }

    @Override
    public TestElement createTestElement() {
        TLSRawSampler sampler = new TLSRawSampler();
        modifyTestElement(sampler);
        sampler.setComment("TLS Sampler Comment.");
        return sampler;
    }

    /**
     * Mirror data from GUI to TestElement
     */
    @Override
    public void modifyTestElement(TestElement sampler) {
        super.configureTestElement(sampler);

        if (sampler instanceof TLSRawSampler) {
            TLSRawSampler rawSampler = (TLSRawSampler) sampler;
            rawSampler.setHostName(hostName.getText());
            rawSampler.setPort(port.getText());
            rawSampler.setTLSVersion(tlsVersion.getText());
            rawSampler.setCipherList(cipherList.getText());
            rawSampler.setClientCert(clientCert.getText().trim());
            rawSampler.setClientCertPassword(clientCertPassword.getText().trim());
            rawSampler.setData(transformCRLF(requestData.getText()));
        }
    }

    /**
     * first replace removes old \r\n
     * second eliminates orphan \r
     * third make all newlines - old and new  like \r\n
     */
    private String transformCRLF(String str) {
        return str.replace("\r\n", "\n").replace("\r", "").replace("\n", "\r\n");
    }

    @Override
    public void clearGui() {
        super.clearGui();
        initFields();
    }

    @Override
    public String getLabelResource() {
        return this.getClass().getSimpleName();
    }

    private void init() {
        setLayout(new BorderLayout(0, 5));
        setBorder(makeBorder());

        add(JMeterPluginsUtils.addHelpLinkToPanel(makeTitlePanel(), "TLS Request"), BorderLayout.NORTH);

        JPanel mainPanel = new JPanel(new GridBagLayout());

        GridBagConstraints labelConstraints = new GridBagConstraints();
        labelConstraints.anchor = GridBagConstraints.FIRST_LINE_END;

        GridBagConstraints editConstraints = new GridBagConstraints();
        editConstraints.anchor = GridBagConstraints.FIRST_LINE_START;
        editConstraints.weightx = 1.0;
        editConstraints.fill = GridBagConstraints.HORIZONTAL;

        addToPanel(mainPanel, labelConstraints, 0, 1, new JLabel("Hostname: ", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 1, hostName = new JTextField(20));
        
        addToPanel(mainPanel, labelConstraints, 0, 2, new JLabel("Port: ", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 2, port = new JTextField(20));

        editConstraints.insets = new java.awt.Insets(2, 0, 0, 0);
        labelConstraints.insets = new java.awt.Insets(2, 0, 0, 0);

        addToPanel(mainPanel, labelConstraints, 0, 3, new JLabel("TLS Version: ", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 3, tlsVersion = new JTextField(20));

        addToPanel(mainPanel, labelConstraints, 0, 4, new JLabel("Cipher List: ", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 4, cipherList = new JTextField(20));
        
        addToPanel(mainPanel, labelConstraints, 0, 5, new JLabel("Client Certificate PKCS File: ", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 5, clientCert = new JTextField(20));
        
        addToPanel(mainPanel, labelConstraints, 0, 6, new JLabel("Client Certificate PKCS Password", JLabel.RIGHT));
        addToPanel(mainPanel, editConstraints, 1, 6, clientCertPassword = new JTextField(20));

        addToPanel(mainPanel, labelConstraints, 0, 7, new JLabel("Request Data: ", JLabel.RIGHT));

        editConstraints.fill = GridBagConstraints.BOTH;
        requestData = new JTextArea();
        addToPanel(mainPanel, editConstraints, 1, 7, GuiBuilderHelper.getTextAreaScrollPaneContainer(requestData, 20));


        editConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        labelConstraints.insets = new java.awt.Insets(4, 0, 0, 0);


        JPanel container = new JPanel(new BorderLayout());
        container.add(mainPanel, BorderLayout.NORTH);
        add(container, BorderLayout.CENTER);
    }

    private void addToPanel(JPanel panel, GridBagConstraints constraints, int col, int row, JComponent component) {
        constraints.gridx = col;
        constraints.gridy = row;
        panel.add(component, constraints);
    }

    private void initFields() {
        hostName.setText("${HostName}");
        tlsVersion.setText("TLSv1.2");
        cipherList.setText("");
        clientCert.setText("c:\\pkcs");
        clientCertPassword.setText("passw0rd");
        requestData.setText("GET / HTTP/1.0\r\n"
                + "Host: localhost\r\n"
                + "Connection: close\r\n"
                + "\r\n");
    }
}


最终我们实现的界面如下:

技术分享

编写JMeter扩展(2)编写采样器代码对应的GUI

标签:jmeter

原文地址:http://coderlouis.blog.51cto.com/10063254/1629642

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!