Commit b9b94a9fc86b2858f929f4d0e08994ad057600ba
test: TestDataBuilders in den master gemergt
Showing
5 changed files
with
184 additions
and
1 deletions
src/main/java/net/ziemers/swxercise/lg/model/user/Profile.java
| ... | ... | @@ -133,7 +133,7 @@ public class Profile extends BaseEntity { |
| 133 | 133 | return username; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | - public void setUsername(String username) { | |
| 136 | + private void setUsername(String username) { | |
| 137 | 137 | this.username = username; |
| 138 | 138 | } |
| 139 | 139 | |
| ... | ... | @@ -183,4 +183,9 @@ public class Profile extends BaseEntity { |
| 183 | 183 | this.mailaddress = mailaddress; |
| 184 | 184 | } |
| 185 | 185 | |
| 186 | + public Profile withMailaddress(final String mailaddress) { | |
| 187 | + setMailaddress(mailaddress); | |
| 188 | + return this; | |
| 189 | + } | |
| 190 | + | |
| 186 | 191 | } | ... | ... |
src/main/java/net/ziemers/swxercise/lg/model/user/User.java
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/AbstractTestDataBuilder.java
0 โ 100644
| 1 | +package net.ziemers.swxercise.lg.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/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/ProfileTestDataBuilder.java
0 โ 100644
| 1 | +package net.ziemers.swxercise.lg.testdata.testdatabuilder.user; | |
| 2 | + | |
| 3 | +import net.ziemers.swxercise.lg.model.user.Profile; | |
| 4 | +import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder; | |
| 5 | + | |
| 6 | +import javax.persistence.EntityManager; | |
| 7 | + | |
| 8 | +public class ProfileTestDataBuilder extends AbstractTestDataBuilder { | |
| 9 | + | |
| 10 | + private String username = "tziemer"; | |
| 11 | + | |
| 12 | + private String password = "secret"; | |
| 13 | + | |
| 14 | + private String mailaddress = "tziemer@ziemers.net"; | |
| 15 | + | |
| 16 | + public ProfileTestDataBuilder(final EntityManager em) { | |
| 17 | + super(em); | |
| 18 | + } | |
| 19 | + | |
| 20 | + @Override | |
| 21 | + public Profile build() { | |
| 22 | + return new Profile(username, password) | |
| 23 | + .withMailaddress(mailaddress); | |
| 24 | + } | |
| 25 | + | |
| 26 | + public ProfileTestDataBuilder withUsername(final String username) { | |
| 27 | + this.username = username; | |
| 28 | + return this; | |
| 29 | + } | |
| 30 | + | |
| 31 | + public ProfileTestDataBuilder withPassword(final String password) { | |
| 32 | + this.password = password; | |
| 33 | + return this; | |
| 34 | + } | |
| 35 | + | |
| 36 | + public ProfileTestDataBuilder withMailaddress(final String mailaddress) { | |
| 37 | + this.mailaddress = mailaddress; | |
| 38 | + return this; | |
| 39 | + } | |
| 40 | + | |
| 41 | +} | ... | ... |
src/test/java/net/ziemers/swxercise/lg/testdata/testdatabuilder/user/UserTestDataBuilder.java
0 โ 100644
| 1 | +package net.ziemers.swxercise.lg.testdata.testdatabuilder.user; | |
| 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 | +import net.ziemers.swxercise.lg.testdata.testdatabuilder.AbstractTestDataBuilder; | |
| 7 | + | |
| 8 | +import javax.persistence.EntityManager; | |
| 9 | + | |
| 10 | +public class UserTestDataBuilder extends AbstractTestDataBuilder<User> { | |
| 11 | + | |
| 12 | + private String firstname = "Hein"; | |
| 13 | + | |
| 14 | + private String lastname = "Bloed"; | |
| 15 | + | |
| 16 | + private Profile profile; | |
| 17 | + | |
| 18 | + private Address address = null; | |
| 19 | + | |
| 20 | + public UserTestDataBuilder(final EntityManager em) { | |
| 21 | + super(em); | |
| 22 | + profile = new ProfileTestDataBuilder(em).build(); | |
| 23 | + } | |
| 24 | + | |
| 25 | + @Override | |
| 26 | + public User build() { | |
| 27 | + return new User(firstname, lastname) | |
| 28 | + .withProfile(profile) | |
| 29 | + .withAddress(address); | |
| 30 | + } | |
| 31 | + | |
| 32 | + public UserTestDataBuilder withFirstname(final String firstname) { | |
| 33 | + this.firstname = firstname; | |
| 34 | + return this; | |
| 35 | + } | |
| 36 | + | |
| 37 | + public UserTestDataBuilder withLastname(final String lastname) { | |
| 38 | + this.lastname = lastname; | |
| 39 | + return this; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public UserTestDataBuilder withProfile(final Profile profile) { | |
| 43 | + this.profile = profile; | |
| 44 | + return this; | |
| 45 | + } | |
| 46 | + | |
| 47 | + public UserTestDataBuilder withAddress(final Address address) { | |
| 48 | + this.address = address; | |
| 49 | + return this; | |
| 50 | + } | |
| 51 | + | |
| 52 | +} | |
| 53 | + | ... | ... |