Index: trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java
===================================================================
--- trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java	(revision 1896)
+++ trunk/autoquest-plugin-uml/src/main/java/de/ugoe/cs/autoquest/plugin/uml/UMLUtils.java	(revision 1897)
@@ -270,5 +270,6 @@
      *            name of the interaction
      * @param testContextName
-     *            Name of the test context that should be used. If this value is null, the first test context found is used.
+     *            Name of the test context that should be used. If this value is null, the first
+     *            test context found is used.
      */
     public static void createInteractionFromEventSequence(List<Event> sequence,
@@ -284,8 +285,8 @@
 
         Component testContext = fetchTestContext(model, utpTestContext, testContextName);
-        if( testContext==null ) {
+        if (testContext == null) {
             throw new RuntimeException("Could not find any test context in the model");
         }
-        
+
         Operation operation = testContext.createOwnedOperation(interactionName, null, null);
         operation.applyStereotype(utpTestCase);
@@ -298,5 +299,5 @@
         // create lifelines
         Lifeline userLifeline = null;
-        // List<Port> userPorts = new LinkedList<>();
+        
         for (Property property : testContext.getAllAttributes()) {
             if (property.getAppliedStereotypes().contains(utpSUT)) {
@@ -306,11 +307,18 @@
             }
             else if (property.getType().getAppliedStereotypes().contains(utpTestComponent)) {
+                if( userLifeline!=null ) {
+                    throw new RuntimeException("TestContext must only have one TestComponent for the application of usage-based testing.");
+                }
                 userLifeline = interaction.createLifeline(property.getName());
                 userLifeline.setRepresents(property);
-            }    
+            }
+        }
+        if( userLifeline==null ) {
+            throw new RuntimeException("No TestComponent found, could not create user lifeline.");
+        }
+        if( interaction.getLifelines().size()<2 ) {
+            throw new RuntimeException("Fewer than two lifelines created. No SUT found.");
         }
         
-        // TODO sanity checks for userLifeline!=null, etc.
-
         int i = 0;
         for (Event event : sequence) {
@@ -321,13 +329,13 @@
                 Lifeline msgTargetLifeline;
                 Lifeline msgSourceLifeline;
-                
-                if( serviceName.equals(userLifeline.getName()) ) {
+
+                if (serviceName.equals(userLifeline.getName())) {
                     // message being send to user
                     // currently we just select the first lifeline that is not the user
-                    // this, obviously, has to be replaced with the real service. 
+                    // this, obviously, has to be replaced with the real service.
                     // however, identification of the source of a message is still an open issue
                     msgSourceLifeline = null;
-                    for( Lifeline lifeline : interaction.getLifelines() ) {
-                        if(!lifeline.equals(userLifeline)){
+                    for (Lifeline lifeline : interaction.getLifelines()) {
+                        if (!lifeline.equals(userLifeline)) {
                             msgSourceLifeline = lifeline;
                             break;
@@ -335,18 +343,24 @@
                     }
                     msgTargetLifeline = userLifeline;
-                } else {
+                }
+                else {
                     msgSourceLifeline = userLifeline;
                     msgTargetLifeline = interaction.getLifeline(serviceName);
                 }
-                
-                // TODO null checks for lifelines
-
-                // determine target interface and be able to deal with multiple interfaces
-                List<Interface> targetInterfaces = getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
-                if( targetInterfaces.isEmpty() ) {
-                    throw new RuntimeException("no interface associated with the property " + msgTargetLifeline.getRepresents().getName());
+                if( msgSourceLifeline==null ) {
+                    throw new RuntimeException("Error creating message: could not determine source lifeline.");
+                }
+                if( msgTargetLifeline==null ) {
+                    throw new RuntimeException("Error creating message: could not determine target lifeline.");
+                }
+                // determine correct target interface
+                List<Interface> targetInterfaces =
+                    getRealizedInterfacesFromProperty((Property) msgTargetLifeline.getRepresents());
+                if (targetInterfaces.isEmpty()) {
+                    throw new RuntimeException("no interface associated with the property " +
+                        msgTargetLifeline.getRepresents().getName());
                 }
                 Interface targetInterface = null;
-                for( Interface intface : targetInterfaces ) {
+                for (Interface intface : targetInterfaces) {
                     System.out.println(intface.getOperations());
                     if (getOperationFromName(intface.getOperations(), methodName) != null) {
@@ -356,14 +370,15 @@
                     }
                 }
-                if( targetInterface == null ) {
+                if (targetInterface == null) {
                     StringBuilder errStrBuilder = new StringBuilder();
-                    errStrBuilder.append("operation not found in the implementing interfaces (");
+                    errStrBuilder.append("Error creating message: operation not found in the implementing interfaces (");
                     Iterator<Interface> iter = targetInterfaces.iterator();
-                    while( iter.hasNext() ) {
+                    while (iter.hasNext()) {
                         String interfaceName = iter.next().getName();
                         errStrBuilder.append(interfaceName);
-                        if( iter.hasNext() ) {
+                        if (iter.hasNext()) {
                             errStrBuilder.append(",");
-                        } else {
+                        }
+                        else {
                             errStrBuilder.append("): " + methodName);
                         }
@@ -371,66 +386,42 @@
                     throw new RuntimeException(errStrBuilder.toString());
                 }
-                
-                // create message ASYNCH
+
+                Operation calledOperation =
+                    getOperationFromName(targetInterface.getOperations(), methodName);
+
+                // setup for both SYNCH and ASYNCH calls
+                MessageOccurrenceSpecification callSendFragment =
+                    (MessageOccurrenceSpecification) interaction
+                        .createFragment(i + ":" + methodName + "_callSendFragment",
+                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+                MessageOccurrenceSpecification callRecvFragment =
+                    (MessageOccurrenceSpecification) interaction
+                        .createFragment(i + ":" + methodName + "_callRecvFragment",
+                                        UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
+
+                callSendFragment.setCovered(msgSourceLifeline);
+                callRecvFragment.setCovered(msgTargetLifeline);
+
+                // create call
+                Message callMessage = interaction.createMessage(methodName);
+                callMessage.setSignature(calledOperation);
+                callMessage.setConnector(inferConnector(msgSourceLifeline, msgTargetLifeline));
+                callMessage.setSendEvent(callSendFragment);
+                callMessage.setReceiveEvent(callRecvFragment);
+                callSendFragment.setMessage(callMessage);
+                callRecvFragment.setMessage(callMessage);
+
+                // TODO somehow infer if called operation is SYNCH or ASYNCH
+                // possibly requires additional stereotype
                 boolean asynch = false;
-                if( asynch ) {
-                    MessageOccurrenceSpecification sendFragment =
-                        (MessageOccurrenceSpecification) interaction
-                            .createFragment(i + ":" + methodName + "_sendFragment",
-                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-                    MessageOccurrenceSpecification recvFragment =
-                        (MessageOccurrenceSpecification) interaction
-                            .createFragment(i + ":" + methodName + "_recvFragment",
-                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-    
-                    sendFragment.setCovered(msgSourceLifeline);
-                    recvFragment.setCovered(msgTargetLifeline);
-    
-                    Message message = interaction.createMessage(methodName);
-                    if (getOperationFromName(targetInterface.getOperations(), methodName) == null) {
-                        System.out.println("operation not found in the " + targetInterface.getName() + " interface: " + methodName);
-                    }
-                    message.setSignature(getOperationFromName(targetInterface.getOperations(),
-                                                              methodName));
-                    message.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
-                    message.setSendEvent(sendFragment);
-                    message.setReceiveEvent(recvFragment);
-    
-                    // now the connector needs to be determined
-                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
-                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
-                    
-                    for( Property userAttribute : userAttributes ) {
-                        if( userAttribute instanceof Port ) {
-                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
-                            for( ConnectorEnd userEnd : userEnds ) {
-                                Connector userConnector = (Connector) userEnd.eContainer();
-                                for( Property targetAttribute : targetAttributes ) {
-                                    if( targetAttribute instanceof Port ) {
-                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
-                                        for( ConnectorEnd targetEnd : targetEnds ) {
-                                            Connector targetConnector = (Connector) targetEnd.eContainer();
-                                            if( targetConnector==userConnector ) {
-                                                message.setConnector(targetConnector);
-                                            }
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                    }
-                    sendFragment.setMessage(message);
-                    recvFragment.setMessage(message);
-                } else {
+                if (asynch) {
+                    // Create ASYNCH call
+                    callMessage.setMessageSort(MessageSort.ASYNCH_CALL_LITERAL);
+                }
+                else {
                     // SYNCH call
-                    MessageOccurrenceSpecification callSendFragment =
-                        (MessageOccurrenceSpecification) interaction
-                            .createFragment(i + ":" + methodName + "_callSendFragment",
-                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-                    MessageOccurrenceSpecification callRecvFragment =
-                        (MessageOccurrenceSpecification) interaction
-                            .createFragment(i + ":" + methodName + "_callRecvFragment",
-                                            UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-                    
+                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
+
+                    // setup reply and behavior execution specifications
                     MessageOccurrenceSpecification replySendFragment =
                         (MessageOccurrenceSpecification) interaction
@@ -441,60 +432,22 @@
                             .createFragment(i + ":" + methodName + "_replyRecvFragment",
                                             UMLPackage.Literals.MESSAGE_OCCURRENCE_SPECIFICATION);
-    
-                    BehaviorExecutionSpecification sourceBehaviorExecutionSpecification = 
+
+                    replySendFragment.setCovered(msgTargetLifeline);
+                    replyRecvFragment.setCovered(msgSourceLifeline);
+                    
+                    /*BehaviorExecutionSpecification sourceBehaviorExecutionSpecification =
                         (BehaviorExecutionSpecification) interaction
                             .createFragment(":" + methodName + "_sourceBhvExecSpec",
                                             UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
-                    BehaviorExecutionSpecification targetBehaviorExecutionSpecification = 
+                    BehaviorExecutionSpecification targetBehaviorExecutionSpecification =
                         (BehaviorExecutionSpecification) interaction
                             .createFragment(":" + methodName + "_targetBhvExecSpec",
                                             UMLPackage.Literals.BEHAVIOR_EXECUTION_SPECIFICATION);
-                    
-                    
-                    callSendFragment.setCovered(msgSourceLifeline);
-                    callRecvFragment.setCovered(msgTargetLifeline);
-                    replySendFragment.setCovered(msgTargetLifeline);
-                    replyRecvFragment.setCovered(msgSourceLifeline);
-                    
+
                     sourceBehaviorExecutionSpecification.setStart(callSendFragment);
                     sourceBehaviorExecutionSpecification.setFinish(replyRecvFragment);
                     targetBehaviorExecutionSpecification.setStart(callRecvFragment);
-                    targetBehaviorExecutionSpecification.setFinish(replySendFragment);
-    
-                    Operation calledOperation = getOperationFromName(targetInterface.getOperations(), methodName);
-                    
-                    // create call
-                    Message callMessage = interaction.createMessage(methodName);
-                    callMessage.setSignature(calledOperation);
-                    callMessage.setMessageSort(MessageSort.SYNCH_CALL_LITERAL);
-                    callMessage.setSendEvent(callSendFragment);
-                    callMessage.setReceiveEvent(callRecvFragment);
-                    callSendFragment.setMessage(callMessage);
-                    callRecvFragment.setMessage(callMessage);
-    
-                    // now the connector needs to be determined
-                    EList<Property> userAttributes = ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
-                    EList<Property> targetAttributes = ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
-                    
-                    for( Property userAttribute : userAttributes ) {
-                        if( userAttribute instanceof Port ) {
-                            EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
-                            for( ConnectorEnd userEnd : userEnds ) {
-                                Connector userConnector = (Connector) userEnd.eContainer();
-                                for( Property targetAttribute : targetAttributes ) {
-                                    if( targetAttribute instanceof Port ) {
-                                        EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
-                                        for( ConnectorEnd targetEnd : targetEnds ) {
-                                            Connector targetConnector = (Connector) targetEnd.eContainer();
-                                            if( targetConnector==userConnector ) {
-                                                callMessage.setConnector(targetConnector);
-                                            }
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                    }
-                    
+                    targetBehaviorExecutionSpecification.setFinish(replySendFragment);*/
+
                     // create reply
                     Message replyMessage = interaction.createMessage(methodName + "_reply");
@@ -506,5 +459,4 @@
                     replyRecvFragment.setMessage(replyMessage);
                 }
-                
 
                 i++;
@@ -535,5 +487,6 @@
         for (InteractionFragment interactionFragment : interactionFragments) {
             if (interactionFragment.getName() != null &&
-                interactionFragment.getName().endsWith("_recvFragment"))
+                interactionFragment.getName().endsWith("_recvFragment")) // TODO must be more
+                                                                         // generic
             {
                 String serviceName =
@@ -563,7 +516,10 @@
      *            model to be extended
      * @param usageProfile
-     *            usage profile used as foundation 
-     */
-    public static void createScheduling(Model model, IStochasticProcess usageProfile, String testContextName) {
+     *            usage profile used as foundation
+     */
+    public static void createScheduling(Model model,
+                                        IStochasticProcess usageProfile,
+                                        String testContextName)
+    {
 
         final Profile utpProfile = model.getAppliedProfile("utp");
@@ -572,5 +528,5 @@
 
         Component testContext = fetchTestContext(model, utpTestContext, testContextName);
-        if( testContext==null ) {
+        if (testContext == null) {
             throw new RuntimeException("Could not find any test context in the model");
         }
@@ -743,59 +699,55 @@
         return matching;
     }
-    
+
     private static List<Interface> getRealizedInterfacesFromProperty(Property property) {
         return getRealizedInterfaceFromComponent((Component) property.getType());
     }
-    
+
     private static List<Interface> getRealizedInterfaceFromComponent(Component comp) {
         List<Interface> interfaces = new LinkedList<>();
-        //Interface myInterface = null;
-        for( Property property : comp.getAttributes() ) {
-            if( property instanceof Port ) {
+        // Interface myInterface = null;
+        for (Property property : comp.getAttributes()) {
+            if (property instanceof Port) {
                 Port port = (Port) property;
-                if( !port.isConjugated() ) {
-                    interfaces.addAll(port.getProvideds());
-                    /*
-                    if( myInterface==null ) {
-                        myInterface = port.getProvideds().get(0);
-                    } 
-                    else if( myInterface!=port.getProvideds().get(0)) {
-                        System.err.println("multiple different interfaces found");
-                    }
-                    */
+                if (!port.isConjugated()) {
+                    interfaces.addAll(port.getProvideds());                  
                 }
             }
         }
         return interfaces;
-        //return myInterface;
-        //return ((Port) comp.getAttributes().get(0)).getInterface();
-        //Realization realization = (Realization) comp.getNestedClassifiers().get(0).getRelationships(UMLPackage.Literals.REALIZATION).get(0);
-        //return (Interface) realization.getSuppliers().get(0);
-    }
-    
-    private static Component fetchTestContext(Package pkg, Stereotype utpTestContext, String testContextName) {
+    }
+
+    private static Component fetchTestContext(Package pkg,
+                                              Stereotype utpTestContext,
+                                              String testContextName)
+    {
         List<Component> testContexts = fetchTestContextRecursively(pkg, utpTestContext);
-        if( testContexts.isEmpty() ) {
+        if (testContexts.isEmpty()) {
             return null;
         }
-        if( testContextName!=null ) {
-            for( Component testContext : testContexts ) {
-                if( testContextName.equals(testContext.getName()) ) {
+        if (testContextName != null) {
+            for (Component testContext : testContexts) {
+                if (testContextName.equals(testContext.getName())) {
                     return testContext;
                 }
             }
             return null;
-        } else {
+        }
+        else {
             return testContexts.get(0);
         }
     }
-    
-    private static List<Component> fetchTestContextRecursively(Package pkg, Stereotype utpTestContext) {
+
+    private static List<Component> fetchTestContextRecursively(Package pkg,
+                                                               Stereotype utpTestContext)
+    {
         List<Component> testContexts = new LinkedList<>();
-        for( Element element : pkg.getOwnedElements() ) {
-            if( element instanceof Package ) {
+        for (Element element : pkg.getOwnedElements()) {
+            if (element instanceof Package) {
                 testContexts.addAll(fetchTestContextRecursively((Package) element, utpTestContext));
             }
-            if( element instanceof Component && element.getAppliedStereotypes().contains(utpTestContext) ) {
+            if (element instanceof Component &&
+                element.getAppliedStereotypes().contains(utpTestContext))
+            {
                 testContexts.add((Component) element);
             }
@@ -803,3 +755,40 @@
         return testContexts;
     }
+
+    /**
+     * <p>
+     * Infers connector between two lifelines. 
+     * TODO: currently assumes only one connector between two lifelines possible. I need to make sure this assumption is valid.
+     * </p>
+     * 
+     * @param userAttributes
+     * @param targetAttributes
+     */
+    private static Connector inferConnector(Lifeline msgSourceLifeline, Lifeline msgTargetLifeline)
+    {
+        EList<Property> userAttributes =
+            ((Component) msgSourceLifeline.getRepresents().getType()).getAttributes();
+        EList<Property> targetAttributes =
+            ((Component) msgTargetLifeline.getRepresents().getType()).getAttributes();
+        for (Property userAttribute : userAttributes) {
+            if (userAttribute instanceof Port) {
+                EList<ConnectorEnd> userEnds = ((Port) userAttribute).getEnds();
+                for (ConnectorEnd userEnd : userEnds) {
+                    Connector userConnector = (Connector) userEnd.eContainer();
+                    for (Property targetAttribute : targetAttributes) {
+                        if (targetAttribute instanceof Port) {
+                            EList<ConnectorEnd> targetEnds = ((Port) targetAttribute).getEnds();
+                            for (ConnectorEnd targetEnd : targetEnds) {
+                                Connector targetConnector = (Connector) targetEnd.eContainer();
+                                if (targetConnector == userConnector) {
+                                    return targetConnector;
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
 }
