PODAM - Gathering Requirements
Join the DZone community and get the full member experience.
Join For Freeas announced in my previous article i am starting a new project to provide an easy api to fill in pojos with dummy data.
the goal of this article is to collect a first draft of requirements for podam. ok, so what's needed by a tool to fill pojos with dummy data?
- the tool should provide a black box through a simple function: given as argument a pojo class, the function must return an instance of the pojo class with all its instance variables filled with data, unless otherwise stated by a custom annotation (see next point).
- the possibility, through annotations, to customise the behaviour of such tool. for instance one might want to skip the auto-generation of data for some pojo attributes; or one might want to pilot the values assigned to a pojo: for instance on a numeric attribute the value assigned by the tool should be between 5 and 100. other customisation could go in the direction of fine grained value assignments to properties (boolean, date, etc)
- the possibility to be integrated with tools such as junit, although podam should not extend junit but rather junit could make use of podam through composition. in this scenario, instead of invoking a factory function which returns an instance of a pojo, one could declared a @podam annotation on a (instance) local variable in a junit (or other testing framework) test and the factory method would then be invoked.
- for podam to work the pojo being set with dummy data must have a non-argument constructor
let's try to capture the above requirements in steps. let's start with the simplest scenario, e.g. i want a pojo with all its attributes (including its graph objects) set with some dummy values. given the following domain model:
a client has got an address and one or more bankaccounts. if i am writing a junit test for a service which uses the client pojo, i'd like podam to offer something like this:
public class clientserviceunittest {
@junit
public void testclientservice() throws exception {
clientservice service = easymock.createmock(clientservice.class);
client clientdto = podamfactory.createdummy(client.class);
service.persistclient(clientdto);
easymock.expectlastcall();
//etc. replay and mock validation
}
}
podamfactory should return a client pojo whose primitive types attributes are filled with values but also whose graphed objects are filled with data. so the client will have an address object filled with dummy data and a collection of one or more bank accounts.
now let's look at some more interesting scenarios. let's say that i wanted a client pojo filled with data but i wanted the address attribute to be skipped. i could write something like the following:
//imports omitted
public class client implements serializable {
private string firstname;
private string lastname;
@podam(skip=true) private address address;
private list<bankaccount> bankaccounts;
//constructors, getters/setters, equals/hashcode and tostring methods omitted for brevity
}
i could use a @podam annotation with boolean attribute skip = true to indicate that a certain attribute should not be filled by podam. of course for graphed objects to be filled with dummy data, these should be podam-compliant (e.g. have a no-arguments constructor).
another scenario could be that i wanted to specify exactly how many bank accounts i wanted. using the same class as before i could have:
//imports omitted
public class client implements serializable {
private string firstname;
private string lastname;
private address address;
@podam(nbrelements=5)
private list<bankaccount> bankaccounts;
//constructors, getters/setters, equals/hashcode and tostring methods omitted for brevity
}
by using the nbrelements attribute i could instruct podam on how many elements in the list i'd like to be filled with dummy data. so it seems that a @podam annotation is emerging from the initial requirements. the question here is whether such annotation (which belongs mainly to the testing world) would be acceptable in a production pojo code. my view is that annotations in general are innocuous and that the benefits of having fine grained control over auto-generated dummy data during testing outweighs the cons of having a "test related" import in our code.
let's see if i can define some attributes of a @podam annotation.
- skip. (boolean). it instructs the tool to skip the auto-generation of dummy data for the annotated attribute. the default is false.
- value. (string). it asks the tool to assign exactly the value specified in the attribute. the default is a blank string.
- minvalue. (numeric). it requires the tool to assign a numeric value to the annotated attribute which should not be less than the value defined in the annotation.
- maxvalue. (numeric). similar to minvalue except that the value assigned to the annotated attribute should not be greater than the value indicated by the annotation. the default is the maximum value for the type of the annotated attribute.
- nbrelements. in case of a collection, the number of elements the tool should create with dummy values. the default is 1.
- length. (numeric). the length of the dummy string value assigned to the annotated attribute. the default can be an arbitrary number, such as 5, 10, 20, etc.
so far these are the attributes i could think of. what do you think folks?
from http://tedone.typepad.com/blog/2011/03/podam-gathering-requirements.html
Opinions expressed by DZone contributors are their own.
Comments