Index: lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardContentDAO.java =================================================================== diff -u -ra766c8fe51dfa314947b20ef98e525d63b328fbe -rc4e52cfb69c2d7ca6178878497c2175537a837db --- lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardContentDAO.java (.../TestNoticeboardContentDAO.java) (revision a766c8fe51dfa314947b20ef98e525d63b328fbe) +++ lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardContentDAO.java (.../TestNoticeboardContentDAO.java) (revision c4e52cfb69c2d7ca6178878497c2175537a837db) @@ -27,27 +27,18 @@ import org.lamsfoundation.lams.tool.noticeboard.NbDataAccessTestCase; import org.lamsfoundation.lams.tool.noticeboard.NoticeboardContent; -import org.lamsfoundation.lams.tool.noticeboard.NoticeboardSession; import org.lamsfoundation.lams.tool.noticeboard.dao.hibernate.NoticeboardContentDAO; -import org.springframework.orm.hibernate.HibernateObjectRetrievalFailureException; - - -//import java.util.*; - /** * @author mtruong * - * TODO To change the template for this generated type comment go to - * Window - Preferences - Java - Code Style - Code Templates + * JUnit Test Cases to test the NoticeboardContentDAO class */ public class TestNoticeboardContentDAO extends NbDataAccessTestCase { private NoticeboardContentDAO noticeboardDAO; private NoticeboardSessionDAO nbSessionDAO; - //private NoticeboardContent noticeboardContent; - private boolean cleanContentData = true; - private boolean cleanAllData = true; + private boolean cleanContentData = true; public TestNoticeboardContentDAO(String name) { @@ -62,80 +53,77 @@ //set up default noticeboard content for each test noticeboardDAO = (NoticeboardContentDAO) this.context.getBean("nbContentDAO"); nbSessionDAO = (NoticeboardSessionDAO) this.context.getBean("nbSessionDAO"); - this.initNbContentData(); - this.initNbSessionContent(); - //super.initAllData(); + super.initAllData(); } /** * @see NbDataAccessTestCase#tearDown() */ - protected void tearDown() throws Exception { + protected void tearDown() throws Exception + { super.tearDown(); //remove noticeboard content after each test - if (cleanAllData) + if(cleanContentData) { - super.cleanAllData(); + super.cleanNbContentData(TEST_NB_ID); } - else if(cleanContentData) - { - super.cleanNbContentData(); - } } - public void testgetNbContentById() + + public void testgetNbContentByUID() { - /* Retrieve the previously saved noticeboard object from the database */ + nbContent = noticeboardDAO.getNbContentByUID(new Long(1)); + + assertEquals(nbContent.getUid(), new Long(1)); + assertContentEqualsDefaultData(nbContent); + + } + + public void testfindNbContentByID() + { + nbContent = noticeboardDAO.findNbContentById(TEST_NB_ID); + + assertContentEqualsTestData(nbContent); - nbContent = noticeboardDAO.getNbContentById(getTestNoticeboardId()); - - /* Ensure that all content is what is was suppose to be */ - //assertEquals(noticeboardContent.getNoticeboardId(), TEST_NB_ID); - - assertEquals(nbContent.getTitle(), TEST_TITLE); - assertEquals(nbContent.getContent(), TEST_CONTENT); - assertEquals(nbContent.getOnlineInstructions(), TEST_ONLINE_INSTRUCTIONS); - assertEquals(nbContent.getOfflineInstructions(), TEST_OFFLINE_INSTRUCTIONS); - assertEquals(nbContent.isDefineLater(), TEST_DEFINE_LATER); - assertEquals(nbContent.isForceOffline(), TEST_FORCE_OFFLINE); - assertEquals(nbContent.getCreatorUserId(), TEST_CREATOR_USER_ID); - assertEquals(nbContent.getDateCreated(), TEST_DATE_CREATED); - - - - } - public void testloadNbContentById(Long nbContentId) - { - /* Retrieve the previously saved noticeboard object from the database */ - - nbContent = noticeboardDAO.loadNbContentById(getTestNoticeboardId()); - - /* Ensure that all content is what is was suppose to be */ - //assertEquals(noticeboardContent.getNoticeboardId(), TEST_NB_ID); - - assertEquals(nbContent.getTitle(), TEST_TITLE); - assertEquals(nbContent.getContent(), TEST_CONTENT); - assertEquals(nbContent.getOnlineInstructions(), TEST_ONLINE_INSTRUCTIONS); - assertEquals(nbContent.getOfflineInstructions(), TEST_OFFLINE_INSTRUCTIONS); - assertEquals(nbContent.isDefineLater(), TEST_DEFINE_LATER); - assertEquals(nbContent.isForceOffline(), TEST_FORCE_OFFLINE); - assertEquals(nbContent.getCreatorUserId(), TEST_CREATOR_USER_ID); - assertEquals(nbContent.getDateCreated(), TEST_DATE_CREATED); - } + // Test to see if trying to retrieve a non-existent object would + // return null or not. + + Long nonExistentId = new Long(88777); + assertNbContentIsNull(nonExistentId); + } + + + public void testremoveNoticeboard() + { + cleanContentData = false; + + + nbContent = noticeboardDAO.findNbContentById(TEST_NB_ID); + + noticeboardDAO.removeNoticeboard(nbContent); + + assertNbSessionIsNull(TEST_SESSION_ID); //check if child table is deleted + assertNbContentIsNull(TEST_NB_ID); + } + + public void testremoveNoticeboardById() + { + cleanContentData = false; + + + noticeboardDAO.removeNoticeboard(TEST_NB_ID); + + assertNbSessionIsNull(TEST_SESSION_ID); + assertNbContentIsNull(TEST_NB_ID); + } + public void testgetNbContentBySession() { nbContent = noticeboardDAO.getNbContentBySession(TEST_SESSION_ID); - assertEquals(nbContent.getTitle(), TEST_TITLE); - assertEquals(nbContent.getContent(), TEST_CONTENT); - assertEquals(nbContent.getOnlineInstructions(), TEST_ONLINE_INSTRUCTIONS); - assertEquals(nbContent.getOfflineInstructions(), TEST_OFFLINE_INSTRUCTIONS); - assertEquals(nbContent.isDefineLater(), TEST_DEFINE_LATER); - assertEquals(nbContent.isForceOffline(), TEST_FORCE_OFFLINE); - assertEquals(nbContent.getCreatorUserId(), TEST_CREATOR_USER_ID); - assertEquals(nbContent.getDateCreated(), TEST_DATE_CREATED); + assertContentEqualsTestData(nbContent); } public void testsaveNbContent() @@ -145,81 +133,46 @@ * TODO: change this, actually test the save method */ + + nbContent = noticeboardDAO.findNbContentById(getTestNoticeboardId()); - nbContent = noticeboardDAO.loadNbContentById(getTestNoticeboardId()); + assertContentEqualsTestData(nbContent); - assertEquals(nbContent.getNbContentId(), getTestNoticeboardId()); - - assertEquals(nbContent.getTitle(), TEST_TITLE); - assertEquals(nbContent.getContent(), TEST_CONTENT); - assertEquals(nbContent.getOnlineInstructions(), TEST_ONLINE_INSTRUCTIONS); - assertEquals(nbContent.getOfflineInstructions(), TEST_OFFLINE_INSTRUCTIONS); - assertEquals(nbContent.isDefineLater(), TEST_DEFINE_LATER); - assertEquals(nbContent.isForceOffline(), TEST_FORCE_OFFLINE); - assertEquals(nbContent.getCreatorUserId(), TEST_CREATOR_USER_ID); - assertEquals(nbContent.getDateCreated(), TEST_DATE_CREATED); - - } + } public void testupdateNbContent() { // Update the noticeboard to have a new value for its content field String newContent = "New updated content"; - nbContent = noticeboardDAO.loadNbContentById(getTestNoticeboardId()); + nbContent = noticeboardDAO.findNbContentById(getTestNoticeboardId()); nbContent.setContent(newContent); noticeboardDAO.updateNbContent(nbContent); //Check whether the value has been updated - nbContent = noticeboardDAO.getNbContentById(getTestNoticeboardId()); + nbContent = noticeboardDAO.findNbContentById(getTestNoticeboardId()); assertEquals(nbContent.getContent(), newContent); - } - - public void testremoveNoticeboard() - { - cleanContentData = false; - cleanAllData = false; - NoticeboardContent nb = noticeboardDAO.getNbContentById(TEST_NB_ID); - noticeboardDAO.removeNbSessions(nb); - noticeboardDAO.removeNoticeboard(TEST_NB_ID); - try - { - - NoticeboardContent emptyNbContent = noticeboardDAO.getNbContentById(TEST_NB_ID); - fail("An exception should be raised as the object has already been deleted"); - } - catch (HibernateObjectRetrievalFailureException e) - { - assertTrue(true); - } - - } - + } + public void testremoveNbSessions() { - cleanAllData = false; - cleanContentData = true; + nbContent = noticeboardDAO.findNbContentById(getTestNoticeboardId()); - nbContent = noticeboardDAO.getNbContentById(getTestNoticeboardId()); noticeboardDAO.removeNbSessions(nbContent); - NoticeboardContent nb = noticeboardDAO.getNbContentById(getTestNoticeboardId()); - try - { - NoticeboardSession nbSession = nbSessionDAO.getNbSessionById(TEST_SESSION_ID); - fail("An exception should be raised"); - } - catch (HibernateObjectRetrievalFailureException e) - { - assertTrue(true); - } - + nbContent.getNbSessions().clear(); //Have to remove/empty the collection before deleting it. + //otherwise exception will occur + noticeboardDAO.updateNbContent(nbContent); + NoticeboardContent nb = noticeboardDAO.findNbContentById(getTestNoticeboardId()); + assertNotNull(nb); + assertNbSessionIsNull(TEST_SESSION_ID); } + } Index: lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardSessionDAO.java =================================================================== diff -u -ra766c8fe51dfa314947b20ef98e525d63b328fbe -rc4e52cfb69c2d7ca6178878497c2175537a837db --- lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardSessionDAO.java (.../TestNoticeboardSessionDAO.java) (revision a766c8fe51dfa314947b20ef98e525d63b328fbe) +++ lams_tool_nb/test/java/org/lamsfoundation/lams/tool/noticeboard/dao/hibernate/TestNoticeboardSessionDAO.java (.../TestNoticeboardSessionDAO.java) (revision c4e52cfb69c2d7ca6178878497c2175537a837db) @@ -19,32 +19,32 @@ *http://www.gnu.org/licenses/gpl.txt */ -/* - * Created on May 16, 2005 - * - * TODO To change the template for this generated file go to - * Window - Preferences - Java - Code Style - Code Templates - */ package org.lamsfoundation.lams.tool.noticeboard.dao.hibernate; +import java.util.Date; + import org.lamsfoundation.lams.tool.noticeboard.NbDataAccessTestCase; import org.lamsfoundation.lams.tool.noticeboard.NoticeboardContent; import org.lamsfoundation.lams.tool.noticeboard.NoticeboardSession; -//import org.lamsfoundation.lams.tool.noticeboard.NoticeboardContent; -//import org.lamsfoundation.lams.tool.noticeboard.NoticeboardSession; + /** * @author mtruong * + * JUnit Test Cases to test the NoticeboardSessionDAO class */ public class TestNoticeboardSessionDAO extends NbDataAccessTestCase { private NoticeboardSessionDAO nbSessionDAO; private NoticeboardContentDAO nbContentDAO; + private NoticeboardSession nbSession = null; + private NoticeboardContent nbContent = null; - private boolean cleanSessionContentData = true; + //private boolean cleanSessionContentData = false; + private boolean cleanContentData = true; + public TestNoticeboardSessionDAO(String name) { super(name); @@ -58,108 +58,120 @@ nbContentDAO = (NoticeboardContentDAO) this.context.getBean("nbContentDAO"); nbSessionDAO = (NoticeboardSessionDAO) this.context.getBean("nbSessionDAO"); - - /* initialise data for noticeboard content and create a session containing this data */ - this.initNbContentData(); - this.initNbSessionContent(); + super.initAllData(); } /** * @see NbDataAccessTestCase#tearDown() */ protected void tearDown() throws Exception { super.tearDown(); - //remove noticeboard content after each test - if (cleanSessionContentData) + if(cleanContentData) { - - super.cleanAllData(); - + super.cleanNbContentData(TEST_NB_ID); } - else - { - super.cleanNbContentData(); - } + } - public void testgetNbSessionById() + public void testgetNbSessionByUID() { - - //NoticeboardSession newNbSession = nbSessionDAO.getNbSessionById(TEST_SESSION_ID); - nbSession = nbSessionDAO.getNbSessionById(TEST_SESSION_ID); - - - assertEquals("Validate session id ",nbSession.getNbSessionId(), TEST_SESSION_ID); - assertEquals("Validate content id ",nbSession.getNbContent().getNbContentId(), TEST_NB_ID); - assertEquals("Validate session start date", nbSession.getSessionStartDate(), TEST_SESSION_START_DATE); - assertEquals("Validate session end date", nbSession.getSessionEndDate(), TEST_SESSION_END_DATE); - assertEquals("Validate session status", nbSession.getSessionStatus(), TEST_SESSION_STATUS); - - + nbSession = nbSessionDAO.getNbSessionByUID(new Long(1)); //default test data which is always in db + + assertEquals(nbSession.getNbSessionId(), DEFAULT_SESSION_ID); + assertEquals(nbSession.getSessionStatus(), DEFAULT_SESSION_STATUS); + } - - public void testloadNbSessionById() - { - nbSession = nbSessionDAO.loadNbSessionById(TEST_SESSION_ID); - assertEquals("Validate session id ",nbSession.getNbSessionId(), TEST_SESSION_ID); - assertEquals("Validate content id ",nbSession.getNbContent().getNbContentId(), TEST_NB_ID); - assertEquals("Validate session start date", nbSession.getSessionStartDate(), TEST_SESSION_START_DATE); - assertEquals("Validate session end date", nbSession.getSessionEndDate(), TEST_SESSION_END_DATE); - assertEquals("Validate session status", nbSession.getSessionStatus(), TEST_SESSION_STATUS); - + public void testfindNbSessionById() + { + nbSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + assertEqualsForSessionContent(nbSession); + + Long nonExistentSessionId = new Long(7657); + assertSessionObjectIsNull(nonExistentSessionId); } - + public void testsaveNbSession() { - /* remove data that has been setup by setUp() */ - this.cleanNbSessionContent(); - - NoticeboardContent nb = nbContentDAO.getNbContentById(TEST_NB_ID); - - nbSession = new NoticeboardSession(TEST_SESSION_ID, - nb, - TEST_SESSION_START_DATE, - TEST_SESSION_END_DATE, - TEST_SESSION_STATUS); - - nbSessionDAO.saveNbSession(nbSession); - - assertEquals("Validate session id ",nbSession.getNbSessionId(), TEST_SESSION_ID); - assertEquals("Validate content id ",nbSession.getNbContent().getNbContentId(), TEST_NB_ID); - assertEquals("Validate session start date", nbSession.getSessionStartDate(), TEST_SESSION_START_DATE); - assertEquals("Validate session end date", nbSession.getSessionEndDate(), TEST_SESSION_END_DATE); - assertEquals("Validate session status", nbSession.getSessionStatus(), TEST_SESSION_STATUS); - - - } + NoticeboardContent nbContentToReference = nbContentDAO.findNbContentById(TEST_NB_ID); + + Long newSessionId = new Long(2222); + Date newDateCreated = new Date(System.currentTimeMillis()); + NoticeboardSession newSessionObject = new NoticeboardSession(newSessionId, + nbContentToReference, + newDateCreated, + NoticeboardSession.NOT_ATTEMPTED); + + nbContentToReference.getNbSessions().add(newSessionObject); + nbContentDAO.updateNbContent(nbContentToReference); + + nbSessionDAO.saveNbSession(newSessionObject); + + //Retrieve the newly added session object and test its values + + nbSession = nbSessionDAO.findNbSessionById(newSessionId); + + assertEquals(nbSession.getNbSessionId(), newSessionId); + assertEquals(nbSession.getSessionStartDate(), newDateCreated); + + } - public void testupdateNbSession() { - String sessionStatus = "Suspended"; - - nbSession = nbSessionDAO.getNbSessionById(TEST_SESSION_ID); - - nbSession.setSessionStatus(sessionStatus); - - nbSessionDAO.updateNbSession(nbSession); - - assertEquals("Validate session id ",nbSession.getNbSessionId(), TEST_SESSION_ID); - assertEquals("Validate new session status",nbSession.getSessionStatus(), sessionStatus); - } + nbSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + + nbSession.setSessionStatus(NoticeboardSession.COMPLETED); + + nbSessionDAO.updateNbSession(nbSession); + + NoticeboardSession updatedSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + + assertEquals(updatedSession.getSessionStatus(), NoticeboardSession.COMPLETED); + } - - public void testremoveNbSession() + public void testremoveNbSessionByUID() { - cleanSessionContentData = false; - nbSessionDAO.removeNbSession(TEST_SESSION_ID); - - NoticeboardSession session = new NoticeboardSession(); - - assertNull(session.getNbSessionId()); - + NoticeboardSession existingSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + Long uid = existingSession.getUid(); + + NoticeboardContent referencedContent = existingSession.getNbContent(); + + nbSessionDAO.removeNbSessionByUID(uid); + referencedContent.getNbSessions().remove(existingSession); + + nbContentDAO.updateNbContent(referencedContent); + + assertSessionObjectIsNull(TEST_SESSION_ID); } + public void testremoveNbSessionById() + { + nbSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + nbContent = nbSession.getNbContent(); + nbContent.getNbSessions().remove(nbSession); + + nbSessionDAO.removeNbSession(TEST_SESSION_ID); + + nbContentDAO.updateNbContent(nbContent); + + assertSessionObjectIsNull(TEST_SESSION_ID); + + } + + public void testremoveNbSession() + { + nbSession = nbSessionDAO.findNbSessionById(TEST_SESSION_ID); + nbContent = nbSession.getNbContent(); + nbContent.getNbSessions().remove(nbSession); + + nbSessionDAO.removeNbSession(nbSession); + + nbContentDAO.updateNbContent(nbContent); + + assertSessionObjectIsNull(TEST_SESSION_ID); + } + + + }