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.mockoperation;
014:
015: import java.awt.event.ActionEvent;
016: import java.util.ArrayList;
017: import java.util.List;
018:
019: import com.eviware.soapui.config.DispatchStyleConfig;
020: import com.eviware.soapui.impl.wsdl.WsdlInterface;
021: import com.eviware.soapui.impl.wsdl.WsdlOperation;
022: import com.eviware.soapui.impl.wsdl.WsdlProject;
023: import com.eviware.soapui.impl.wsdl.actions.iface.AbstractSwingAction;
024: import com.eviware.soapui.impl.wsdl.mock.WsdlMockOperation;
025: import com.eviware.soapui.impl.wsdl.mock.WsdlMockResult;
026: import com.eviware.soapui.model.iface.Interface;
027: import com.eviware.soapui.model.iface.Operation;
028: import com.eviware.soapui.model.mock.MockResponse;
029: import com.eviware.soapui.model.util.ModelItemNames;
030: import com.eviware.soapui.support.UISupport;
031: import com.eviware.soapui.support.action.swing.DefaultActionList;
032: import com.eviware.soapui.support.xml.XmlUtils;
033: import com.eviware.x.form.XFormDialog;
034: import com.eviware.x.form.XFormField;
035: import com.eviware.x.form.XFormFieldListener;
036: import com.eviware.x.form.support.ADialogBuilder;
037: import com.eviware.x.form.support.AField;
038: import com.eviware.x.form.support.AForm;
039: import com.eviware.x.form.support.AField.AFieldType;
040:
041: /**
042: * Displays the options for the specified WsdlMockOperation
043: *
044: * @author ole.matzura
045: */
046:
047: public class WsdlMockOperationOptionsAction extends
048: AbstractSwingAction<WsdlMockOperation> {
049: private XFormDialog dialog;
050: private DefineNamespacesAction defineNamespacesAction;
051: private WsdlProject project;
052:
053: public WsdlMockOperationOptionsAction(
054: WsdlMockOperation mockOperation) {
055: super ("MockOperation Options",
056: "Sets options for this MockOperation", mockOperation);
057:
058: project = mockOperation.getMockService().getProject();
059: }
060:
061: @Override
062: public void actionPerformed(ActionEvent arg0,
063: WsdlMockOperation mockOperation) {
064: if (dialog == null) {
065: DefaultActionList actions = new DefaultActionList();
066: defineNamespacesAction = new DefineNamespacesAction(
067: mockOperation);
068: actions.addAction(defineNamespacesAction);
069:
070: dialog = ADialogBuilder.buildDialog(Form.class, actions);
071: dialog.getFormField(Form.DISPATCH_STYLE)
072: .addFormFieldListener(new XFormFieldListener() {
073:
074: public void valueChanged(
075: XFormField sourceField,
076: String newValue, String oldValue) {
077: boolean enabled = newValue.equals("XPATH")
078: || newValue.equals("SCRIPT");
079:
080: enableXPathFields(enabled);
081: defineNamespacesAction.setEnabled(enabled);
082: }
083: });
084:
085: dialog.getFormField(Form.INTERFACE).addFormFieldListener(
086: new XFormFieldListener() {
087:
088: public void valueChanged(
089: XFormField sourceField,
090: String newValue, String oldValue) {
091: WsdlInterface iface = project
092: .getInterfaceByName(newValue);
093: dialog.setOptions(Form.OPERATION,
094: new ModelItemNames<Operation>(iface
095: .getOperations())
096: .getNames());
097: }
098: });
099: }
100:
101: List<Interface> interfaces = new ArrayList<Interface>();
102: for (int c = 0; c < project.getInterfaceCount(); c++) {
103: if (project.getInterfaceAt(c).getOperationCount() > 0)
104: interfaces.add(project.getInterfaceAt(c));
105: }
106:
107: dialog.setOptions(Form.INTERFACE,
108: new ModelItemNames<Interface>(interfaces).getNames());
109: dialog.setOptions(Form.OPERATION,
110: new ModelItemNames<Operation>(interfaces.get(0)
111: .getOperations()).getNames());
112:
113: dialog.setValue(Form.INTERFACE, mockOperation.getOperation()
114: .getInterface().getName());
115: dialog.setValue(Form.OPERATION, mockOperation.getOperation()
116: .getName());
117:
118: dialog.setOptions(Form.DEFAULT_RESPONSE,
119: new ModelItemNames<MockResponse>(mockOperation
120: .getMockResponses()).getNames());
121:
122: dialog.setValue(Form.DISPATCH_STYLE, mockOperation
123: .getDispatchStyle().toString());
124: dialog.setValue(Form.DISPATCH_PATH, mockOperation
125: .getDispatchPath());
126: dialog.setValue(Form.DEFAULT_RESPONSE, mockOperation
127: .getDefaultResponse());
128:
129: enableXPathFields(dialog.getValue(Form.DISPATCH_STYLE).equals(
130: "XPATH")
131: || dialog.getValue(Form.DISPATCH_STYLE)
132: .equals("SCRIPT"));
133:
134: if (dialog.show()) {
135: mockOperation.setDispatchStyle(DispatchStyleConfig.Enum
136: .forString(dialog.getValue(Form.DISPATCH_STYLE)));
137: mockOperation.setDispatchPath(dialog
138: .getValue(Form.DISPATCH_PATH));
139: mockOperation.setDefaultResponse(dialog
140: .getValue(Form.DEFAULT_RESPONSE));
141:
142: WsdlOperation operation = project
143: .getInterfaceByName(dialog.getValue(Form.INTERFACE))
144: .getOperationByName(dialog.getValue(Form.OPERATION));
145:
146: if (operation != mockOperation.getOperation())
147: mockOperation.setOperation(operation);
148: }
149: }
150:
151: private void enableXPathFields(boolean enabled) {
152: dialog.getFormField(Form.DISPATCH_PATH).setEnabled(enabled);
153: dialog.getFormField(Form.DEFAULT_RESPONSE).setEnabled(enabled);
154: defineNamespacesAction.setEnabled(enabled);
155: }
156:
157: @AForm(description="Set options for this MockOperation",name="MockOperation Options")
158: private class Form {
159: @AField(description="Specifies the operation to be mocked",name="Operation",type=AFieldType.ENUMERATION)
160: public final static String OPERATION = "Operation";
161:
162: @AField(description="Specifies the interface containing the operation to be mocked",name="Interface",type=AFieldType.ENUMERATION)
163: public final static String INTERFACE = "Interface";
164:
165: @AField(description="How to dispatch requests to responses",name="Dispatch Style",type=AFieldType.ENUMERATION,values={"SEQUENCE","RANDOM","XPATH","SCRIPT"})
166: public final static String DISPATCH_STYLE = "Dispatch Style";
167:
168: @AField(description="The XPath to use for selecting the corresponding response",name="Dispatch Path",type=AFieldType.STRINGAREA)
169: public final static String DISPATCH_PATH = "Dispatch Path";
170:
171: @AField(description="Default response to use",name="Default Response",type=AFieldType.ENUMERATION)
172: public final static String DEFAULT_RESPONSE = "Default Response";
173: }
174:
175: public class DefineNamespacesAction extends
176: AbstractSwingAction<WsdlMockOperation> {
177: public DefineNamespacesAction(WsdlMockOperation mockOperation) {
178: super ("Define Namespaces",
179: "Defines namespaces from last Mock Request",
180: mockOperation);
181: }
182:
183: @Override
184: public void actionPerformed(ActionEvent arg0,
185: WsdlMockOperation mockOperation) {
186: WsdlMockResult result = mockOperation.getLastMockResult();
187: if (result == null || result.getMockRequest() == null) {
188: UISupport
189: .showErrorMessage("Missing request to define from");
190: } else {
191: try {
192: String ns = XmlUtils.declareXPathNamespaces(result
193: .getMockRequest().getRequestContent());
194: if (ns != null) {
195: ns += dialog.getValue(Form.DISPATCH_PATH);
196: dialog.setValue(Form.DISPATCH_PATH, ns);
197: }
198: } catch (Exception e) {
199: UISupport.showErrorMessage(e);
200: }
201: }
202: }
203: }
204: }
|