From: <ro...@us...> - 2009-03-10 10:42:06
|
Revision: 70 http://cse-ip.svn.sourceforge.net/cse-ip/?rev=70&view=rev Author: roekens Date: 2009-03-10 10:41:52 +0000 (Tue, 10 Mar 2009) Log Message: ----------- - added dependency check for category hierarchy - added test - added comments Modified Paths: -------------- cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/main/java/de/campussource/cse/cdmm/domain/Category.java cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/CategoryDaoTest.java Modified: cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/main/java/de/campussource/cse/cdmm/domain/Category.java =================================================================== --- cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/main/java/de/campussource/cse/cdmm/domain/Category.java 2009-03-10 10:14:25 UTC (rev 69) +++ cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/main/java/de/campussource/cse/cdmm/domain/Category.java 2009-03-10 10:41:52 UTC (rev 70) @@ -52,17 +52,55 @@ this.parent = parent; } - public boolean addChild(Category category){ + + /** + * Convenience method for dependency check to prevent circular dependencies. + * True if circular dependency would be created. + * + * @param newChildCategory new child category to be added to this + * @return true, if newChildCategory is present in hierarchy above this, else false + */ + private boolean checkForCircularDependencies(Category newChildCategory){ + if (this.equals(newChildCategory)){ + return true; + } + if (this.isRoot()){ + return false; + } + return this.parent.checkForCircularDependencies(newChildCategory); + } + + /** + * Private method for adding category to list of children. + * If list of children does not exist, it is created. + * + * @param category child category + * @return success of operation + */ + private boolean addChild(Category category){ if (this.children == null){ this.children = new ArrayList<Category>(); } - if (!category.getParent().equals(this)){ + if (!category.getParent().equals(this)||checkForCircularDependencies(category)){ return false; } return this.children.add(category); } + /** + * Convenience method for parent <-> children connection of categories. + * Parent of this is set to parent category and list of children of parent category + * is updated with this. + * + * @param parent parent category + * @return Success of operation + */ + public boolean addToParentCategory(Category parent){ + this.parent = parent; + return parent.addChild(this); + } + @XmlElementWrapper(name=Constants.CATEGORIES) @XmlElement(name=Constants.CATEGORY) public List<Category> getChildren() { @@ -94,6 +132,12 @@ this.parent = parent; } + + /** + * Convenience method for checking if category is root category. + * + * @return true if category is root category, else returning false + */ @XmlTransient public boolean isRoot(){ return (parent==null); Modified: cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/CategoryDaoTest.java =================================================================== --- cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/CategoryDaoTest.java 2009-03-10 10:14:25 UTC (rev 69) +++ cse-ip/trunk/sandbox/cse-ip/sc-cdmm/src/test/java/de/campussource/cse/cdmm/CategoryDaoTest.java 2009-03-10 10:41:52 UTC (rev 70) @@ -3,6 +3,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; import org.junit.Before; import org.junit.Test; @@ -19,6 +21,9 @@ private Category category; private Category parentCategory; + /** + * Creates a category object, a parent object for this category and the category dao. + */ @Before public void setUp() { category = newCategory(); @@ -28,6 +33,9 @@ txBegin(); } + /** + * Checks if category can be persisted + */ @Test public void testPersistCategory() { dao.persist(category); @@ -36,6 +44,9 @@ } + /** + * Checks if category can be found by id + */ @Test public void testFindCategory() { dao.persist(category); @@ -45,6 +56,9 @@ assertEquals(category, coid); } + /** + * Checks if category can be deleted + */ @Test public void testRemoveCategory() { dao.persist(category); @@ -55,10 +69,12 @@ assertNull(dao.find(category.getId())); } + /** + * Checks if cascading saving and cascading deleting work + */ @Test public void testCascading(){ - category.setParent(parentCategory); - parentCategory.addChild(category); + category.addToParentCategory(parentCategory); dao.persist(category); txCommit(); assertNotNull(category.getId()); @@ -70,12 +86,26 @@ assertNull(dao.find(category.getId())); } + /** + * Checks if searching for not existing ids returns null + */ @Test public void testDoNotFindById() { Category foundCategory = dao.find(0L); assertNull(foundCategory); } + /** + * Checks if circular category dependencies are prevented + */ + @Test + public void testCircularDependencyCheck(){ + assertTrue(category.addToParentCategory(parentCategory)); + dao.persist(category); + assertFalse(parentCategory.addToParentCategory(category)); + } + + private Category newCategory() { return new Category(createUniqueId()); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |