Index: lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/AttachmentTest.java =================================================================== diff -u --- lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/AttachmentTest.java (revision 0) +++ lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/AttachmentTest.java (revision b62674f9ee01f587c9be2cc2409f16f94e3ed518) @@ -0,0 +1,52 @@ +package org.lamsfoundation.lams.tool.forum.persistence; + +import org.lamsfoundation.lams.tool.forum.core.FactoryException; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; + +import java.util.List; + +import junit.framework.TestCase; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 7/06/2005 + * Time: 15:06:55 + * To change this template use File | Settings | File Templates. + */ +public class AttachmentTest extends TestCase { + protected void setUp() throws Exception { + super.setUp(); + } + + public void testCreateAndDeleteForum() throws FactoryException { + //Populate an Attachment entity for test purposes + AttachmentDao attachmentDao = (AttachmentDao) GenericObjectFactoryImpl.getInstance().lookup(AttachmentDao.class); + Attachment instructions = new Attachment(); + instructions.setData("instructions byte array".getBytes()); + instructions.setType(false); + attachmentDao.saveOrUpdate(instructions); + + AttachmentDao dao = (AttachmentDao) GenericObjectFactoryImpl.getInstance().lookup(AttachmentDao.class); + dao.saveOrUpdate(instructions); + assertNotNull(instructions.getId()); + + //load + + Attachment reloaded = (Attachment) dao.getById(instructions.getId()); + assertEquals("persisted and reloaded byte array should be equal", "instructions byte array", new String(instructions.getData())); + + //find + List values = dao.findByNamedQuery("allAttachments"); + assertTrue("find all result not containing object", values.contains(instructions)); + + //delete + dao.delete(reloaded); + assertNull("object not deleted", dao.getById(instructions.getId()) ); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +} Index: lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/ForumTest.java =================================================================== diff -u --- lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/ForumTest.java (revision 0) +++ lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/ForumTest.java (revision b62674f9ee01f587c9be2cc2409f16f94e3ed518) @@ -0,0 +1,97 @@ +package org.lamsfoundation.lams.tool.forum.persistence; + +import org.lamsfoundation.lams.tool.forum.core.FactoryException; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; + +import java.util.List; +import java.util.Set; +import java.util.HashSet; + +import junit.framework.TestCase; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 7/06/2005 + * Time: 10:38:21 + * To change this template use File | Settings | File Templates. + */ +public class ForumTest extends TestCase { + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testCreateAndDeleteForum() throws FactoryException { + //Populate a Forum entity for test purposes + Forum entity = new Forum(); + entity.setTitle("Lams Forum"); + entity.setLockWhenFinished(false); + entity.setForceOffLine(true); + entity.setAllowAnnomity(true); + entity.setCreatedBy(new Long("1000")); + + Set attachments = new HashSet(); + + AttachmentDao attachmentDao = (AttachmentDao) GenericObjectFactoryImpl.getInstance().lookup(AttachmentDao.class); + Attachment instructions = new Attachment(); + instructions.setData("instructions byte array".getBytes()); + instructions.setType(true); + attachments.add(instructions); + attachmentDao.saveOrUpdate(instructions); + + /* + Attachment onLineInstructions = new Attachment(); + onLineInstructions.setData("on line instructions byte array".getBytes()); + instructions.setType("ONLINEINSTRUCTIONS"); + attachments.add(onLineInstructions); + attachmentDao.saveOrUpdate(onLineInstructions); + + Attachment offLineInstructions = new Attachment(); + offLineInstructions.setData("off line instructions byte array".getBytes()); + instructions.setType("OFFLINEINSTRUCTIONS"); + attachments.add(offLineInstructions); + attachmentDao.saveOrUpdate(offLineInstructions);*/ + + entity.setAttachments(attachments); + + //save + ForumDao dao = (ForumDao) GenericObjectFactoryImpl.getInstance().lookup(ForumDao.class); + dao.saveOrUpdate(entity); + assertNotNull(entity.getId()); + assertNotNull("date created is null", entity.getCreated()); + assertNotNull("date updated is null", entity.getUpdated()); + assertEquals("date created and updated are different for first save", entity.getCreated(), entity.getUpdated()); + + //load + Forum reloaded = (Forum) dao.getById(entity.getId()); + assertEquals("reloaded object not equal", entity, reloaded); + assertEquals("date difference in database and memory", entity.getCreated().getTime()/1000, reloaded.getCreated().getTime()/1000); + assertEquals("date difference in database and memory", entity.getUpdated().getTime()/1000, reloaded.getUpdated().getTime()/1000); + assertEquals("title should be Lams Forum", "Lams Forum", reloaded.getTitle()); + assertEquals("lockWhenFinished should be false", false, reloaded.getLockWhenFinished()); + assertEquals("forceOffline should be true", true, reloaded.getForceOffLine()); + assertEquals("allowAnnomity should be true", true, reloaded.getAllowAnnomity()); + //validate attachment relations + assertEquals("should have 1 attachments", 1, reloaded.getAttachments().size()); + Set reloadedSet = reloaded.getAttachments(); + + assertTrue("reloaded set does not contain instructions attachment", reloadedSet.contains(instructions)); + Attachment[] child = (Attachment[]) reloadedSet.toArray(new Attachment[0]); + assertEquals("attachments type should be same", instructions.getType(), child[0].getType()); + assertEquals("attachment bytes should be the same", new String(instructions.getData()), new String(child[0].getData())); + + //find + List values = dao.findByNamedQuery("allForums"); + assertTrue("find all result not containing object", values.contains(entity)); + + //delete + dao.delete(reloaded); + assertNull("object not deleted", dao.getById(entity.getId()) ); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + +} Index: lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/GenericEntityTest.java =================================================================== diff -u --- lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/GenericEntityTest.java (revision 0) +++ lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/GenericEntityTest.java (revision b62674f9ee01f587c9be2cc2409f16f94e3ed518) @@ -0,0 +1,56 @@ +package org.lamsfoundation.lams.tool.forum.persistence; + +import org.lamsfoundation.lams.tool.forum.core.FactoryException; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; + +import java.util.List; + +import junit.framework.TestCase; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 6/06/2005 + * Time: 13:46:53 + * To change this template use File | Settings | File Templates. + */ +public class GenericEntityTest extends TestCase { + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testCreateAndDeleteGenericEntity() throws FactoryException { + GenericEntity entity = new GenericEntity(); + entity.setCreatedBy(new Long("1002")); + entity.setModifiedBy(new Long("1004")); + + //save + GenericEntityDao dao = (GenericEntityDao) GenericObjectFactoryImpl.getInstance().lookup(GenericEntityDao.class); + dao.saveOrUpdate(entity); + assertNotNull(entity.getId()); + assertNotNull("date created is null", entity.getCreated()); + assertNotNull("date updated is null", entity.getUpdated()); + assertEquals("date created and updated are different for first save", entity.getCreated(), entity.getUpdated()); + + //load + GenericEntity reloaded = (GenericEntity) dao.getById(entity.getId()); + assertEquals("reloaded object not equal", entity, reloaded); + assertEquals("date difference in database and memory", entity.getCreated().getTime()/1000, reloaded.getCreated().getTime()/1000); + assertEquals("date difference in database and memory", entity.getUpdated().getTime()/1000, reloaded.getUpdated().getTime()/1000); + assertEquals("createdBy difference in database and memory", entity.getCreatedBy(), reloaded.getCreatedBy()); + assertEquals("createdBy difference in database and memory", entity.getCreatedBy(), reloaded.getCreatedBy()); + + //find + List values = dao.findByNamedQuery("all"); + assertTrue("find all result not containing object", values.contains(entity)); + + //delete + dao.delete(reloaded); + assertNull("object not deleted", dao.getById(entity.getId()) ); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } +} Index: lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/MessageTest.java =================================================================== diff -u --- lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/MessageTest.java (revision 0) +++ lams_tool_forum/test/java/org/lamsfoundation/lams/tool/forum/persistence/MessageTest.java (revision b62674f9ee01f587c9be2cc2409f16f94e3ed518) @@ -0,0 +1,107 @@ +package org.lamsfoundation.lams.tool.forum.persistence; + +import org.lamsfoundation.lams.tool.forum.core.FactoryException; +import org.lamsfoundation.lams.tool.forum.core.GenericObjectFactoryImpl; + +import java.util.List; +import java.util.Set; +import java.util.HashSet; + +import junit.framework.TestCase; + +/** + * Created by IntelliJ IDEA. + * User: conradb + * Date: 7/06/2005 + * Time: 12:42:05 + * To change this template use File | Settings | File Templates. + */ +public class MessageTest extends TestCase { + + protected void setUp() throws Exception { + super.setUp(); + } + + public void testCreateAndDeleteMessage() throws FactoryException { + //Populate a Forum entity for test purposes + Forum forum = new Forum(); + + //save + ForumDao dao = (ForumDao) GenericObjectFactoryImpl.getInstance().lookup(ForumDao.class); + dao.saveOrUpdate(forum); + + Message message = new Message(); + message.setBody("Test Message"); + message.setSubject("Test Message"); + message.setForum(forum); + message.setIsAnnonymous(false); + message.setIsAuthored(true); + message.setCreatedBy(new Long(1000)); + message.setModifiedBy(new Long(1002)); + + MessageDao messageDao = (MessageDao) GenericObjectFactoryImpl.getInstance().lookup(MessageDao.class); + messageDao.saveOrUpdate(message); + + assertNotNull(message.getId()); + assertNotNull("date created is null", message.getCreated()); + assertNotNull("date updated is null", message.getUpdated()); + assertEquals("date created and updated are different for first save", message.getCreated(), message.getUpdated()); + + //load + Message reloaded = (Message) messageDao.getById(message.getId()); + assertEquals("reloaded message not equal", message, reloaded); + assertEquals("reloaded message body should be: Test Message", "Test Message", reloaded.getBody()); + assertEquals("reloaded message Subject should be: Test Message", "Test Message", reloaded.getSubject()); + assertEquals("reloaded message Forum not equal", forum.getId(), reloaded.getForum().getId()); + assertEquals("reloaded message isAnnonymous not equal", false, reloaded.getIsAnnonymous()); + assertEquals("reloaded message isAuthored not equal", true, reloaded.getIsAuthored()); + assertEquals("reloaded message createdBy not equal", new Long(1000), reloaded.getCreatedBy()); + assertEquals("reloaded message modifiedBy not equal", new Long(1002), reloaded.getModifiedBy()); + + Message message2 = new Message(); + message2.setBody("Test Message2"); + message2.setSubject("Test Message2"); + message2.setForum(forum); + message2.setIsAnnonymous(true); + message2.setIsAuthored(true); + message2.setCreatedBy(new Long(1005)); + message2.setModifiedBy(new Long(1006)); + + messageDao.saveOrUpdate(message2); + Message reloaded2 = (Message) messageDao.getById(message2.getId()); + assertEquals("reloaded message not equal", message2, reloaded2); + assertEquals("reloaded message body should be: Test Message", "Test Message2", reloaded2.getBody()); + assertEquals("reloaded message Subject should be: Test Message", "Test Message2", reloaded2.getSubject()); + assertEquals("reloaded message Forum not equal", forum.getId(), reloaded2.getForum().getId()); + assertEquals("reloaded message isAnnonymous not equal", true, reloaded2.getIsAnnonymous()); + assertEquals("reloaded message isAuthored not equal", true, reloaded2.getIsAuthored()); + assertEquals("reloaded message createdBy not equal", new Long(1005), reloaded2.getCreatedBy()); + assertEquals("reloaded message modifiedBy not equal", new Long(1006), reloaded2.getModifiedBy()); + + //find + List values = dao.findByNamedQuery("allMessages"); + assertTrue("find all result not containing object", values.contains(message)); + assertTrue("find all result not containing object", values.contains(message2)); + + Set replies = new HashSet(); + replies.add(message2); + reloaded.setReplies(replies); + + messageDao.saveOrUpdate(reloaded); + + reloaded = (Message) messageDao.getById(message.getId()); + Set reloadedReplies = reloaded.getReplies(); + assertTrue("reloaded message does not have a child", reloadedReplies.contains(message2)); + + + //delete + messageDao.delete(reloaded); + dao.delete(forum); + assertNull("message object not deleted", messageDao.getById(message.getId())); + assertNull("reply message object not deleted", messageDao.getById(message2.getId())); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } +}