001: package org.contineo.core.document.dao;
002:
003: import java.util.Collection;
004:
005: import org.contineo.core.AbstractCoreTestCase;
006: import org.contineo.core.document.Document;
007: import org.contineo.core.document.Version;
008: import org.contineo.core.document.dao.DocumentDAO;
009: import org.contineo.core.document.dao.TermDAO;
010: import org.contineo.core.security.Menu;
011: import org.contineo.core.security.dao.MenuDAO;
012:
013: /**
014: * Test case for <code>HibernateDocumentDAO</code>
015: *
016: * @author Marco Meschieri
017: * @version $Id:$
018: * @since 3.0
019: */
020: public class HibernateDocumentDAOTest extends AbstractCoreTestCase {
021:
022: // Instance under test
023: private DocumentDAO dao;
024:
025: private MenuDAO menuDao;
026:
027: public HibernateDocumentDAOTest(String name) {
028: super (name);
029: }
030:
031: protected void setUp() throws Exception {
032: super .setUp();
033:
034: // Retrieve the instance under test from spring context. Make sure that
035: // it is an HibernateDocumentDAO
036: dao = (DocumentDAO) context.getBean("DocumentDAO");
037: menuDao = (MenuDAO) context.getBean("MenuDAO");
038: }
039:
040: public void testDelete() {
041: assertTrue(dao.delete(100));
042: Document doc = dao.findByPrimaryKey(100);
043: assertNull(doc);
044: }
045:
046: public void testDeleteByMenuId() {
047: TermDAO termDAO = (TermDAO) context.getBean("TermDAO");
048: termDAO.delete(103);
049: assertEquals(0, termDAO.findByMenuId(103).size());
050: termDAO.delete(1);
051: assertTrue(dao.deleteByMenuId(103));
052: Document doc = dao.findByPrimaryKey(1);
053: assertNull(doc);
054: }
055:
056: public void testFindAll() {
057: Collection<Document> documents = dao.findAll();
058: assertNotNull(documents);
059: assertEquals(2, documents.size());
060: }
061:
062: public void testFindByMenuId() {
063: Document doc = dao.findByMenuId(103);
064: assertNotNull(doc);
065: assertEquals(1, doc.getDocId());
066: assertEquals(103, doc.getMenuId());
067:
068: //Try with unexisting menu
069: doc = dao.findByMenuId(1000);
070: assertNull(doc);
071: }
072:
073: public void testFindByPrimaryKey() {
074: Document doc = dao.findByPrimaryKey(1);
075: assertNotNull(doc);
076: assertEquals(1, doc.getDocId());
077: assertEquals("testDocname", doc.getDocName());
078: assertEquals(2, doc.getVersions().size());
079: assertNotNull(doc.getMenu());
080: assertEquals(103, doc.getMenu().getMenuId());
081:
082: // Try with unexisting document
083: doc = dao.findByPrimaryKey(99);
084: assertNull(doc);
085: }
086:
087: public void testFindByUserName() {
088: Collection<Integer> ids = dao.findByUserName("sebastian");
089: assertNotNull(ids);
090: assertEquals(2, ids.size());
091: assertTrue(ids.contains(new Integer(2)));
092:
093: // Try with a user without documents
094: ids = dao.findByUserName("test");
095: assertNotNull(ids);
096: assertEquals(0, ids.size());
097: }
098:
099: public void testFindMenuIdByKeyword() {
100: Collection<Integer> ids = dao.findMenuIdByKeyword("abc");
101: assertNotNull(ids);
102: assertEquals(1, ids.size());
103: assertEquals(new Integer(103), ids.iterator().next());
104:
105: ids = dao.findMenuIdByKeyword("xxx");
106: assertNotNull(ids);
107: assertEquals(0, ids.size());
108: }
109:
110: public void testStore() {
111: Document doc = new Document();
112: Menu menu = menuDao.findByPrimaryKey(Menu.MENUID_HOME);
113: doc.setMenu(menu);
114: doc.setDocPublisher("admin");
115: doc.setDocName("test");
116: doc.addKeyword("pippo");
117: doc.addKeyword("pluto");
118: Version version = new Version();
119: version.setVersion("1.0");
120: version.setVersionComment("comment");
121: doc.addVersion(version);
122: assertTrue(dao.store(doc));
123: assertEquals(3, doc.getDocId());
124: doc = dao.findByPrimaryKey(3);
125: assertNotNull(doc);
126: assertEquals(3, doc.getDocId());
127: assertEquals(2, doc.getKeywords().size());
128: assertTrue(doc.getKeywords().contains("pluto"));
129: assertEquals(1, doc.getVersions().size());
130: assertEquals(version, doc.getVersion("1.0"));
131:
132: //Try to change the version comment
133: doc = dao.findByPrimaryKey(3);
134: version = doc.getVersion("1.0");
135: version.setVersionComment("xxxx");
136: dao.store(doc);
137: doc = dao.findByPrimaryKey(3);
138: version = doc.getVersion("1.0");
139: assertEquals("xxxx", version.getVersionComment());
140:
141: // Load an existing document and modify it
142: doc = dao.findByPrimaryKey(1);
143: assertNotNull(doc);
144: assertEquals("testDocname", doc.getDocName());
145: assertEquals(2, doc.getVersions().size());
146: assertEquals(3, doc.getKeywords().size());
147: doc.setDocName("xxxx");
148: assertTrue(dao.store(doc));
149: doc = dao.findByPrimaryKey(1);
150: assertNotNull(doc);
151: assertEquals(1, doc.getDocId());
152: assertEquals("xxxx", doc.getDocName());
153: assertEquals(2, doc.getVersions().size());
154: assertEquals(3, doc.getKeywords().size());
155: }
156:
157: public void testToKeywords() {
158: Collection<String> coll = dao.toKeywords("my name is tom");
159: assertNotNull(coll);
160: assertEquals(2, coll.size());
161:
162: coll = dao.toKeywords("il mio nome e' tom");
163: assertNotNull(coll);
164: assertEquals(3, coll.size());
165: }
166:
167: public void testFindKeywords() {
168: Collection<String> keywords = dao.findKeywords("a", "admin");
169: assertNotNull(keywords);
170: assertEquals(1, keywords.size());
171: assertEquals("abc", keywords.iterator().next());
172: }
173: }
|