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.testcase;
014:
015: import java.util.ArrayList;
016: import java.util.HashSet;
017: import java.util.List;
018: import java.util.Set;
019:
020: import org.apache.log4j.Logger;
021:
022: import com.eviware.soapui.config.LoadTestConfig;
023: import com.eviware.soapui.config.TestCaseConfig;
024: import com.eviware.soapui.config.TestStepConfig;
025: import com.eviware.soapui.impl.wsdl.AbstractWsdlModelItem;
026: import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
027: import com.eviware.soapui.impl.wsdl.loadtest.LoadTestAssertion;
028: import com.eviware.soapui.impl.wsdl.loadtest.WsdlLoadTest;
029: import com.eviware.soapui.impl.wsdl.loadtest.assertions.TestStepStatusAssertion;
030: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
031: import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepFactory;
032: import com.eviware.soapui.impl.wsdl.teststeps.registry.WsdlTestStepRegistry;
033: import com.eviware.soapui.model.support.PropertiesMap;
034: import com.eviware.soapui.model.testsuite.LoadTest;
035: import com.eviware.soapui.model.testsuite.TestCase;
036: import com.eviware.soapui.model.testsuite.TestRunListener;
037: import com.eviware.soapui.model.testsuite.TestStep;
038: import com.eviware.soapui.support.UISupport;
039: import com.eviware.soapui.support.action.swing.ActionList;
040: import com.eviware.soapui.support.action.swing.DefaultActionList;
041:
042: /**
043: * TestCase implementation for WSDL projects
044: *
045: * @author Ole.Matzura
046: */
047:
048: public class WsdlTestCase extends AbstractWsdlModelItem<TestCaseConfig>
049: implements TestCase {
050: private final static Logger logger = Logger
051: .getLogger(WsdlTestCase.class);
052: public final static String KEEP_SESSION_PROPERTY = WsdlTestCase.class
053: .getName()
054: + "@keepSession";
055: public final static String FAIL_ON_ERROR_PROPERTY = WsdlTestCase.class
056: .getName()
057: + "@failOnError";
058: public final static String FAIL_ON_ERRORS_PROPERTY = WsdlTestCase.class
059: .getName()
060: + "@failOnErrors";
061: public final static String DISCARD_OK_RESULTS = WsdlTestCase.class
062: .getName()
063: + "@discardOkResults";
064: private static final String SEARCH_PROPERTIES_PROPERTY = WsdlTestCase.class
065: .getName()
066: + "@searchProperties";
067:
068: private final WsdlTestSuite testSuite;
069: private List<WsdlTestStep> testSteps = new ArrayList<WsdlTestStep>();
070: private List<WsdlLoadTest> loadTests = new ArrayList<WsdlLoadTest>();
071: private Set<TestRunListener> testRunListeners = new HashSet<TestRunListener>();
072: private DefaultActionList createActions;
073: private final boolean forLoadTest;
074:
075: public WsdlTestCase(WsdlTestSuite testSuite, TestCaseConfig config,
076: boolean forLoadTest) {
077: super (config, testSuite, "/testCase.gif");
078:
079: this .testSuite = testSuite;
080: this .forLoadTest = forLoadTest;
081:
082: List<TestStepConfig> testStepConfigs = config.getTestStepList();
083: for (TestStepConfig tsc : testStepConfigs) {
084: WsdlTestStep testStep = createTestStepFromConfig(tsc);
085: if (testStep != null) {
086: ensureUniqueName(testStep);
087: testSteps.add(testStep);
088: }
089: }
090:
091: for (TestStep step : testSteps) {
092: WsdlTestStep testStep = (WsdlTestStep) step;
093: testStep.postInit(testStep.getConfig());
094: }
095:
096: if (!forLoadTest) {
097: List<LoadTestConfig> loadTestConfigs = config
098: .getLoadTestList();
099: for (LoadTestConfig tsc : loadTestConfigs) {
100: WsdlLoadTest loadTest = new WsdlLoadTest(this , tsc);
101: loadTests.add(loadTest);
102: }
103:
104: // addAction( new ShowDesktopPanelAction( "Open TestCase Editor",
105: // "Opens the TestCase Editor for this TestCase", this ));
106: // addAction( ActionSupport.SEPARATOR_ACTION );
107: // addAction( new AddNewLoadTestAction( this ));
108: // addAction( new CloneTestCaseAction( this ));
109: // addAction( new ClearTestCaseAction( this ));
110: // addAction( ActionSupport.SEPARATOR_ACTION );
111: // addAction( new TestCaseOptionsAction( this ));
112:
113: // createActions = new DefaultActionList( "New Step" );
114: // WsdlTestStepRegistry registry = WsdlTestStepRegistry.getInstance();
115: // WsdlTestStepFactory[] factories = (WsdlTestStepFactory[]) registry.getFactories();
116: //
117: // for (int c = 0; c < factories.length; c++)
118: // {
119: // if (factories[c].canCreate())
120: // createActions.addAction( new AddTestStepAction(factories[c]));
121: // }
122: //
123: // addAction( new ActionSupport.ActionListAction( createActions ));
124: // addAction( ActionSupport.SEPARATOR_ACTION );
125: // addAction( new RenameTestCaseAction( this ) );
126: // addAction( new DeleteTestCaseAction( this ) );
127: // addAction( ActionSupport.SEPARATOR_ACTION );
128: // addAction( new ShowOnlineHelpAction( HelpUrls.TESTCASE_HELP_URL ));
129: }
130:
131: // init default configs
132: if (!config.isSetFailOnError())
133: config.setFailOnError(true);
134:
135: if (!config.isSetFailTestCaseOnErrors())
136: config.setFailTestCaseOnErrors(true);
137:
138: if (!config.isSetKeepSession())
139: config.setKeepSession(false);
140: }
141:
142: public boolean getKeepSession() {
143: return getConfig().getKeepSession();
144: }
145:
146: public void setKeepSession(boolean keepSession) {
147: boolean old = getKeepSession();
148: if (old != keepSession) {
149: getConfig().setKeepSession(keepSession);
150: notifyPropertyChanged(KEEP_SESSION_PROPERTY, old,
151: keepSession);
152: }
153: }
154:
155: public boolean getFailOnError() {
156: return getConfig().getFailOnError();
157: }
158:
159: public boolean getFailTestCaseOnErrors() {
160: return getConfig().getFailTestCaseOnErrors();
161: }
162:
163: public void setFailOnError(boolean failOnError) {
164: boolean old = getFailOnError();
165: if (old != failOnError) {
166: getConfig().setFailOnError(failOnError);
167: notifyPropertyChanged(FAIL_ON_ERROR_PROPERTY, old,
168: failOnError);
169: }
170: }
171:
172: public void setFailTestCaseOnErrors(boolean failTestCaseOnErrors) {
173: boolean old = getFailTestCaseOnErrors();
174: if (old != failTestCaseOnErrors) {
175: getConfig().setFailTestCaseOnErrors(failTestCaseOnErrors);
176: notifyPropertyChanged(FAIL_ON_ERRORS_PROPERTY, old,
177: failTestCaseOnErrors);
178: }
179: }
180:
181: public boolean getSearchProperties() {
182: return getConfig().getSearchProperties();
183: }
184:
185: public void setSearchProperties(boolean searchProperties) {
186: boolean old = getSearchProperties();
187: if (old != searchProperties) {
188: getConfig().setSearchProperties(searchProperties);
189: notifyPropertyChanged(SEARCH_PROPERTIES_PROPERTY, old,
190: searchProperties);
191: }
192: }
193:
194: public boolean getDiscardOkResults() {
195: return getConfig().getDiscardOkResults();
196: }
197:
198: public void setDiscardOkResults(boolean discardOkResults) {
199: boolean old = getDiscardOkResults();
200: if (old != discardOkResults) {
201: getConfig().setDiscardOkResults(discardOkResults);
202: notifyPropertyChanged(DISCARD_OK_RESULTS, old,
203: discardOkResults);
204: }
205: }
206:
207: private WsdlTestStep createTestStepFromConfig(TestStepConfig tsc) {
208: WsdlTestStepFactory factory = WsdlTestStepRegistry
209: .getInstance().getFactory(tsc.getType());
210: if (factory != null) {
211: WsdlTestStep testStep = factory.buildTestStep(this , tsc,
212: forLoadTest);
213: return testStep;
214: } else {
215: logger.error("Failed to create test step for ["
216: + tsc.getName() + "]");
217: return null;
218: }
219: }
220:
221: private boolean ensureUniqueName(WsdlTestStep testStep) {
222: String name = testStep.getName();
223: while (name == null || getTestStepByName(name) != null) {
224: if (name == null)
225: name = testStep.getName();
226: else {
227: int cnt = 0;
228:
229: while (getTestStepByName(name) != null) {
230: cnt++;
231: name = testStep.getName() + " " + cnt;
232: }
233:
234: if (cnt == 0)
235: break;
236: }
237:
238: name = UISupport.prompt(
239: "TestStep name must be unique, please specify new name for step\n"
240: + "[" + testStep.getName()
241: + "] in TestCase ["
242: + getTestSuite().getProject().getName()
243: + "->" + getTestSuite().getName() + "->"
244: + getName() + "]", "Change TestStep name",
245: name);
246:
247: if (name == null)
248: return false;
249: }
250:
251: if (!name.equals(testStep.getName()))
252: testStep.setName(name);
253:
254: return true;
255: }
256:
257: public WsdlLoadTest addNewLoadTest(String name) {
258: WsdlLoadTest loadTest = new WsdlLoadTest(this , getConfig()
259: .addNewLoadTest());
260: loadTest.setStartDelay(0);
261: loadTest.setName(name);
262: loadTests.add(loadTest);
263:
264: loadTest.addAssertion(TestStepStatusAssertion.STEP_STATUS_TYPE,
265: LoadTestAssertion.ANY_TEST_STEP, false);
266:
267: ((WsdlTestSuite) getTestSuite()).fireLoadTestAdded(loadTest);
268:
269: return loadTest;
270: }
271:
272: public void removeLoadTest(WsdlLoadTest loadTest) {
273: int ix = loadTests.indexOf(loadTest);
274:
275: loadTests.remove(ix);
276:
277: try {
278: ((WsdlTestSuite) getTestSuite())
279: .fireLoadTestRemoved(loadTest);
280: } finally {
281: loadTest.release();
282: getConfig().removeLoadTest(ix);
283: }
284: }
285:
286: public WsdlTestSuite getTestSuite() {
287: return testSuite;
288: }
289:
290: public WsdlTestStep cloneStep(WsdlTestStep testStep, String name) {
291: return testStep.clone(this , name);
292: }
293:
294: public WsdlTestStep getTestStepAt(int index) {
295: return testSteps.get(index);
296: }
297:
298: public int getTestStepCount() {
299: return testSteps.size();
300: }
301:
302: public WsdlLoadTest getLoadTestAt(int index) {
303: return loadTests.get(index);
304: }
305:
306: public LoadTest getLoadTestByName(String loadTestName) {
307: return (LoadTest) getWsdlModelItemByName(loadTests,
308: loadTestName);
309: }
310:
311: public int getLoadTestCount() {
312: return loadTests.size();
313: }
314:
315: public WsdlTestStep addTestStep(TestStepConfig stepConfig) {
316: return insertTestStep(stepConfig, -1);
317: }
318:
319: public WsdlTestStep addTestStep(String type, String name) {
320: TestStepConfig newStepConfig = WsdlTestStepRegistry
321: .getInstance().getFactory(type).createNewTestStep(this ,
322: name);
323: if (newStepConfig != null) {
324: return addTestStep(newStepConfig);
325: } else
326: return null;
327: }
328:
329: public WsdlTestStep insertTestStep(String type, String name,
330: int index) {
331: TestStepConfig newStepConfig = WsdlTestStepRegistry
332: .getInstance().getFactory(type).createNewTestStep(this ,
333: name);
334: if (newStepConfig != null) {
335: return insertTestStep(newStepConfig, index);
336: } else
337: return null;
338: }
339:
340: public WsdlTestStep importTestStep(WsdlTestStep testStep,
341: String name, int index) {
342: testStep.onSave();
343: TestStepConfig newStepConfig = (TestStepConfig) testStep
344: .getConfig().copy();
345: newStepConfig.setName(name);
346: return insertTestStep(newStepConfig, index);
347: }
348:
349: public WsdlTestStep[] importTestSteps(WsdlTestStep[] testSteps,
350: int index) {
351: TestStepConfig[] newStepConfigs = new TestStepConfig[testSteps.length];
352:
353: for (int c = 0; c < testSteps.length; c++) {
354: testSteps[c].onSave();
355: newStepConfigs[c] = (TestStepConfig) testSteps[c]
356: .getConfig().copy();
357: }
358:
359: return insertTestSteps(newStepConfigs, index);
360: }
361:
362: public WsdlTestStep insertTestStep(TestStepConfig stepConfig, int ix) {
363: TestStepConfig newStepConfig = ix == -1 ? getConfig()
364: .addNewTestStep() : getConfig().insertNewTestStep(ix);
365: newStepConfig.set(stepConfig);
366: WsdlTestStep testStep = createTestStepFromConfig(newStepConfig);
367:
368: if (!ensureUniqueName(testStep))
369: return null;
370:
371: if (ix == -1)
372: testSteps.add(testStep);
373: else
374: testSteps.add(ix, testStep);
375:
376: testStep.postInit(testStep.getConfig());
377:
378: if (getTestSuite() != null)
379: ((WsdlTestSuite) getTestSuite()).fireTestStepAdded(
380: testStep, ix == -1 ? testSteps.size() - 1 : ix);
381:
382: return testStep;
383: }
384:
385: public WsdlTestStep[] insertTestSteps(TestStepConfig[] stepConfig,
386: int ix) {
387: WsdlTestStep[] result = new WsdlTestStep[stepConfig.length];
388:
389: for (int c = 0; c < stepConfig.length; c++) {
390: TestStepConfig newStepConfig = ix == -1 ? getConfig()
391: .addNewTestStep() : getConfig().insertNewTestStep(
392: ix + c);
393: newStepConfig.set(stepConfig[c]);
394: WsdlTestStep testStep = createTestStepFromConfig(newStepConfig);
395:
396: if (!ensureUniqueName(testStep))
397: return null;
398:
399: if (ix == -1)
400: testSteps.add(testStep);
401: else
402: testSteps.add(ix + c, testStep);
403:
404: result[c] = testStep;
405: }
406:
407: for (int c = 0; c < result.length; c++) {
408: result[c].postInit(result[c].getConfig());
409:
410: if (getTestSuite() != null)
411: ((WsdlTestSuite) getTestSuite()).fireTestStepAdded(
412: result[c], getIndexOfTestStep(result[c]));
413: }
414:
415: return result;
416: }
417:
418: public void removeTestStep(WsdlTestStep testStep) {
419: int ix = testSteps.indexOf(testStep);
420: if (ix == -1) {
421: logger.error("TestStep [" + testStep.getName()
422: + "] passed to removeTestStep in testCase ["
423: + getName() + "] not found");
424: return;
425: }
426:
427: testSteps.remove(ix);
428:
429: try {
430: ((WsdlTestSuite) getTestSuite()).fireTestStepRemoved(
431: testStep, ix);
432: } finally {
433: testStep.release();
434:
435: for (int c = 0; c < getConfig().sizeOfTestStepArray(); c++) {
436: if (testStep.getConfig() == getConfig()
437: .getTestStepArray(c)) {
438: getConfig().removeTestStep(c);
439: break;
440: }
441: }
442: }
443: }
444:
445: public WsdlTestCaseRunner run(PropertiesMap properties,
446: boolean async) {
447: WsdlTestCaseRunner runner = new WsdlTestCaseRunner(this ,
448: properties);
449: runner.start(async);
450: return runner;
451: }
452:
453: public void addTestRunListener(TestRunListener listener) {
454: if (listener == null)
455: throw new RuntimeException("listener must not be null");
456:
457: testRunListeners.add(listener);
458: }
459:
460: public void removeTestRunListener(TestRunListener listener) {
461: testRunListeners.remove(listener);
462: }
463:
464: public TestRunListener[] getTestRunListeners() {
465: return testRunListeners
466: .toArray(new TestRunListener[testRunListeners.size()]);
467: }
468:
469: WsdlTestStep[] getTestSteps() {
470: return testSteps.toArray(new WsdlTestStep[testSteps.size()]);
471: }
472:
473: public int getIndexOfTestStep(TestStep step) {
474: return testSteps.indexOf(step);
475: }
476:
477: /**
478: * Moves a step by the specified offset, a bit awkward since xmlbeans doesn't support reordering
479: * of arrays, we need to create copies of the contained XmlObjects
480: *
481: * @param ix
482: * @param offset
483: */
484:
485: public void moveTestStep(int ix, int offset) {
486: if (offset == 0)
487: return;
488: WsdlTestStep step = (WsdlTestStep) testSteps.get(ix);
489:
490: if (ix + offset >= testSteps.size())
491: offset = testSteps.size() - ix - 1;
492:
493: testSteps.remove(ix);
494: testSteps.add(ix + offset, step);
495:
496: TestStepConfig[] configs = new TestStepConfig[testSteps.size()];
497:
498: for (int c = 0; c < testSteps.size(); c++) {
499: if (offset > 0) {
500: if (c < ix)
501: configs[c] = (TestStepConfig) getConfig()
502: .getTestStepArray(c).copy();
503: else if (c < (ix + offset))
504: configs[c] = (TestStepConfig) getConfig()
505: .getTestStepArray(c + 1).copy();
506: else if (c == ix + offset)
507: configs[c] = (TestStepConfig) getConfig()
508: .getTestStepArray(ix).copy();
509: else
510: configs[c] = (TestStepConfig) getConfig()
511: .getTestStepArray(c).copy();
512: } else {
513: if (c < ix + offset)
514: configs[c] = (TestStepConfig) getConfig()
515: .getTestStepArray(c).copy();
516: else if (c == ix + offset)
517: configs[c] = (TestStepConfig) getConfig()
518: .getTestStepArray(ix).copy();
519: else if (c <= ix)
520: configs[c] = (TestStepConfig) getConfig()
521: .getTestStepArray(c - 1).copy();
522: else
523: configs[c] = (TestStepConfig) getConfig()
524: .getTestStepArray(c).copy();
525: }
526: }
527:
528: getConfig().setTestStepArray(configs);
529: for (int c = 0; c < configs.length; c++) {
530: ((WsdlTestStep) testSteps.get(c))
531: .resetConfigOnMove(getConfig().getTestStepArray(c));
532: }
533:
534: ((WsdlTestSuite) getTestSuite()).fireTestStepMoved(step, ix,
535: offset);
536: }
537:
538: public int getIndexOfLoadTest(LoadTest loadTest) {
539: return loadTests.indexOf(loadTest);
540: }
541:
542: public int getTestStepIndexByName(String stepName) {
543: for (int c = 0; c < testSteps.size(); c++) {
544: if (testSteps.get(c).getName().equals(stepName))
545: return c;
546: }
547:
548: return -1;
549: }
550:
551: public TestStep findPreviousStepOfType(TestStep referenceStep,
552: Class stepClass) {
553: int currentStepIndex = getIndexOfTestStep(referenceStep);
554: int ix = currentStepIndex - 1;
555: while (ix >= 0
556: && !getTestStepAt(ix).getClass().equals(stepClass)) {
557: ix--;
558: }
559:
560: return ix < 0 ? null : getTestStepAt(ix);
561: }
562:
563: public TestStep findNextStepOfType(TestStep referenceStep,
564: Class stepClass) {
565: int currentStepIndex = getIndexOfTestStep(referenceStep);
566: int ix = currentStepIndex + 1;
567: while (ix < getTestStepCount()
568: && !getTestStepAt(ix).getClass().equals(stepClass)) {
569: ix++;
570: }
571:
572: return ix >= getTestStepCount() ? null : getTestStepAt(ix);
573: }
574:
575: public List<TestStep> getTestStepList() {
576: List<TestStep> result = new ArrayList<TestStep>();
577: for (TestStep step : testSteps)
578: result.add(step);
579:
580: return result;
581: }
582:
583: public List<TestStep> getTestStepsOfType(
584: Class<? extends WsdlTestStep> stepType) {
585: List<TestStep> result = new ArrayList<TestStep>();
586: for (TestStep step : testSteps)
587: if (step.getClass().isAssignableFrom(stepType))
588: result.add(step);
589:
590: return result;
591: }
592:
593: public WsdlTestStep getTestStepByName(String stepName) {
594: return (WsdlTestStep) getWsdlModelItemByName(testSteps,
595: stepName);
596: }
597:
598: public WsdlLoadTest cloneLoadTest(WsdlLoadTest loadTest, String name) {
599: loadTest.onSave();
600:
601: LoadTestConfig loadTestConfig = getConfig().addNewLoadTest();
602: loadTestConfig.set(loadTest.getConfig().copy());
603:
604: WsdlLoadTest newLoadTest = new WsdlLoadTest(this ,
605: loadTestConfig);
606: newLoadTest.setName(name);
607: loadTests.add(newLoadTest);
608:
609: ((WsdlTestSuite) getTestSuite()).fireLoadTestAdded(newLoadTest);
610:
611: return newLoadTest;
612: }
613:
614: public void release() {
615: super .release();
616:
617: for (WsdlTestStep testStep : testSteps)
618: testStep.release();
619:
620: for (WsdlLoadTest loadTest : loadTests)
621: loadTest.release();
622: }
623:
624: public ActionList getCreateActions() {
625: return createActions;
626: }
627:
628: public void resetConfigOnMove(TestCaseConfig testCaseArray) {
629: setConfig(testCaseArray);
630: int mod = 0;
631:
632: List<TestStepConfig> configs = getConfig().getTestStepList();
633: for (int c = 0; c < configs.size(); c++) {
634: if (WsdlTestStepRegistry.getInstance().hasFactory(
635: configs.get(c))) {
636: ((WsdlTestStep) testSteps.get(c - mod))
637: .resetConfigOnMove(configs.get(c));
638: } else
639: mod++;
640: }
641:
642: List<LoadTestConfig> loadTestConfigs = getConfig()
643: .getLoadTestList();
644: for (int c = 0; c < loadTestConfigs.size(); c++) {
645: loadTests.get(c).resetConfigOnMove(loadTestConfigs.get(c));
646: }
647: }
648:
649: @Override
650: public void onSave() {
651: for (WsdlTestStep testStep : testSteps)
652: testStep.onSave();
653:
654: for (WsdlLoadTest loadTest : loadTests)
655: loadTest.onSave();
656: }
657: }
|