Permalink
Browse files

cleaning up and using own Builder for each Entity for the tests

  • Loading branch information...
dbremer-tonn committed Jun 3, 2011
1 parent fba4dae commit aa128fb46b4e2e1043ca618059fc3a8d71733844
@@ -1,11 +1,11 @@
package de.lbb.mentoring.example.model;
-import javax.persistence.EntityManager;
-
-import de.lbb.mentoring.example.model.testdatabuilder.TestdataBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
import org.junit.Assert;
import org.junit.Test;
+import javax.persistence.EntityManager;
+
public class EqualityTest extends AbstractEntityTest
{
@@ -15,7 +15,7 @@
@Test
public void testEmployeeIdentity()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
@@ -40,9 +40,9 @@ public void testEmployeeIdentity()
* Showing equality problem when loading same entity from different em
*/
@Test
- public void testEmployeeAttachDetechment()
+ public void testEmployeeAttachDetachment()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
@@ -1,6 +1,7 @@
package de.lbb.mentoring.example.model;
-import de.lbb.mentoring.example.model.testdatabuilder.TestdataBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.DepartmentBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.PartTimeEmployeeBuilder;
import org.junit.Assert;
import org.junit.Test;
@@ -17,14 +18,14 @@
@Test
public void testHibernateProxy()
{
- Employee employee = TestdataBuilder.createParttimeEmployee("Hans", "Mueller");
+ Employee employee = new PartTimeEmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
// Creating Department
- Department department = TestdataBuilder.createDepartment("Abteilung 1");
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
employee.setDepartment(department);
em.getTransaction().begin();
em.persist(employee);
@@ -45,7 +46,7 @@ public void testHibernateProxy()
@Test
public void testHibernateProxy2()
{
- Employee employee = TestdataBuilder.createParttimeEmployee("Hans", "Mueller");
+ Employee employee = new PartTimeEmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
em.persist(employee);
@@ -1,6 +1,7 @@
package de.lbb.mentoring.example.model;
-import de.lbb.mentoring.example.model.testdatabuilder.TestdataBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.PartTimeEmployeeBuilder;
import org.junit.Assert;
import org.junit.Test;
@@ -15,14 +16,14 @@
@Test
public void testEmployeeInheritance()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
- Employee parttimeEmployee = TestdataBuilder.createParttimeEmployee("Peter", "Schneider");
+ Employee parttimeEmployee = new PartTimeEmployeeBuilder().withAge(25).withName("Peter").withSurename("Schneider").build();
// saving Employee
em.getTransaction().begin();
@@ -17,8 +17,6 @@
@Test
public void testEmployeeQueryValueObjects()
{
-// Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
-
// Using a builder for Entities to test
Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
@@ -1,6 +1,7 @@
package de.lbb.mentoring.example.model;
-import de.lbb.mentoring.example.model.testdatabuilder.TestdataBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.DepartmentBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
import org.junit.Assert;
import org.junit.Test;
@@ -17,14 +18,14 @@
@Test
public void testEmployeeDepartment()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
em.persist(employee);
em.getTransaction().commit();
// Creating Department
- Department department = TestdataBuilder.createDepartment("Abteilung 1");
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
employee.setDepartment(department);
em.getTransaction().begin();
em.persist(employee);
@@ -44,13 +45,13 @@ public void testEmployeeDepartment()
public void testDepartmentEmployees()
{
// Creating Department
- Department department = TestdataBuilder.createDepartment("Abteilung 1");
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
// saving Department
em.getTransaction().begin();
em.persist(department);
em.getTransaction().commit();
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
List<Employee> empls = new ArrayList<Employee>();
empls.add(employee);
@@ -1,10 +1,13 @@
package de.lbb.mentoring.example.model;
-import de.lbb.mentoring.example.model.testdatabuilder.TestdataBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.DepartmentBuilder;
+import de.lbb.mentoring.example.model.testdatabuilder.EmployeeBuilder;
import org.junit.Assert;
import org.junit.Test;
-import javax.persistence.*;
+import javax.persistence.EntityManager;
+import javax.persistence.FlushModeType;
+import javax.persistence.RollbackException;
public class TransactionTest extends AbstractEntityTest
{
@@ -15,8 +18,8 @@
@Test
public void testIsolationLevel()
{
- Employee employee = TestdataBuilder.createEmployee("Max", "Mustername");
- Department department = TestdataBuilder.createDepartment("Abteilung 1");
+Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
+ Department department = new DepartmentBuilder().withName("Abteilung 1").build();
em.getTransaction().begin();
// From the spec:
@@ -47,7 +50,7 @@ public void testIsolationLevel()
@Test(expected = RollbackException.class)
public void testOptimisticLocking()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
@@ -83,7 +86,7 @@ public void testOptimisticLocking()
@Test
public void testPessimisticLocking()
{
- Employee employee = TestdataBuilder.createEmployee("Hans", "Mueller");
+ Employee employee = new EmployeeBuilder().withAge(30).withName("Hans").withSurename("Mueller").build();
// saving Employee
em.getTransaction().begin();
@@ -0,0 +1,30 @@
+package de.lbb.mentoring.example.model.testdatabuilder;
+
+import de.lbb.mentoring.example.model.Department;
+
+
+public class DepartmentBuilder
+{
+ protected String name = null;
+ protected String surename = null;
+ protected int age = 0;
+
+ public DepartmentBuilder()
+ {
+ }
+
+ public DepartmentBuilder withName(String name)
+ {
+ this.name = name;
+ return this;
+ }
+
+ public Department build()
+ {
+ Department department = new Department();
+ if (this.name != null)
+ department.setName(this.name);
+
+ return department;
+ }
+}
@@ -5,9 +5,9 @@
public class EmployeeBuilder
{
- private String name = null;
- private String surename = null;
- private int age = 0;
+ protected String name = null;
+ protected String surename = null;
+ protected int age = 0;
public EmployeeBuilder()
{
@@ -0,0 +1,41 @@
+package de.lbb.mentoring.example.model.testdatabuilder;
+
+import de.lbb.mentoring.example.model.ParttimeEmployee;
+
+import java.util.Date;
+
+
+public class PartTimeEmployeeBuilder extends EmployeeBuilder
+{
+ protected Date fromDate = null;
+ protected Date toDate = null;
+
+
+ public PartTimeEmployeeBuilder()
+ {
+ }
+
+ public PartTimeEmployeeBuilder withFromDate(Date fromDate)
+ {
+ this.fromDate = fromDate;
+ return this;
+ }
+
+ public PartTimeEmployeeBuilder withToDate(Date toDate)
+ {
+ this.toDate = toDate;
+ return this;
+ }
+
+ public ParttimeEmployee build()
+ {
+ ParttimeEmployee employee = new ParttimeEmployee();
+ employee.setAge(this.age);
+ if (this.name != null)
+ employee.setName(this.name);
+ if (this.surename != null)
+ employee.setSurename(this.surename);
+
+ return employee;
+ }
+}
@@ -1,37 +0,0 @@
-package de.lbb.mentoring.example.model.testdatabuilder;
-
-import de.lbb.mentoring.example.model.Department;
-import de.lbb.mentoring.example.model.Employee;
-import de.lbb.mentoring.example.model.ParttimeEmployee;
-
-/**
- * simple Databuilder which does not scale
- * Better use specific builder for entities to test @see EmployeeBuilder
- */
-public class TestdataBuilder
-{
- public static Employee createEmployee(String name, String surename) {
- Employee employee = new Employee();
- employee.setName(name);
- employee.setSurename(surename);
-
- return employee;
- }
-
- public static Employee createParttimeEmployee(String name, String surename) {
- Employee employee = new ParttimeEmployee();
- employee.setName(name);
- employee.setSurename(surename);
-
- return employee;
- }
-
- public static Department createDepartment(String name) {
- Department department = new Department();
- department.setName(name);
-
- return department;
- }
-
-
-}

0 comments on commit aa128fb

Please sign in to comment.