KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > rule > ruletrigger > RuleTriggerTest


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.repo.rule.ruletrigger;
18
19 import org.alfresco.model.ContentModel;
20 import org.alfresco.repo.content.MimetypeMap;
21 import org.alfresco.service.cmr.repository.ContentService;
22 import org.alfresco.service.cmr.repository.ContentWriter;
23 import org.alfresco.service.cmr.repository.NodeRef;
24 import org.alfresco.service.cmr.repository.NodeService;
25 import org.alfresco.service.cmr.repository.StoreRef;
26 import org.alfresco.service.cmr.rule.RuleType;
27 import org.alfresco.util.BaseSpringTest;
28
29 /**
30  * Rule trigger test
31  *
32  * @author Roy Wetherall
33  */

34 public class RuleTriggerTest extends BaseSpringTest
35 {
36     private static final String JavaDoc ON_CREATE_NODE_TRIGGER = "on-create-node-trigger";
37     private static final String JavaDoc ON_UPDATE_NODE_TRIGGER = "on-update-node-trigger";
38     private static final String JavaDoc ON_DELETE_NODE_TRIGGER = "on-delete-node-trigger";
39     private static final String JavaDoc ON_CREATE_CHILD_ASSOCIATION_TRIGGER = "on-create-child-association-trigger";
40     private static final String JavaDoc ON_DELETE_CHILD_ASSOCIATION_TRIGGER = "on-delete-child-association-trigger";
41     private static final String JavaDoc ON_CREATE_ASSOCIATION_TRIGGER = "on-create-association-trigger";
42     private static final String JavaDoc ON_DELETE_ASSOCIATION_TRIGGER = "on-delete-association-trigger";
43     private static final String JavaDoc ON_CONTENT_UPDATE_TRIGGER = "on-content-update-trigger";
44     private static final String JavaDoc ON_CONTENT_CREATE_TRIGGER = "on-content-create-trigger";
45
46     private NodeService nodeService;
47     private ContentService contentService;
48     
49     private StoreRef testStoreRef;
50     private NodeRef rootNodeRef;
51     
52     @Override JavaDoc
53     protected void onSetUpInTransaction() throws Exception JavaDoc
54     {
55         this.nodeService = (NodeService)this.applicationContext.getBean("nodeService");
56         this.contentService = (ContentService)this.applicationContext.getBean("contentService");
57         
58         this.testStoreRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
59         this.rootNodeRef = this.nodeService.getRootNode(this.testStoreRef);
60     }
61     
62     public void testOnCreateNodeTrigger()
63     {
64         TestRuleType ruleType = createTestRuleType(ON_CREATE_NODE_TRIGGER);
65         assertFalse(ruleType.rulesTriggered);
66         
67         // Try and trigger the type
68
this.nodeService.createNode(
69                 this.rootNodeRef,
70                 ContentModel.ASSOC_CHILDREN,
71                 ContentModel.ASSOC_CHILDREN,
72                 ContentModel.TYPE_CONTAINER);
73         
74         // Check to see if the rule type has been triggered
75
assertTrue(ruleType.rulesTriggered);
76     }
77     
78     public void testOnUpdateNodeTrigger()
79     {
80         NodeRef nodeRef = this.nodeService.createNode(
81                 this.rootNodeRef,
82                 ContentModel.ASSOC_CHILDREN,
83                 ContentModel.ASSOC_CHILDREN,
84                 ContentModel.TYPE_CONTAINER).getChildRef();
85         
86         TestRuleType ruleType = createTestRuleType(ON_UPDATE_NODE_TRIGGER);
87         assertFalse(ruleType.rulesTriggered);
88         
89         // Try and trigger the type
90
this.nodeService.setProperty(nodeRef, ContentModel.PROP_NAME, "nameChanged");
91         
92         // Check to see if the rule type has been triggered
93
assertTrue(ruleType.rulesTriggered);
94     }
95     
96     public void testOnDeleteNodeTrigger()
97     {
98         NodeRef nodeRef = this.nodeService.createNode(
99                 this.rootNodeRef,
100                 ContentModel.ASSOC_CHILDREN,
101                 ContentModel.ASSOC_CHILDREN,
102                 ContentModel.TYPE_CONTAINER).getChildRef();
103         
104         TestRuleType ruleType = createTestRuleType(ON_DELETE_NODE_TRIGGER);
105         assertFalse(ruleType.rulesTriggered);
106         
107         // Try and trigger the type
108
this.nodeService.deleteNode(nodeRef);
109         
110         // Check to see if the rule type has been triggered
111
assertTrue(ruleType.rulesTriggered);
112     }
113     
114     public void testOnCreateChildAssociationTrigger()
115     {
116         NodeRef nodeRef = this.nodeService.createNode(
117                 this.rootNodeRef,
118                 ContentModel.ASSOC_CHILDREN,
119                 ContentModel.ASSOC_CHILDREN,
120                 ContentModel.TYPE_CONTAINER).getChildRef();
121         NodeRef nodeRef2 = this.nodeService.createNode(
122                 this.rootNodeRef,
123                 ContentModel.ASSOC_CHILDREN,
124                 ContentModel.ASSOC_CHILDREN,
125                 ContentModel.TYPE_CONTAINER).getChildRef();
126         
127         TestRuleType ruleType = createTestRuleType(ON_CREATE_CHILD_ASSOCIATION_TRIGGER);
128         assertFalse(ruleType.rulesTriggered);
129         
130         // Try and trigger the type
131
this.nodeService.addChild(
132                 nodeRef,
133                 nodeRef2,
134                 ContentModel.ASSOC_CHILDREN,
135                 ContentModel.ASSOC_CHILDREN);
136         
137         // Check to see if the rule type has been triggered
138
assertTrue(ruleType.rulesTriggered);
139     }
140     
141     public void testOnDeleteChildAssociationTrigger()
142     {
143         NodeRef nodeRef = this.nodeService.createNode(
144                 this.rootNodeRef,
145                 ContentModel.ASSOC_CHILDREN,
146                 ContentModel.ASSOC_CHILDREN,
147                 ContentModel.TYPE_CONTAINER).getChildRef();
148         NodeRef nodeRef2 = this.nodeService.createNode(
149                 this.rootNodeRef,
150                 ContentModel.ASSOC_CHILDREN,
151                 ContentModel.ASSOC_CHILDREN,
152                 ContentModel.TYPE_CONTAINER).getChildRef();
153         this.nodeService.addChild(
154                 nodeRef,
155                 nodeRef2,
156                 ContentModel.ASSOC_CHILDREN,
157                 ContentModel.ASSOC_CHILDREN);
158         
159         TestRuleType ruleType = createTestRuleType(ON_DELETE_CHILD_ASSOCIATION_TRIGGER);
160         assertFalse(ruleType.rulesTriggered);
161         
162         // Try and trigger the type
163
this.nodeService.removeChild(nodeRef, nodeRef2);
164         
165         // Check to see if the rule type has been triggered
166
assertTrue(ruleType.rulesTriggered);
167     }
168     
169     public void testOnCreateAssociationTrigger()
170     {
171         NodeRef nodeRef = this.nodeService.createNode(
172                 this.rootNodeRef,
173                 ContentModel.ASSOC_CHILDREN,
174                 ContentModel.ASSOC_CHILDREN,
175                 ContentModel.TYPE_CONTAINER).getChildRef();
176         NodeRef nodeRef2 = this.nodeService.createNode(
177                 this.rootNodeRef,
178                 ContentModel.ASSOC_CHILDREN,
179                 ContentModel.ASSOC_CHILDREN,
180                 ContentModel.TYPE_CONTAINER).getChildRef();
181         
182         TestRuleType ruleType = createTestRuleType(ON_CREATE_ASSOCIATION_TRIGGER);
183         assertFalse(ruleType.rulesTriggered);
184         
185         // Try and trigger the type
186
this.nodeService.createAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN);
187         
188         // Check to see if the rule type has been triggered
189
assertTrue(ruleType.rulesTriggered);
190     }
191     
192     public void testOnDeleteAssociationTrigger()
193     {
194         NodeRef nodeRef = this.nodeService.createNode(
195                 this.rootNodeRef,
196                 ContentModel.ASSOC_CHILDREN,
197                 ContentModel.ASSOC_CHILDREN,
198                 ContentModel.TYPE_CONTAINER).getChildRef();
199         NodeRef nodeRef2 = this.nodeService.createNode(
200                 this.rootNodeRef,
201                 ContentModel.ASSOC_CHILDREN,
202                 ContentModel.ASSOC_CHILDREN,
203                 ContentModel.TYPE_CONTAINER).getChildRef();
204         this.nodeService.createAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN);
205         
206         TestRuleType ruleType = createTestRuleType(ON_DELETE_ASSOCIATION_TRIGGER);
207         assertFalse(ruleType.rulesTriggered);
208         
209         // Try and trigger the type
210
this.nodeService.removeAssociation(nodeRef, nodeRef2, ContentModel.ASSOC_CHILDREN);
211         
212         // Check to see if the rule type has been triggered
213
assertTrue(ruleType.rulesTriggered);
214     }
215     
216     public void testOnContentCreateTrigger()
217     {
218         NodeRef nodeRef = this.nodeService.createNode(
219                 this.rootNodeRef,
220                 ContentModel.ASSOC_CHILDREN,
221                 ContentModel.ASSOC_CHILDREN,
222                 ContentModel.TYPE_CONTENT).getChildRef();
223         
224         TestRuleType contentCreate = createTestRuleType(ON_CONTENT_CREATE_TRIGGER);
225         assertFalse(contentCreate.rulesTriggered);
226         
227         // Try and trigger the type
228
ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
229         contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
230         contentWriter.setEncoding("UTF-8");
231         contentWriter.putContent("some content");
232         
233         // Check to see if the rule type has been triggered
234
assertTrue(contentCreate.rulesTriggered);
235     }
236     
237     public void testOnContentUpdateTrigger()
238     {
239         NodeRef nodeRef = this.nodeService.createNode(
240                 this.rootNodeRef,
241                 ContentModel.ASSOC_CHILDREN,
242                 ContentModel.ASSOC_CHILDREN,
243                 ContentModel.TYPE_CONTENT).getChildRef();
244         
245         TestRuleType contentUpdate = createTestRuleType(ON_CONTENT_UPDATE_TRIGGER);
246         assertFalse(contentUpdate.rulesTriggered);
247         
248         // Try and trigger the type
249
ContentWriter contentWriter = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
250         contentWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
251         contentWriter.setEncoding("UTF-8");
252         contentWriter.putContent("some content");
253         
254         // Check to see if the rule type has been triggered
255
assertFalse(contentUpdate.rulesTriggered);
256         
257         // Try and trigger the type
258
ContentWriter contentWriter2 = this.contentService.getWriter(nodeRef, ContentModel.PROP_CONTENT, true);
259         contentWriter2.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
260         contentWriter2.setEncoding("UTF-8");
261         contentWriter2.putContent("more content some content");
262         
263         // Check to see if the rule type has been triggered
264
assertTrue(contentUpdate.rulesTriggered);
265     }
266     
267     private TestRuleType createTestRuleType(String JavaDoc ruleTriggerName)
268     {
269         RuleTrigger ruleTrigger = (RuleTrigger)this.applicationContext.getBean(ruleTriggerName);
270         assertNotNull(ruleTrigger);
271         TestRuleType ruleType = new TestRuleType();
272         ruleTrigger.registerRuleType(ruleType);
273         return ruleType;
274     }
275     
276     private class TestRuleType implements RuleType
277     {
278         public boolean rulesTriggered = false;
279
280         public String JavaDoc getName()
281         {
282             return "testRuleType";
283         }
284
285         public String JavaDoc getDisplayLabel()
286         {
287             return "displayLabel";
288         }
289
290         public void triggerRuleType(NodeRef nodeRef, NodeRef actionedUponNodeRef)
291         {
292             // Indicate that the rules have been triggered
293
this.rulesTriggered = true;
294         }
295     }
296 }
297
Popular Tags