com.eviware.soapui.impl.wsdl.panels.teststeps.WsdlTestRequestDesktopPanel.java Source code

Java tutorial

Introduction

Here is the source code for com.eviware.soapui.impl.wsdl.panels.teststeps.WsdlTestRequestDesktopPanel.java

Source

/*
 *  soapui, copyright (C) 2006 eviware.com 
 *
 *  SoapUI is free software; you can redistribute it and/or modify it under the 
 *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
 *  either version 2.1 of the License, or (at your option) any later version.
 *
 *  SoapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 *  See the GNU Lesser General Public License for more details at gnu.org.
 */

package com.eviware.soapui.impl.wsdl.panels.teststeps;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JToolBar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.eviware.soapui.SoapUI;
import com.eviware.soapui.impl.wsdl.WsdlSubmitContext;
import com.eviware.soapui.impl.wsdl.panels.request.AbstractWsdlRequestDesktopPanel;
import com.eviware.soapui.impl.wsdl.panels.request.components.SoapMessageXmlEditor;
import com.eviware.soapui.impl.wsdl.panels.request.components.editor.support.ValidationError;
import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.XmlOutlineEditor;
import com.eviware.soapui.impl.wsdl.panels.request.components.editor.views.XmlSourceEditor;
import com.eviware.soapui.impl.wsdl.panels.support.XmlObjectTree;
import com.eviware.soapui.impl.wsdl.panels.support.XmlObjectTreeModel.XmlTreeNode;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.AddXPathContentAssertionAction;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.AddXPathContentAssertionFromSource;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.TransferFromPropertyAction;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.TransferFromPropertyFromSource;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.TransferToPropertyAction;
import com.eviware.soapui.impl.wsdl.panels.teststeps.actions.TransferToPropertyFromSource;
import com.eviware.soapui.impl.wsdl.support.HelpUrls;
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
import com.eviware.soapui.impl.wsdl.teststeps.actions.AddAssertionAction;
import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError;
import com.eviware.soapui.model.ModelItem;
import com.eviware.soapui.model.iface.Submit;
import com.eviware.soapui.model.iface.SubmitContext;
import com.eviware.soapui.model.iface.Request.SubmitException;
import com.eviware.soapui.model.testsuite.LoadTestRunner;
import com.eviware.soapui.model.testsuite.TestRunner;
import com.eviware.soapui.monitor.support.TestMonitorListenerAdapter;
import com.eviware.soapui.support.UISupport;
import com.eviware.soapui.support.xml.JXEditTextArea;

/**
 * DesktopPanel for WsdlTestRequest. Essentially a copy of WsdlRequestDesktopPanel with assertions.
 * 
 * @author Ole.Matzura
 */

