Implementing the function class

If you have a function that is written for HCL OneTest™ API version 8.5, you can convert it to work in version 8.6 or later.

For details, see Converting existing function classes with Eclipse.

Note: You can find the source code for the example in the examples\FunctionsSamplePlugin\src folder of your HCL OneTest™ API installation.

Java class extending from function

Under the src folder of the plug-in project, create a Java™ class that extends from com.ghc.ghTester.expressions.Function (for example, com.samples.functions.FormatDate).

Note: Ensure that the class is placed in a package other than the default package. To do so, make a valid package declaration at the top of the Java class.

Now the following methods need to be implemented:

  • The default, public constructor
  • create(int, Vector)
  • evaluate(Object)

Default constructor

Create a default, public constructor with an empty body. You can add other initialization as required, but it does not have to do anything, by default. The default constructor must be public because of the way that functions are created.

public FormatDate() {
}

Override create (int size, Vector) parameters

The create(int, Vector) method is a factory method that creates an instance of the particular function with the parameters provided. The vector of parameters needs to be treated as functions themselves and are likely to need processing at run time.

public Function create(int size, Vector params) {
   Function outputFormat = null;
   if (size == 3) {
      outputFormat = (Function) params.get(2);
   }
   return new FormatDate((Function) params.get(0), (Function) params
                  .get(1), outputFormat);
}

The three-argument constructor that is used in the example can be found in the source that is provided in the examples folder of the HCL OneTest™ API installation folder.

Override evaluate(Object) data

The evaluate(Object) method performs the actual work of the function. HCL OneTest™ API passes the context of the current evaluation to your function. You can use this method to obtain the result from embedded functions and then return your function’s own result.

public Object evaluate(Object data) {
   String date = m_fDate.evaluateAsString(data);
   String inputFormat = m_fInputFormat.evaluateAsString(data);
   String outputFormat = "yyyy-MM-dd"; // Default format
      if (m_fOutputFormat != null) {
   outputFormat = m_fOutputFormat.evaluateAsString(data);
   }

   //...
   if (EvalUtils.isString(date)) {
      date = EvalUtils.getString(date);
   }
   if (EvalUtils.isString(inputFormat)) {
      inputFormat = EvalUtils.getString(inputFormat);
   }
   if (EvalUtils.isString(outputFormat)) {
      outputFormat = EvalUtils.getString(outputFormat);
   }

   SimpleDateFormat inputFormatter = new 
SimpleDateFormat(inputFormat);
   String formattedDate = "";
   try {
      Date d = inputFormatter.parse(date);
      SimpleDateFormat outputFormatter =new 
SimpleDateFormat(outputFormat);
      formattedDate = outputFormatter.format(d);
   } catch (ParseException ex) {
      // ...
   }

   return "\"" + formattedDate + "\"";
}
Note: When you are implementing your evaluate method, the vector of parameters that is passed to you are functions that must be evaluated to discover their value. Where a string literal is expected, the evaluated function must return a value that is surrounded by quotation marks. A helper function is available to assist with the removal: EvalUtils.getString(Object string).
Feedback