diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA-context.xml b/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA-context.xml index 7719d0cbaa..5111e9c152 100644 --- a/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA-context.xml +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA-context.xml @@ -25,5 +25,9 @@ <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> + <bean id="projectScheduleDao" class="se.su.dsv.scipro.data.dao.jpa.ProjectScheduleDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + </beans> diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA.java b/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA.java index 0ed09f8cc4..104cf5edbb 100644 --- a/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA.java +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestEventDaoJPA.java @@ -2,6 +2,7 @@ package se.su.dsv.scipro.dao.jpa; import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.junit.Assert; @@ -15,7 +16,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; import se.su.dsv.scipro.data.dao.interfaces.EventDao; +import se.su.dsv.scipro.data.dao.interfaces.ProjectScheduleDao; import se.su.dsv.scipro.data.dataobjects.Event; +import se.su.dsv.scipro.data.dataobjects.ProjectSchedule; /** * @author Richard Wilkinson - richard.wilkinson@jweekend.com @@ -27,16 +30,36 @@ public class TestEventDaoJPA { @Autowired private EventDao eventDao; + + @Autowired + private ProjectScheduleDao projectScheduleDao; protected Event event; + + protected Event deletedEvent; + + protected ProjectSchedule schedule; @Before public void startTransaction() { + schedule = new ProjectSchedule(); + schedule = projectScheduleDao.save(schedule); + event = new Event(); - event.setLocation("new Location"); - event.setTitle("new Title"); + event.setTitle("Event Title"); + event.setDescription("Event Description"); + event.setDueDate(new Date(System.currentTimeMillis() + 100000)); + event.setProjectSchedule(schedule); event = eventDao.save(event); + + deletedEvent = new Event(); + deletedEvent.setTitle("Deleted Event title"); + deletedEvent.setDescription("Deleted Event description"); + deletedEvent.setDueDate(new Date(System.currentTimeMillis() + 100000)); + deletedEvent.setDeleted(true); + deletedEvent.setProjectSchedule(schedule); + deletedEvent = eventDao.save(deletedEvent); } @@ -70,9 +93,17 @@ public class TestEventDaoJPA { eventDao.delete(event); Assert.assertEquals(0, eventDao.countAll()); } + + @Test + @Transactional + @Rollback + public void testLazyDelete() { + event = eventDao.lazyDelete(event); + Assert.assertEquals(0, eventDao.countAll()); + } /** - * Test method for {@link se.su.dsv.scipro.data.dao.jpa.AbstractDaoJPAImpl#load(java.io.Serializable)}. + * Test method for {@link se.su.dsv.scipro.data.dao.jpa.AbstractDaoJPAImp#load(java.io.Serializable)}. */ @Test @Transactional @Rollback @@ -82,12 +113,28 @@ public class TestEventDaoJPA { } /** - * Test method for {@link se.su.dsv.scipro.data.dao.jpa.AbstractDaoJPAImpl#save(se.su.dsv.scipro.data.dataobjects.DomainObject)}. + * Test method for {@link se.su.dsv.scipro.data.dao.jpa.AbstractDaoJPAImp#save(se.su.dsv.scipro.data.dataobjects.DomainObject)}. */ @Test @Transactional @Rollback public void testSave() { //if we have got this far then save works } + + @Test + @Transactional + @Rollback + public void testCountAllLazyDeleted() { + Assert.assertEquals(1, eventDao.countAllLazyDeleted()); + } + + @Test + @Transactional + @Rollback + public void testFindAllLazyDeteted() { + List<Event> events = new ArrayList<Event>(); + events.add(deletedEvent); + Assert.assertEquals(events, eventDao.findAllLazyDeleted()); + } } diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA-context.xml b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA-context.xml new file mode 100644 index 0000000000..a2c0acc3b8 --- /dev/null +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA-context.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" + default-autowire="byName"> + + <bean id="entityManagerFactory" + class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> + <property name="persistenceUnitName" value="testPersistenceUnit" /> + </bean> + + <!-- + enable the configuration of transactional behavior based on + annotations + --> + <tx:annotation-driven transaction-manager="transactionManager" /> + + <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="projectDao" class="se.su.dsv.scipro.data.dao.jpa.ProjectDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="roleDao" class="se.su.dsv.scipro.data.dao.jpa.RoleDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="userDao" class="se.su.dsv.scipro.data.dao.jpa.UserDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="projectFollowerDao" class="se.su.dsv.scipro.data.dao.jpa.ProjectFollowerDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> +</beans> \ No newline at end of file diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA.java b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA.java new file mode 100644 index 0000000000..78a1f1a176 --- /dev/null +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectDaoJPA.java @@ -0,0 +1,349 @@ +package se.su.dsv.scipro.dao.jpa; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import se.su.dsv.scipro.data.dao.interfaces.ProjectDao; +import se.su.dsv.scipro.data.dao.interfaces.ProjectFollowerDao; +import se.su.dsv.scipro.data.dao.interfaces.RoleDao; +import se.su.dsv.scipro.data.dao.interfaces.UserDao; +import se.su.dsv.scipro.data.dataobjects.Employee; +import se.su.dsv.scipro.data.dataobjects.Project; +import se.su.dsv.scipro.data.dataobjects.ProjectFollower; +import se.su.dsv.scipro.data.dataobjects.Student; +import se.su.dsv.scipro.data.dataobjects.User; +import se.su.dsv.scipro.data.enums.ProjectTeamMemberRoles; +import se.su.dsv.scipro.data.enums.ProjectStatus; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class TestProjectDaoJPA { + + @Autowired + private ProjectDao projectDao; + + @Autowired + private RoleDao roleDao; + + @Autowired + private UserDao userDao; + + @Autowired + private ProjectFollowerDao projectFollowerDao; + + private User userWithActiveAndInactive; + private User userWithActiveAndCompleted; + private User userWithCompleted; + private User userWithInactive; + + private User userWithHeadSupervisor; + private User userNotHeadSupervisor; + + private User userWithReviewer; + private User userWithCoSupervisor; + private User userWithCoSupervisorAndReviewer; + + private Project activeProject; + private Project inactiveProject; + private Project completedProject; + + @Before + public void startTransaction(){ + + userWithHeadSupervisor = new User(); + userWithHeadSupervisor.setFirstName("userWithHeadSupervisor firstname"); + userWithHeadSupervisor.setLastName("userWithHeadSupervisor lastname"); + userWithHeadSupervisor = userDao.save(userWithHeadSupervisor); + + Employee employeeWithHeadSupervisor = new Employee(); + employeeWithHeadSupervisor.setUser(userWithHeadSupervisor); + employeeWithHeadSupervisor = (Employee) roleDao.save(employeeWithHeadSupervisor); + + userNotHeadSupervisor = new User(); + userNotHeadSupervisor.setFirstName("userNotSupervisor firstname"); + userNotHeadSupervisor.setLastName("userNotSupervisor lastname"); + userNotHeadSupervisor = userDao.save(userNotHeadSupervisor); + + Employee employeeNotHeadSupervisor = new Employee(); + employeeNotHeadSupervisor.setUser(userNotHeadSupervisor); + employeeNotHeadSupervisor = (Employee) roleDao.save(employeeNotHeadSupervisor); + + userWithReviewer = new User(); + userWithReviewer.setFirstName("userWithReviewer firstname"); + userWithReviewer.setLastName("userWithReviewer lastname"); + userWithReviewer = userDao.save(userWithReviewer); + + Employee employeeWithReviewer = new Employee(); + employeeWithReviewer.setUser(userWithReviewer); + employeeWithReviewer = (Employee) roleDao.save(employeeWithReviewer); + + userWithCoSupervisor = new User(); + userWithCoSupervisor.setFirstName("userWithCoSupervisor firstname"); + userWithCoSupervisor.setLastName("userWithCoSupervisor lastname"); + userWithCoSupervisor = userDao.save(userWithCoSupervisor); + + Employee employeeWithCoSupervisor = new Employee(); + employeeWithCoSupervisor.setUser(userWithCoSupervisor); + employeeWithCoSupervisor = (Employee) roleDao.save(employeeWithCoSupervisor); + + userWithCoSupervisorAndReviewer = new User(); + userWithCoSupervisorAndReviewer.setFirstName("userWithCoSupervisorAndReviewer firstname"); + userWithCoSupervisorAndReviewer.setLastName("userWithCoSupervisorAndReviewer lastname"); + userWithCoSupervisorAndReviewer = userDao.save(userWithCoSupervisorAndReviewer); + + Employee employeeWithCoSupervisorAndReviewer = new Employee(); + employeeWithCoSupervisorAndReviewer.setUser(userWithCoSupervisorAndReviewer); + employeeWithCoSupervisorAndReviewer = (Employee) roleDao.save(employeeWithCoSupervisorAndReviewer); + + userWithActiveAndInactive = new User(); + userWithActiveAndInactive.setFirstName("userWithActiveAndInactive firstname"); + userWithActiveAndInactive.setLastName("userWithActiveAndInactive lastname"); + userWithActiveAndInactive = userDao.save(userWithActiveAndInactive); + + Student studentWithActiveAndInactive = new Student(); + studentWithActiveAndInactive.setUser(userWithActiveAndInactive); + studentWithActiveAndInactive = (Student) roleDao.save(studentWithActiveAndInactive); + + userWithActiveAndCompleted = new User(); + userWithActiveAndCompleted.setFirstName("userWithActiveAndCompleted firstname"); + userWithActiveAndCompleted.setLastName("userWithActiveAndCompleted lastname"); + userWithActiveAndCompleted = userDao.save(userWithActiveAndCompleted); + + Student studentWithActiveAndCompleted = new Student(); + studentWithActiveAndCompleted.setUser(userWithActiveAndCompleted); + studentWithActiveAndCompleted = (Student) roleDao.save(studentWithActiveAndCompleted); + + userWithCompleted = new User(); + userWithCompleted.setFirstName("userWithCompleted firstname"); + userWithCompleted.setLastName("userWithCompleted lastname"); + userWithCompleted = userDao.save(userWithCompleted); + + Student studentWithCompleted = new Student(); + studentWithCompleted.setUser(userWithCompleted); + studentWithCompleted = (Student) roleDao.save(studentWithCompleted); + + userWithInactive = new User(); + userWithInactive.setFirstName("userWithInactive firstname"); + userWithInactive.setLastName("userWithInactive lastname"); + userWithInactive = userDao.save(userWithInactive); + + Student studentWithInactive = new Student(); + studentWithInactive.setUser(userWithInactive); + studentWithInactive = (Student) roleDao.save(studentWithInactive); + + activeProject = new Project(); + activeProject.setTitle("Active Project 1"); + activeProject.addProjectParticipant(studentWithActiveAndInactive); + activeProject.addProjectParticipant(studentWithActiveAndCompleted); + activeProject.setHeadSupervisor(employeeWithHeadSupervisor); + activeProject = projectDao.save(activeProject); + + ProjectFollower reviewerFollower = new ProjectFollower(); + reviewerFollower.setFollower(employeeWithReviewer); + reviewerFollower.setProject(activeProject); + reviewerFollower.setProjectRole(ProjectTeamMemberRoles.REVIEWER); + reviewerFollower = projectFollowerDao.save(reviewerFollower); + + ProjectFollower coSupervisorFollower = new ProjectFollower(); + coSupervisorFollower.setFollower(employeeWithCoSupervisor); + coSupervisorFollower.setProject(activeProject); + coSupervisorFollower.setProjectRole(ProjectTeamMemberRoles.CO_SUPERVISOR); + coSupervisorFollower = projectFollowerDao.save(coSupervisorFollower); + + ProjectFollower coSupervisorFollower2 = new ProjectFollower(); + coSupervisorFollower2.setFollower(employeeWithCoSupervisorAndReviewer); + coSupervisorFollower2.setProject(activeProject); + coSupervisorFollower2.setProjectRole(ProjectTeamMemberRoles.CO_SUPERVISOR); + coSupervisorFollower2 = projectFollowerDao.save(coSupervisorFollower2); + + inactiveProject = new Project(); + inactiveProject.setTitle("Inactive project"); + inactiveProject.addProjectParticipant(studentWithActiveAndInactive); + inactiveProject.addProjectParticipant(studentWithInactive); + inactiveProject.setProjectStatus(ProjectStatus.INACTIVE); + inactiveProject.setHeadSupervisor(employeeWithHeadSupervisor); + inactiveProject = projectDao.save(inactiveProject); + + ProjectFollower reviewerFollower2 = new ProjectFollower(); + reviewerFollower2.setFollower(employeeWithCoSupervisorAndReviewer); + reviewerFollower2.setProject(inactiveProject); + reviewerFollower2.setProjectRole(ProjectTeamMemberRoles.REVIEWER); + reviewerFollower2 = projectFollowerDao.save(reviewerFollower2); + + completedProject = new Project(); + completedProject.setTitle("Completed project"); + completedProject.addProjectParticipant(studentWithActiveAndCompleted); + completedProject.addProjectParticipant(studentWithCompleted); + completedProject.setProjectStatus(ProjectStatus.COMPLETED); + completedProject.setHeadSupervisor(employeeWithHeadSupervisor); + completedProject = projectDao.save(completedProject); + } + + @Test + @Transactional + @Rollback + public void testFindAll(){ + List<Project> list = new ArrayList<Project>(); + list.add(activeProject); + list.add(inactiveProject); + list.add(completedProject); + Assert.assertEquals(list, projectDao.findAll()); + } + + @Test + @Transactional + @Rollback + public void testCountAll() { + Assert.assertEquals(3, projectDao.countAll()); + } + + @Test + @Transactional + @Rollback + public void testLoad() { + Project p = projectDao.load(activeProject.getId()); + Assert.assertEquals(p, activeProject); + } + + @Test + @Transactional + @Rollback + public void testGetProjectsByParticipant(){ + List<Project> list = new ArrayList<Project>(); + list.add(activeProject); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndInactive, null)); + + list = new ArrayList<Project>(); + list.add(activeProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.ACTIVE)); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndCompleted, ProjectStatus.ACTIVE)); + + list = new ArrayList<Project>(); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.INACTIVE)); + + list = new ArrayList<Project>(); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.COMPLETED)); + + list = new ArrayList<Project>(); + list.add(activeProject); + list.add(completedProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndCompleted, null)); + + list = new ArrayList<Project>(); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithActiveAndCompleted, ProjectStatus.INACTIVE)); + + list = new ArrayList<Project>(); + list.add(completedProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithCompleted, null)); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithCompleted, ProjectStatus.COMPLETED)); + + list = new ArrayList<Project>(); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithInactive, null)); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithInactive, ProjectStatus.INACTIVE)); + + list = new ArrayList<Project>(); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithInactive, ProjectStatus.ACTIVE)); + Assert.assertEquals(list, projectDao.getProjectsByParticipant(userWithCompleted, ProjectStatus.ACTIVE)); + } + + @Test + @Transactional + @Rollback + public void testCountProjectsByParticipant(){ + Assert.assertEquals(2, projectDao.countProjectsByParticipant(userWithActiveAndCompleted, null)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithActiveAndCompleted, ProjectStatus.ACTIVE)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithActiveAndCompleted, ProjectStatus.COMPLETED)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithActiveAndCompleted, ProjectStatus.INACTIVE)); + + Assert.assertEquals(2, projectDao.countProjectsByParticipant(userWithActiveAndInactive, null)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.ACTIVE)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.COMPLETED)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithActiveAndInactive, ProjectStatus.INACTIVE)); + + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithCompleted, null)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithCompleted, ProjectStatus.ACTIVE)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithCompleted, ProjectStatus.COMPLETED)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithCompleted, ProjectStatus.INACTIVE)); + + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithInactive, null)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithInactive, ProjectStatus.ACTIVE)); + Assert.assertEquals(0, projectDao.countProjectsByParticipant(userWithInactive, ProjectStatus.COMPLETED)); + Assert.assertEquals(1, projectDao.countProjectsByParticipant(userWithInactive, ProjectStatus.INACTIVE)); + } + + @Test + @Transactional + @Rollback + public void testGetProjectsByHeadSupervisor(){ + List<Project> list = new ArrayList<Project>(); + list.add(activeProject); + list.add(completedProject); + list.add(inactiveProject); + + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userWithHeadSupervisor, null)); + + list = new ArrayList<Project>(); + list.add(activeProject); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userWithHeadSupervisor, ProjectStatus.ACTIVE)); + + list = new ArrayList<Project>(); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userWithHeadSupervisor, ProjectStatus.INACTIVE)); + + list = new ArrayList<Project>(); + list.add(completedProject); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userWithHeadSupervisor, ProjectStatus.COMPLETED)); + + list = new ArrayList<Project>(); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userNotHeadSupervisor, null)); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userNotHeadSupervisor, ProjectStatus.COMPLETED)); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userNotHeadSupervisor, ProjectStatus.INACTIVE)); + Assert.assertEquals(list, projectDao.getProjectsByHeadSupervisor(userNotHeadSupervisor, ProjectStatus.ACTIVE)); + } + + @Test + @Transactional + @Rollback + public void testGetProjectsByProjectTeamMember(){ + ArrayList<Project> list = new ArrayList<Project>(); + list.add(activeProject); + + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisor, null, null)); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithReviewer, null, null)); + + list = new ArrayList<Project>(); + list.add(activeProject); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisorAndReviewer, null, null)); + + list = new ArrayList<Project>(); + list.add(activeProject); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisorAndReviewer, ProjectStatus.ACTIVE, null)); + + list = new ArrayList<Project>(); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisorAndReviewer, ProjectStatus.COMPLETED, null)); + + list = new ArrayList<Project>(); + list.add(activeProject); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisorAndReviewer, null, ProjectTeamMemberRoles.CO_SUPERVISOR)); + + list = new ArrayList<Project>(); + list.add(inactiveProject); + Assert.assertEquals(list, projectDao.getProjectsByProjectTeamMember(userWithCoSupervisorAndReviewer, null, ProjectTeamMemberRoles.REVIEWER)); + + } +} diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA-context.xml b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA-context.xml new file mode 100644 index 0000000000..fcde6f07fc --- /dev/null +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA-context.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="UTF-8"?> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd + http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" + default-autowire="byName"> + + <bean id="entityManagerFactory" + class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> + <property name="persistenceUnitName" value="testPersistenceUnit" /> + </bean> + + <!-- + enable the configuration of transactional behavior based on + annotations + --> + <tx:annotation-driven transaction-manager="transactionManager" /> + + <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + + <bean id="eventDao" class="se.su.dsv.scipro.data.dao.jpa.EventDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="projectScheduleDao" class="se.su.dsv.scipro.data.dao.jpa.ProjectScheduleDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> + + <bean id="projectDao" class="se.su.dsv.scipro.data.dao.jpa.ProjectDaoJPAImp"> + <property name="entityManagerFactory" ref="entityManagerFactory" /> + </bean> +</beans> diff --git a/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA.java b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA.java new file mode 100644 index 0000000000..c15735bf53 --- /dev/null +++ b/src/test/java/se/su/dsv/scipro/dao/jpa/TestProjectScheduleDaoJPA.java @@ -0,0 +1,204 @@ +package se.su.dsv.scipro.dao.jpa; + +import java.util.ArrayList; +import java.util.Calendar; +import java.util.GregorianCalendar; +import java.util.List; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.annotation.Rollback; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.transaction.annotation.Transactional; + +import se.su.dsv.scipro.data.dao.interfaces.EventDao; +import se.su.dsv.scipro.data.dao.interfaces.ProjectDao; +import se.su.dsv.scipro.data.dao.interfaces.ProjectScheduleDao; +import se.su.dsv.scipro.data.dataobjects.Event; +import se.su.dsv.scipro.data.dataobjects.Project; +import se.su.dsv.scipro.data.dataobjects.ProjectSchedule; + +/** + * @author Dan Kjellman <dan-kjel@dsv.su.se> + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration +public class TestProjectScheduleDaoJPA { + + @Autowired + private EventDao eventDao; + + @Autowired + private ProjectScheduleDao projectScheduleDao; + + @Autowired + private ProjectDao projectDao; + + protected ProjectSchedule ps; + + protected Project project; + + protected Event oldDueDateEvent; + protected Event oldDoneDueDateEvent; + protected Event futureDueDateEvent; + protected Event oldDueDateDeletedEvent; + protected Event futureDueDateDeletedEvent; + + @Before + public void startTransaction() { + project = new Project(); + + project = projectDao.save(project); + + ps = new ProjectSchedule(); + ps.setProject(project); + ps = projectScheduleDao.save(ps); + + GregorianCalendar calBeforeToday = new GregorianCalendar(); + calBeforeToday.add(Calendar.MONTH, -1); + + GregorianCalendar calAfterToday = new GregorianCalendar(); + calAfterToday.add(Calendar.MONTH, 1); + + oldDueDateEvent = new Event(); + oldDueDateEvent.setTitle("oldDueDateEvent title"); + oldDueDateEvent.setDescription("oldDueDateEvent description"); + oldDueDateEvent.setDueDate(calBeforeToday.getTime()); + oldDueDateEvent.setProjectSchedule(ps); + oldDueDateEvent = eventDao.save(oldDueDateEvent); + + oldDoneDueDateEvent = new Event(); + oldDoneDueDateEvent.setTitle("oldDoneDueDateEvent title"); + oldDoneDueDateEvent.setDescription("oldDoneDueDateEvent description"); + oldDoneDueDateEvent.setDone(true); + oldDoneDueDateEvent.setDueDate(calBeforeToday.getTime()); + oldDoneDueDateEvent.setProjectSchedule(ps); + oldDoneDueDateEvent = eventDao.save(oldDoneDueDateEvent); + + futureDueDateEvent = new Event(); + futureDueDateEvent.setTitle("futureDueDateEvent title"); + futureDueDateEvent.setDescription("futureDueDateEvent description"); + futureDueDateEvent.setDueDate(calAfterToday.getTime()); + futureDueDateEvent.setProjectSchedule(ps); + futureDueDateEvent = eventDao.save(futureDueDateEvent); + + oldDueDateDeletedEvent = new Event(); + oldDueDateDeletedEvent.setTitle("oldDueDateDeletedEvent title"); + oldDueDateDeletedEvent.setDescription("oldDueDateDeletedEvent description"); + oldDueDateDeletedEvent.setDeleted(true); + oldDueDateDeletedEvent.setDueDate(calBeforeToday.getTime()); + oldDueDateDeletedEvent.setProjectSchedule(ps); + oldDueDateDeletedEvent = eventDao.save(oldDueDateDeletedEvent); + + futureDueDateDeletedEvent = new Event(); + futureDueDateDeletedEvent.setTitle("futureDueDateDeletedEvent title"); + futureDueDateDeletedEvent.setDescription("futureDueDateDeletedEvent description"); + futureDueDateDeletedEvent.setDueDate(calAfterToday.getTime()); + futureDueDateDeletedEvent.setDeleted(true); + futureDueDateDeletedEvent.setProjectSchedule(ps); + futureDueDateDeletedEvent = eventDao.save(futureDueDateDeletedEvent); + } + + @Test + @Transactional + @Rollback + public void testFindAll(){ + List<ProjectSchedule> list = new ArrayList<ProjectSchedule>(); + list.add(ps); + Assert.assertEquals(list, projectScheduleDao.findAll()); + } + + @Test + @Transactional + @Rollback + public void testCountAll() { + Assert.assertEquals(1, projectScheduleDao.countAll()); + } + + @Test + @Transactional + @Rollback + public void testLoad() { + ProjectSchedule ps2 = projectScheduleDao.load(ps.getId()); + Assert.assertEquals(ps, ps2); + } + + @Test + @Transactional + @Rollback + public void testGetAllEventsByProjectSchedule(){ + List<Event> notDeleted = new ArrayList<Event>(); + notDeleted.add(oldDoneDueDateEvent); + notDeleted.add(oldDueDateEvent); + notDeleted.add(futureDueDateEvent); + + List<Event> fromQuery = projectScheduleDao.getAllEventsByProjectSchedule(ps); + + Assert.assertEquals(notDeleted, fromQuery); + } + + @Test + @Transactional + @Rollback + public void testGetUpcomingEventsByProjectSchedule(){ + List<Event> notDeleted = new ArrayList<Event>(); + notDeleted.add(futureDueDateEvent); + + List<Event> fromQuery = projectScheduleDao.getUpcomingEventsByProjectSchedule(ps, false); + + Assert.assertEquals(notDeleted, fromQuery); + } + + @Test + @Transactional + @Rollback + public void testGetUpcomingAndNotDoneEventsByProjectSchedule(){ + System.out.println("ALSNGALSNG"); + List<Event> notDeleted = new ArrayList<Event>(); + notDeleted.add(oldDueDateEvent); + notDeleted.add(futureDueDateEvent); + + List<Event> fromQuery = projectScheduleDao.getUpcomingEventsByProjectSchedule(ps, true); + + Assert.assertEquals(notDeleted, fromQuery); + } + + @Test + @Transactional + @Rollback + public void testGetOldEventsByProjectSchedule(){ + List<Event> notDeleted = new ArrayList<Event>(); + notDeleted.add(oldDueDateEvent); + notDeleted.add(oldDoneDueDateEvent); + + List<Event> fromQuery = projectScheduleDao.getOldEventsByProjectSchedule(ps, null); + + Assert.assertEquals(notDeleted, fromQuery); + } + + @Test + @Transactional + @Rollback + public void testGetNotDoneOldEventsByProjectSchedule(){ + List<Event> notDeleted = new ArrayList<Event>(); + notDeleted.add(oldDueDateEvent); + + List<Event> fromQuery = projectScheduleDao.getOldEventsByProjectSchedule(ps, false); + + Assert.assertEquals(notDeleted, fromQuery); + } + + @Test + @Transactional + @Rollback + public void testCountOverDueEvent(){ + Assert.assertEquals(1, projectScheduleDao.countOverDueEvents(ps)); + } + +}