net.sf.jzeno.tests
Class Populator

java.lang.Object
  extended by net.sf.jzeno.tests.Populator

public class Populator
extends java.lang.Object

Populator is a helper class for facilitating the creation of test enitities.


Constructor Summary
Populator()
           
 
Method Summary
 void clear()
           Removes all objects ever created, or enlisted on this populator.
 java.lang.Object create(java.lang.Class clazz)
           Creates a new entity in the database, and fills in all primitive properties.
 java.lang.Object create(java.lang.Class clazz, java.lang.String prefix)
           Creates a new entity in the database, and fills in all primitive properties.
 void delete(java.lang.Object o)
           Removes the indicated object from the database.
 void end()
           Ends the started transaction.
 java.lang.Object[] executeQuery(java.lang.String query)
           Executes a given HQL query
 java.lang.Object[] executeQuery(java.lang.String query, java.lang.String paramName, java.lang.Object paramValue)
           Executes a given HQL query
 java.lang.Object find(java.lang.Class clazz)
           
 java.lang.Object[] find(java.lang.Class clazz, int howMany)
           
 java.lang.Object find(java.lang.String className)
           
 java.lang.Object[] find(java.lang.String className, int howMany)
           Retrieves a number of objects of the specified type.
 java.util.List findAll(java.lang.Class clazz)
           
 java.util.List findAll(java.lang.String className)
           Retrieves ALL available objects of the given type.
 java.lang.String getRandomNumericString(int length)
           This operation generates random strings of given length.
 Session getSession()
           Convenience function to retrieve the current session.
 boolean isAutoCreate()
           
 Entity load(Entity outOfSessionObject)
           Loads a fresh copy of the given object from the DB.
 void rollback()
           Rolls back the started transaction.
 void save(java.lang.Object o)
           
 void setAutoCreate(boolean autoCreate)
           
 Session start()
           Starts a new transaction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Populator

public Populator()
Method Detail

create

public java.lang.Object create(java.lang.Class clazz)

Creates a new entity in the database, and fills in all primitive properties. Strings are filled in with "Anonymous", a sequence number (unique to the instance of the populator), a dot and the name of the property itself. Numbers are filled in with a random value between 0 and 10000. Booleans are set to false, and the object returned is associated with the Active status object. This method assumes such a Active status object exists beforehand. Date attributes are set to the Date/Time of creation.


create

public java.lang.Object create(java.lang.Class clazz,
                               java.lang.String prefix)

Creates a new entity in the database, and fills in all primitive properties. Strings are filled in with the prefix specified, a dot and the name of the property itself. Numbers are filled in with a random value between 0 and 10000. Booleans are set to false. Date attributes are set to the Date/Time of creation.

many-to-one relations are by default filled in with a random selected object of the right type. Similar to doing p.find(TypeOfRelation.class) and object.setXYZ(..) yourself.


start

public Session start()

Starts a new transaction.


end

public void end()

Ends the started transaction.


rollback

public void rollback()

Rolls back the started transaction.


clear

public void clear()

Removes all objects ever created, or enlisted on this populator. This operation can work in an existing transaction, or will create a transaction of its own when no transaction is available. If a transaction is created, it will also close that temporary transaction.


find

public java.lang.Object[] find(java.lang.String className,
                               int howMany)

Retrieves a number of objects of the specified type. Useful for when you need some marginal entity for you test entities, but it's not relay important which specific instance gets used.

Returns:
null if no objects found.

findAll

public java.util.List findAll(java.lang.String className)

Retrieves ALL available objects of the given type.


findAll

public java.util.List findAll(java.lang.Class clazz)

find

public java.lang.Object find(java.lang.Class clazz)

find

public java.lang.Object[] find(java.lang.Class clazz,
                               int howMany)

find

public java.lang.Object find(java.lang.String className)

executeQuery

public java.lang.Object[] executeQuery(java.lang.String query)

Executes a given HQL query


executeQuery

public java.lang.Object[] executeQuery(java.lang.String query,
                                       java.lang.String paramName,
                                       java.lang.Object paramValue)

Executes a given HQL query


getSession

public Session getSession()

Convenience function to retrieve the current session. Handy for doing on the fly queries, etc...


save

public void save(java.lang.Object o)

delete

public void delete(java.lang.Object o)

Removes the indicated object from the database.


getRandomNumericString

public java.lang.String getRandomNumericString(int length)

This operation generates random strings of given length. It's usefull to mark certain objects in your unit test with a unique indicator.


load

public Entity load(Entity outOfSessionObject)

Loads a fresh copy of the given object from the DB.


isAutoCreate

public boolean isAutoCreate()

setAutoCreate

public void setAutoCreate(boolean autoCreate)