databene

 
  • Increase font size
  • Default font size
  • Decrease font size

Base File Format

<?xml version="1.0" encoding="iso-8859-1"?>
<setup xmlns="http://databene.org/benerator/0.5.8"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://databene.org/benerator/0.5.8 http://databene.org/benerator-0.5.8.xsd">

    ...content here...

</setup>

Hello World

Descriptor:

    <import defaults="true"/>
    <create-entities name="message" count="5" consumer="ConsoleExporter" >
        <attribute name="text" type="string" pattern="(Hello|Hi|Howdy) (World|Earth|Planet)"/>
    </create-entities>

Output:

message[text=Hello World]
message[text=Hi World]
message[text=Hello Planet]
message[text=Hello Planet]
message[text=Howdy Earth]

Simple-Type Data Generation

Regular-Expression-based

  <attribute name="wecomeMessage" type="string" pattern="(Hello|Hi|Howdy) (World|Earth|Planet)"/>
<attribute name="phone" pattern="[0-9]{3}-[2-9]{1}[0-9]{2}-[0-9]{4} {0,1}[0-9]{1,5}" />

List-based

  <attribute name="rank" type="string" pattern="A,B,C"/>

Script-based

  <variable name="block" pattern="[A-Z]" />
<variable name="buildingNo" pattern="[1-9]" />
<attribute name="building" script="{Building ${block}${buildingNo}}"/>

Decisions in FreeMarker

A common requirement is to have alternative ways to render some data, depending on context or necessary data conversion, e.g. for entities with multifield constraints or mapping  of null values or booleans. The simplest way 

The simplest way to configure this is a FreeMarker condition expression in the general form:

<attribute name="test" script="[#ftl][#if condition]expression[#else]expression[/#if]" />

An example that mixes data of two alternatives alt1 and alt2 (possibly taken from two different data sets) is:

<attribute name="test" script="[#ftl][#if random < percentage]${alt1}[#else]${alt2}[/#if]" /> 

Please note, that random and percentage can be variables or properties defined before and may not be wrapped with ${}!

Exporting entities in different formats

Writing entities to a CSV file

    <create-entities name="Person" count="5">
        <id name="id" type="int"/>
        <attribute name="name" type="string"/>
        <consumer class="org.databene.platform.csv.CSVEntityExporter">
            <property name="uri"      value="users.csv"/>
            <property name="encoding" value="UTF-8"/>
            <property name="columns"  value="id,name"/>
        </consumer>
    </create-entities>

creates a file 'users.csv' in UTF-8 encoding with the columns id and name:

id,name
1,GCZJWGBIOIVUS
2,IQMJKBSLETWOZQRIPEE
3,IKBSQEFCCHG
4,A
5,XGQOLYXSVWPJLRMVCLDTLMBVT

Database related

Defining a database

<database id="db" url="jdbc:hsqldb:hsql://localhost" driver="org.hsqldb.jdbcDriver" user="sa" password="" schema="public" batch="false"/>  

Executing DDL

from a file

<execute uri="create-tables" target="db" onError="fatal"/>

inline

<execute target="db" type="sql" onError="warn"> 
  CREATE TABLE db_example (
    id   int         NOT NULL,
    name varchar(16) NOT NULL,
    PRIMARY KEY (id)
  );
INSERT INTO db_example (id, name) VALUES (1, "Alpha")</execute>  

Importing data from a from a DbUnit XML file

<create-entities source="shop.dbunit.xml" consumer="db"/> 

Exporting generated data to a SQL file

The SQLEntityExporter needs a reference to a database that holds metadata for the tables to be written to the export file.

<database id="db" ... />

<consumer class="SQLEntityExporter">
    <property name="uri" value="my.sql" />
    <property name="database" ref="db"
</consumer>