ItEr16S12CreacionProxectoPlanificacion: Creating entities without mapping and associated tests.
This commit is contained in:
parent
258041fde6
commit
dd7b7f1a11
8 changed files with 406 additions and 0 deletions
|
|
@ -0,0 +1,49 @@
|
|||
package org.navalplanner.business.planner.entities;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class Dependency {
|
||||
|
||||
public enum Type {
|
||||
END_START, START_START, END_END, START_END;
|
||||
}
|
||||
|
||||
public static Dependency createDependency(TaskElement origin,
|
||||
TaskElement destination, Type type) {
|
||||
Dependency dependency = new Dependency(origin, destination, type);
|
||||
origin.add(dependency);
|
||||
destination.add(dependency);
|
||||
return dependency;
|
||||
}
|
||||
|
||||
private TaskElement origin;
|
||||
|
||||
private TaskElement destination;
|
||||
|
||||
private Type type;
|
||||
|
||||
/**
|
||||
* It's needed by Hibernate. DO NOT USE.
|
||||
*/
|
||||
public Dependency() {
|
||||
}
|
||||
|
||||
private Dependency(TaskElement origin, TaskElement end, Type type) {
|
||||
this.origin = origin;
|
||||
this.destination = end;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public TaskElement getOrigin() {
|
||||
return origin;
|
||||
}
|
||||
|
||||
public TaskElement getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package org.navalplanner.business.planner.entities;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.navalplanner.business.orders.entities.HoursGroup;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class Task extends TaskElement {
|
||||
|
||||
public static Task createTask(HoursGroup hoursGroup) {
|
||||
return new Task(hoursGroup);
|
||||
}
|
||||
|
||||
private HoursGroup hoursGroup;
|
||||
|
||||
/**
|
||||
* For hibernate, DO NOT USE
|
||||
*/
|
||||
public Task() {
|
||||
|
||||
}
|
||||
|
||||
private Task(HoursGroup hoursGroup) {
|
||||
Validate.notNull(hoursGroup);
|
||||
this.hoursGroup = hoursGroup;
|
||||
}
|
||||
|
||||
public HoursGroup getHoursGroup() {
|
||||
return this.hoursGroup;
|
||||
}
|
||||
|
||||
public Integer getHours() {
|
||||
return hoursGroup.getWorkingHours();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
package org.navalplanner.business.planner.entities;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
import org.navalplanner.business.orders.entities.OrderElement;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public abstract class TaskElement {
|
||||
private Date startDate;
|
||||
|
||||
private Date endDate;
|
||||
|
||||
private OrderElement orderElement;
|
||||
|
||||
private Set<Dependency> dependenciesWithThisOrigin = new HashSet<Dependency>();
|
||||
|
||||
private Set<Dependency> dependenciesWithThisDestination = new HashSet<Dependency>();
|
||||
|
||||
public void setOrderElement(OrderElement orderElement)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
Validate.notNull(orderElement, "orderElement must be not null");
|
||||
if (this.orderElement != null)
|
||||
throw new IllegalStateException(
|
||||
"once a orderElement is set, it cannot be changed");
|
||||
this.orderElement = orderElement;
|
||||
}
|
||||
|
||||
public OrderElement getOrderElement() {
|
||||
return orderElement;
|
||||
}
|
||||
|
||||
public Set<Dependency> getDependenciesWithThisOrigin() {
|
||||
return Collections.unmodifiableSet(dependenciesWithThisOrigin);
|
||||
}
|
||||
|
||||
public Set<Dependency> getDependenciesWithThisDestination() {
|
||||
return Collections.unmodifiableSet(dependenciesWithThisDestination);
|
||||
}
|
||||
|
||||
public Date getStartDate() {
|
||||
return startDate;
|
||||
}
|
||||
|
||||
public void setStartDate(Date startDate) {
|
||||
this.startDate = startDate;
|
||||
}
|
||||
|
||||
public Date getEndDate() {
|
||||
return endDate;
|
||||
}
|
||||
|
||||
public void setEndDate(Date endDate) {
|
||||
this.endDate = endDate;
|
||||
}
|
||||
|
||||
void add(Dependency dependency) {
|
||||
if (this.equals(dependency.getOrigin())) {
|
||||
dependenciesWithThisOrigin.add(dependency);
|
||||
}
|
||||
if (this.equals(dependency.getDestination())) {
|
||||
dependenciesWithThisDestination.add(dependency);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package org.navalplanner.business.planner.entities;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.Validate;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class TaskGroup extends TaskElement {
|
||||
|
||||
private List<TaskElement> taskElements = new ArrayList<TaskElement>();
|
||||
|
||||
public List<TaskElement> getTaskElements() {
|
||||
return taskElements;
|
||||
}
|
||||
|
||||
public void addTaskElement(TaskElement task) {
|
||||
Validate.notNull(task);
|
||||
taskElements.add(task);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,55 @@
|
|||
package org.navalplanner.business.test.planner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.planner.entities.Dependency;
|
||||
import org.navalplanner.business.planner.entities.TaskElement;
|
||||
import org.navalplanner.business.planner.entities.Dependency.Type;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class DependencyTest {
|
||||
private Dependency dependency;
|
||||
private TaskElement origin;
|
||||
private TaskElement destination;
|
||||
private Type type;
|
||||
|
||||
public DependencyTest() {
|
||||
origin = TaskTest.createValidTask();
|
||||
destination = TaskTest.createValidTask();
|
||||
type = Type.END_START;
|
||||
dependency = Dependency.createDependency(origin, destination, type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dependencyHasOriginProperty() {
|
||||
assertThat(dependency.getOrigin(), equalTo(origin));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dependencyHasDestinationProperty() {
|
||||
assertThat(dependency.getDestination(), equalTo(destination));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dependencyHasTypeProperty() {
|
||||
assertThat(dependency.getType(), equalTo(type));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void creatingDependencyImpliesAssociatingItWithTheRelatedTasks() {
|
||||
assertFalse(origin.getDependenciesWithThisDestination().contains(
|
||||
dependency));
|
||||
assertTrue(origin.getDependenciesWithThisOrigin().contains(dependency));
|
||||
|
||||
assertTrue(destination.getDependenciesWithThisDestination().contains(
|
||||
dependency));
|
||||
assertFalse(destination.getDependenciesWithThisOrigin().contains(
|
||||
dependency));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
package org.navalplanner.business.test.planner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.orders.entities.OrderLine;
|
||||
import org.navalplanner.business.planner.entities.Dependency;
|
||||
import org.navalplanner.business.planner.entities.Task;
|
||||
import org.navalplanner.business.planner.entities.TaskElement;
|
||||
import org.navalplanner.business.planner.entities.Dependency.Type;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class TaskElementTest {
|
||||
|
||||
private TaskElement task = new Task();
|
||||
|
||||
private TaskElement taskWithOrderLine;
|
||||
|
||||
private Dependency exampleDependency;
|
||||
|
||||
public TaskElementTest() {
|
||||
this.taskWithOrderLine = new Task();
|
||||
this.taskWithOrderLine.setOrderElement(new OrderLine());
|
||||
this.exampleDependency = Dependency.createDependency(new Task(),
|
||||
new Task(), Type.END_START);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskElementHasAOneToOneRelationshipWithOrderElement() {
|
||||
OrderLine order = new OrderLine();
|
||||
task.setOrderElement(order);
|
||||
assertSame(order, task.getOrderElement());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void orderElementCannotBeSetToNull() {
|
||||
task.setOrderElement(null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void onceSetOrderElementCannotBeChanged() {
|
||||
taskWithOrderLine.setOrderElement(new OrderLine());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initiallyAssociatedDependenciesAreEmpty() {
|
||||
assertTrue(task.getDependenciesWithThisDestination().isEmpty());
|
||||
assertTrue(task.getDependenciesWithThisOrigin().isEmpty());
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void dependenciesWithThisOriginCollectionCannotBeModified() {
|
||||
task.getDependenciesWithThisOrigin().add(exampleDependency);
|
||||
}
|
||||
|
||||
@Test(expected = UnsupportedOperationException.class)
|
||||
public void dependenciesWithThisDestinationCollectionCannotBeModified() {
|
||||
task.getDependenciesWithThisDestination().add(exampleDependency);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskElementHasStartDateProperty() {
|
||||
Date now = new Date();
|
||||
task.setStartDate(now);
|
||||
assertThat(task.getStartDate(), equalTo(now));
|
||||
task.setEndDate(now);
|
||||
assertThat(task.getEndDate(), equalTo(now));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package org.navalplanner.business.test.planner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.planner.entities.Task;
|
||||
import org.navalplanner.business.planner.entities.TaskElement;
|
||||
import org.navalplanner.business.planner.entities.TaskGroup;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class TaskGroupTest {
|
||||
private TaskGroup taskGroup = new TaskGroup();
|
||||
|
||||
@Test
|
||||
public void taskGroupIsAnInstanceOfTaskElement() {
|
||||
assertTrue(taskGroup instanceof TaskElement);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskGroupHasManyTaskElements() {
|
||||
List<TaskElement> tasks = taskGroup.getTaskElements();
|
||||
assertTrue("a task group has no task elements initially", tasks
|
||||
.isEmpty());
|
||||
TaskElement child1 = new Task();
|
||||
taskGroup.addTaskElement(child1);
|
||||
TaskGroup child2 = new TaskGroup();
|
||||
taskGroup.addTaskElement(child2);
|
||||
List<TaskElement> taskElements = taskGroup.getTaskElements();
|
||||
assertThat(taskElements.size(), equalTo(2));
|
||||
assertThat(taskGroup.getTaskElements(), equalTo(Arrays.asList(child1,
|
||||
child2)));
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void cantAndNullTaskElement() {
|
||||
taskGroup.addTaskElement(null);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
package org.navalplanner.business.test.planner;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.navalplanner.business.orders.entities.HoursGroup;
|
||||
import org.navalplanner.business.planner.entities.Task;
|
||||
import org.navalplanner.business.planner.entities.TaskElement;
|
||||
|
||||
/**
|
||||
* @author Óscar González Fernández <ogonzalez@igalia.com>
|
||||
*/
|
||||
public class TaskTest {
|
||||
|
||||
private Task task;
|
||||
private HoursGroup hoursGroup;
|
||||
|
||||
public TaskTest() {
|
||||
hoursGroup = new HoursGroup();
|
||||
hoursGroup.setWorkingHours(3);
|
||||
task = Task.createTask(hoursGroup);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskIsASubclassOfTaskElement() {
|
||||
assertTrue(task instanceof TaskElement);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskHasNumberOfHours() {
|
||||
assertThat(task.getHours(), equalTo(hoursGroup.getWorkingHours()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void taskMustHaveOneHoursGroup() {
|
||||
HoursGroup hoursGroup = task.getHoursGroup();
|
||||
assertNotNull(hoursGroup);
|
||||
}
|
||||
|
||||
public static TaskElement createValidTask() {
|
||||
HoursGroup hours = new HoursGroup();
|
||||
hours.setWorkingHours(20);
|
||||
return Task.createTask(hours);
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue