1 17 package org.alfresco.sample.contentHits; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.content.MimetypeMap; 25 import org.alfresco.service.cmr.repository.ContentService; 26 import org.alfresco.service.cmr.repository.ContentWriter; 27 import org.alfresco.service.cmr.repository.NodeRef; 28 import org.alfresco.service.cmr.repository.NodeService; 29 import org.alfresco.service.namespace.QName; 30 import org.alfresco.util.BaseAlfrescoSpringTest; 31 32 37 public class ContentHitsAspectTest extends BaseAlfrescoSpringTest 38 { 39 42 public void testContentHitsApsectBehaviour() 43 { 44 NodeService nodeService = (NodeService)this.applicationContext.getBean("nodeService"); 46 ContentService contentService = (ContentService)this.applicationContext.getBean("contentService"); 47 48 Map <QName, Serializable > properties = new HashMap <QName, Serializable >(1); 50 properties.put(ContentModel.PROP_NAME, "contentHits.txt"); 51 NodeRef nodeRef = nodeService.createNode( 52 this.rootNodeRef, 53 ContentModel.ASSOC_CHILDREN, 54 QName.createQName("{contentHitsAspectTest}countedContent"), 55 ContentModel.TYPE_CONTENT, 56 properties).getChildRef(); 57 58 nodeService.addAspect(nodeRef, ContentHitsAspect.ASPECT_CONTENT_HITS, null); 60 61 checkHitCountValues(nodeService, nodeRef, 0, 0); 63 64 ContentWriter contentWriter = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 66 contentWriter.setEncoding("UTF-8"); 67 contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 68 contentWriter.putContent("Putting some initial content onto the node."); 69 70 checkHitCountValues(nodeService, nodeRef, 1, 0); 72 73 contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); 75 contentService.getReader(nodeRef, ContentModel.PROP_CONTENT); 76 77 checkHitCountValues(nodeService, nodeRef, 1, 2); 79 80 ContentWriter contentWriter2 = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true); 82 contentWriter2.putContent("Updating the existing content."); 83 84 checkHitCountValues(nodeService, nodeRef, 2, 2); 86 } 87 88 96 private void checkHitCountValues(NodeService nodeService, NodeRef nodeRef, int expectedUpdateCount, int expectedReadCount) 97 { 98 int currentUpdateCount = ((Integer )nodeService.getProperty(nodeRef, ContentHitsAspect.PROP_UPDATE_COUNT)).intValue(); 100 101 assertEquals(expectedUpdateCount, currentUpdateCount); 103 104 int currentReadCount = ((Integer )nodeService.getProperty(nodeRef, ContentHitsAspect.PROP_READ_COUNT)).intValue(); 106 107 assertEquals(expectedReadCount, currentReadCount); 109 } 110 } 111 | Popular Tags |