Chapter 9. Plug-ins

Table of Contents

9.1. Create a Plug-in

The PETA test description language has been developed with two aspects in mind: Keep it simple to be easily learned, and make it extendable to enable adaption to user's demands. A plug-in extends PETA 's build-in functionality, e.g. to create or transform data like timestamps, message IDs, or some values that have to be calculated from other values. It is realized as a Java class that implements the de.verit.peta.engine.plugin.Plugin interface.

Until now, the implemented test case requestCourseList sends a fixed timestamp in the client's request. Of course, the test case would fail if the server checks this timestamp and declines the request if it does not match a time criterion, for example if the timestamp has to be 'near' the current timestamp. In this case, a timestamp for the current time has to be inserted into the request message. PETA offers the extension mechanism of plug-ins to handle such a situation. In this chapter, a plug-in named Timestamp will be implemented, that returns a timestamp of a specified format. Then, this plug-in will be used in the test case to insert the current timestamp into the request.

[Note]Further information

Detailed information about PETA plug-ins can be found in the PETA Core Reference Manual.

9.1. Create a Plug-in

To create a new PETA plug-in that creates a timestamp, press the Create a new plug-in button in the toolbar. In the wizard, set the source folder of the plug-in's java file to TutorialProject/src and enter a package name. Enter Timestamp as the class name and press the Next button.

Figure 9.1.  Create new PETA plug-in

Create new PETA plug-in


Enter a description for this plug-in on the second page, for example Creates a timestamp in specified format using the current time., and press the Next button to reach the last page.

Figure 9.2. Description of the new PETA plug-in

Description of the new PETA plug-in


To specify the plug-in's argument, press the Add button, enter format as the name and activate the Required checkbox. Keep the other two checkboxes ( Multiple allowed and Requires key ) unselected and press OK.

Figure 9.3.  Definition of the PETA plug-in's argument

Definition of the PETA plug-in's argument


Finally, to leave the wizard, press the Finish button. A java file will be created in the specified package and opened in the editor.

Figure 9.4.  Arguments of new PETA plug-in

Arguments of new PETA plug-in


The following example shows the implementation of the Timestamp plug-in.

Example 9.1.  Implementation of the Timestamp plug-in

package de.verit.peta.tutorial.plugin;

import java.text.SimpleDateFormat;
import java.util.Date;

import de.verit.peta.engine.plugin.Plugin;
import de.verit.peta.modules.Arguments;

/**
 * Implementation of a peta plugin that creates a timestamp from
 * current time.
 * The format of the timestamp must be specified by argument
 * 'format'.
 */
public class Timestamp implements Plugin {

	/**
	 * Constructor.
	 */
	public Timestamp() {
		super();
	}

	/**
	 * @see Plugin#process(de.verit.peta.modules.Arguments)
	 */
	public String process(Arguments arguments)
	    throws Exception {

		// create formatter from argument
		String format = arguments.get("format");
		SimpleDateFormat formatter =
		    new SimpleDateFormat(format);

		// get current date
		Date currentDate = new Date();

		return formatter.format(currentDate);
	}

}

The plug-in is now ready for use in the test case. To insert the current timestamp into the request, expand the operation of the first event, select the template and open the wizard (right-click, select Properties ). Press the Add Substitution button and enter /request/timestamp as the Constant Path that shall be substituted. To use the plug-in, press the Add Plug-in button and select Timestamp from the list of available plug-ins. Timestamp should now appear in the lower left window. Expand it, select its argument format and specify the argument's value by pressing Add Constant and entering the format string yyyy-MM-dd HH:mm:ss.

Figure 9.5. Timestamp insertion

Timestamp insertion


When the test case is executed now, the current timestamp is sent with the client's request.