KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > dictionary > DictionaryModelTypeTest


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.dictionary;
18
19 import org.alfresco.model.ContentModel;
20 import org.alfresco.repo.content.MimetypeMap;
21 import org.alfresco.repo.transaction.TransactionUtil;
22 import org.alfresco.service.cmr.coci.CheckOutCheckInService;
23 import org.alfresco.service.cmr.dictionary.DictionaryException;
24 import org.alfresco.service.cmr.dictionary.DictionaryService;
25 import org.alfresco.service.cmr.dictionary.ModelDefinition;
26 import org.alfresco.service.cmr.repository.ContentWriter;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.namespace.NamespaceService;
29 import org.alfresco.service.namespace.QName;
30 import org.alfresco.util.BaseAlfrescoSpringTest;
31 import org.alfresco.util.PropertyMap;
32
33 /**
34  * Dictionary model type unit test
35  *
36  * @author Roy Wetherall
37  */

38 public class DictionaryModelTypeTest extends BaseAlfrescoSpringTest
39 {
40     /** QName of the test model */
41     private static final QName TEST_MODEL_ONE = QName.createQName("{http://www.alfresco.org/test/testmodel1/1.0}testModelOne");
42     
43     /** Test model XML */
44     public static final String JavaDoc MODEL_ONE_XML =
45         "<model name='test1:testModelOne' xmlns='http://www.alfresco.org/model/dictionary/1.0'>" +
46         
47         " <description>Test model one</description>" +
48         " <author>Alfresco</author>" +
49         " <published>2005-05-30</published>" +
50         " <version>1.0</version>" +
51         
52         " <imports>" +
53         " <import uri='http://www.alfresco.org/model/dictionary/1.0' prefix='d'/>" +
54         " </imports>" +
55     
56         " <namespaces>" +
57         " <namespace uri='http://www.alfresco.org/test/testmodel1/1.0' prefix='test1'/>" +
58         " </namespaces>" +
59
60         " <types>" +
61        
62         " <type name='test1:base'>" +
63         " <title>Base</title>" +
64         " <description>The Base Type</description>" +
65         " <properties>" +
66         " <property name='test1:prop1'>" +
67         " <type>d:text</type>" +
68         " </property>" +
69         " </properties>" +
70         " </type>" +
71         
72         " </types>" +
73         
74         "</model>";
75     
76     public static final String JavaDoc MODEL_ONE_MODIFIED_XML =
77         "<model name='test1:testModelOne' xmlns='http://www.alfresco.org/model/dictionary/1.0'>" +
78         
79         " <description>Test model one (updated)</description>" +
80         " <author>Alfresco</author>" +
81         " <published>2005-05-30</published>" +
82         " <version>1.1</version>" +
83         
84         " <imports>" +
85         " <import uri='http://www.alfresco.org/model/dictionary/1.0' prefix='d'/>" +
86         " </imports>" +
87     
88         " <namespaces>" +
89         " <namespace uri='http://www.alfresco.org/test/testmodel1/1.0' prefix='test1'/>" +
90         " </namespaces>" +
91
92         " <types>" +
93        
94         " <type name='test1:base'>" +
95         " <title>Base</title>" +
96         " <description>The Base Type</description>" +
97         " <properties>" +
98         " <property name='test1:prop1'>" +
99         " <type>d:text</type>" +
100         " </property>" +
101         " <property name='test1:prop2'>" +
102         " <type>d:text</type>" +
103         " </property>" +
104         " </properties>" +
105         " </type>" +
106         
107         " </types>" +
108         
109         "</model>";
110
111     /** Services used in tests */
112     private DictionaryService dictionaryService;
113     private NamespaceService namespaceService;
114     private CheckOutCheckInService cociService;
115     private DictionaryDAO dictionaryDAO;
116     
117     /**
118      * On setup in transaction override
119      */

120     @Override JavaDoc
121     protected void onSetUpInTransaction() throws Exception JavaDoc
122     {
123         
124         super.onSetUpInTransaction();
125         
126         // Get the required services
127
this.dictionaryService = (DictionaryService)this.applicationContext.getBean("dictionaryService");
128         this.namespaceService = (NamespaceService)this.applicationContext.getBean("namespaceService");
129         this.cociService = (CheckOutCheckInService)this.applicationContext.getBean("checkOutCheckInService");
130         this.dictionaryDAO = (DictionaryDAO)this.applicationContext.getBean("dictionaryDAO");
131         
132     }
133     
134     /**
135      * Test the creation of dictionary model nodes
136      */

137     public void testCreateAndUpdateDictionaryModelNodeContent()
138     {
139         try
140         {
141             // Check that the model has not yet been loaded into the dictionary
142
this.dictionaryService.getModel(TEST_MODEL_ONE);
143             fail("This model has not yet been loaded into the dictionary service");
144         }
145         catch (DictionaryException exception)
146         {
147             // We expect this exception
148
}
149         
150         // Check that the namespace is not yet in the namespace service
151
String JavaDoc uri = this.namespaceService.getNamespaceURI("test1");
152         assertNull(uri);
153         
154         // Create a model node
155
PropertyMap properties = new PropertyMap(1);
156         properties.put(ContentModel.PROP_MODEL_ACTIVE, true);
157         final NodeRef modelNode = this.nodeService.createNode(
158                 this.rootNodeRef,
159                 ContentModel.ASSOC_CHILDREN,
160                 QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"),
161                 ContentModel.TYPE_DICTIONARY_MODEL,
162                 properties).getChildRef();
163         assertNotNull(modelNode);
164         
165         // Add the model content to the model node
166
ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
167         contentWriter.setEncoding("UTF-8");
168         contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
169         contentWriter.putContent(MODEL_ONE_XML);
170         
171         // End the transaction to force update
172
setComplete();
173         endTransaction();
174         
175         final NodeRef workingCopy = TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<NodeRef>()
176         {
177             public NodeRef doWork() throws Exception JavaDoc
178             {
179                 // Check that the meta data has been extracted from the model
180
assertEquals(QName.createQName("{http://www.alfresco.org/test/testmodel1/1.0}testModelOne"),
181                              DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_NAME));
182                 assertEquals("Test model one", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_DESCRIPTION));
183                 assertEquals("Alfresco", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_AUTHOR));
184                 //System.out.println(this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_PUBLISHED_DATE));
185
assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
186                 
187                 // Check that the model is now available from the dictionary
188
ModelDefinition modelDefinition2 = DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
189                 assertNotNull(modelDefinition2);
190                 assertEquals("Test model one", modelDefinition2.getDescription());
191                 
192                 // Check that the namespace has been added to the namespace service
193
String JavaDoc uri2 = DictionaryModelTypeTest.this.namespaceService.getNamespaceURI("test1");
194                 assertEquals(uri2, "http://www.alfresco.org/test/testmodel1/1.0");
195                 
196                 // Lets check the node out and update the content
197
NodeRef workingCopy = DictionaryModelTypeTest.this.cociService.checkout(modelNode);
198                 ContentWriter contentWriter2 = DictionaryModelTypeTest.this.contentService.getWriter(workingCopy, ContentModel.PROP_CONTENT, true);
199                 contentWriter2.putContent(MODEL_ONE_MODIFIED_XML);
200                 
201                 return workingCopy;
202             }
203         });
204         
205         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
206         {
207             public Object JavaDoc doWork() throws Exception JavaDoc
208             {
209                 // Check that the policy has not been fired since we have updated a working copy
210
assertEquals("1.0", DictionaryModelTypeTest.this.nodeService.getProperty(workingCopy, ContentModel.PROP_MODEL_VERSION));
211                 
212                 // Now check the model changed back in
213
DictionaryModelTypeTest.this.cociService.checkin(workingCopy, null);
214                 return null;
215             }
216         });
217    
218         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
219         {
220             public Object JavaDoc doWork() throws Exception JavaDoc
221             {
222                 // Now check that the model has been updated
223
assertEquals("1.1", DictionaryModelTypeTest.this.nodeService.getProperty(modelNode, ContentModel.PROP_MODEL_VERSION));
224                 return null;
225             }
226         });
227        
228     }
229     
230     public void testIsActiveFlagAndDelete()
231     {
232         this.dictionaryDAO.removeModel(TEST_MODEL_ONE);
233         
234         try
235         {
236             // Check that the model has not yet been loaded into the dictionary
237
this.dictionaryService.getModel(TEST_MODEL_ONE);
238             fail("This model has not yet been loaded into the dictionary service");
239         }
240         catch (DictionaryException exception)
241         {
242             // We expect this exception
243
}
244
245         // Create a model node
246
PropertyMap properties = new PropertyMap(1);
247         final NodeRef modelNode = this.nodeService.createNode(
248                 this.rootNodeRef,
249                 ContentModel.ASSOC_CHILDREN,
250                 QName.createQName(NamespaceService.ALFRESCO_URI, "dictionaryModels"),
251                 ContentModel.TYPE_DICTIONARY_MODEL,
252                 properties).getChildRef();
253         assertNotNull(modelNode);
254         
255         // Add the model content to the model node
256
ContentWriter contentWriter = this.contentService.getWriter(modelNode, ContentModel.PROP_CONTENT, true);
257         contentWriter.setEncoding("UTF-8");
258         contentWriter.setMimetype(MimetypeMap.MIMETYPE_XML);
259         contentWriter.putContent(MODEL_ONE_XML);
260         
261         // End the transaction to force update
262
setComplete();
263         endTransaction();
264         
265         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
266         {
267             public Object JavaDoc doWork() throws Exception JavaDoc
268             {
269                 // The model should not yet be loaded
270
try
271                 {
272                     // Check that the model has not yet been loaded into the dictionary
273
DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
274                     fail("This model has not yet been loaded into the dictionary service");
275                 }
276                 catch (DictionaryException exception)
277                 {
278                     // We expect this exception
279
}
280                 
281                 // Set the isActive flag
282
DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, true);
283                 
284                 return null;
285             }
286         });
287         
288         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
289         {
290             public Object JavaDoc doWork() throws Exception JavaDoc
291             {
292                 // The model should now be loaded
293
assertNotNull(DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE));
294                 
295                 // Set the isActive flag
296
DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, false);
297                 
298                 return null;
299             }
300         });
301         
302         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
303         {
304             public Object JavaDoc doWork() throws Exception JavaDoc
305             {
306                 // The model should not be loaded
307
try
308                 {
309                     // Check that the model has not yet been loaded into the dictionary
310
DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
311                     fail("This model has not yet been loaded into the dictionary service");
312                 }
313                 catch (DictionaryException exception)
314                 {
315                     // We expect this exception
316
}
317                 
318                 // Set the isActive flag
319
DictionaryModelTypeTest.this.nodeService.setProperty(modelNode, ContentModel.PROP_MODEL_ACTIVE, true);
320                 
321                 return null;
322             }
323         });
324         
325         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
326         {
327             public Object JavaDoc doWork() throws Exception JavaDoc
328             {
329                 // The model should now be loaded
330
assertNotNull(DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE));
331                 
332                 DictionaryModelTypeTest.this.nodeService.deleteNode(modelNode);
333                 
334                 // The model should not be loaded
335
try
336                 {
337                     // Check that the model has not yet been loaded into the dictionary
338
DictionaryModelTypeTest.this.dictionaryService.getModel(TEST_MODEL_ONE);
339                     fail("This model has not yet been loaded into the dictionary service");
340                 }
341                 catch (DictionaryException exception)
342                 {
343                     // We expect this exception
344
}
345                 
346                 return null;
347             }
348         });
349     }
350 }
351
Popular Tags