From b0b0d662dce9de30c12299f5a5d2a610502f6889 Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Tue, 28 May 2013 19:14:55 +0200 Subject: [PATCH 1/6] Fix failing test when running mvn install Some test cases in DerivedAllocationGeneratorTest were failing during a mvn install. It might not happen in all systems since it depends on the order the tests are executed. It's also failing if run individually. The problem is that ScenarioBootstrap must be executed before executing these tests. When distributing the resources, the DayAssignments of the resource for the current scenario are queried. An IScenarioManager must have been registered in Registry plus the main scenario initialized. The stack trace is: java.lang.IllegalStateException: loadRequiredData should have been called on org.libreplan.business.scenar ios.bootstrap.ScenariosBootstrap at org.libreplan.business.scenarios.bootstrap.ScenariosBootstrap.getMain(ScenariosBootstrap.java:81) ... at com.sun.proxy.$Proxy42.getMain(Unknown Source) at org.libreplan.business.scenarios.OnlyMainScenarioAwareManager.getCurrent(OnlyMainScenarioAwareManager.java:38) at org.libreplan.business.resources.entities.Resource$UsingScenarioManager.calculateAssignments(Resource.java:185) at org.libreplan.business.resources.entities.Resource$DayAssignmentsState.getAssignments(Resource.java:172) at org.libreplan.business.resources.entities.Resource.getAssignments(Resource.java:886) at org.libreplan.business.resources.entities.Resource.getAssignmentsForDay(Resource.java:153) at org.libreplan.business.resources.entities.Resource.getAssignedDurationDiscounting(Resource.java:861) at org.libreplan.business.planner.entities.AssignedEffortForResource$AssignedEffortDiscounting.getAssignedDurationAt(AssignedEff ortForResource.java:169) at org.libreplan.business.planner.entities.EffortDistributor$ResourceWithDerivedData.withAvailableCapacityOn(EffortDistributor.j ava:232) at org.libreplan.business.planner.entities.EffortDistributor.resourcesFromMoreDesirableToLess(EffortDistributor.java:408) at org.libreplan.business.planner.entities.EffortDistributor.assignAllPossibleWithoutOvertime(EffortDistributor.java:387) at org.libreplan.business.planner.entities.EffortDistributor.distributeForDay_(EffortDistributor.java:356) at org.libreplan.business.planner.entities.EffortDistributor.distributeForDay(EffortDistributor.java:341) at org.libreplan.business.planner.entities.DerivedAllocationGenerator.createAssignments(DerivedAllocationGenerator.java:114) at org.libreplan.business.planner.entities.DerivedAllocationGenerator.generate(DerivedAllocationGenerator.java:68) at org.libreplan.business.test.planner.entities.DerivedAllocationGeneratorTest.forOneResourceTheHoursGeneratedAreGotFromAlpha(De rivedAllocationGeneratorTest.java:205) --- .../DerivedAllocationGeneratorTest.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/libreplan-business/src/test/java/org/libreplan/business/test/planner/entities/DerivedAllocationGeneratorTest.java b/libreplan-business/src/test/java/org/libreplan/business/test/planner/entities/DerivedAllocationGeneratorTest.java index 14a78398f..eb770009c 100644 --- a/libreplan-business/src/test/java/org/libreplan/business/test/planner/entities/DerivedAllocationGeneratorTest.java +++ b/libreplan-business/src/test/java/org/libreplan/business/test/planner/entities/DerivedAllocationGeneratorTest.java @@ -27,6 +27,8 @@ import static org.easymock.classextension.EasyMock.createNiceMock; import static org.easymock.classextension.EasyMock.replay; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertThat; +import static org.libreplan.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE; +import static org.libreplan.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE; import static org.libreplan.business.test.planner.entities.DayAssignmentMatchers.haveHours; import static org.libreplan.business.workingday.EffortDuration.hours; @@ -41,7 +43,9 @@ import java.util.Map; import java.util.Set; import org.joda.time.LocalDate; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.libreplan.business.planner.entities.DayAssignment; import org.libreplan.business.planner.entities.DerivedAllocation; import org.libreplan.business.planner.entities.DerivedAllocationGenerator; @@ -54,13 +58,28 @@ import org.libreplan.business.resources.entities.MachineWorkerAssignment; import org.libreplan.business.resources.entities.MachineWorkersConfigurationUnit; import org.libreplan.business.resources.entities.Resource; import org.libreplan.business.resources.entities.Worker; +import org.libreplan.business.scenarios.bootstrap.IScenariosBootstrap; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Óscar González Fernández */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE, + BUSINESS_SPRING_CONFIG_TEST_FILE }) public class DerivedAllocationGeneratorTest { - private Machine machine = null; + @Autowired + private IScenariosBootstrap scenariosBootstrap; + + @Before + public void loadRequiredData() { + scenariosBootstrap.loadRequiredData(); + } + + private final Machine machine = null; private ResourceAllocation derivedFrom; private IWorkerFinder finder; From 5bc3d42bbe113e2ba89e9b56b45f752df68f6000 Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Tue, 28 May 2013 20:14:33 +0200 Subject: [PATCH 2/6] Avoid build warning Specify the encoding used in the project instead of using the platform default one. This could lead to not portable builds among different platforms. --- pom.xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pom.xml b/pom.xml index 066b55e0a..d6e7274af 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,8 @@ true true + + UTF-8 - - Date: Wed, 29 May 2013 18:49:44 +0200 Subject: [PATCH 4/6] Use the same configuration as in other tests Otherwise it creates a new Spring context instead of keeping using the previous one. This doesn't seem specially dangerous, but in some circumstances can lead to idle transactions with associated locks that hang up the execution of the tests. This happened with PostgreSQL version 9.1 in WorkReportServiceTest.importValidDescriptionValuesToWorkReport(). --- .../web/test/ws/basecalendars/BaseCalendarServiceTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java b/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java index 69b819ea5..f2b9642e9 100644 --- a/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java +++ b/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java @@ -25,7 +25,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.libreplan.business.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_FILE; import static org.libreplan.web.WebappGlobalNames.WEBAPP_SPRING_CONFIG_FILE; +import static org.libreplan.web.WebappGlobalNames.WEBAPP_SPRING_SECURITY_CONFIG_FILE; import static org.libreplan.web.test.WebappGlobalNames.WEBAPP_SPRING_CONFIG_TEST_FILE; +import static org.libreplan.web.test.WebappGlobalNames.WEBAPP_SPRING_SECURITY_CONFIG_TEST_FILE; import static org.libreplan.web.test.ws.common.Util.getUniqueName; import java.util.ArrayList; @@ -73,9 +75,10 @@ import org.springframework.transaction.annotation.Transactional; */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { BUSINESS_SPRING_CONFIG_FILE, - WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE }) + WEBAPP_SPRING_CONFIG_FILE, WEBAPP_SPRING_CONFIG_TEST_FILE, + WEBAPP_SPRING_SECURITY_CONFIG_FILE, + WEBAPP_SPRING_SECURITY_CONFIG_TEST_FILE }) @Transactional - public class BaseCalendarServiceTest { @Autowired From 48c3b1093e96d7b05449495142f93b7a6f63a0d8 Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Sat, 1 Jun 2013 16:25:37 +0200 Subject: [PATCH 5/6] Remove added exception types This test was causing errors on CalendarExceptionTypeServiceTest because it was leaving behind some CalendarExceptionTypes in the database. --- .../BaseCalendarServiceTest.java | 99 ++++++++++++++----- 1 file changed, 73 insertions(+), 26 deletions(-) diff --git a/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java b/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java index f2b9642e9..3ab56083d 100644 --- a/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java +++ b/libreplan-webapp/src/test/java/org/libreplan/web/test/ws/basecalendars/BaseCalendarServiceTest.java @@ -38,14 +38,18 @@ import java.util.List; import javax.xml.datatype.XMLGregorianCalendar; +import org.hibernate.Query; +import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.libreplan.business.calendars.daos.IBaseCalendarDAO; +import org.libreplan.business.calendars.daos.ICalendarExceptionDAO; import org.libreplan.business.calendars.daos.ICalendarExceptionTypeDAO; import org.libreplan.business.calendars.entities.BaseCalendar; import org.libreplan.business.calendars.entities.CalendarData; +import org.libreplan.business.calendars.entities.CalendarException; import org.libreplan.business.calendars.entities.CalendarExceptionType; import org.libreplan.business.calendars.entities.CalendarExceptionTypeColor; import org.libreplan.business.calendars.entities.Capacity; @@ -55,6 +59,7 @@ import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.entities.IConfigurationBootstrap; import org.libreplan.business.common.exceptions.InstanceNotFoundException; import org.libreplan.business.workingday.EffortDuration; +import org.libreplan.web.test.ws.calendarexceptiontypes.api.CalendarExceptionTypeServiceTest; import org.libreplan.ws.calendars.api.BaseCalendarDTO; import org.libreplan.ws.calendars.api.BaseCalendarListDTO; import org.libreplan.ws.calendars.api.CalendarDataDTO; @@ -64,9 +69,10 @@ import org.libreplan.ws.calendars.api.ICalendarService; import org.libreplan.ws.common.api.InstanceConstraintViolationsDTO; import org.libreplan.ws.common.impl.DateConverter; 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.test.context.transaction.AfterTransaction; +import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; /** @@ -90,6 +96,9 @@ public class BaseCalendarServiceTest { @Autowired private ICalendarExceptionTypeDAO calendarExceptionTypeDAO; + @Autowired + private ICalendarExceptionDAO calendarExceptionDAO; + @Autowired private IConfigurationDAO configurationDAO; @@ -102,8 +111,6 @@ public class BaseCalendarServiceTest { @Autowired private IConfigurationBootstrap configurationBootstrap; - private final String typeCode = "TypeCode_A"; - @Before public void loadConfiguration() { @@ -120,22 +127,66 @@ public class BaseCalendarServiceTest { } - @Test - @Rollback(false) - // FIXME: This exception type is kept in DB and it may cause problems in - // other tests, for example in - // CalendarExceptionTypeTest.exportExceptionTypes(), depending on the - // execution order. We must ensure that - public void givenCalendarExceptionTypeStored() { - CalendarExceptionType calendarExceptionType = CalendarExceptionType - .create("name", CalendarExceptionTypeColor.DEFAULT, false); - calendarExceptionType.setCode(typeCode); + private CalendarExceptionType addedExceptionType; - calendarExceptionTypeDAO.save(calendarExceptionType); - calendarExceptionTypeDAO.flush(); - sessionFactory.getCurrentSession().evict(calendarExceptionType); - calendarExceptionType.dontPoseAsTransientObjectAnymore(); + @BeforeTransaction + public void ensureOneExceptionType() { + addedExceptionType = transactionService + .runOnTransaction(new IOnTransaction() { + @Override + public CalendarExceptionType execute() { + CalendarExceptionType result; + result = CalendarExceptionType.create("name", + CalendarExceptionTypeColor.DEFAULT, false); + result.setCode("TypeCode_A"); + calendarExceptionTypeDAO.save(result); + return result; + } + }); + addedExceptionType.dontPoseAsTransientObjectAnymore(); + } + + + /** + * It removes added {@link CalendarExceptionType} to avoid problems in other + * tests. The associated calendar exceptions are also removed, so the + * {@link CalendarExceptionType} can be removed without error. + * + * More concretely, it was causing problem in + * {@link CalendarExceptionTypeServiceTest} if it was executed after this + * test. + */ + @AfterTransaction + public void removeAddedExceptionType() { + transactionService.runOnTransaction(new IOnTransaction() { + @Override + public Void execute() { + try { + removeAssociatedCalendarData(); + removeAddedType(); + return null; + } catch (InstanceNotFoundException e) { + throw new RuntimeException(e); + } + } + + @SuppressWarnings("unchecked") + private void removeAssociatedCalendarData() { + Session s = sessionFactory.getCurrentSession(); + Query query = s.createQuery( + "from CalendarException e where e.type = :type") + .setParameter("type", addedExceptionType); + List found = query.list(); + for (CalendarException each : found) { + s.delete(each); + } + } + + private void removeAddedType() throws InstanceNotFoundException { + calendarExceptionTypeDAO.remove(addedExceptionType.getId()); + } + }); } private Date getValidDate(int day) { @@ -170,21 +221,17 @@ public class BaseCalendarServiceTest { } @Test - // FIXME: when this test finishes, the two new calendar exceptions are not - // being removed. This is a problem to delete the calendar exception type - // and causes conflicts in other tests, for example in - // CalendarExceptionTypeTest.exportExceptionTypes() public void testAddValidBaseCalendar() throws InstanceNotFoundException { /* Build valid base calendar "bc1" (5 constraint violations). */ /* Build a calendar exception */ CalendarExceptionDTO exceptionDTO_1 = new CalendarExceptionDTO( getUniqueName(), toXml(getValidDate(0)), new Integer(7), - typeCode); + addedExceptionType.getCode()); CalendarExceptionDTO exceptionDTO_2 = new CalendarExceptionDTO( getUniqueName(), toXml(getValidDate(1)), new Integer(7), - typeCode); + addedExceptionType.getCode()); List calendarExceptions = new ArrayList(); calendarExceptions.add(exceptionDTO_1); @@ -248,16 +295,16 @@ public class BaseCalendarServiceTest { /* Build two calendar exception with the same date */ CalendarExceptionDTO exceptionDTO_1 = new CalendarExceptionDTO( getUniqueName(), toXml(getValidDate(0)), new Integer(7), - typeCode); + addedExceptionType.getCode()); CalendarExceptionDTO exceptionDTO_2 = new CalendarExceptionDTO( getUniqueName(), toXml(getValidDate(0)), new Integer(7), - typeCode); + addedExceptionType.getCode()); /* Build two calendar exception with the past date */ CalendarExceptionDTO exceptionDTO_3 = new CalendarExceptionDTO( getUniqueName(), toXml(getInvalidDate()), new Integer(7), - typeCode); + addedExceptionType.getCode()); /* Build two calendar exception with the invalid type */ CalendarExceptionDTO exceptionDTO_4 = new CalendarExceptionDTO( From 05a9767477f059c9965a5f1f3d549a0a053e1c45 Mon Sep 17 00:00:00 2001 From: Oscar Gonzalez Fernandez Date: Sat, 1 Jun 2013 20:24:12 +0200 Subject: [PATCH 6/6] Fix execution order depending error in test Since the scenario was created in the same transaction (due to using @Before annotation) was not visible in givenOrderFromPrepareForCreate. Now use a @BeforeTransaction instead so it's visible before. --- .../test/java/org/libreplan/web/orders/OrderModelTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libreplan-webapp/src/test/java/org/libreplan/web/orders/OrderModelTest.java b/libreplan-webapp/src/test/java/org/libreplan/web/orders/OrderModelTest.java index 2e54907a6..4973c9991 100644 --- a/libreplan-webapp/src/test/java/org/libreplan/web/orders/OrderModelTest.java +++ b/libreplan-webapp/src/test/java/org/libreplan/web/orders/OrderModelTest.java @@ -43,7 +43,6 @@ import javax.annotation.Resource; import org.easymock.EasyMock; import org.hibernate.SessionFactory; -import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; @@ -83,6 +82,7 @@ import org.springframework.test.annotation.NotTransactional; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; import org.zkoss.zk.ui.Desktop; @@ -116,7 +116,7 @@ public class OrderModelTest { @Resource private IDataBootstrap scenariosBootstrap; - @Before + @BeforeTransaction public void loadRequiredaData() { defaultAdvanceTypesBootstrapListener.loadRequiredData(); configurationBootstrap.loadRequiredData(); @@ -238,7 +238,7 @@ public class OrderModelTest { return null; } }); - Order order = (Order) orderModel.getOrder(); + Order order = orderModel.getOrder(); order.setName("name"); order.setCode("code"); order.setInitDate(new Date());