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>

An 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 a new TestdataBuilder without persistence.
AbstractTestdataBuilder(javax.persistence.EntityManager entityManager)
          Creates a new TestdataBuilder with persistence.
 
Method Summary
 T buildAndSave()
          Creates a new instance of type T and saves the instance.
protected  void ensureEntityManager()
          Ensure the TestdataBuilder is constructed with an EntityManager
protected  javax.persistence.EntityManager getEntityManager()
          Returns the EntityManager or null.
protected  int getId()
          Returns an integer value from a static counter.
protected  boolean hasEntityManager()
          Returns whether the TestdataBuilder is constructed with an 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 a new TestdataBuilder with persistence.

Parameters:
entityManager - EntityManager to be used by this TestdataBuilder

AbstractTestdataBuilder

public AbstractTestdataBuilder()
Creates a 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 an EntityManager

Returns:
true if EntityManager is available, else false

ensureEntityManager

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

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

buildAndSave

public final T buildAndSave()
Creates a new instance of type T and saves 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 an EntityManager

getId

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

Returns:
value of the static counter.


Copyright © 2013 akquinet AG. All Rights Reserved.