de.akquinet.jbosscc.needle.db.testdata
Class AbstractTestdataBuilder<T>

java.lang.Object
  extended by de.akquinet.jbosscc.needle.db.testdata.AbstractTestdataBuilder<T>
Type Parameters:
T - The type of the object to build.
All Implemented Interfaces:
TestdataBuilder<T>

public abstract class AbstractTestdataBuilder<T>
extends Object
implements TestdataBuilder<T>

A abstract implementation of TestdataBuilder.


 Implementation example:

 public class PersonTestDataBuilder extends AbstractTestdataBuilder {

  ...

        public PersonTestDataBuilder() {
         super();
        }

        public PersonTestDataBuilder(EntityManager entityManager) {
         super(entityManager);
        }

        public PersonTestDataBuilder withName(String name){
         this.withName = name;
         return this;
        }

        public PersonTestDataBuilder withAddress(Address address){
         this.withName = name;
         return this;
        }

        public Person build() {
         Person person = new Person();
         ...
         return person;
        }


 Usage example:

 Person transientPerson = new PersonTestDataBuilder(em).build();
 Person persistedPerson = new PersonTestDataBuilder(em).buildAndSave();
 new PersonTestDataBuilder(em).withAddress(address).buildAndSave();



 


Constructor Summary
AbstractTestdataBuilder()
          Creates an new TestdataBuilder without persistence.
AbstractTestdataBuilder(javax.persistence.EntityManager entityManager)
          Creates an new TestdataBuilder with persistence.
 
Method Summary
 T buildAndSave()
          Creates a new instance of type T and save the instance.
protected  void ensureEntityManager()
          Ensure the TestdataBuilder is constructed with a EntityManager
protected  javax.persistence.EntityManager getEntityManager()
          Returns the EntityManager or null.
protected  int getId()
          Returns an integer value from an static counter.
protected  boolean hasEntityManager()
          Returns whether the TestdataBuilder is constructed with a EntityManager
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.akquinet.jbosscc.needle.db.testdata.TestdataBuilder
build
 

Constructor Detail

AbstractTestdataBuilder

public AbstractTestdataBuilder(javax.persistence.EntityManager entityManager)
Creates an new TestdataBuilder with persistence.

Parameters:
entityManager -

AbstractTestdataBuilder

public AbstractTestdataBuilder()
Creates an new TestdataBuilder without persistence.

Method Detail

getEntityManager

protected final javax.persistence.EntityManager getEntityManager()
Returns the EntityManager or null.

Returns:
EntityManager or null

hasEntityManager

protected final boolean hasEntityManager()
Returns whether the TestdataBuilder is constructed with a EntityManager

Returns:
true if EntityManager is available, else false

ensureEntityManager

protected final void ensureEntityManager()
Ensure the TestdataBuilder is constructed with a EntityManager

Throws:
IllegalStateException - if the TestdataBuilder is constructed without a EntityManager

buildAndSave

public final T buildAndSave()
Creates a new instance of type T and save the instance. Executed within a new transaction.

Specified by:
buildAndSave in interface TestdataBuilder<T>
Returns:
a new persisted instance of type T
Throws:
IllegalStateException - if the TestdataBuilder is constructed without a EntityManager

getId

protected final int getId()
Returns an integer value from an static counter.

Returns:
value of the static counter.


Copyright © 2012 akquinet AG. All Rights Reserved.