public class WsdlTestRequestDesktopPanel
        extends AbstractWsdlRequestDesktopPanel<WsdlTestRequestStep, WsdlTestRequest> {
    private final static Log log = LogFactory.getLog(WsdlTestRequestDesktopPanel.class);
    private JTextArea logArea;
    private InternalTestMonitorListener testMonitorListener = new InternalTestMonitorListener();
    private JButton addAssertionButton;
    protected boolean updatingRequest;
    private WsdlTestRequestDesktopPanel.InternalSubmitListener submitListener;
    private AssertionsPanel assertionsPanel;
    protected XmlTreeNode selectedTreeNode;
    private JSplitPane outerSplit;

    public WsdlTestRequestDesktopPanel(WsdlTestRequestStep requestStep) {
        super(requestStep);

        init(requestStep.getTestRequest());

        SoapUI.getTestMonitor().addTestMonitorListener(testMonitorListener);
        setEnabled(!SoapUI.getTestMonitor().hasRunningTest(requestStep.getTestCase()));
    }

    private Component buildLogPanel() {
        JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT);
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

        logArea = new JTextArea();
        logArea.setEditable(false);
        logArea.setToolTipText("Request Log");

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(new JScrollPane(logArea), BorderLayout.CENTER);

        tabbedPane.addTab("Assertions", buildAssertionsPanel());
        tabbedPane.addTab("Request Log", panel);
        return UISupport.createTabPanel(tabbedPane, true);
    }

    private Component buildAssertionsPanel() {
        assertionsPanel = new AssertionsPanel(getRequest()) {
            protected void selectError(AssertionError error) {
                SoapMessageXmlEditor editor = getResponseEditor();
                //editor.getCurrentView().setCurrentLine( error.getLineNumber()-1 );
                editor.requestFocus();
            }
        };

        return assertionsPanel;
    }

    public void setContent(JComponent content) {
        outerSplit.setTopComponent(content);
    }

    public void removeContent(JComponent content) {
        outerSplit.setTopComponent(null);
    }

    protected String getHelpUrl() {
        return HelpUrls.TESTREQUESTEDITOR_HELP_URL;
    }

    protected JComponent buildContent() {
        JComponent component = super.buildContent();

        outerSplit = UISupport.createVerticalSplit();
        outerSplit.setTopComponent(component);
        outerSplit.setBottomComponent(buildLogPanel());
        outerSplit.setDividerLocation(350);
        outerSplit.setResizeWeight(0.9);
        outerSplit.setBorder(null);

        return outerSplit;
    }

    protected SoapMessageXmlEditor buildRequestEditor() {
        SoapMessageXmlEditor editor = super.buildRequestEditor();

        XmlOutlineEditor outlineEditor = editor.getOutlineEditor();
        if (outlineEditor != null) {
            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.add(new TransferFromPropertyAction(getRequest(), outlineEditor));

            outlineEditor.getOutlineTable().setComponentPopupMenu(popupMenu);

            XmlSourceEditor sourceEditor = editor.getSourceEditor();
            if (sourceEditor != null) {
                JPopupMenu editorPopup = sourceEditor.getEditorPopup();
                editorPopup.insert(new TransferFromPropertyFromSource(getRequest(), sourceEditor, outlineEditor),
                        0);
            }
        }

        return editor;
    }

    protected SoapMessageXmlEditor buildResponseEditor() {
        SoapMessageXmlEditor editor = super.buildResponseEditor();

        XmlOutlineEditor outlineEditor = editor.getOutlineEditor();
        if (outlineEditor != null) {
            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.add(new AddXPathContentAssertionAction(getRequest(), outlineEditor));
            popupMenu.add(new TransferToPropertyAction(getRequest(), outlineEditor));

            outlineEditor.getOutlineTable().setComponentPopupMenu(popupMenu);

            XmlSourceEditor sourceEditor = editor.getSourceEditor();
            if (sourceEditor != null) {
                JPopupMenu editorPopup = sourceEditor.getEditorPopup();
                editorPopup.insert(
                        new AddXPathContentAssertionFromSource(getRequest(), sourceEditor, outlineEditor), 0);
                editorPopup.insert(new TransferToPropertyFromSource(getRequest(), sourceEditor, outlineEditor), 0);
            }
        }

        return editor;
    }

    protected JComponent buildToolbar() {
        addAssertionButton = createActionButton(new AddAssertionAction(getRequest()), true);
        return super.buildToolbar();
    }

    protected void insertButtons(JToolBar toolbar) {
        toolbar.add(addAssertionButton);
    }

    public void setEnabled(boolean enabled) {
        if (enabled == true)
            enabled = !SoapUI.getTestMonitor().hasRunningLoadTest(getModelItem().getTestCase());

        super.setEnabled(enabled);
        addAssertionButton.setEnabled(enabled);
        assertionsPanel.setEnabled(enabled);

        if (SoapUI.getTestMonitor().hasRunningLoadTest(getRequest().getTestCase())) {
            getRequest().removeSubmitListener(submitListener);
        } else {
            getRequest().addSubmitListener(submitListener);
        }
    }

    protected Submit doSubmit() throws SubmitException {
        return getRequest().submit(new WsdlSubmitContext(getModelItem()), true);
    }

    protected InternalSubmitListener createSubmitListener() {
        submitListener = new InternalSubmitListener();
        return submitListener;
    }

    private class InternalSubmitListener extends AbstractWsdlRequestDesktopPanel.InternalSubmitListener {
        private long startTime;
        private SimpleDateFormat sdf;

        private InternalSubmitListener() {
            super();
            sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }

        public boolean beforeSubmit(Submit submit, SubmitContext context) {
            boolean result = super.beforeSubmit(submit, context);
            startTime = System.currentTimeMillis();
            return result;
        }

        protected void logMessages(String message, String infoMessage) {
            super.logMessages(message, infoMessage);

            String logText = logArea.getText() + sdf.format(new Date(startTime)) + " - " + message + "\r\n";
            logArea.setText(logText);
            logArea.setCaretPosition(logText.length());
        }
    }

    public boolean onClose(boolean canCancel) {
        if (super.onClose(canCancel)) {
            assertionsPanel.release();
            SoapUI.getTestMonitor().removeTestMonitorListener(testMonitorListener);

            return true;
        }

        return false;
    }

    private final static class ValidationListMouseAdapter extends MouseAdapter {
        private final JList list;
        private final JXEditTextArea textArea;

        public ValidationListMouseAdapter(JList list, JXEditTextArea textArea) {
            this.list = list;
            this.textArea = textArea;
        }

        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() < 2)
                return;

            int ix = list.getSelectedIndex();
            if (ix == -1)
                return;

            Object obj = list.getModel().getElementAt(ix);
            if (obj instanceof AssertionError) {
                AssertionError error = (AssertionError) obj;
                if (error.getLineNumber() >= 0) {
                    textArea.setCaretPosition(textArea.getLineStartOffset(error.getLineNumber() - 1));
                    textArea.requestFocus();
                } else
                    Toolkit.getDefaultToolkit().beep();
            } else
                Toolkit.getDefaultToolkit().beep();
        }
    }

    public boolean dependsOn(ModelItem modelItem) {
        return modelItem == getRequest() || modelItem == getModelItem() || modelItem == getRequest().getOperation()
                || modelItem == getRequest().getOperation().getInterface()
                || modelItem == getRequest().getOperation().getInterface().getProject()
                || modelItem == getModelItem().getTestCase()
                || modelItem == getModelItem().getTestCase().getTestSuite();
    }

    private class InternalTestMonitorListener extends TestMonitorListenerAdapter {
        public void loadTestFinished(LoadTestRunner runner) {
            setEnabled(!SoapUI.getTestMonitor().hasRunningTest(getModelItem().getTestCase()));
        }

        public void loadTestStarted(LoadTestRunner runner) {
            setEnabled(false);
        }

        public void testCaseFinished(TestRunner runner) {
            setEnabled(!SoapUI.getTestMonitor().hasRunningTest(getModelItem().getTestCase()));
        }

        public void testCaseStarted(TestRunner runner) {
            setEnabled(false);
        }
    }

    public String getTitle() {
        return "TestRequest: [" + getModelItem().getName() + "] - " + getModelItem().getTestStepTitle();
    }

    protected ValidationError[] validateResponseXml() {
        if (getSubmit() == null)
            getModelItem().getTestRequest().assertRequest(new WsdlSubmitContext(getModelItem()));

        return super.validateResponseXml();
    }

}