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; diff --git a/libreplan-business/src/test/resources/libreplan-business-spring-config-test.xml b/libreplan-business/src/test/resources/libreplan-business-spring-config-test.xml index 0c89eb0b7..3b48f8430 100644 --- a/libreplan-business/src/test/resources/libreplan-business-spring-config-test.xml +++ b/libreplan-business/src/test/resources/libreplan-business-spring-config-test.xml @@ -7,17 +7,11 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> - - () { + @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) { @@ -167,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); @@ -245,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( diff --git a/libreplan-webapp/src/test/resources/libreplan-webapp-spring-config-test.xml b/libreplan-webapp/src/test/resources/libreplan-webapp-spring-config-test.xml index 64d97c1f5..7cfe39394 100644 --- a/libreplan-webapp/src/test/resources/libreplan-webapp-spring-config-test.xml +++ b/libreplan-webapp/src/test/resources/libreplan-webapp-spring-config-test.xml @@ -7,11 +7,6 @@ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> - true true + + UTF-8