1 package org.alfresco.repo.dictionary; 2 3 import java.text.MessageFormat ; 4 import java.util.ArrayList ; 5 import java.util.List ; 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 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 57 private BehaviourFilter behaviourFilter; 58 59 60 private DictionaryRepositoryBootstrap bootstrap; 61 62 63 private SearchService searchService; 64 65 66 private DictionaryDAO dictionaryDAO; 67 68 69 private TransactionService transactionService; 70 71 72 private AuthenticationComponent authenticationComponent; 73 74 77 @Override 78 protected void onSetUpInTransaction() throws Exception 79 { 80 super.onSetUpInTransaction(); 81 82 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 103 List <RepositoryLocation> locations = new ArrayList <RepositoryLocation>(); 104 locations.add(location); 105 this.bootstrap.setRepositoryLocations(locations); 106 } 107 108 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 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 } 149 150 this.bootstrap.bootstrap(); 152 153 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 177 private NodeRef createModelNode( 178 String uri, 179 String prefix, 180 String modelLocalName, 181 String importStatement, 182 String description, 183 String typeName, 184 String propertyName) 185 { 186 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 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 221 private String getModelString( 222 String uri, 223 String prefix, 224 String modelLocalName, 225 String importStatement, 226 String description, 227 String typeName, 228 String propertyName) 229 { 230 return MessageFormat.format( 231 TEMPLATE_MODEL_XML, 232 new Object []{ 233 "'" + prefix +":" + modelLocalName + "'", 234 description, 235 importStatement, 236 "'" + uri + "'", 237 "'" + prefix + "'", 238 "'" + prefix + ":" + typeName + "'", 239 "'" + prefix + ":" + propertyName + "'"}); 240 } 241 } 242 | Popular Tags |