KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > executer > ContentMetadataExtracterTest


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
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.repo.action.executer;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.alfresco.model.ContentModel;
23 import org.alfresco.repo.action.ActionImpl;
24 import org.alfresco.repo.content.MimetypeMap;
25 import org.alfresco.repo.content.transform.AbstractContentTransformerTest;
26 import org.alfresco.repo.security.authentication.AuthenticationComponent;
27 import org.alfresco.service.cmr.repository.ContentService;
28 import org.alfresco.service.cmr.repository.ContentWriter;
29 import org.alfresco.service.cmr.repository.NodeRef;
30 import org.alfresco.service.cmr.repository.NodeService;
31 import org.alfresco.service.cmr.repository.StoreRef;
32 import org.alfresco.service.namespace.QName;
33 import org.alfresco.util.BaseSpringTest;
34 import org.alfresco.util.GUID;
35
36 /**
37  * Test of the ActionExecuter for extracting metadata. Note: This test makes
38  * assumptions about the PDF test data for PdfBoxExtracter.
39  *
40  * @author Jesper Steen Møller
41  */

42 public class ContentMetadataExtracterTest extends BaseSpringTest
43 {
44     protected static final String JavaDoc QUICK_TITLE = "The quick brown fox jumps over the lazy dog";
45     protected static final String JavaDoc QUICK_DESCRIPTION = "Gym class featuring a brown fox and lazy dog";
46     protected static final String JavaDoc QUICK_CREATOR = "Nevin Nollop";
47
48     private NodeService nodeService;
49     private ContentService contentService;
50     private StoreRef testStoreRef;
51     private NodeRef rootNodeRef;
52     private NodeRef nodeRef;
53
54     private ContentMetadataExtracter executer;
55
56     private final static String JavaDoc ID = GUID.generate();
57
58     @Override JavaDoc
59     protected void onSetUpInTransaction() throws Exception JavaDoc
60     {
61         this.nodeService = (NodeService) this.applicationContext.getBean("nodeService");
62         this.contentService = (ContentService) this.applicationContext.getBean("contentService");
63         
64         AuthenticationComponent authenticationComponent = (AuthenticationComponent)applicationContext.getBean("authenticationComponent");
65         authenticationComponent.setSystemUserAsCurrentUser();
66
67         // Create the store and get the root node
68
this.testStoreRef = this.nodeService.createStore(
69                 StoreRef.PROTOCOL_WORKSPACE,
70                 "Test_" + System.currentTimeMillis());
71         this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
72
73         // Create the node used for tests
74
this.nodeRef = this.nodeService.createNode(
75                 this.rootNodeRef, ContentModel.ASSOC_CHILDREN,
76                 QName.createQName("{test}testnode"),
77                 ContentModel.TYPE_CONTENT).getChildRef();
78
79         // Setup the content from the PDF test data
80
ContentWriter cw = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
81         cw.setMimetype(MimetypeMap.MIMETYPE_PDF);
82         cw.putContent(AbstractContentTransformerTest.loadQuickTestFile("pdf"));
83
84         // Get the executer instance
85
this.executer = (ContentMetadataExtracter) this.applicationContext.getBean(ContentMetadataExtracter.NAME);
86     }
87
88     /**
89      * Test execution of the extraction itself
90      */

91     public void testFromBlanks()
92     {
93         // Test that the action writes properties when they don't exist or are
94
// unset
95

96         // Get the old props
97
Map JavaDoc<QName, Serializable JavaDoc> props = this.nodeService.getProperties(this.nodeRef);
98         props.remove(ContentModel.PROP_AUTHOR);
99         props.put(ContentModel.PROP_TITLE, "");
100         props.put(ContentModel.PROP_DESCRIPTION, null); // Wonder how this will
101
// be handled
102
this.nodeService.setProperties(this.nodeRef, props);
103
104         // Execute the action
105
ActionImpl action = new ActionImpl(ID, SetPropertyValueActionExecuter.NAME, null);
106
107         this.executer.execute(action, this.nodeRef);
108
109         // Check that the properties have been set
110
assertEquals(QUICK_TITLE, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_TITLE));
111         assertEquals(QUICK_DESCRIPTION, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_DESCRIPTION));
112         assertEquals(QUICK_CREATOR, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_AUTHOR));
113     }
114
115     /**
116      * Test execution of the pragmatic approach
117      */

118     public void testFromPartial()
119     {
120         // Test that the action does not overwrite properties that are already
121
// set
122
String JavaDoc myCreator = "Null-op";
123         String JavaDoc myTitle = "The hot dog is eaten by the city fox";
124
125         // Get the old props
126
Map JavaDoc<QName, Serializable JavaDoc> props = this.nodeService.getProperties(this.nodeRef);
127         props.put(ContentModel.PROP_AUTHOR, myCreator);
128         props.put(ContentModel.PROP_TITLE, myTitle);
129         props.remove(ContentModel.PROP_DESCRIPTION); // Allow this baby
130
this.nodeService.setProperties(this.nodeRef, props);
131
132         // Execute the action
133
ActionImpl action = new ActionImpl(ID, SetPropertyValueActionExecuter.NAME, null);
134
135         this.executer.execute(action, this.nodeRef);
136
137         // Check that the properties have been preserved
138
assertEquals(myTitle, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_TITLE));
139         assertEquals(myCreator, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_AUTHOR));
140
141         // But this one should have been set
142
assertEquals(QUICK_DESCRIPTION, this.nodeService.getProperty(this.nodeRef, ContentModel.PROP_DESCRIPTION));
143
144     }
145
146     // If we implement other policies than "pragmatic", they should be tested as
147
// well...
148
}
149
Popular Tags