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.support;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: import com.eviware.soapui.SoapUI;
019: import com.eviware.soapui.impl.wsdl.WsdlProject;
020: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
021: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
022: import com.eviware.soapui.model.ModelItem;
023: import com.eviware.soapui.model.testsuite.TestSuite;
024: import com.eviware.soapui.support.UISupport;
025: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
026:
027: /**
028: * Base class for actions that add TestSteps to a TestCase
029: *
030: * @author ole.matzura
031: */
032:
033: public abstract class AbstractAddToTestCaseAction<T extends ModelItem>
034: extends AbstractSoapUIAction<T> {
035: public AbstractAddToTestCaseAction(String name, String description) {
036: super (name, description);
037: }
038:
039: protected WsdlTestCase getTargetTestCase(WsdlProject project) {
040: List<WsdlTestCase> testCases = new ArrayList<WsdlTestCase>();
041: List<WsdlTestSuite> testSuites = new ArrayList<WsdlTestSuite>();
042: List<String> testCaseNames = new ArrayList<String>();
043: WsdlTestCase testCase = null;
044:
045: if (project.getTestSuiteCount() == 0) {
046: return addNewTestSuiteAndTestCase(project);
047: }
048:
049: for (int c = 0; c < project.getTestSuiteCount(); c++) {
050: WsdlTestSuite testSuite = (WsdlTestSuite) project
051: .getTestSuiteAt(c);
052: for (int i = 0; i < testSuite.getTestCaseCount(); i++) {
053: testCase = (WsdlTestCase) testSuite.getTestCaseAt(i);
054:
055: testCases.add(testCase);
056: testCaseNames.add((testCaseNames.size() + 1) + ": "
057: + testSuite.getName() + " - "
058: + testCase.getName());
059: testSuites.add(testSuite);
060: }
061:
062: testCases.add(null);
063: testSuites.add(testSuite);
064: testCaseNames.add((testCaseNames.size() + 1) + ": "
065: + testSuite.getName() + " -> Create new TestCase");
066: }
067:
068: if (testCases.size() == 0) {
069: List<String> testSuiteNames = new ArrayList<String>();
070:
071: for (int c = 0; c < project.getTestSuiteCount(); c++) {
072: TestSuite testSuite = project.getTestSuiteAt(c);
073: testSuiteNames.add((testSuiteNames.size() + 1) + ": "
074: + testSuite.getName());
075: }
076:
077: String selection = (String) UISupport.prompt(
078: "Select TestSuite to create TestCase in",
079: "Select TestSuite", testSuiteNames.toArray());
080: if (selection == null)
081: return null;
082:
083: WsdlTestSuite testSuite = (WsdlTestSuite) project
084: .getTestSuiteAt(testSuiteNames.indexOf(selection));
085:
086: String name = UISupport.prompt(
087: "Enter name for TestCase create",
088: "Create TestCase", "TestCase "
089: + (testSuite.getTestCaseCount() + 1));
090: if (name == null)
091: return null;
092:
093: return testSuite.addNewTestCase(name);
094: } else {
095: testCases.add(null);
096: testSuites.add(null);
097: testCaseNames.add((testCaseNames.size() + 1)
098: + ": -> Create new TestSuite");
099:
100: String selection = (String) UISupport.prompt(
101: "Select TestCase", "Select TestCase", testCaseNames
102: .toArray());
103: if (selection == null)
104: return null;
105:
106: testCase = testCases.get(testCaseNames.indexOf(selection));
107: while (testCase != null
108: && SoapUI.getTestMonitor().hasRunningLoadTest(
109: testCase)) {
110: UISupport
111: .showErrorMessage("Can not add to TestCase that is currently LoadTesting");
112:
113: selection = (String) UISupport.prompt(
114: "Select TestCase", "Select TestCase",
115: testCaseNames.toArray());
116: if (selection == null)
117: return null;
118:
119: testCase = testCases.get(testCaseNames
120: .indexOf(selection));
121: }
122:
123: // selected create new?
124: if (testCase == null) {
125: WsdlTestSuite testSuite = testSuites.get(testCaseNames
126: .indexOf(selection));
127:
128: // selected create new testsuite?
129: if (testSuite == null) {
130: return addNewTestSuiteAndTestCase(project);
131: } else {
132: String name = UISupport
133: .prompt(
134: "Enter name for TestCase create",
135: "Create TestCase",
136: "TestCase "
137: + (testSuite
138: .getTestCaseCount() + 1));
139: if (name == null)
140: return null;
141:
142: return testSuite.addNewTestCase(name);
143: }
144: }
145: }
146:
147: return testCase;
148: }
149:
150: protected WsdlTestCase addNewTestSuiteAndTestCase(
151: WsdlProject project) {
152: String testSuiteName = UISupport.prompt(
153: "Missing TestSuite in project, enter name to create",
154: "Create TestSuite", "TestSuite "
155: + (project.getTestSuiteCount() + 1));
156: if (testSuiteName == null)
157: return null;
158:
159: String testCaseName = UISupport.prompt(
160: "Enter name for TestCase create", "Create TestCase",
161: "TestCase 1");
162: if (testCaseName == null)
163: return null;
164:
165: WsdlTestSuite testSuite = (WsdlTestSuite) project
166: .addNewTestSuite(testSuiteName);
167: return testSuite.addNewTestCase(testCaseName);
168: }
169: }
|