org.specrunner.plugins.core.language
Class PluginSentence

java.lang.Object
  extended by org.specrunner.parameters.core.ParameterHolder
      extended by org.specrunner.plugins.core.AbstractPlugin
          extended by org.specrunner.plugins.core.language.PluginSentence
All Implemented Interfaces:
IParameterHolder, IActionType, IParalelPlugin, IPlugin, ISleepPlugin, ITestPlugin, ITimedPlugin, IWaitPlugin

public class PluginSentence
extends AbstractPlugin

A natural language plugin to perform pattern matching like JBehave/Cucumber/Twist fixtures. Method match by default is performed from Java to arguments, the other direction is possible using after=true.

Author:
Thiago Santos

Field Summary
protected static ICache<Class<?>,List<Method>> cacheMethods
          Cache of type to methods annotated with sentence.
protected static ICache<String,Pattern> cachePatterns
          Cache of patterns.
 
Fields inherited from class org.specrunner.plugins.core.AbstractPlugin
FEATURE_CONDITION, FEATURE_NORMALIZED, FEATURE_SLEEP, FEATURE_THREADSAFE, FEATURE_TIMEOUT, FEATURE_WAIT
 
Constructor Summary
PluginSentence()
           
 
Method Summary
 void doEnd(IContext context, IResultSet result)
          The end action. i.e. after a loop specification perform something in a newly generated table.
protected  void extractMethodNameArguments(IContext context, Object target, StringBuilder methodName, List<Object> arguments)
          Extract text and arguments.
protected  boolean fromAnnotations(String value, Object target, StringBuilder text, List<Object> args)
          Perform method search from annotations.
 ActionType getActionType()
          Get the plugin type.
 Boolean getAfter()
          The flag status.
 String getMethod()
          Get method name.
protected  Method getMethodAfter(Object target, String method, List<Object> args)
          Get object method.
protected  Method getMethodBefore(Object target, String method, List<Object> args)
          Get object method.
protected  Object getObject(IContext context)
          Get the object instance to be used by plugin actions.
protected  void onlyArgs(IContext context, nu.xom.Node node, StringBuilder text, List<Object> args)
          Extract arguments from nodes.
protected  void onlyText(String text, StringBuilder methodName, List<Object> arguments)
          Extract parameters from text.
protected  void prepareArgumentsAfter(IContext context, Method method, List<Object> arguments)
          Prepare argument list, based on parameter types.
protected  void prepareArgumentsBefore(IContext context, Method method, List<Object> arguments)
          Prepare argument list, based on parameter types.
protected  String removePlaceholders(Method method, String str)
          Remove place holders registered in Placeholders. i.e.
 void setAfter(Boolean after)
          The flag to perform method lookup after arguments conversion.
 void setMethod(String method)
          The method to call to this sentence.
 
Methods inherited from class org.specrunner.plugins.core.AbstractPlugin
copy, doStart, getCondition, getConditionModel, getNormalized, getNormalized, getParent, getSleep, getSleepModel, getThreadsafe, getTimeout, getTimeoutModel, getWait, getWaitModel, initialize, setCondition, setConditionModel, setNormalized, setParent, setSleep, setSleepModel, setThreadsafe, setTimeout, setTimeoutModel, setWait, setWaitModel, toString
 
Methods inherited from class org.specrunner.parameters.core.ParameterHolder
getParameters, setParameters
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.specrunner.parameters.IParameterHolder
getParameters, setParameters
 

Field Detail

cacheMethods

protected static ICache<Class<?>,List<Method>> cacheMethods
Cache of type to methods annotated with sentence.


cachePatterns

protected static ICache<String,Pattern> cachePatterns
Cache of patterns.

Constructor Detail

PluginSentence

public PluginSentence()
Method Detail

getActionType

public ActionType getActionType()
Description copied from interface: IActionType
Get the plugin type.

Returns:
The type.

getMethod

public String getMethod()
Get method name.

Returns:
The method name.

