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.io.File;
016: import java.util.List;
017:
018: import com.eviware.soapui.SoapUI;
019: import com.eviware.soapui.impl.wsdl.WsdlInterface;
020: import com.eviware.soapui.impl.wsdl.WsdlOperation;
021: import com.eviware.soapui.impl.wsdl.WsdlRequest;
022: import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
023: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest;
024: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep;
025: import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestStep;
026: import com.eviware.soapui.model.iface.Request;
027: import com.eviware.soapui.model.testsuite.TestCase;
028: import com.eviware.soapui.model.testsuite.TestSuite;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
031: import com.eviware.soapui.support.xml.XmlUtils;
032: import com.eviware.x.form.XFormDialog;
033: import com.eviware.x.form.XFormField;
034: import com.eviware.x.form.XFormFieldListener;
035: import com.eviware.x.form.support.ADialogBuilder;
036: import com.eviware.x.form.support.AField;
037: import com.eviware.x.form.support.AForm;
038: import com.eviware.x.form.support.AField.AFieldType;
039:
040: /**
041: * Updates the definition of a WsdlInterface.
042: *
043: * @author Ole.Matzura
044: */
045:
046: public class UpdateInterfaceAction extends
047: AbstractSoapUIAction<WsdlInterface> {
048: public static final String SOAPUI_ACTION_ID = "UpdateInterfaceAction";
049: private XFormDialog dialog = null;
050:
051: public UpdateInterfaceAction() {
052: super ("Update Definition",
053: "Reloads the definition for this interface and its operations");
054: }
055:
056: public void perform(WsdlInterface iface, Object param) {
057: if (RemoveInterfaceAction.hasRunningDependingTests(iface)) {
058: UISupport
059: .showErrorMessage("Cannot update Interface due to running depending tests");
060: return;
061: }
062:
063: if (dialog == null) {
064: dialog = ADialogBuilder.buildDialog(Form.class);
065: dialog.setBooleanValue(Form.CREATE_REQUESTS, true);
066: dialog.getFormField(Form.CREATE_BACKUPS).setEnabled(false);
067: dialog.getFormField(Form.RECREATE_OPTIONAL).setEnabled(
068: false);
069: dialog.getFormField(Form.RECREATE_REQUESTS)
070: .addFormFieldListener(new XFormFieldListener() {
071:
072: public void valueChanged(
073: XFormField sourceField,
074: String newValue, String oldValue) {
075: boolean enabled = dialog
076: .getBooleanValue(Form.RECREATE_REQUESTS);
077:
078: dialog.getFormField(Form.CREATE_BACKUPS)
079: .setEnabled(enabled);
080: dialog.getFormField(Form.RECREATE_OPTIONAL)
081: .setEnabled(enabled);
082: }
083: });
084: }
085:
086: dialog.setValue(Form.DEFINITION_URL, iface.getDefinition());
087: if (!dialog.show())
088: return;
089:
090: String url = dialog.getValue(Form.DEFINITION_URL);
091: if (url == null || url.trim().length() == 0)
092: return;
093:
094: try {
095: File file = new File(url);
096: if (file.exists())
097: url = file.toURL().toString();
098: } catch (Exception e) {
099: SoapUI.logError(e);
100: }
101:
102: boolean createRequests = dialog
103: .getBooleanValue(Form.CREATE_REQUESTS);
104:
105: try {
106: if (iface.updateDefinition(url, createRequests)) {
107: if (dialog.getBooleanValue(Form.RECREATE_REQUESTS)) {
108: int cnt = recreateRequests(iface, dialog
109: .getBooleanValue(Form.RECREATE_OPTIONAL),
110: dialog.getBooleanValue(Form.CREATE_BACKUPS));
111:
112: UISupport.showInfoMessage(
113: "Update of interface successfull, [" + cnt
114: + "] requests have"
115: + " been udpated.",
116: "Update Definition");
117: } else {
118: UISupport.showInfoMessage(
119: "Update of interface successfull",
120: "Update Definition");
121: }
122: } else
123: UISupport.showInfoMessage("Update of interface failed",
124: "Update Definition");
125: } catch (Exception e1) {
126: UISupport.showInfoMessage("Failed to update interface: ["
127: + e1 + "]", "Update Definition");
128: SoapUI.logError(e1);
129: }
130: }
131:
132: protected int recreateRequests(WsdlInterface iface,
133: boolean buildOptional, boolean createBackups) {
134: int count = 0;
135:
136: // first check operations
137: for (int c = 0; c < iface.getOperationCount(); c++) {
138: WsdlOperation operation = iface.getOperationAt(c);
139: String newRequest = operation.createRequest(buildOptional);
140: List<Request> requests = operation.getRequests();
141:
142: for (Request request : requests) {
143: String requestContent = request.getRequestContent();
144: String req = XmlUtils.transferValues(requestContent,
145: newRequest);
146:
147: // changed?
148: if (!req.equals(requestContent)) {
149: if (!XmlUtils.prettyPrintXml(req).equals(
150: XmlUtils.prettyPrintXml(requestContent))) {
151: if (createBackups) {
152: WsdlRequest backupRequest = operation
153: .addNewRequest("Backup of ["
154: + request.getName() + "]");
155: ((WsdlRequest) request).copyTo(
156: backupRequest, false, false);
157: }
158:
159: ((WsdlRequest) request).setRequestContent(req);
160: count++;
161: }
162: }
163: }
164: }
165:
166: // now check testsuites..
167: for (TestSuite testSuite : iface.getProject().getTestSuites()) {
168: for (TestCase testCase : testSuite.getTestCaseList()) {
169: for (int c = 0; c < testCase.getTestStepCount(); c++) {
170: WsdlTestStep testStep = (WsdlTestStep) testCase
171: .getTestStepAt(c);
172: if (testStep instanceof WsdlTestRequestStep) {
173: WsdlTestRequest testRequest = ((WsdlTestRequestStep) testStep)
174: .getTestRequest();
175: String newRequest = testRequest.getOperation()
176: .createRequest(buildOptional);
177:
178: String req = XmlUtils.transferValues(
179: testRequest.getRequestContent(),
180: newRequest);
181:
182: // changed?
183: if (!req
184: .equals(testRequest.getRequestContent())) {
185: if (createBackups) {
186: ((WsdlTestCase) testCase)
187: .importTestStep(
188: testStep,
189: "Backup of ["
190: + testStep
191: .getName()
192: + "]", -1)
193: .setDisabled(true);
194: }
195:
196: ((WsdlRequest) testRequest)
197: .setRequestContent(req);
198: count++;
199: }
200: }
201: }
202: }
203: }
204:
205: return count;
206: }
207:
208: @AForm(description="Specify Update Definition options",name="Update Definition")
209: protected interface Form {
210: @AField(name="Definition URL",description="The URL or file for the updated definition",type=AFieldType.FILE)
211: public final static String DEFINITION_URL = "Definition URL";
212:
213: @AField(name="Create New Requests",description="Create default requests for new methods",type=AFieldType.BOOLEAN)
214: public final static String CREATE_REQUESTS = "Create New Requests";
215:
216: @AField(name="Recreate Requests",description="Recreate existing request with the new schema",type=AFieldType.BOOLEAN)
217: public final static String RECREATE_REQUESTS = "Recreate Requests";
218:
219: @AField(name="Recreate Optional",description="Recreate optional content when updating requests",type=AFieldType.BOOLEAN)
220: public final static String RECREATE_OPTIONAL = "Recreate Optional";
221:
222: @AField(name="Create Backups",description="Create backup copies of changed requests",type=AFieldType.BOOLEAN)
223: public final static String CREATE_BACKUPS = "Create Backups";
224: }
225: }
|