KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > sample > contentHits > ContentHitsAspectTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.sample.contentHits;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
33  * Content hits apsect sample unit test.
34  *
35  * @author Roy Wetherall
36  */

37 public class ContentHitsAspectTest extends BaseAlfrescoSpringTest
38 {
39     /**
40      * Test the contentHits aspect behaviour
41      */

42     public void testContentHitsApsectBehaviour()
43     {
44         // Get the node and content services
45
NodeService nodeService = (NodeService)this.applicationContext.getBean("nodeService");
46         ContentService contentService = (ContentService)this.applicationContext.getBean("contentService");
47         
48         // Create the content node
49
Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>(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         // Apply the content hits aspect
59
nodeService.addAspect(nodeRef, ContentHitsAspect.ASPECT_CONTENT_HITS, null);
60         
61         // Check the count hit values
62
checkHitCountValues(nodeService, nodeRef, 0, 0);
63         
64         // Add some content to the node
65
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         // Check the content hit values
71
checkHitCountValues(nodeService, nodeRef, 1, 0);
72         
73         // Read the content a couple of times
74
contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
75         contentService.getReader(nodeRef, ContentModel.PROP_CONTENT);
76         
77         // Check the content hit values
78
checkHitCountValues(nodeService, nodeRef, 1, 2);
79         
80         // Update the content again
81
ContentWriter contentWriter2 = contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
82         contentWriter2.putContent("Updating the existing content.");
83         
84         // Check the content hit values
85
checkHitCountValues(nodeService, nodeRef, 2, 2);
86     }
87     
88     /**
89      * Helper method to check that contentHits apsect currently holds the expected values
90      *
91      * @param nodeService the node service
92      * @param nodeRef the node reference
93      * @param expectedUpdateCount the expected update count value
94      * @param expectedReadCount the expected read count value
95      */

96     private void checkHitCountValues(NodeService nodeService, NodeRef nodeRef, int expectedUpdateCount, int expectedReadCount)
97     {
98         // Get the update count value
99
int currentUpdateCount = ((Integer JavaDoc)nodeService.getProperty(nodeRef, ContentHitsAspect.PROP_UPDATE_COUNT)).intValue();
100         
101         // Assert that it matches the expected value
102
assertEquals(expectedUpdateCount, currentUpdateCount);
103         
104         // Get the read count value
105
int currentReadCount = ((Integer JavaDoc)nodeService.getProperty(nodeRef, ContentHitsAspect.PROP_READ_COUNT)).intValue();
106         
107         // Assert that it matches the expected value
108
assertEquals(expectedReadCount, currentReadCount);
109     }
110 }
111
Popular Tags