Invoking Benerator Programmatically
For integrating Benerator with other applications, you can invoke it programmatically in a Java appliaction too.
Invoking Benerator as a whole
For executing descriptor files do the following:
// create an instance of the Descriptor runner specifying the descriptor file |
Making Benerator generate data and invoke a custom class with it
If you want to use Benerator for feeding a custom class with generated data, implement the Consumer interface in a way that connects to your program, instantiate it and register it with the BeneratorContext:
| DescriptorRunner runner = new DescriptorRunner("path/to/file/benerator.xml"); BeneratorContext context = runner.getContext(); context.setValidate(false); // add a custom Consumer MyConsumer myConsumer = new MyConsumer(); context.set("myConsumer", myConsumer); runner.run(); |
A simplistic implementation could simply write entities to the console, e.g.
| class MyConsumer extends AbstractConsumer<Entity> { List<Entity> products = new ArrayList<Entity>(); public void startConsuming(Entity entity) { products.add(entity); } } |
Using generators defined in a descriptor file
You can define data generation in a Benerator descriptor file, make Benerator configure the generator and hand it out to your application.
First, define a descriptor file, e.g.
| <?xml version="1.0" encoding="iso-8859-1"?> <setup xmlns="http://databene.org/benerator/0.6.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://databene.org/benerator/0.6.0 http://databene.org/benerator-0.6.0.xsd"> <import defaults="true" /> <generate type="address"> <attribute name="city" values="'Munich', 'New York', 'Tokyo'" /> </generate> </setup> |
Then you can get the address generator from Benerator by calling:
BeneratorContext context = new BeneratorContext(); |


