1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.uml;
|
---|
16 |
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.Iterator;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Map;
|
---|
21 | import java.util.Set;
|
---|
22 | import java.util.logging.Level;
|
---|
23 |
|
---|
24 | import org.apache.commons.lang.mutable.MutableInt;
|
---|
25 | import org.eclipse.uml2.uml.CallConcurrencyKind;
|
---|
26 | import org.eclipse.uml2.uml.Component;
|
---|
27 | import org.eclipse.uml2.uml.Connector;
|
---|
28 | import org.eclipse.uml2.uml.DataType;
|
---|
29 | import org.eclipse.uml2.uml.InstanceSpecification;
|
---|
30 | import org.eclipse.uml2.uml.InstanceValue;
|
---|
31 | import org.eclipse.uml2.uml.Interaction;
|
---|
32 | import org.eclipse.uml2.uml.Interface;
|
---|
33 | import org.eclipse.uml2.uml.Lifeline;
|
---|
34 | import org.eclipse.uml2.uml.LiteralBoolean;
|
---|
35 | import org.eclipse.uml2.uml.LiteralInteger;
|
---|
36 | import org.eclipse.uml2.uml.LiteralReal;
|
---|
37 | import org.eclipse.uml2.uml.LiteralString;
|
---|
38 | import org.eclipse.uml2.uml.Message;
|
---|
39 | import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
|
---|
40 | import org.eclipse.uml2.uml.MessageSort;
|
---|
41 | import org.eclipse.uml2.uml.Model;
|
---|
42 | import org.eclipse.uml2.uml.Operation;
|
---|
43 | import org.eclipse.uml2.uml.Package;
|
---|
44 | import org.eclipse.uml2.uml.Parameter;
|
---|
45 | import org.eclipse.uml2.uml.PrimitiveType;
|
---|
46 | import org.eclipse.uml2.uml.Property;
|
---|
47 | import org.eclipse.uml2.uml.Slot;
|
---|
48 | import org.eclipse.uml2.uml.UMLPackage;
|
---|
49 | import org.w3c.dom.Element;
|
---|
50 |
|
---|
51 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
52 | import de.ugoe.cs.autoquest.plugin.http.SOAPUtils;
|
---|
53 | import de.ugoe.cs.autoquest.plugin.http.eventcore.SimpleSOAPEventType.CallType;
|
---|
54 | import de.ugoe.cs.util.StringTools;
|
---|
55 | import de.ugoe.cs.util.console.Console;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Utility class to create UML Interactions from event sequences
|
---|
60 | * </p>
|
---|
61 | *
|
---|
62 | * @author Steffen Herbold
|
---|
63 | */
|
---|
64 | class UMLInteractionCreator {
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * model for which the interactions are created
|
---|
68 | */
|
---|
69 | private final Model model;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * name of the test context in which the interactions are created
|
---|
73 | */
|
---|
74 | private final String testContextName;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * defines whether random message bodies are used or the ones associated with the SOAP events
|
---|
78 | */
|
---|
79 | private final boolean useRandomMsgBodies;
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * TODO
|
---|
83 | */
|
---|
84 | private final Map<String, InstanceSpecification> instanceSpecificationCache;
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * <p>
|
---|
88 | * Creates a new UMLInteractionCreator
|
---|
89 | * </p>
|
---|
90 | *
|
---|
91 | * @param model
|
---|
92 | * UML model to which the interaction is added
|
---|
93 | * @param testContextName
|
---|
94 | * Name of the test context that should be used. If this value is null, the first
|
---|
95 | * test context found is used.
|
---|
96 | * @param useRandomRequestBodies
|
---|
97 | * defines is random request bodies are used or the body of the associated event
|
---|
98 | */
|
---|
99 | public UMLInteractionCreator(Model model, String testContextName, boolean useRandomMsgBodies) {
|
---|
100 | this.model = model;
|
---|
101 | this.testContextName = testContextName;
|
---|
102 | this.useRandomMsgBodies = useRandomMsgBodies;
|
---|
103 | instanceSpecificationCache = new HashMap<String, InstanceSpecification>();
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * <p>
|
---|
108 | * Creates the UML Interaction for the given sequence
|
---|
109 | * </p>
|
---|
110 | *
|
---|
111 | * @param sequence
|
---|
112 | * for which the interaction is created
|
---|
113 | * @param interactionName
|
---|
114 | * name of the interaction
|
---|
115 | * @return created interaction
|
---|
116 | */
|
---|
117 | public Interaction createInteraction(List<Event> sequence, String interactionName) {
|
---|
118 | final Component testContext = UMLUtils.fetchTestContext(model, testContextName);
|
---|
119 | if (testContext == null) {
|
---|
120 | throw new RuntimeException("Could not find any test context in the model");
|
---|
121 | }
|
---|
122 |
|
---|
123 | final Operation operation = testContext.createOwnedOperation(interactionName, null, null);
|
---|
124 | operation.applyStereotype(UTPUtils.getTestCaseStereotype(model));
|
---|
125 |
|
---|
126 | final Interaction interaction =
|
---|
127 | (Interaction) testContext.createPackagedElement(interactionName + "_Impl",
|
---|
128 | UMLPackage.Literals.INTERACTION);
|
---|
129 | operation.getMethods().add(interaction);
|
---|
130 |
|
---|
131 | // create lifelines
|
---|
132 | Lifeline userLifeline = null;
|
---|
133 |
|
---|
134 | for (Property property : UMLUtils.fetchAllSUTProperties(testContext)) {
|
---|
135 | String serviceName = property.getName();
|
---|
136 | Lifeline targetLifeline = interaction.createLifeline(serviceName);
|
---|
137 | targetLifeline.setRepresents(property);
|
---|
138 | }
|
---|
139 | for (Property property : UMLUtils.fetchAllTestComponentProperties(testContext)) {
|
---|
140 | userLifeline = interaction.createLifeline(property.getName());
|
---|
141 | userLifeline.setRepresents(property);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (userLifeline == null) {
|
---|
145 | throw new RuntimeException("No TestComponent found, could not create user lifeline.");
|
---|
146 | }
|
---|
147 | if (interaction.getLifelines().size() < 2) {
|
---|
148 | throw new RuntimeException("Fewer than two lifelines created. No SUT found.");
|
---|
149 | }
|
---|
150 |
|
---|
151 | int i = 0;
|
---|
152 | for (Event event : sequence) {
|
---|
153 | if (!(event.equals(Event.STARTEVENT) || event.equals(Event.ENDEVENT))) {
|
---|
154 | String serviceName = SOAPUtils.getServiceNameFromEvent(event);
|
---|
155 | String methodName = SOAPUtils.getCalledMethodFromEvent(event);
|
---|
156 | String clientName = SOAPUtils.getClientNameFromEvent(event);
|
---|
157 | String prefix = interactionName + "_" + i + "_" + methodName + "_";
|
---|
158 | // determine lifelines
|
---|
159 | Lifeline msgTargetLifeline;
|
---|
160 | Lifeline msgSourceLifeline;
|
---|
161 |
|
---|
162 | msgSourceLifeline = interaction.getLifeline(clientName);
|
---|
163 | msgTargetLifeline = interaction.getLifeline(serviceName);
|
---|
164 |
|
---|
165 | if (msgSourceLifeline == null) {
|
---|
166 | throw new RuntimeException(
|
---|
167 | "Error creating message: could not determine source lifeline for component: " +
|
---|
168 | clientName);
|
---|
169 | }
|
---|
170 | if (msgTargetLifeline == null) {
|
---|
171 | throw new RuntimeException(
|
---|
172 | "Error creating message: could not determine target lifeline for component: " +
|
---|
173 | serviceName);
|
---|
174 | }
|
---|
175 | // determine correct target interface
|
---|
176 | Set<Interface> targetInterfaces =
|
---|
177 | UMLUtils.getRealizedInterfacesFromProperty((Property) msgTargetLifeline
|
---|
178 | .getRepresents());
|
---|
179 | if (targetInterfaces.isEmpty()) {
|
---|
180 | throw new RuntimeException("no interface associated with the property " +
|
---|
181 | msgTargetLifeline.getRepresents().getName());
|
---|
182 | }
|
---|
183 | Interface targetInterface = null;
|
---|
184 | for (Interface intface : targetInterfaces) {
|
---|
185 | if (UMLUtils.getOperationFromName(intface.getOperations(), methodName) != null)
|
---|
186 | {
|
---|
187 | // interface found
|
---|
188 | targetInterface = intface;
|
---|
189 | break;
|
---|
190 | }
|
---|
191 | }
|
---|
192 | if (targetInterface == null) {
|
---|
193 | StringBuilder errStrBuilder = new StringBuilder();
|
---|
194 | errStrBuilder
|
---|
195 | .append("Error creating message: operation not found in the implementing interfaces (");
|
---|
196 | Iterator<Interface> iter = targetInterfaces.iterator();
|
---|
197 | while (iter.hasNext()) {
|
---|
198 | String interfaceName = iter.next().getName();
|
---|
199 | errStrBuilder.append(interfaceName);
|
---|
200 | if (iter.hasNext()) {
|
---|
201 | errStrBuilder.append(",");
|
---|
202 | }
|
---|
203 | else {
|
---|
204 | errStrBuilder.append("): " + methodName);
|
---|
205 | }
|
---|
206 | }
|
---|
207 | throw new RuntimeException(errStrBuilder.toString());
|
---|
208 | }
|
---|
209 |
|
---|
210 | Operation calledOperation =
|
---|
211 | UMLUtils.getOperationFromName(targetInterface.getOperations(), methodName);
|
---|
212 | // get connector
|
---|
213 | Connector connector =
|
---|
214 | UMLUtils.inferConnector(msgSourceLifeline, msgTargetLifeline, targetInterface);
|
---|
215 | if (connector == null) {
|
---|
216 | throw new RuntimeException(
|
---|
217 | "Error creating message: could not find connector between the two life lines that supports the target interface at the target lifeline");
|
---|
218 | }
|
---|
219 |
|
---|
220 | boolean asynch = false;
|
---|
221 | if (calledOperation.getConcurrency() == CallConcurrencyKind.CONCURRENT_LITERAL) {
|
---|
222 | asynch = true;
|
---|
223 | }
|
---|
224 |
|
---|
225 | if (SOAPUtils.isSOAPRequest(event)) {
|
---|
226 | // setup for both SYNCH and ASYNCH calls
|
---|
227 | MessageOccurrenceSpecification callSendFragment =
|
---|
228 | (MessageOccurrenceSpecification) interaction
|
---|
229 | .createFragment(prefix + "callSendFragment",
|
---|
230 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
231 | MessageOccurrenceSpecification callRecvFragment =
|
---|
232 | (MessageOccurrenceSpecification) interaction
|
---|
233 | .createFragment(prefix + "callRecvFragment",
|
---|
234 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
235 |
|
---|
236 | callSendFragment.setCovered(msgSourceLifeline);
|
---|
237 | callRecvFragment.setCovered(msgTargetLifeline);
|
---|
238 |
|
---|
239 | // create call
|
---|
240 | Message callMessage = interaction.createMessage(prefix + "call");
|
---|
241 | callMessage.setSignature(calledOperation);
|
---|
242 | setMessageParameters(callMessage, calledOperation, event, prefix);
|
---|
243 | callMessage.setConnector(connector);
|
---|
244 | callMessage.setSendEvent(callSendFragment);
|
---|
245 | callMessage.setReceiveEvent(callRecvFragment);
|
---|
246 | callSendFragment.setMessage(callMessage);
|
---|
247 | callRecvFragment.setMessage(callMessage);
|
---|
248 |
|
---|
249 | if (asynch) {
|
---|
250 | // Create ASYNCH call
|
---|
251 | callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
|
---|
252 | }
|
---|
253 | else {
|
---|
254 | // SYNCH call
|
---|
255 | callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | if (!asynch && SOAPUtils.isSOAPResponse(event)) {
|
---|
259 | // setup reply and behavior execution specifications
|
---|
260 | MessageOccurrenceSpecification replySendFragment =
|
---|
261 | (MessageOccurrenceSpecification) interaction
|
---|
262 | .createFragment(prefix + "replySendFragment",
|
---|
263 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
264 | MessageOccurrenceSpecification replyRecvFragment =
|
---|
265 | (MessageOccurrenceSpecification) interaction
|
---|
266 | .createFragment(prefix + "replyRecvFragment",
|
---|
267 | UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
|
---|
268 |
|
---|
269 | replySendFragment.setCovered(msgTargetLifeline);
|
---|
270 | replyRecvFragment.setCovered(msgSourceLifeline);
|
---|
271 |
|
---|
272 | /*
|
---|
273 | * BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =
|
---|
274 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
275 | * + "_sourceBhvExecSpec",
|
---|
276 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
277 | * BehaviorExecutionSpecification targetBehaviorExecutionSpecification =
|
---|
278 | * (BehaviorExecutionSpecification) interaction .createFragment(":" + methodName
|
---|
279 | * + "_targetBhvExecSpec",
|
---|
280 | * UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
|
---|
281 | *
|
---|
282 | * sourceBehaviorExecutionSpecification.setStart(callSendFragment);
|
---|
283 | * sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment);
|
---|
284 | * targetBehaviorExecutionSpecification.setStart(callRecvFragment);
|
---|
285 | * targetBehaviorExecutionSpecification.setFinish(replySendFragment);
|
---|
286 | */
|
---|
287 |
|
---|
288 | // create reply
|
---|
289 | Message replyMessage = interaction.createMessage(prefix + "_reply");
|
---|
290 | replyMessage.setMessageSort(MessageSort.REPLY_LITERAL);
|
---|
291 | replyMessage.setSignature(calledOperation);
|
---|
292 | // setReplyMessageParameters(replyMessage, calledOperation);
|
---|
293 | setMessageParameters(replyMessage, calledOperation, event, prefix);
|
---|
294 | replyMessage.setConnector(connector);
|
---|
295 | replyMessage.setSendEvent(replySendFragment);
|
---|
296 | replyMessage.setReceiveEvent(replyRecvFragment);
|
---|
297 | replySendFragment.setMessage(replyMessage);
|
---|
298 | replyRecvFragment.setMessage(replyMessage);
|
---|
299 | }
|
---|
300 |
|
---|
301 | i++;
|
---|
302 | }
|
---|
303 | }
|
---|
304 | return interaction;
|
---|
305 | }
|
---|
306 |
|
---|
307 | /**
|
---|
308 | * <p>
|
---|
309 | * Sets values for the parameters of a call message. The values are, if possible, inferred from
|
---|
310 | * the event that is provided.
|
---|
311 | * </p>
|
---|
312 | *
|
---|
313 | * @param message
|
---|
314 | * call message for which the parameters are set
|
---|
315 | * @param calledOperation
|
---|
316 | * operation that is called by the message
|
---|
317 | * @param event
|
---|
318 | * event that provides the parameters; in case of null, default values are assumed
|
---|
319 | * @param prefix
|
---|
320 | * prefix of the call message; used to create good warnings and debugging information
|
---|
321 | */
|
---|
322 | private void setMessageParameters(Message message,
|
---|
323 | Operation calledOperation,
|
---|
324 | Event event,
|
---|
325 | String prefix)
|
---|
326 | {
|
---|
327 | Element requestBody;
|
---|
328 | if (SOAPUtils.isSOAPRequest(event)) {
|
---|
329 | requestBody =
|
---|
330 | SOAPUtils.getSoapBodyFromEvent(event, useRandomMsgBodies, CallType.REQUEST);
|
---|
331 | }
|
---|
332 | else {
|
---|
333 | requestBody =
|
---|
334 | SOAPUtils.getSoapBodyFromEvent(event, useRandomMsgBodies, CallType.RESPONSE);
|
---|
335 | }
|
---|
336 | Package instSpecPkg = null;
|
---|
337 | MutableInt instSpecNumber = new MutableInt(0);
|
---|
338 |
|
---|
339 | // Set parameters of operation
|
---|
340 | for (Parameter param : calledOperation.getOwnedParameters()) {
|
---|
341 | if (instSpecPkg == null) {
|
---|
342 | instSpecPkg = getOrCreateInstanceSpecificationPackage(event);
|
---|
343 | }
|
---|
344 |
|
---|
345 | String path = calledOperation.getName() + ":" + param.getType().getName();
|
---|
346 | if ((UMLUtils.isInParameter(param) && SOAPUtils.isSOAPRequest(event)) ||
|
---|
347 | (UMLUtils.isOutParameter(param) && SOAPUtils.isSOAPResponse(event)))
|
---|
348 | {
|
---|
349 |
|
---|
350 | // create parameters node
|
---|
351 | if (!(param.getType() instanceof DataType)) {
|
---|
352 | throw new RuntimeException("TODO error handling; parameters missing");
|
---|
353 | }
|
---|
354 | DataType parametersNode = (DataType) param.getType();
|
---|
355 | InstanceSpecification instSpecParameters =
|
---|
356 | (InstanceSpecification) instSpecPkg
|
---|
357 | .createPackagedElement(prefix + "instspec" + instSpecNumber.intValue() +
|
---|
358 | "_" + param.getType().getName(),
|
---|
359 | UMLPackage.Literals.INSTANCE_SPECIFICATION);
|
---|
360 | instSpecParameters.getClassifiers().add((DataType) param.getType());
|
---|
361 | instSpecNumber.setValue(instSpecNumber.intValue() + 1);
|
---|
362 | InstanceValue instanceValue =
|
---|
363 | (InstanceValue) message.createArgument(param.getName(), param.getType(),
|
---|
364 | UMLPackage.Literals.INSTANCE_VALUE);
|
---|
365 | instanceValue.setInstance(instSpecParameters);
|
---|
366 |
|
---|
367 | for (Property internalParameter : parametersNode.getAllAttributes()) {
|
---|
368 | if (internalParameter.getType() instanceof DataType) {
|
---|
369 | List<org.w3c.dom.Element> paramNodes =
|
---|
370 | SOAPUtils.getMatchingChildNode(internalParameter.getType().getName(),
|
---|
371 | requestBody);
|
---|
372 | int multiplicityChosen = paramNodes.size();
|
---|
373 |
|
---|
374 | if (multiplicityChosen == 0 && internalParameter.getLower() > 0) {
|
---|
375 | Console
|
---|
376 | .traceln(Level.WARNING,
|
---|
377 | "required attribute not found in SOAP message: " + path);
|
---|
378 | Console
|
---|
379 | .traceln(Level.WARNING,
|
---|
380 | "setting default values for this attribute and all its children");
|
---|
381 | Console.traceln(Level.FINE, "XML structure of path:" +
|
---|
382 | StringTools.ENDLINE + SOAPUtils.getSerialization(requestBody));
|
---|
383 | multiplicityChosen = internalParameter.getLower();
|
---|
384 | }
|
---|
385 | for (int i = 0; i < multiplicityChosen; i++) {
|
---|
386 | org.w3c.dom.Element paramNode = null;
|
---|
387 | if (!paramNodes.isEmpty()) {
|
---|
388 | paramNode = paramNodes.get(i);
|
---|
389 | }
|
---|
390 |
|
---|
391 | Slot slot = instSpecParameters.createSlot();
|
---|
392 | slot.setDefiningFeature(internalParameter);
|
---|
393 |
|
---|
394 | InstanceValue value =
|
---|
395 | (InstanceValue) slot
|
---|
396 | .createValue(internalParameter.getName() + "_" + i,
|
---|
397 | internalParameter.getType(),
|
---|
398 | UMLPackage.Literals.INSTANCE_VALUE);
|
---|
399 | InstanceSpecification instSpec = createInstanceSpecification((DataType) internalParameter
|
---|
400 | .getType(),
|
---|
401 | instSpecPkg, prefix,
|
---|
402 | instSpecNumber, paramNode,
|
---|
403 | path);
|
---|
404 | value.setInstance(instSpec);
|
---|
405 | /*
|
---|
406 | * InstanceValue value = (InstanceValue) argument .createOperand(null,
|
---|
407 | * internalParameter.getType(), UMLPackage.Literals.INSTANCE_VALUE);
|
---|
408 | * value.setInstance(instSpec);
|
---|
409 | */
|
---|
410 | }
|
---|
411 | }
|
---|
412 | else if (internalParameter.getType() instanceof PrimitiveType) {
|
---|
413 | createSlotPrimitiveType(instSpecParameters, internalParameter, requestBody,
|
---|
414 | path);
|
---|
415 | }
|
---|
416 | }
|
---|
417 | }
|
---|
418 | else {
|
---|
419 | // set literalNull for out and return parameters
|
---|
420 | // argument.createOperand(null, param.getType(), UMLPackage.Literals.LITERAL_NULL);
|
---|
421 | message.createArgument(param.getName(), param.getType(),
|
---|
422 | UMLPackage.Literals.LITERAL_NULL);
|
---|
423 | }
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * <p>
|
---|
429 | * Creates an {@link InstanceSpecification} for a data type in the given package. The values are
|
---|
430 | * inferred, if possible, from the DOM node. The prefix and the path are used for naming the
|
---|
431 | * instance specification and to provide good warnings and debug information in case of
|
---|
432 | * problems.
|
---|
433 | * </p>
|
---|
434 | *
|
---|
435 | * @param type
|
---|
436 | * DataType for which the {@link InstanceSpecification} is created
|
---|
437 | * @param pkg
|
---|
438 | * package in which the {@link InstanceSpecification} is created
|
---|
439 | * @param prefix
|
---|
440 | * prefix used for naming the {@link InstanceSpecification}
|
---|
441 | * @param currentNode
|
---|
442 | * node of a DOM from which values are inferred
|
---|
443 | * @param path
|
---|
444 | * used for warnings and debug information
|
---|
445 | * @return {@link InstanceSpecification} for the given type
|
---|
446 | */
|
---|
447 | private InstanceSpecification createInstanceSpecification(DataType type,
|
---|
448 | Package pkg,
|
---|
449 | String prefix,
|
---|
450 | MutableInt instSpecNumber,
|
---|
451 | org.w3c.dom.Element currentNode,
|
---|
452 | String path)
|
---|
453 | {
|
---|
454 | if( instanceSpecificationCache.containsKey(SOAPUtils.getSerialization(currentNode)) ) {
|
---|
455 | return instanceSpecificationCache.get(SOAPUtils.getSerialization(currentNode));
|
---|
456 | }
|
---|
457 | if ("".equals(path)) {
|
---|
458 | path = type.getName();
|
---|
459 | }
|
---|
460 |
|
---|
461 | InstanceSpecification instSpec =
|
---|
462 | (InstanceSpecification) pkg
|
---|
463 | .createPackagedElement(prefix + "instspec" + instSpecNumber.intValue() + "_" +
|
---|
464 | type.getName(),
|
---|
465 | UMLPackage.Literals.INSTANCE_SPECIFICATION);
|
---|
466 | instSpec.getClassifiers().add(type);
|
---|
467 | instSpecNumber.setValue(instSpecNumber.intValue() + 1);
|
---|
468 | for (Property prop : type.getAllAttributes()) {
|
---|
469 | if (prop.getType() instanceof PrimitiveType) {
|
---|
470 | createSlotPrimitiveType(instSpec, prop, currentNode, path);
|
---|
471 | }
|
---|
472 | else if (prop.getType() instanceof DataType) {
|
---|
473 | List<org.w3c.dom.Element> attributeNodes = null;
|
---|
474 | int multiplicityChosen = 0;
|
---|
475 | if (currentNode != null) {
|
---|
476 | attributeNodes = SOAPUtils.getMatchingChildNode(prop.getName(), currentNode);
|
---|
477 | multiplicityChosen = attributeNodes.size();
|
---|
478 | }
|
---|
479 |
|
---|
480 | if (multiplicityChosen == 0 && prop.getLower() > 0) {
|
---|
481 | if (currentNode != null) {
|
---|
482 | Console.traceln(Level.WARNING,
|
---|
483 | "required attribute not found in SOAP message: " + path +
|
---|
484 | "." + prop.getName());
|
---|
485 | Console
|
---|
486 | .traceln(Level.WARNING,
|
---|
487 | "setting default values for this attribute and all its children");
|
---|
488 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
489 | SOAPUtils.getSerialization(currentNode));
|
---|
490 | }
|
---|
491 | multiplicityChosen = prop.getLower();
|
---|
492 | }
|
---|
493 | for (int i = 0; i < multiplicityChosen; i++) {
|
---|
494 | org.w3c.dom.Element attributeNode = null;
|
---|
495 | if (attributeNodes != null && !attributeNodes.isEmpty()) {
|
---|
496 | attributeNode = attributeNodes.get(i);
|
---|
497 | }
|
---|
498 |
|
---|
499 | Slot slot = instSpec.createSlot();
|
---|
500 | slot.setDefiningFeature(prop);
|
---|
501 |
|
---|
502 | InstanceValue value =
|
---|
503 | (InstanceValue) slot.createValue(prop.getName() + "_" + i, prop.getType(),
|
---|
504 | UMLPackage.Literals.INSTANCE_VALUE);
|
---|
505 | value.setInstance(createInstanceSpecification((DataType) prop.getType(), pkg,
|
---|
506 | prefix, instSpecNumber,
|
---|
507 | attributeNode,
|
---|
508 | path + "." + prop.getName()));
|
---|
509 | }
|
---|
510 | }
|
---|
511 | else {
|
---|
512 | Console.traceln(Level.SEVERE, "property neither DataType nor PrimitiveType: " +
|
---|
513 | prop.getType());
|
---|
514 | throw new RuntimeException(
|
---|
515 | "can only handle DataType and PrimitiveType properties but was: " +
|
---|
516 | prop.getType().getClass().getName());
|
---|
517 | }
|
---|
518 | }
|
---|
519 | instanceSpecificationCache.put(SOAPUtils.getSerialization(currentNode), instSpec);
|
---|
520 | return instSpec;
|
---|
521 | }
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * <p>
|
---|
525 | * Creates a {@link Slot} in an {@link InstanceSpecification} for a primitive type.
|
---|
526 | * </p>
|
---|
527 | *
|
---|
528 | * @param instSpec
|
---|
529 | * instance specification to which the slot is added
|
---|
530 | * @param prop
|
---|
531 | * property that describes the slot
|
---|
532 | * @param currentNode
|
---|
533 | * DOM node from which is value for the slot is inferred
|
---|
534 | * @param path
|
---|
535 | * used for warnings and debug information
|
---|
536 | */
|
---|
537 | private static void createSlotPrimitiveType(InstanceSpecification instSpec,
|
---|
538 | Property prop,
|
---|
539 | org.w3c.dom.Element currentNode,
|
---|
540 | String path)
|
---|
541 | {
|
---|
542 | List<String> attributeValues = SOAPUtils.getValuesFromElement(prop.getName(), currentNode);
|
---|
543 |
|
---|
544 | if (attributeValues.isEmpty()) {
|
---|
545 | if (prop.getLower() == 0) {
|
---|
546 | // ignoring optional attribute
|
---|
547 | return;
|
---|
548 | }
|
---|
549 | else {
|
---|
550 | if (currentNode != null) {
|
---|
551 | Console.traceln(Level.WARNING,
|
---|
552 | "required attribute not found in SOAP message: " + path + "." +
|
---|
553 | prop.getName());
|
---|
554 | Console.traceln(Level.WARNING, "setting default values for this attribute");
|
---|
555 | Console.traceln(Level.FINE, "XML structure of path:" + StringTools.ENDLINE +
|
---|
556 | SOAPUtils.getSerialization(currentNode));
|
---|
557 | }
|
---|
558 | attributeValues.add(null);
|
---|
559 | }
|
---|
560 | }
|
---|
561 | for (String attributeValue : attributeValues) {
|
---|
562 | Slot slot = instSpec.createSlot();
|
---|
563 | slot.setDefiningFeature(prop);
|
---|
564 | if ("String".equals(prop.getType().getName())) {
|
---|
565 | LiteralString value =
|
---|
566 | (LiteralString) slot.createValue(prop.getName(), null,
|
---|
567 | UMLPackage.Literals.LITERAL_STRING);
|
---|
568 | if (attributeValue != null) {
|
---|
569 | value.setValue(attributeValue);
|
---|
570 | }
|
---|
571 | else {
|
---|
572 | value.setValue("foobar");
|
---|
573 | }
|
---|
574 | }
|
---|
575 | else if ("Integer".equals(prop.getType().getName())) {
|
---|
576 | LiteralInteger value =
|
---|
577 | (LiteralInteger) slot.createValue(prop.getName(), null,
|
---|
578 | UMLPackage.Literals.LITERAL_INTEGER);
|
---|
579 | if (attributeValue != null) {
|
---|
580 | value.setValue(Integer.parseInt(attributeValue));
|
---|
581 | }
|
---|
582 | else {
|
---|
583 | value.setValue(42);
|
---|
584 | }
|
---|
585 | }
|
---|
586 | else if ("Boolean".equals(prop.getType().getName())) {
|
---|
587 | LiteralBoolean value =
|
---|
588 | (LiteralBoolean) slot.createValue(prop.getName(), null,
|
---|
589 | UMLPackage.Literals.LITERAL_BOOLEAN);
|
---|
590 | if (attributeValue != null) {
|
---|
591 | value.setValue(Boolean.parseBoolean(attributeValue));
|
---|
592 | }
|
---|
593 | else {
|
---|
594 | value.setValue(true);
|
---|
595 | }
|
---|
596 | }
|
---|
597 | else if ("Real".equals(prop.getType().getName())) {
|
---|
598 | LiteralReal value =
|
---|
599 | (LiteralReal) slot.createValue(prop.getName(), null,
|
---|
600 | UMLPackage.Literals.LITERAL_REAL);
|
---|
601 | if (attributeValue != null) {
|
---|
602 | value.setValue(Double.parseDouble(attributeValue));
|
---|
603 | }
|
---|
604 | else {
|
---|
605 | value.setValue(3.14);
|
---|
606 | }
|
---|
607 | }
|
---|
608 | else {
|
---|
609 | Console.traceln(Level.SEVERE, "could not create literal for primitive type: " +
|
---|
610 | prop.getType().getName());
|
---|
611 | throw new RuntimeException("unknown primitive type: " + prop.getType().getName());
|
---|
612 | }
|
---|
613 | }
|
---|
614 | }
|
---|
615 |
|
---|
616 | /**
|
---|
617 | * <p>
|
---|
618 | * Gets or creates a {@link Package} for {@link InstanceSpecification} created by the
|
---|
619 | * usage-based testing. Each service gets its own sub-package within a package called
|
---|
620 | * UBT_InstanceSpecifications. "
|
---|
621 | * </p>
|
---|
622 | *
|
---|
623 | * @param event
|
---|
624 | * event from which the service name is inferred
|
---|
625 | * @return package for the {@link InstanceSpecification}s
|
---|
626 | */
|
---|
627 | private Package getOrCreateInstanceSpecificationPackage(Event event) {
|
---|
628 | String pkgUBTInstSpecs = "UBT_InstanceSpecifications";
|
---|
629 | Package ubtInstSpecPkg = (Package) model.getOwnedMember(pkgUBTInstSpecs);
|
---|
630 | if (ubtInstSpecPkg == null) {
|
---|
631 | ubtInstSpecPkg =
|
---|
632 | (Package) model.createPackagedElement(pkgUBTInstSpecs, UMLPackage.Literals.PACKAGE);
|
---|
633 | }
|
---|
634 | String serviceName = SOAPUtils.getServiceNameFromEvent(event);
|
---|
635 | Package serviceInstSpecPkg = (Package) ubtInstSpecPkg.getOwnedMember(serviceName);
|
---|
636 | if (serviceInstSpecPkg == null) {
|
---|
637 | serviceInstSpecPkg =
|
---|
638 | (Package) ubtInstSpecPkg.createPackagedElement(serviceName,
|
---|
639 | UMLPackage.Literals.PACKAGE);
|
---|
640 | }
|
---|
641 | return serviceInstSpecPkg;
|
---|
642 | }
|
---|
643 |
|
---|
644 | }
|
---|