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.teststeps;
014:
015: import java.io.FileNotFoundException;
016: import java.io.FileOutputStream;
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.net.MalformedURLException;
020: import java.net.URL;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.Enumeration;
024: import java.util.List;
025:
026: import javax.swing.ImageIcon;
027:
028: import com.eviware.soapui.config.PropertiesStepConfig;
029: import com.eviware.soapui.config.PropertyConfig;
030: import com.eviware.soapui.config.TestStepConfig;
031: import com.eviware.soapui.config.PropertiesStepConfig.Properties;
032: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
033: import com.eviware.soapui.model.testsuite.TestRunContext;
034: import com.eviware.soapui.model.testsuite.TestRunner;
035: import com.eviware.soapui.model.testsuite.TestStep;
036: import com.eviware.soapui.model.testsuite.TestStepProperty;
037: import com.eviware.soapui.model.testsuite.TestStepResult;
038: import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus;
039: import com.eviware.soapui.support.UISupport;
040: import com.eviware.soapui.support.types.StringList;
041:
042: /**
043: * TestStep that holds an arbitrary number of custom properties
044: *
045: * @author ole.matzura
046: */
047:
048: public class WsdlPropertiesTestStep extends WsdlTestStep {
049: private PropertiesStepConfig propertiesStepConfig;
050: private List<StepProperty> properties = new ArrayList<StepProperty>();
051: private ImageIcon okIcon;
052: private ImageIcon failedIcon;
053:
054: public WsdlPropertiesTestStep(WsdlTestCase testCase,
055: TestStepConfig config, boolean forLoadTest) {
056: super (testCase, config, true, forLoadTest);
057:
058: if (!forLoadTest) {
059: okIcon = UISupport.createImageIcon("/properties_step.gif");
060: failedIcon = UISupport
061: .createImageIcon("/properties_step_failed.gif");
062:
063: setIcon(okIcon);
064: }
065:
066: if (config.getConfig() == null) {
067: propertiesStepConfig = (PropertiesStepConfig) config
068: .addNewConfig().changeType(
069: PropertiesStepConfig.type);
070: propertiesStepConfig.addNewProperties();
071: propertiesStepConfig.setCreateMissingOnLoad(true);
072: } else {
073: propertiesStepConfig = (PropertiesStepConfig) config
074: .getConfig().changeType(PropertiesStepConfig.type);
075: if (propertiesStepConfig.isSetProperties()) {
076: Properties props = propertiesStepConfig.getProperties();
077: for (int c = 0; c < props.sizeOfPropertyArray(); c++) {
078: StepProperty stepProperty = new StepProperty(props
079: .getPropertyArray(c));
080: properties.add(stepProperty);
081: addProperty(stepProperty);
082: }
083: } else {
084: propertiesStepConfig.addNewProperties();
085: }
086:
087: if (!propertiesStepConfig.isSetSaveFirst())
088: propertiesStepConfig.setSaveFirst(true);
089: }
090: }
091:
092: public TestStepResult run(TestRunner testRunner,
093: TestRunContext testRunContext) {
094: WsdlTestStepResult result = new WsdlTestStepResult(this );
095:
096: if (okIcon != null)
097: setIcon(okIcon);
098:
099: result.setStatus(TestStepStatus.OK);
100: result.startTimer();
101:
102: if (isSaveFirst())
103: saveDuringRun(result);
104:
105: String source = getSource();
106: if (source != null && source.trim().length() > 0) {
107: try {
108: int cnt = loadProperties(source,
109: isCreateMissingOnLoad());
110:
111: result.setStatus(TestStepStatus.OK);
112: result.addMessage("Loaded " + cnt
113: + " properties from [" + source + "]");
114: } catch (IOException e) {
115: result.stopTimer();
116: result.addMessage("Failed to load properties from ["
117: + source + "]");
118: result.setStatus(TestStepStatus.FAILED);
119: result.setError(e);
120:
121: if (failedIcon != null)
122: setIcon(failedIcon);
123: }
124: }
125:
126: if (!isSaveFirst())
127: saveDuringRun(result);
128:
129: result.stopTimer();
130:
131: return result;
132: }
133:
134: private boolean saveDuringRun(WsdlTestStepResult result) {
135: String target = getTarget();
136: if (target != null && target.trim().length() > 0) {
137: try {
138: int cnt = saveProperties(target);
139:
140: result.setStatus(TestStepStatus.OK);
141: result.addMessage("Saved " + cnt + " properties to ["
142: + target + "]");
143: } catch (IOException e) {
144: result.stopTimer();
145: result.addMessage("Failed to save properties to ["
146: + target + "]");
147: result.setStatus(TestStepStatus.FAILED);
148: result.setError(e);
149:
150: if (failedIcon != null)
151: setIcon(failedIcon);
152:
153: return false;
154: }
155: }
156:
157: return true;
158: }
159:
160: private int saveProperties(String target) throws IOException {
161: java.util.Properties props = new java.util.Properties();
162:
163: int cnt = 0;
164: for (StepProperty p : properties) {
165: String name = p.getName();
166: props.setProperty(name, p.getValue());
167: cnt++;
168: }
169:
170: props.store(getPropertiesOutputStream(target), "TestStep ["
171: + getName() + "] properties");
172:
173: return cnt;
174: }
175:
176: private FileOutputStream getPropertiesOutputStream(String target)
177: throws FileNotFoundException {
178: String fileProperty = System.getProperty(target);
179: if (fileProperty != null)
180: target = fileProperty;
181:
182: return new FileOutputStream(target);
183: }
184:
185: private int loadProperties(String source, boolean createMissing)
186: throws IOException {
187: // override methods so propertynames are returned in readorder
188: java.util.Properties props = new java.util.Properties() {
189: public StringList names = new StringList();
190:
191: @Override
192: public synchronized Object put(Object key, Object value) {
193: names.add(key.toString());
194: return super .put(key, value);
195: }
196:
197: @Override
198: public Enumeration<?> propertyNames() {
199: return Collections.enumeration(names);
200: }
201: };
202:
203: props.load(getPropertiesInputStream(source));
204:
205: int cnt = 0;
206: Enumeration names = props.propertyNames();
207: while (names.hasMoreElements()) {
208: String name = names.nextElement().toString();
209: TestStepProperty property = getProperty(name);
210: if (property != null) {
211: property.setValue(props.get(name).toString());
212: cnt++;
213: } else if (createMissing) {
214: addProperty(name).setValue(props.get(name).toString());
215: cnt++;
216: }
217: }
218:
219: return cnt;
220: }
221:
222: private InputStream getPropertiesInputStream(String source)
223: throws IOException {
224: String fileProperty = System.getProperty(source);
225: if (fileProperty != null)
226: source = fileProperty;
227:
228: URL url = null;
229:
230: try {
231: url = new URL(source);
232: } catch (MalformedURLException e) {
233: url = new URL("file:" + source);
234: }
235:
236: return url.openStream();
237: }
238:
239: public StepProperty getTestStepPropertyAt(int index) {
240: return properties.get(index);
241: }
242:
243: public int getStepPropertyCount() {
244: return properties.size();
245: }
246:
247: public String getSource() {
248: return propertiesStepConfig.getSource();
249: }
250:
251: public void setSource(String source) {
252: propertiesStepConfig.setSource(source);
253: }
254:
255: public String getTarget() {
256: return propertiesStepConfig.getTarget();
257: }
258:
259: public void setTarget(String target) {
260: propertiesStepConfig.setTarget(target);
261: }
262:
263: public void resetConfigOnMove(TestStepConfig config) {
264: super .resetConfigOnMove(config);
265:
266: propertiesStepConfig = (PropertiesStepConfig) config
267: .getConfig().changeType(PropertiesStepConfig.type);
268: for (int c = 0; c < propertiesStepConfig.getProperties()
269: .sizeOfPropertyArray(); c++) {
270: properties.get(c).setConfig(
271: propertiesStepConfig.getProperties()
272: .getPropertyArray(c));
273: }
274: }
275:
276: public StepProperty addProperty(String name) {
277: PropertyConfig property = propertiesStepConfig.getProperties()
278: .addNewProperty();
279: property.setName(name);
280: StepProperty stepProperty = new StepProperty(property);
281: properties.add(stepProperty);
282: addProperty(stepProperty);
283: firePropertyAdded(name);
284: return stepProperty;
285: }
286:
287: public void removeProperty(String name) {
288: for (int c = 0; c < properties.size(); c++) {
289: if (properties.get(c).getName().equalsIgnoreCase(name)) {
290: removePropertyAt(c);
291: return;
292: }
293: }
294: }
295:
296: public void removePropertyAt(int index) {
297: String name = properties.get(index).getName();
298: properties.remove(index);
299: deleteProperty(name);
300: firePropertyRemoved(name);
301: propertiesStepConfig.getProperties().removeProperty(index);
302: }
303:
304: /**
305: * Internal property class
306: *
307: * @author ole
308: */
309:
310: public class StepProperty implements TestStepProperty {
311: private PropertyConfig propertyConfig;
312:
313: public StepProperty(PropertyConfig propertyConfig) {
314: this .propertyConfig = propertyConfig;
315: }
316:
317: public void setConfig(PropertyConfig propertyConfig) {
318: this .propertyConfig = propertyConfig;
319: }
320:
321: public String getName() {
322: return propertyConfig.getName();
323: }
324:
325: public void setName(String name) {
326: String oldName = getName();
327: propertyConfig.setName(name);
328: propertyRenamed(oldName);
329: }
330:
331: public String getDescription() {
332: return null;
333: }
334:
335: public String getValue() {
336: return propertyConfig.getValue();
337: }
338:
339: public void setValue(String value) {
340: String oldValue = getValue();
341: propertyConfig.setValue(value);
342:
343: firePropertyValueChanged(getName(), oldValue, value);
344: }
345:
346: public boolean isReadOnly() {
347: return false;
348: }
349:
350: public TestStep getTestStep() {
351: return WsdlPropertiesTestStep.this ;
352: }
353: }
354:
355: public int loadProperties(boolean createMissing) throws IOException {
356: return loadProperties(getSource(), createMissing);
357: }
358:
359: public int saveProperties() throws IOException {
360: return saveProperties(getTarget());
361: }
362:
363: public boolean isCreateMissingOnLoad() {
364: return propertiesStepConfig.getCreateMissingOnLoad();
365: }
366:
367: public void setCreateMissingOnLoad(boolean b) {
368: propertiesStepConfig.setCreateMissingOnLoad(b);
369: }
370:
371: public boolean isSaveFirst() {
372: return propertiesStepConfig.getSaveFirst();
373: }
374:
375: public void setSaveFirst(boolean b) {
376: propertiesStepConfig.setSaveFirst(b);
377: }
378: }
|