Commit 8e100d323a74df61ac57fe6990b6a431ac931b63
1 parent
619ec32c
test: einige TestDataBuilders (WIP)
Showing
3 changed files
with
134 additions
and
0 deletions
src/main/java/net/ziemers/swxercise/lg/model/testdata/testdatabuilder/AbstractTestDataBuilder.java
0 → 100644
| 1 | +package net.ziemers.swxercise.lg.model.testdata.testdatabuilder; | ||
| 2 | + | ||
| 3 | +import javax.persistence.EntityManager; | ||
| 4 | + | ||
| 5 | +public abstract class AbstractTestDataBuilder<T> { | ||
| 6 | + | ||
| 7 | + private static int count = 0; | ||
| 8 | + | ||
| 9 | + private EntityManager entityManager; | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * Creates an new TestdataBuilder with persistence. | ||
| 13 | + * | ||
| 14 | + * @param entityManager | ||
| 15 | + */ | ||
| 16 | + public AbstractTestDataBuilder(final EntityManager entityManager) { | ||
| 17 | + this.entityManager = entityManager; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * Creates an new TestdataBuilder without persistence. | ||
| 22 | + */ | ||
| 23 | + public AbstractTestDataBuilder() { | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + public abstract T build(); | ||
| 27 | + | ||
| 28 | + /** | ||
| 29 | + * Returns the EntityManager or null. | ||
| 30 | + * | ||
| 31 | + * @return {@link EntityManager} or null | ||
| 32 | + */ | ||
| 33 | + protected final EntityManager getEntityManager() { | ||
| 34 | + return entityManager; | ||
| 35 | + } | ||
| 36 | + | ||
| 37 | + /** | ||
| 38 | + * Ensure the TestdataBuilder is constructed with a | ||
| 39 | + * {@link EntityManager} | ||
| 40 | + * | ||
| 41 | + * @throws IllegalStateException | ||
| 42 | + * if the TestdataBuilder is constructed without a | ||
| 43 | + * {@link EntityManager} | ||
| 44 | + */ | ||
| 45 | + protected final void ensureEntityManager() { | ||
| 46 | + if (entityManager == null) { | ||
| 47 | + throw new IllegalStateException("Cannot persist w/o entity manager"); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * {@inheritDoc} Executed within a new transaction. | ||
| 53 | + * | ||
| 54 | + * @throws IllegalStateException | ||
| 55 | + * if the TestdataBuilder is constructed without a | ||
| 56 | + * {@link EntityManager} | ||
| 57 | + */ | ||
| 58 | + public final T buildAndSave() { | ||
| 59 | + ensureEntityManager(); | ||
| 60 | + try { | ||
| 61 | + final T obj = build(); | ||
| 62 | + entityManager.persist(obj); | ||
| 63 | + return obj; | ||
| 64 | + } catch (Exception e) { | ||
| 65 | + throw new RuntimeException(e); | ||
| 66 | + } | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + /** | ||
| 70 | + * Returns an integer value from an static counter. | ||
| 71 | + * | ||
| 72 | + * @return value of the static counter. | ||
| 73 | + */ | ||
| 74 | + protected final int getId() { | ||
| 75 | + return count++; | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | +} | ||
| 79 | + |
src/main/java/net/ziemers/swxercise/lg/model/testdata/testdatabuilder/UserTestDataBuilder.java
0 → 100644
| 1 | +package net.ziemers.swxercise.lg.model.testdata.testdatabuilder; | ||
| 2 | + | ||
| 3 | +import net.ziemers.swxercise.lg.model.user.Address; | ||
| 4 | +import net.ziemers.swxercise.lg.model.user.Profile; | ||
| 5 | +import net.ziemers.swxercise.lg.model.user.User; | ||
| 6 | + | ||
| 7 | +public class UserTestDataBuilder extends AbstractTestDataBuilder<User> { | ||
| 8 | + | ||
| 9 | + private String firstname = "Hein"; | ||
| 10 | + | ||
| 11 | + private String lastname = "Bloed"; | ||
| 12 | + | ||
| 13 | + private Profile profile; | ||
| 14 | + | ||
| 15 | + private Address address = null; | ||
| 16 | + | ||
| 17 | + public UserTestDataBuilder(final EntityManger em) { | ||
| 18 | + super(em); | ||
| 19 | + profile = new ProfileTestDataBuilder(em).build(); | ||
| 20 | + } | ||
| 21 | + | ||
| 22 | + @Override | ||
| 23 | + public User build() { | ||
| 24 | + return new User(firstname, lastname) | ||
| 25 | + .withProfile(profile) | ||
| 26 | + .withAddress(address); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + public UserTestDataBuilder withFirstname(final String firstname) { | ||
| 30 | + this.firstname = firstname; | ||
| 31 | + return this; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public UserTestDataBuilder withLastname(final String lastname) { | ||
| 35 | + this.lastname = lastname; | ||
| 36 | + return this; | ||
| 37 | + } | ||
| 38 | + | ||
| 39 | + public UserTestDataBuidler withProfile(final Profile profile) { | ||
| 40 | + this.profile = profile; | ||
| 41 | + return this; | ||
| 42 | + } | ||
| 43 | + | ||
| 44 | + public UserTestDataBuilder withAddress(final Address address) { | ||
| 45 | + this.address = address; | ||
| 46 | + return this; | ||
| 47 | + } | ||
| 48 | + | ||
| 49 | +} | ||
| 50 | + |
src/main/java/net/ziemers/swxercise/lg/model/user/User.java
| @@ -71,4 +71,9 @@ public class User extends BaseEntity { | @@ -71,4 +71,9 @@ public class User extends BaseEntity { | ||
| 71 | this.address = address; | 71 | this.address = address; |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | + public User withAddress(final Address address) { | ||
| 75 | + setAddress(address); | ||
| 76 | + return this; | ||
| 77 | + } | ||
| 78 | + | ||
| 74 | } | 79 | } |