setMethod

public void setMethod(String method)
The method to call to this sentence.

Parameters:
method - The method name.

getAfter

public Boolean getAfter()
The flag status.

Returns:
true, to perform method lookup after arguments conversion, false, to perform method.

setAfter

public void setAfter(Boolean after)
The flag to perform method lookup after arguments conversion.

Parameters:
after - true, to lookup method after arguments conversion, false, otherwise.

doEnd

public void doEnd(IContext context,
                  IResultSet result)
           throws PluginException
Description copied from interface: IPlugin
The end action. i.e. after a loop specification perform something in a newly generated table. Performed after doStart().

Specified by:
doEnd in interface IPlugin
Overrides:
doEnd in class AbstractPlugin
Parameters:
context - The context.
result - The result set.
Throws:
PluginException - On execution errors.

getObject

protected Object getObject(IContext context)
Get the object instance to be used by plugin actions.

Parameters:
context - The context.
Returns:
An object instance where all actions will taken over.

extractMethodNameArguments

protected void extractMethodNameArguments(IContext context,
                                          Object target,
                                          StringBuilder methodName,
                                          List<Object> arguments)
                                   throws PluginException
Extract text and arguments.

Parameters:
context - The context.
target - The target object.
methodName - The text part.
arguments - The argument objects.
Throws:
PluginException - On errors.

fromAnnotations

protected boolean fromAnnotations(String value,
                                  Object target,
                                  StringBuilder text,
                                  List<Object> args)
                           throws PluginException
Perform method search from annotations.

Parameters:
value - The node text value.
target - The target object.
text - The text.
args - The arguments.
Returns:
true, if some method annotation matches the value, false, otherwise.
Throws:
PluginException - On invalid placeholder replacement.

removePlaceholders

protected String removePlaceholders(Method method,
                                    String str)
                             throws PluginException
Remove place holders registered in Placeholders. i.e. $int will be replaced to '(\\d+)'.

Parameters:
method - The method.
str - The string to be replaced.
Returns:
The replaced string.
Throws:
PluginException - On invalid placeholder replacement.

onlyText

protected void onlyText(String text,
                        StringBuilder methodName,
                        List<Object> arguments)
Extract parameters from text. In this approach, all parameters are expected as strings delimited by quotes.

Parameters:
text - The text.
methodName - The method name.
arguments - The arguments.

onlyArgs

protected void onlyArgs(IContext context,
                        nu.xom.Node node,
                        StringBuilder text,
                        List<Object> args)
                 throws PluginException
Extract arguments from nodes.

Parameters:
context - The context.
node - The node.
text - The text.
args - The arguments.
Throws:
PluginException - On errors.

getMethodBefore

protected Method getMethodBefore(Object target,
                                 String method,
                                 List<Object> args)
                          throws PluginException
Get object method.

Parameters:
target - The object target.
method - The method name.
args - Possible arguments.
Returns:
The corresponding method.
Throws:
PluginException - On method lookup errors.

prepareArgumentsBefore

protected void prepareArgumentsBefore(IContext context,
                                      Method method,
                                      List<Object> arguments)
                               throws PluginException
Prepare argument list, based on parameter types.

Parameters:
context - The context.
method - The method.
arguments - The arguments.
Throws:
PluginException - On preparation errors.

getMethodAfter

protected Method getMethodAfter(Object target,
                                String method,
                                List<Object> args)
                         throws PluginException
Get object method.

Parameters:
target - The object target.
method - The method name.
args - Possible arguments.
Returns:
The corresponding method.
Throws:
PluginException - On method lookup errors.

prepareArgumentsAfter

protected void prepareArgumentsAfter(IContext context,
                                     Method method,
                                     List<Object> arguments)
                              throws PluginException
Prepare argument list, based on parameter types.

Parameters:
context - The context.
method - The method.
arguments - The arguments.
Throws:
PluginException - On preparation errors.


Copyright © 2014. All rights reserved.