001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.actions.iface;
014:
015: import java.util.Arrays;
016: import java.util.List;
017:
018: import com.eviware.soapui.impl.wsdl.WsdlInterface;
019: import com.eviware.soapui.impl.wsdl.WsdlOperation;
020: import com.eviware.soapui.impl.wsdl.WsdlProject;
021: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
022: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
023: import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestRequestStepFactory;
024: import com.eviware.soapui.model.support.ModelSupport;
025: import com.eviware.soapui.support.UISupport;
026: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
027: import com.eviware.x.form.XFormDialog;
028: import com.eviware.x.form.XFormOptionsField;
029: import com.eviware.x.form.support.ADialogBuilder;
030: import com.eviware.x.form.support.AField;
031: import com.eviware.x.form.support.AForm;
032: import com.eviware.x.form.support.AField.AFieldType;
033:
034: /**
035: * Generates a TestSuite for the specified Interface
036: *
037: * @author ole.matzura
038: */
039:
040: public class GenerateTestSuiteAction extends
041: AbstractSoapUIAction<WsdlInterface> {
042: public GenerateTestSuiteAction() {
043: super ("Generate TestSuite",
044: "Generates TestSuite with TestCase(s) for all Operations in this Interface");
045: }
046:
047: public void perform(WsdlInterface target, Object param) {
048: generateTestSuite(target);
049: }
050:
051: public WsdlTestSuite generateTestSuite(WsdlInterface iface) {
052: XFormDialog dialog = ADialogBuilder
053: .buildDialog(GenerateForm.class);
054: dialog.setValue(GenerateForm.STYLE,
055: "One TestCase for each Operation");
056: dialog.setValue(GenerateForm.REQUEST_CONTENT,
057: "Create new empty requests");
058: String[] names = ModelSupport.getNames(iface.getOperations());
059: dialog.setOptions(GenerateForm.OPERATIONS, names);
060: XFormOptionsField operationsFormField = (XFormOptionsField) dialog
061: .getFormField(GenerateForm.OPERATIONS);
062: operationsFormField.setSelectedOptions(names);
063:
064: WsdlProject project = (WsdlProject) iface.getProject();
065: String[] testSuites = ModelSupport.getNames(
066: new String[] { "<create>" }, project.getTestSuites());
067: dialog.setOptions(GenerateForm.TESTSUITE, testSuites);
068:
069: if (dialog.show()) {
070: List<String> operations = Arrays.asList(operationsFormField
071: .getSelectedOptions());
072: if (operations.size() == 0) {
073: UISupport.showErrorMessage("No Operations selected..");
074: return null;
075: }
076:
077: String testSuiteName = dialog
078: .getValue(GenerateForm.TESTSUITE);
079:
080: if (testSuiteName.equals("<create>"))
081: testSuiteName = UISupport.prompt(
082: "Enter name of TestSuite to create",
083: "Generate TestSuite", iface.getName()
084: + " TestSuite");
085:
086: if (testSuiteName != null
087: && testSuiteName.trim().length() > 0) {
088: WsdlTestSuite testSuite = (WsdlTestSuite) project
089: .getTestSuiteByName(testSuiteName);
090:
091: if (testSuite == null) {
092: testSuite = (WsdlTestSuite) project
093: .addNewTestSuite(testSuiteName);
094: }
095:
096: int style = dialog.getValueIndex(GenerateForm.STYLE);
097: boolean useExistingRequests = dialog
098: .getValueIndex(GenerateForm.REQUEST_CONTENT) == 0;
099: boolean generateLoadTest = dialog
100: .getBooleanValue(GenerateForm.GENERATE_LOADTEST);
101: if (style == 0) {
102: generateMulipleTestCases(testSuite, iface,
103: useExistingRequests, generateLoadTest,
104: operations);
105: } else if (style == 1) {
106: generateSingleTestCase(testSuite, iface,
107: useExistingRequests, generateLoadTest,
108: operations);
109: }
110:
111: return testSuite;
112: }
113: }
114:
115: return null;
116: }
117:
118: private void generateSingleTestCase(WsdlTestSuite testSuite,
119: WsdlInterface iface, boolean useExisting,
120: boolean createLoadTest, List<String> operations) {
121: WsdlTestCase testCase = testSuite.addNewTestCase(iface
122: .getName()
123: + " TestSuite");
124:
125: for (int i = 0; i < iface.getOperationCount(); i++) {
126: WsdlOperation operation = (WsdlOperation) iface
127: .getOperationAt(i);
128: if (!operations.contains(operation.getName()))
129: continue;
130:
131: testCase.addTestStep(WsdlTestRequestStepFactory
132: .createConfig(operation, operation.getName()));
133: }
134:
135: if (createLoadTest) {
136: testCase.addNewLoadTest("LoadTest 1");
137: }
138:
139: UISupport.showDesktopPanel(testCase);
140: }
141:
142: private void generateMulipleTestCases(WsdlTestSuite testSuite,
143: WsdlInterface iface, boolean useExisting,
144: boolean createLoadTest, List<String> operations) {
145: for (int i = 0; i < iface.getOperationCount(); i++) {
146: WsdlOperation operation = (WsdlOperation) iface
147: .getOperationAt(i);
148: if (!operations.contains(operation.getName()))
149: continue;
150:
151: WsdlTestCase testCase = testSuite.addNewTestCase(operation
152: .getName()
153: + " TestCase");
154:
155: if (useExisting && operation.getRequestCount() > 0) {
156: for (int x = 0; x < operation.getRequestCount(); x++) {
157: testCase.addTestStep(WsdlTestRequestStepFactory
158: .createConfig(operation.getRequestAt(x),
159: operation.getName()));
160: }
161: } else {
162: testCase.addTestStep(WsdlTestRequestStepFactory
163: .createConfig(operation, operation.getName()));
164: }
165:
166: if (createLoadTest) {
167: testCase.addNewLoadTest("LoadTest 1");
168: }
169: }
170:
171: UISupport.showDesktopPanel(testSuite);
172: }
173:
174: @AForm(name="Generate TestSuite",description="Generates TestSuite with TestCase(s) for all Operations in this Interface")
175: private class GenerateForm {
176: @AField(name="TestSuite",description="The TestSuite to create or use",type=AFieldType.ENUMERATION)
177: public final static String TESTSUITE = "TestSuite";
178:
179: @AField(name="Style",description="Select the style of TestCases to create",type=AFieldType.RADIOGROUP,values={"One TestCase for each Operation","Single TestCase with one Request for each Operation"})
180: public final static String STYLE = "Style";
181:
182: @AField(name="Request Content",description="Select how to create Test Requests",type=AFieldType.RADIOGROUP,values={"Use existing Requests in Interface","Create new empty requests"})
183: public final static String REQUEST_CONTENT = "Request Content";
184:
185: @AField(name="Operations",description="The Operations for which to Generate Tests",type=AFieldType.MULTILIST)
186: public final static String OPERATIONS = "Operations";
187:
188: @AField(name="Generate LoadTest",description="Generates a default LoadTest for each created TestCase",type=AFieldType.BOOLEAN)
189: public final static String GENERATE_LOADTEST = "Generate LoadTest";
190: }
191: }
|