I have a unit test to check for the correct deletion of an element in a collection property.
The deletion of the object from the collection should result in the deletion of the record from the table.
The test source code:
Code:
public LinkCategoryDaoTest() {
linkCategory0 = new LinkCategory();
linkCategory0.setName("Images");
linkCategory1 = new LinkCategory();
linkCategory1.setName("Pdf");
}
@Before
public void beforeAnyTest() throws Exception {
linkCategory1 = linkCategoryDao.makePersistent(linkCategory1);
linkCategory0 = linkCategoryDao.makePersistent(linkCategory0);
}
@Test
public void testCollection() {
link0 = new Link();
link0.setName("Thalasoft");
link0.setImage("image0.png");
link0.setListOrder(3);
linkCategory0.addLink(link0);
link2 = new Link();
link2.setName("Google");
link2.setImage("image2.gif");
link2.setListOrder(1);
linkCategory0.addLink(link2);
link1 = new Link();
link1.setName("Libe");
link1.setImage("image1.jpg");
link1.setListOrder(2);
linkCategory0.addLink(link1);
linkCategoryDao.flush();
linkCategoryDao.clear();
LinkCategory linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(3, linkCategory.getLinks().size());
assertEquals(link2.getName(), linkCategory.getLinks().get(0).getName());
assertEquals(link1.getName(), linkCategory.getLinks().get(1).getName());
assertEquals(link0.getName(), linkCategory.getLinks().get(2).getName());
linkCategory0.removeLink(link1);
linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(2, linkCategory.getLinks().size()); <<<==== FAILS HERE
assertEquals(link2.getName(), linkCategory.getLinks().get(0).getName());
assertEquals(link0.getName(), linkCategory.getLinks().get(1).getName());
linkCategory0.removeLink(link2);
linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(1, linkCategory.getLinks().size());
assertEquals(link0.getName(), linkCategory.getLinks().get(0).getName());
linkCategory0.removeLink(link0);
linkCategory = linkCategoryDao.findWithId(linkCategory0.getId(), false);
assertEquals(0, linkCategory.getLinks().size());
}
The test fails at the line indicated in the source code.
The error message is:
Quote:
java.lang.AssertionError: expected:<2> but was:<3>
at org.junit.Assert.fail(Assert.java:74)
at org.junit.Assert.failNotEquals(Assert.java:448)
at org.junit.Assert.assertEquals(Assert.java:102)
at org.junit.Assert.assertEquals(Assert.java:323)
at org.junit.Assert.assertEquals(Assert.java:319)
at com.thalasoft.learnintouch.core.dao.LinkCategoryDaoTest.testCollection(LinkCategoryDaoTest.java:87)
The Hibernate mapping is:
Code:
<list name="links" inverse="true" cascade="all">
<key column="category_id" not-null="true" />
<list-index column="list_order" base="1" />
<one-to-many class="com.thalasoft.learnintouch.core.domain.Link" />
</list>
The tables structures:
Code:
mysql> desc link;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| version | int(10) unsigned | NO | | | |
| name | varchar(50) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| image | varchar(50) | YES | | NULL | |
| url | varchar(255) | YES | | NULL | |
| category_id | int(10) unsigned | YES | MUL | NULL | |
| list_order | int(10) unsigned | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
8 rows in set (0.05 sec)
mysql> desc link_category;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| version | int(10) unsigned | NO | | | |
| name | varchar(50) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
Clearly, the deletion does not occur as I hoped it would.
Any clue ?