Java tutorial
/* * 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.teststeps; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.ImageIcon; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.eviware.soapui.config.CallConfig; import com.eviware.soapui.config.RequestStepConfig; import com.eviware.soapui.config.TestStepConfig; import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem; import com.eviware.soapui.impl.wsdl.WsdlInterface; import com.eviware.soapui.impl.wsdl.WsdlOperation; import com.eviware.soapui.impl.wsdl.WsdlRequest; import com.eviware.soapui.impl.wsdl.actions.support.ShowOnlineHelpAction; import com.eviware.soapui.impl.wsdl.panels.teststeps.WsdlTestRequestPanelBuilder; import com.eviware.soapui.impl.wsdl.submit.transports.http.WsdlResponse; import com.eviware.soapui.impl.wsdl.support.HelpUrls; import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase; import com.eviware.soapui.impl.wsdl.teststeps.WsdlRequestAssertion.AssertionStatus; import com.eviware.soapui.impl.wsdl.teststeps.actions.CloneTestStepAction; import com.eviware.soapui.impl.wsdl.teststeps.actions.SelectOperationAction; import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowRequestStepResultsAction; import com.eviware.soapui.impl.wsdl.teststeps.assertions.AssertionError; import com.eviware.soapui.model.PanelBuilder; import com.eviware.soapui.model.iface.Interface; import com.eviware.soapui.model.iface.Operation; import com.eviware.soapui.model.iface.Submit; import com.eviware.soapui.model.iface.Request.SubmitException; import com.eviware.soapui.model.project.Project; import com.eviware.soapui.model.support.InterfaceListenerAdapter; import com.eviware.soapui.model.support.ProjectListenerAdapter; import com.eviware.soapui.model.support.TestStepBeanProperty; import com.eviware.soapui.model.support.TestSuiteListenerAdapter; import com.eviware.soapui.model.testsuite.LoadTestRunner; import com.eviware.soapui.model.testsuite.TestRunContext; import com.eviware.soapui.model.testsuite.TestRunner; import com.eviware.soapui.model.testsuite.TestStep; import com.eviware.soapui.model.testsuite.TestStepResult; import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus; import com.eviware.soapui.support.action.ActionSupport; /** * WsdlTestStep that executes a WsdlTestRequest * * @author Ole.Matzura */ public class WsdlTestRequestStep extends WsdlTestStep implements PropertyChangeListener { private final static Log log = LogFactory.getLog(WsdlTestRequestStep.class); private RequestStepConfig requestStepConfig; private WsdlTestRequest testRequest; private WsdlOperation wsdlOperation; private final InternalProjectListener projectListener = new InternalProjectListener(); private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener(); private final InternalTestSuiteListener testSuiteListener = new InternalTestSuiteListener(); public WsdlTestRequestStep(WsdlTestCase testCase, TestStepConfig config) { super(testCase, config, true); // testCase.addTestRunListener( testRunListener ); if (testCase != null && testCase.getTestSuite() != null) testCase.getTestSuite().addTestSuiteListener(testSuiteListener); if (getConfig().getConfig() != null) { requestStepConfig = (RequestStepConfig) getConfig().getConfig().changeType(RequestStepConfig.type); wsdlOperation = findWsdlOperation(); if (wsdlOperation == null) { log.error("Could not find operation [" + requestStepConfig.getOperation() + "] in interface [" + requestStepConfig.getInterface() + "] for test request"); requestStepConfig.setRequest(null); } else { wsdlOperation.getInterface().getProject().addProjectListener(projectListener); wsdlOperation.getInterface().addInterfaceListener(interfaceListener); // we need to listen for name changes which happen when interfaces are updated.. wsdlOperation.getInterface().addPropertyChangeListener(this); wsdlOperation.addPropertyChangeListener(this); testRequest = new WsdlTestRequest(wsdlOperation, requestStepConfig.getRequest(), this); testRequest.addPropertyChangeListener(this); config.setName(testRequest.getName()); } } else { requestStepConfig = (RequestStepConfig) getConfig().addNewConfig().changeType(RequestStepConfig.type); } addAction(ActionSupport.SEPARATOR_ACTION); addAction(new SelectOperationAction(this)); addAction(new CloneTestStepAction(this, "TestRequest")); addAction(ActionSupport.SEPARATOR_ACTION); addAction(new ShowOnlineHelpAction(HelpUrls.TESTREQUEST_HELP_URL)); // init properties addProperty(new TestStepBeanProperty("Endpoint", false, testRequest, "endpoint", this)); addProperty(new TestStepBeanProperty("Username", false, testRequest, "username", this)); addProperty(new TestStepBeanProperty("Password", false, testRequest, "password", this)); addProperty(new TestStepBeanProperty("Domain", false, testRequest, "domain", this)); addProperty(new TestStepBeanProperty("Request", false, testRequest, "requestContent", this)); addProperty(new TestStepBeanProperty("Response", true, testRequest, "responseContent", this)); } public WsdlTestRequestStep(WsdlTestCase testCase, TestStepConfig testStep, WsdlRequest request) { this(testCase, testStep); requestStepConfig.setInterface(request.getOperation().getInterface().getName()); requestStepConfig.setOperation(request.getOperation().getName()); CallConfig testRequestConfig = requestStepConfig.getRequest(); if (testRequestConfig == null) testRequestConfig = requestStepConfig.addNewRequest(); testRequestConfig.setName(request.getName()); testRequestConfig.setEncoding(request.getEncoding()); testRequestConfig.setEndpoint(request.getEndpoint()); testRequestConfig.setRequest(request.getRequestContent()); if (request.getConfig().getCredentials() != null) testRequestConfig.setCredentials(request.getConfig().getCredentials()); testRequest = new WsdlTestRequest(request.getOperation(), testRequestConfig, this); request.copyAttachmentsTo(testRequest); testRequest.addPropertyChangeListener(this); wsdlOperation = findWsdlOperation(); if (wsdlOperation == null) throw new RuntimeException( "Failed to find operation [" + requestStepConfig.getOperation() + "] for test request"); wsdlOperation.getInterface().addInterfaceListener(interfaceListener); wsdlOperation.getInterface().getProject().addProjectListener(projectListener); } public WsdlTestRequestStep(WsdlTestCase testCase, WsdlTestRequest sourceRequest) { this(testCase, TestStepConfig.Factory.newInstance(), sourceRequest); } public WsdlTestStep clone(WsdlTestCase targetTestCase, String name) { TestStepConfig config = (TestStepConfig) getConfig().copy(); RequestStepConfig stepConfig = (RequestStepConfig) config.getConfig().changeType(RequestStepConfig.type); while (stepConfig.getRequest().sizeOfAttachmentArray() > 0) stepConfig.getRequest().removeAttachment(0); config.setName(name); stepConfig.getRequest().setName(name); WsdlTestRequestStep result = (WsdlTestRequestStep) targetTestCase.addTestStep(config); testRequest.copyAttachmentsTo(result.getTestRequest()); return result; } private WsdlOperation findWsdlOperation() { WsdlTestCase testCase = (WsdlTestCase) getTestCase(); if (testCase == null || testCase.getTestSuite() == null) return null; Project project = testCase.getTestSuite().getProject(); WsdlOperation operation = null; for (int c = 0; c < project.getInterfaceCount(); c++) { if (project.getInterfaceAt(c).getName().equals(requestStepConfig.getInterface())) { WsdlInterface iface = (WsdlInterface) project.getInterfaceAt(c); for (int i = 0; i < iface.getOperationCount(); i++) { if (iface.getOperationAt(i).getName().equals(requestStepConfig.getOperation())) { operation = (WsdlOperation) iface.getOperationAt(i); break; } } if (operation != null) break; } } return operation; } public void release() { super.release(); wsdlOperation = findWsdlOperation(); wsdlOperation.removePropertyChangeListener(this); wsdlOperation.getInterface().getProject().removeProjectListener(projectListener); wsdlOperation.getInterface().removeInterfaceListener(interfaceListener); wsdlOperation.getInterface().removePropertyChangeListener(this); testRequest.removePropertyChangeListener(this); testRequest.release(); } public void resetConfigOnMove(TestStepConfig config) { super.resetConfigOnMove(config); requestStepConfig = (RequestStepConfig) config.getConfig().changeType(RequestStepConfig.type); testRequest.updateConfig(requestStepConfig.getRequest()); } public ImageIcon getIcon() { return testRequest.getIcon(); } public String getName() { return testRequest == null ? super.getName() : testRequest.getName(); } public WsdlTestRequest getTestRequest() { return testRequest; } public void setName(String name) { testRequest.setName(name); } protected PanelBuilder createPanelBuilder() { return new WsdlTestRequestPanelBuilder(testRequest); } public void propertyChange(PropertyChangeEvent arg0) { if (arg0.getSource() == wsdlOperation) { if (arg0.getPropertyName().equals(Operation.NAME_PROPERTY)) { requestStepConfig.setOperation((String) arg0.getNewValue()); } } else if (arg0.getSource() == wsdlOperation.getInterface()) { if (arg0.getPropertyName().equals(Interface.NAME_PROPERTY)) { requestStepConfig.setInterface((String) arg0.getNewValue()); } } else { notifyPropertyChanged(arg0.getPropertyName(), arg0.getOldValue(), arg0.getNewValue()); } } public TestStepResult run(TestRunner runner, TestRunContext runContext) { LoadTestRunner loadTestRunner = (LoadTestRunner) runContext.getProperty(TestRunContext.LOAD_TEST_RUNNER); if (loadTestRunner != null) { testRequest.getIconManager().setEnabled(false); } WsdlTestRequestStepResult testStepResult = new WsdlTestRequestStepResult(this); testStepResult.addAction(new ShowRequestStepResultsAction(testStepResult), true); try { Submit submit = testRequest.submit(runContext, false); WsdlResponse response = (WsdlResponse) submit.getResponse(); if (submit.getStatus() != Submit.Status.CANCELED) { if (response == null) { testStepResult.setStatus(TestStepStatus.FAILED); testStepResult.addMessage("Request is missing response"); } else { testRequest.setResponse(response, runContext); testStepResult.setTimeTaken(response.getTimeTaken()); testStepResult.setSize(response.getContentLength()); testStepResult.setResponse(response); } } else { testStepResult.setStatus(TestStepStatus.CANCELED); testStepResult.addMessage("Request was canceled"); } if (response != null) testStepResult.setRequestContent(response.getRequestContent()); } catch (SubmitException e) { testStepResult.setStatus(TestStepStatus.FAILED); testStepResult.addMessage("SubmitException: " + e); } testStepResult.setDomain(testRequest.getDomain()); testStepResult.setUsername(testRequest.getUsername()); testStepResult.setPassword(testRequest.getPassword()); testStepResult.setEndpoint(testRequest.getEndpoint()); testStepResult.setEncoding(testRequest.getEncoding()); AssertionStatus assertionStatus = testRequest.getAssertionStatus(); switch (assertionStatus) { case FAILED: { testStepResult.setStatus(TestStepStatus.FAILED); for (int c = 0; c < getAssertionCount(); c++) { AssertionError[] errors = getAssertionAt(c).getErrors(); if (errors != null) { for (AssertionError error : errors) { testStepResult.addMessage(error.getMessage()); } } } break; } default: testStepResult.setStatus(TestStepStatus.OK); break; } testRequest.getIconManager().setEnabled(true); return testStepResult; } public WsdlRequestAssertion getAssertionAt(int index) { return testRequest.getAssertionAt(index); } public int getAssertionCount() { return testRequest.getAssertionCount(); } public class InternalProjectListener extends ProjectListenerAdapter { public void interfaceRemoved(Interface iface) { if (wsdlOperation != null && wsdlOperation.getInterface().equals(iface)) { log.debug("Removing test step due to removed interface"); ((WsdlTestCase) getTestCase()).removeTestStep(WsdlTestRequestStep.this); } } } public class InternalInterfaceListener extends InterfaceListenerAdapter { public void operationRemoved(Operation operation) { if (operation.equals(wsdlOperation)) { ((WsdlTestCase) getTestCase()).removeTestStep(WsdlTestRequestStep.this); } } } private class InternalTestSuiteListener extends TestSuiteListenerAdapter { public void testStepRemoved(TestStep testStep, int index) { if (testStep == WsdlTestRequestStep.this) { log.debug("Releasing WsdlTestRequestStep"); wsdlOperation.getInterface().removeInterfaceListener(interfaceListener); wsdlOperation.getInterface().getProject().removeProjectListener(projectListener); getTestCase().getTestSuite().removeTestSuiteListener(testSuiteListener); } } } public boolean cancel() { return false; } public boolean dependsOn(AbstractWsdlModelItem modelItem) { if (modelItem instanceof Interface && testRequest.getOperation().getInterface() == modelItem) { return true; } else if (modelItem instanceof Operation && testRequest.getOperation() == modelItem) { return true; } return false; } }