Class ScriptableDataSet

java.lang.Object
org.dbunit.dataset.AbstractDataSet
de.gmorling.scriptabledataset.ScriptableDataSet
All Implemented Interfaces:
org.dbunit.dataset.IDataSet

public class ScriptableDataSet extends org.dbunit.dataset.AbstractDataSet
An implementation of a DBUnit IDataSet, that allows the use of script expressions in its fields. In order to use a certain scripting language in a scriptable data set, a JSR 223 ("Scripting for the JavaTM Platform") compatible script engine has to exist for that language.

Using the JRuby engine e.g., a scriptable data set file could look like this:

 <dataset>
     <location num="jruby:12/2" addr="jruby:'Webster Street'.reverse" date="jruby:DateTime::now() - 14"/>
 </dataset>
 
A ScriptableDataSet can be created as follows:
 IDataSet wrapped = ...;

 List<ScriptInvocationHandler> handlers = new ArrayList<Class<? extends ScriptInvocationHandler>>();
 handlers.add(new JRubyImportAddingInvocationHandler());

 IDataSet scriptableDS = new ScriptableDataSet(
      wrapped, new ScriptableDataSetConfig("jruby", "jruby:", handlers));
 
where
  • jruby is the name of a scripting language as understood by ScriptEngineManager
  • jruby: is a prefix, that shall precede fields in that scripting language
  • handlers is an optional list of ScriptInvocationHandler s, that can be used to pre-process scripts (e.g. to add common imports) and post-process scripts (e.g. to convert results into data types understood by DBUnit).