ItEr35S14CUAdministracionMateriaisItEr34S14: manage the relation 'category' between Material and MaterialCategory

Methods to manage the relation 'category' in both directions.
Written some tests for this relation.
There are problems with the deletion process (testRemoveMaterial fails).
This commit is contained in:
Diego Pino Garcia 2009-11-23 10:46:58 +01:00 committed by Javier Moran Rua
parent 4c60b9e5a6
commit 08c0b078df
3 changed files with 32 additions and 1 deletions

View file

@ -44,7 +44,7 @@ public class Material extends BaseEntity {
private boolean disabled;
@NotNull
private MaterialCategory category;
private MaterialCategory category = null;
// Default constructor, needed by Hibernate
protected Material() {
@ -72,7 +72,10 @@ public class Material extends BaseEntity {
}
public void setCategory(MaterialCategory category) {
if(this.category!=null)
this.category.removeMaterial(this);
this.category = category;
category.addMaterial(this);
}
public String getDescription() {

View file

@ -97,4 +97,17 @@ public class MaterialCategory extends BaseEntity {
if(subcategory.getParent()!=null)
subcategory.removeParent();
}
public Set<Material> getMaterials() {
return materials;
}
public void addMaterial(Material material) {
materials.add(material);
if(material.getCategory()!=this)
material.setCategory(this);
}
public void removeMaterial(Material material) {
materials.remove(material);
}
}

View file

@ -28,6 +28,7 @@ import static org.navalplanner.business.BusinessGlobalNames.BUSINESS_SPRING_CONF
import static org.navalplanner.business.test.BusinessGlobalNames.BUSINESS_SPRING_CONFIG_TEST_FILE;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.junit.Test;
@ -84,6 +85,7 @@ public class MaterialDAOTest {
public void testRemoveMaterial() throws InstanceNotFoundException {
Material material = createValidMaterial();
materialDAO.save(material);
//material.getCategory().removeMaterial(material);
materialDAO.remove(material.getId());
assertFalse(materialDAO.exists(material.getId()));
}
@ -96,4 +98,17 @@ public class MaterialDAOTest {
List<Material> list = materialDAO.list(Material.class);
assertEquals(previous + 1, list.size());
}
@Test
public void testListMaterialsFromCategory() {
Material material1 = createValidMaterial();
int previous = material1.getCategory().getMaterials().size();
Material material2 = createValidMaterial();
material2.setCategory(material1.getCategory());
materialDAO.save(material1);
materialDAO.save(material2);
Set<Material> list = material1.getCategory().getMaterials();
assertEquals(previous + 1, list.size());
}
}