KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.alfresco.repo.dictionary;
2
3 import java.text.MessageFormat JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.alfresco.model.ContentModel;
8 import org.alfresco.repo.content.MimetypeMap;
9 import org.alfresco.repo.dictionary.DictionaryRepositoryBootstrap.RepositoryLocation;
10 import org.alfresco.repo.policy.BehaviourFilter;
11 import org.alfresco.repo.security.authentication.AuthenticationComponent;
12 import org.alfresco.service.cmr.dictionary.DictionaryException;
13 import org.alfresco.service.cmr.dictionary.ModelDefinition;
14 import org.alfresco.service.cmr.repository.ContentWriter;
15 import org.alfresco.service.cmr.repository.NodeRef;
16 import org.alfresco.service.cmr.search.SearchService;
17 import org.alfresco.service.namespace.QName;
18 import org.alfresco.service.transaction.TransactionService;
19 import org.alfresco.util.BaseAlfrescoSpringTest;
20
21 public class DictionaryRepositoryBootstrapTest extends BaseAlfrescoSpringTest
22 {
23     public static final String JavaDoc TEMPLATE_MODEL_XML =
24         "<model name={0} xmlns=\"http://www.alfresco.org/model/dictionary/1.0\">" +
25         
26         " <description>{1}</description>" +
27         " <author>Alfresco</author>" +
28         " <published>2005-05-30</published>" +
29         " <version>1.0</version>" +
30         
31         " <imports>" +
32         " <import uri=\"http://www.alfresco.org/model/dictionary/1.0\" prefix=\"d\"/>" +
33         " {2} " +
34         " </imports>" +
35     
36         " <namespaces>" +
37         " <namespace uri={3} prefix={4}/>" +
38         " </namespaces>" +
39
40         " <types>" +
41        
42         " <type name={5}>" +
43         " <title>Base</title>" +
44         " <description>The Base Type</description>" +
45         " <properties>" +
46         " <property name={6}>" +
47         " <type>d:text</type>" +
48         " </property>" +
49         " </properties>" +
50         " </type>" +
51         
52         " </types>" +
53         
54         "</model>";
55     
56     /** Behaviour filter */
57     private BehaviourFilter behaviourFilter;
58
59     /** The bootstrap service */
60     private DictionaryRepositoryBootstrap bootstrap;
61
62     /** The search service */
63     private SearchService searchService;
64
65     /** The dictionary DAO */
66     private DictionaryDAO dictionaryDAO;
67
68     /** The transaction service */
69     private TransactionService transactionService;
70
71     /** The authentication service */
72     private AuthenticationComponent authenticationComponent;
73
74     /**
75      * @see org.springframework.test.AbstractTransactionalSpringContextTests#onSetUpInTransaction()
76      */

77     @Override JavaDoc
78     protected void onSetUpInTransaction() throws Exception JavaDoc
79     {
80         super.onSetUpInTransaction();
81         
82         // Get the behaviour filter and turn the behaviour off for the model type
83
this.behaviourFilter = (BehaviourFilter)this.applicationContext.getBean("policyBehaviourFilter");
84         this.behaviourFilter.disableBehaviour(ContentModel.TYPE_DICTIONARY_MODEL);
85         
86         this.searchService = (SearchService)this.applicationContext.getBean("searchService");
87         this.dictionaryDAO = (DictionaryDAO)this.applicationContext.getBean("dictionaryDAO");
88         this.transactionService = (TransactionService)this.applicationContext.getBean("transactionComponent");
89         this.authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
90         
91         this.bootstrap = new DictionaryRepositoryBootstrap();
92         this.bootstrap.setContentService(this.contentService);
93         this.bootstrap.setSearchService(this.searchService);
94         this.bootstrap.setDictionaryDAO(this.dictionaryDAO);
95         this.bootstrap.setAuthenticationComponent(this.authenticationComponent);
96         this.bootstrap.setTransactionService(this.transactionService);
97         
98         RepositoryLocation location = this.bootstrap.new RepositoryLocation();
99         location.setStoreProtocol(this.storeRef.getProtocol());
100         location.setStoreId(this.storeRef.getIdentifier());
101         // NOTE: we are not setting the path for now .. in doing so we are searching the whole dictionary
102

103         List JavaDoc<RepositoryLocation> locations = new ArrayList JavaDoc<RepositoryLocation>();
104         locations.add(location);
105         this.bootstrap.setRepositoryLocations(locations);
106     }
107     
108     /**
109      * Test bootstrap
110      */

111     public void testBootstrap()
112     {
113         createModelNode(
114                 "http://www.alfresco.org/model/test2DictionaryBootstrapFromRepo/1.0",
115                 "test2",
116                 "testModel2",
117                 " <import uri=\"http://www.alfresco.org/model/test1DictionaryBootstrapFromRepo/1.0\" prefix=\"test1\"/> ",
118                 "Test model two",
119                 "base2",
120                 "prop2");
121         createModelNode(
122                 "http://www.alfresco.org/model/test3DictionaryBootstrapFromRepo/1.0",
123                 "test3",
124                 "testModel3",
125                 " <import uri=\"http://www.alfresco.org/model/test1DictionaryBootstrapFromRepo/1.0\" prefix=\"test1\"/> ",
126                 "Test model three",
127                 "base3",
128                 "prop3");
129         createModelNode(
130                 "http://www.alfresco.org/model/test1DictionaryBootstrapFromRepo/1.0",
131                 "test1",
132                 "testModel1",
133                 "",
134                 "Test model one",
135                 "base1",
136                 "prop1");
137                
138         // Check that the model is not in the dictionary yet
139
try
140         {
141             this.dictionaryDAO.getModel(
142                     QName.createQName("http://www.alfresco.org/model/test1DictionaryBootstrapFromRepo/1.0", "testModel1"));
143             fail("The model should not be there.");
144         }
145         catch (DictionaryException exception)
146         {
147             // Ignore since we where expecting this
148
}
149         
150         // Now do the bootstrap
151
this.bootstrap.bootstrap();
152         
153         // Check that the model is now there
154
ModelDefinition modelDefinition1 = this.dictionaryDAO.getModel(
155                 QName.createQName("http://www.alfresco.org/model/test1DictionaryBootstrapFromRepo/1.0", "testModel1"));
156         assertNotNull(modelDefinition1);
157         ModelDefinition modelDefinition2 = this.dictionaryDAO.getModel(
158                 QName.createQName("http://www.alfresco.org/model/test2DictionaryBootstrapFromRepo/1.0", "testModel2"));
159         assertNotNull(modelDefinition2);
160         ModelDefinition modelDefinition3 = this.dictionaryDAO.getModel(
161                 QName.createQName("http://www.alfresco.org/model/test3DictionaryBootstrapFromRepo/1.0", "testModel3"));
162         assertNotNull(modelDefinition3);
163     }
164
165     /**
166      * Create model node
167      *
168      * @param uri
169      * @param prefix
170      * @param modelLocalName
171      * @param importStatement
172      * @param description
173      * @param typeName
174      * @param propertyName
175      * @return
176      */

177     private NodeRef createModelNode(
178             String JavaDoc uri,
179             String JavaDoc prefix,
180             String JavaDoc modelLocalName,
181             String JavaDoc importStatement,
182             String JavaDoc description,
183             String JavaDoc typeName,
184             String JavaDoc propertyName)
185     {
186         // Create a model node
187
NodeRef model = this.nodeService.createNode(
188                 this.rootNodeRef,
189                 ContentModel.ASSOC_CHILDREN,
190                 QName.createQName("{test}models"),
191                 ContentModel.TYPE_DICTIONARY_MODEL).getChildRef();
192         ContentWriter contentWriter1 = this.contentService.getWriter(model, ContentModel.PROP_CONTENT, true);
193         contentWriter1.setEncoding("UTF-8");
194         contentWriter1.setMimetype(MimetypeMap.MIMETYPE_XML);
195         String JavaDoc modelOne = getModelString(
196                     uri,
197                     prefix,
198                     modelLocalName,
199                     importStatement,
200                     description,
201                     typeName,
202                     propertyName);
203         contentWriter1.putContent(modelOne);
204         
205         return model;
206     }
207     
208     /**
209      *
210      * Gets the model string
211      *
212      * @param uri
213      * @param prefix
214      * @param modelLocalName
215      * @param importStatement
216      * @param description
217      * @param typeName
218      * @param propertyName
219      * @return
220      */

221     private String JavaDoc getModelString(
222             String JavaDoc uri,
223             String JavaDoc prefix,
224             String JavaDoc modelLocalName,
225             String JavaDoc importStatement,
226             String JavaDoc description,
227             String JavaDoc typeName,
228             String JavaDoc propertyName)
229     {
230         return MessageFormat.format(
231                 TEMPLATE_MODEL_XML,
232                 new Object JavaDoc[]{
233                         "'" + prefix +":" + modelLocalName + "'",
234                         description,
235                         importStatement,
236                         "'" + uri + "'",
237                         "'" + prefix + "'",
238                         "'" + prefix + ":" + typeName + "'",
239                         "'" + prefix + ":" + propertyName + "'"});
240     }
241 }
242
Popular Tags