@Test public void testSimpleCourse() { final JPAEnvironment env = getEnvironment(); final EntityManager em = env.getEntityManager(); try { env.beginTransaction(em); final Course course = createAndPersistCourse(em); final Long courseId = Long.valueOf(course.getCourseId()); env.commitTransactionAndClear(em); final Course storedCourse = em.find(Course.class, courseId); verify(storedCourse != null, "didn't find course again"); verify(storedCourse.getAttendees() != null, "course lost attendees"); verify( storedCourse.getAttendees().size() == 2, "number of attendees in course (expected: 2, got: " + storedCourse.getAttendees().size() + ")."); for (final Employee attendee : storedCourse.getAttendees()) { verify( attendee.getId() == EMP_ID_DORIS.intValue() || attendee.getId() == EMP_ID_SABINE.intValue(), "Wrong attendee: " + attendee); } } finally { closeEntityManager(em); } }
@Test public void testPositivNonTx() { final EntityManager em = getEnvironment().getEntityManager(); try { Employee emp = em.find(Employee.class, new Integer(7)); verify(emp.getId() == 7, "wrong id"); verify(emp.getDepartment().getName().equals("eins"), "wrong department"); emp = em.find(Employee.class, new Integer(7)); verify(emp.getId() == 7, "wrong id"); Department dep = em.find(Department.class, new Integer(1)); verify(dep.getId() == 1, "wrong id"); } finally { closeEntityManager(em); } }
@Test public void testPositivTx() { final JPAEnvironment env = getEnvironment(); final EntityManager em = env.getEntityManager(); try { env.beginTransaction(em); Employee emp = em.find(Employee.class, new Integer(7)); verify(em.contains(emp), "Object not loaded"); verify(emp.getId() == 7, "wrong id"); verify(emp.getDepartment().getName().equals("eins"), "wrong department"); emp = em.find(Employee.class, new Integer(7)); verify(emp.getId() == 7, "wrong id"); Department dep = em.find(Department.class, new Integer(1)); verify(em.contains(dep), "Object not loaded"); verify(dep.getId() == 1, "wrong id"); env.rollbackTransactionAndClear(em); } finally { closeEntityManager(em); } }
@Override protected void setup() { resetData(); JPAEnvironment env = getEnvironment(); EntityManager em = env.getEntityManager(); try { env.beginTransaction(em); // fills the hobby-array for (int i = 0; i < HOBBY_NAMES.length; i++) { Hobby aHobby = new Hobby(HOBBY_NAMES[i]); HOBBIES[i] = aHobby; em.persist(aHobby); } // creates & persists some departments for (int i = 0; i < NUMBER_OF_DEPARTMENTS; i++) { Department tmpDep = new Department(i, "Department_" + i); // ALL_DEPARTMENTS.put(tmpDep.getName(), tmpDep); em.persist(tmpDep); } // creates & persists some motorvehicles for (int i = 0; i < NUMBER_OF_MOTORVEHICLES; i++) { MotorVehicle tmpMVehicle = new MotorVehicle(); tmpMVehicle.setId(Short.valueOf(++this.vehiclePK)); tmpMVehicle.setBrand("foo-car"); MOTORVEHICLES[i] = tmpMVehicle; em.persist(tmpMVehicle); } // creates & persists some bicycles for (int i = 0; i < NUMBER_OF_BICYCLES; i++) { Bicycle tmpBike = new Bicycle(); tmpBike.setId(Short.valueOf(++this.vehiclePK)); tmpBike.setBrand("foo-brand"); tmpBike.setRiders(new ArrayList<Employee>()); BICYCLES[i] = tmpBike; em.persist(tmpBike); } // creates & persists some empoyees for (int i = 0; i < NUMBER_OF_EMPLOYEES; i++) { /* * sets a department tries to set 10 employees to each department example: employees 1 to 10 are related to * department 1. employees 11 to 20 are mapped to department 2 and so on */ Employee tmpEmp = new Employee( i, this.getRandomGivenName(), this.getRandomSurname(), ALL_DEPARTMENTS.get("Department_" + Math.ceil(i / 100) * 10)); // add two hobbies tmpEmp.addHobby(this.getRandomHobby(tmpEmp.getId())); tmpEmp.addHobby(this.getRandomHobby(tmpEmp.getId())); tmpEmp.addHobby(this.getRandomHobby(tmpEmp.getId())); tmpEmp.addHobby(this.getRandomHobby(tmpEmp.getId())); tmpEmp.addHobby(this.getRandomHobby(tmpEmp.getId())); // every second employee gets a motorvehicle if (i % 2 == 0) { getRandomMotorVehicle(tmpEmp.getId()).setDriver(tmpEmp); } // the others get a bicycle else { getRandomBike(tmpEmp.getId()).getRiders().add(tmpEmp); } // set the period tmpEmp.setEmploymentPeriod(new EmploymentPeriod()); tmpEmp .getEmploymentPeriod() .setStartDate(new Date(System.currentTimeMillis() - 1000000000)); tmpEmp.getEmploymentPeriod().setEndDate(new Date(System.currentTimeMillis() + 1000000000)); em.persist(tmpEmp); } env.commitTransactionAndClear(em); // em.clear(); } finally { closeEntityManager(em); } }