Concordion - Returning Object



Concordion execute command can be used to get the result of a behavior in the form of object using which we can get multiple outputs of a behavior. For example, consider the following requirement −

The full name Robert De is to be broken into first name Robert and last name De.

Here we need to have a split function which accepts a user name and returns a result object having the first name and the last name as its properties so that we can use them.

If we want to write a specification for such a split function which will expect a user name and output a result object, then the following will be the specification −

<p>The full name <span concordion:execute = "#result = split(#TEXT)">Robert 
   De</span> is to be broken into first name 
   <span concordion:assertEquals = "#result.firstName">Robert</span> and last name 
   <span concordion:assertEquals = "#result.lastName">De</span>.</p>

When Concordion parses the document, it will set the value of the special variable #TEXT as the value of the current element as "Robert De" and pass it to the split function. Then it will execute the split() method with parameters as #TEXT using the execute command and set the result into the #result variable and using the result object, print the firstName and the lastName properties as the output.

Example

Let us have a working Eclipse IDE in place and follow the steps given below to create a Concordion application −

Step Description
1 Create a project with the name concordion and create a package com.tutorialspoint under the src folder in the created project.
2 Add the required Concordion libraries using the Add External JARs option as explained in the Concordion - First Application chapter.
3 Create Java class System, Result under the com.tutorialspoint package.
4 Create Fixture class SystemFixture under the specs.tutorialspoint package.
5 Create Specification html System.html under the specs.tutorialspoint package.
6 The final step is to create the content of all the Java files and specification file and run the application as explained below.

Here is the content of Result.java file −

package com.tutorialspoint;
public class Result {
   private String firstName;
   private String lastName;
	
   public String getFirstName() {
      return firstName;
   }
	
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
	
   public String getLastName() {
      return lastName;
   }
	
   public void setLastName(String lastName) {
      this.lastName = lastName;
   } 
}

Here is the content of System.java file −

package com.tutorialspoint;
public class System {
   public Result split(String userName){
      Result result = new Result();
      String[] words = userName.split(" ");
      result.setFirstName(words[0]);
      result.setLastName(words[1]);
      return result;
   }
}

Following is the content of SystemFixture.java file−

package specs.tutorialspoint;

import com.tutorialspoint.Result;
import com.tutorialspoint.System;
import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;

@RunWith(ConcordionRunner.class)

public class SystemFixture {
   System system = new System();
   public Result split(String userName){
      return system.split(userName);
   }  
}

Following is the content of System.html file −

<html xmlns:concordion = "http://www.concordion.org/2007/concordion">
   <head>
      <link href = "../concordion.css" rel = "stylesheet" type = "text/css" />
   </head>

   <body>
      <h1>System Specifications</h1>
      <p>We are building specifications for our online order tracking application.</p>
      <p>Following is the requirement to split full name of a logged in user to its 
         constituents by splitting name by whitespace:</p>
			
      <div class = "example">      
         <h3>Example</h3>
         <p>The full name <span concordion:execute = "#result = split(#TEXT)">Robert 
            De</span> is to be broken into first name <span 
            concordion:assertEquals = "#result.firstName">Robert</span> and last name <span 
            concordion:assertEquals = "#result.lastName">De</span>.</p>
      </div>
		
   </body>
	
</html>

Once you are done with creating the source and specification files, let us run the application as JUnit Test. If everything is fine with your application, then it will produce the following result −

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\concordion\specs\tutorialspoint\System.html
Successes: 1, Failures: 0

System.html is the output of Concordion test run.

concordion Returning Object Output
Advertisements