1 17 package org.alfresco.repo.node.integrity; 18 19 import java.io.InputStream ; 20 21 import javax.transaction.UserTransaction ; 22 23 import junit.framework.TestCase; 24 25 import org.alfresco.model.ContentModel; 26 import org.alfresco.repo.dictionary.DictionaryDAO; 27 import org.alfresco.repo.dictionary.M2Model; 28 import org.alfresco.repo.node.BaseNodeServiceTest; 29 import org.alfresco.repo.security.authentication.AuthenticationComponent; 30 import org.alfresco.service.ServiceRegistry; 31 import org.alfresco.service.cmr.repository.NodeRef; 32 import org.alfresco.service.cmr.repository.NodeService; 33 import org.alfresco.service.cmr.repository.StoreRef; 34 import org.alfresco.service.namespace.QName; 35 import org.alfresco.service.transaction.TransactionService; 36 import org.alfresco.util.ApplicationContextHelper; 37 import org.alfresco.util.PropertyMap; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.springframework.context.ApplicationContext; 41 42 52 public class IntegrityTest extends TestCase 53 { 54 private static Log logger = LogFactory.getLog(IntegrityTest.class); 55 56 public static final String NAMESPACE = "http://www.alfresco.org/test/IntegrityTest"; 57 public static final String TEST_PREFIX = "test"; 58 59 public static final QName TEST_TYPE_WITHOUT_ANYTHING = QName.createQName(NAMESPACE, "typeWithoutAnything"); 60 public static final QName TEST_TYPE_WITH_ASPECT = QName.createQName(NAMESPACE, "typeWithAspect"); 61 public static final QName TEST_TYPE_WITH_PROPERTIES = QName.createQName(NAMESPACE, "typeWithProperties"); 62 public static final QName TEST_TYPE_WITH_ASSOCS = QName.createQName(NAMESPACE, "typeWithAssocs"); 63 public static final QName TEST_TYPE_WITH_CHILD_ASSOCS = QName.createQName(NAMESPACE, "typeWithChildAssocs"); 64 65 public static final QName TEST_ASSOC_NODE_ZEROMANY_ZEROMANY = QName.createQName(NAMESPACE, "assoc-0to* - 0to*"); 66 public static final QName TEST_ASSOC_CHILD_ZEROMANY_ZEROMANY = QName.createQName(NAMESPACE, "child-0to* - 0to*"); 67 public static final QName TEST_ASSOC_NODE_ONE_ONE = QName.createQName(NAMESPACE, "assoc-1to1 - 1to1"); 68 public static final QName TEST_ASSOC_CHILD_ONE_ONE = QName.createQName(NAMESPACE, "child-1to1 - 1to1"); 69 public static final QName TEST_ASSOC_ASPECT_ONE_ONE = QName.createQName(NAMESPACE, "aspect-assoc-1to1 - 1to1"); 70 71 public static final QName TEST_ASPECT_WITH_PROPERTIES = QName.createQName(NAMESPACE, "aspectWithProperties"); 72 public static final QName TEST_ASPECT_WITH_ASSOC = QName.createQName(NAMESPACE, "aspectWithAssoc"); 73 74 public static final QName TEST_PROP_TEXT_A = QName.createQName(NAMESPACE, "prop-text-a"); 75 public static final QName TEST_PROP_TEXT_B = QName.createQName(NAMESPACE, "prop-text-b"); 76 public static final QName TEST_PROP_INT_A = QName.createQName(NAMESPACE, "prop-int-a"); 77 public static final QName TEST_PROP_INT_B = QName.createQName(NAMESPACE, "prop-int-b"); 78 79 private static ApplicationContext ctx; 80 static 81 { 82 ctx = ApplicationContextHelper.getApplicationContext(); 83 } 84 85 private IntegrityChecker integrityChecker; 86 private ServiceRegistry serviceRegistry; 87 private NodeService nodeService; 88 private NodeRef rootNodeRef; 89 private PropertyMap allProperties; 90 private UserTransaction txn; 91 private AuthenticationComponent authenticationComponent; 92 93 public void setUp() throws Exception 94 { 95 DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO"); 96 ClassLoader cl = BaseNodeServiceTest.class.getClassLoader(); 97 InputStream modelStream = cl.getResourceAsStream("org/alfresco/repo/node/integrity/IntegrityTest_model.xml"); 99 assertNotNull(modelStream); 100 M2Model model = M2Model.createModel(modelStream); 101 dictionaryDao.putModel(model); 102 103 integrityChecker = (IntegrityChecker) ctx.getBean("integrityChecker"); 104 integrityChecker.setEnabled(true); 105 integrityChecker.setFailOnViolation(true); 106 integrityChecker.setTraceOn(true); 107 integrityChecker.setMaxErrorsPerTransaction(100); 109 serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY); 110 nodeService = serviceRegistry.getNodeService(); 111 this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent"); 112 113 this.authenticationComponent.setSystemUserAsCurrentUser(); 114 115 TransactionService transactionService = serviceRegistry.getTransactionService(); 117 txn = transactionService.getUserTransaction(); 118 txn.begin(); 119 StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, getName()); 120 if (!nodeService.exists(storeRef)) 121 { 122 nodeService.createStore(storeRef.getProtocol(), storeRef.getIdentifier()); 123 } 124 rootNodeRef = nodeService.getRootNode(storeRef); 125 126 allProperties = new PropertyMap(); 127 allProperties.put(TEST_PROP_TEXT_A, "ABC"); 128 allProperties.put(TEST_PROP_TEXT_B, "DEF"); 129 allProperties.put(TEST_PROP_INT_A, "123"); 130 allProperties.put(TEST_PROP_INT_B, "456"); 131 } 132 133 public void tearDown() throws Exception 134 { 135 authenticationComponent.clearCurrentSecurityContext(); 136 txn.rollback(); 137 } 138 139 142 private NodeRef createNode(String name, QName type, PropertyMap properties) 143 { 144 return nodeService.createNode( 145 rootNodeRef, 146 ContentModel.ASSOC_CHILDREN, 147 QName.createQName(NAMESPACE, name), 148 type, 149 properties 150 ).getChildRef(); 151 } 152 153 private void checkIntegrityNoFailure() throws Exception 154 { 155 integrityChecker.checkIntegrity(); 156 } 157 158 163 private void checkIntegrityExpectFailure(String failureMsg, int expectedCount) 164 { 165 try 166 { 167 integrityChecker.checkIntegrity(); 168 fail(failureMsg); 169 } 170 catch (IntegrityException e) 171 { 172 if (expectedCount >= 0) 173 { 174 assertEquals("Incorrect number of integrity records generated", expectedCount, e.getRecords().size()); 175 } 176 } 177 } 178 179 public void testSetUp() throws Exception 180 { 181 assertNotNull("Static IntegrityChecker not created", integrityChecker); 182 } 183 184 public void testCreateWithoutProperties() throws Exception 185 { 186 NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_PROPERTIES, null); 187 checkIntegrityExpectFailure("Failed to detect missing properties", 1); 188 } 189 190 public void testCreateWithProperties() throws Exception 191 { 192 NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_PROPERTIES, allProperties); 193 checkIntegrityNoFailure(); 194 } 195 196 public void testMandatoryPropertiesRemoved() throws Exception 197 { 198 NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_PROPERTIES, allProperties); 199 200 PropertyMap properties = new PropertyMap(); 202 nodeService.setProperties(nodeRef, properties); 203 204 checkIntegrityExpectFailure("Failed to detect missing removed properties", 1); 205 } 206 207 public void testCreateWithoutPropertiesForAspect() throws Exception 208 { 209 NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_ASPECT, null); 210 211 checkIntegrityExpectFailure("Failed to detect missing properties for aspect", 1); 212 } 213 214 public void testCreateWithPropertiesForAspect() throws Exception 215 { 216 NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_ASPECT, allProperties); 217 checkIntegrityNoFailure(); 218 } 219 220 public void testCreateTargetOfAssocsWithMandatorySourcesPresent() throws Exception 221 { 222 NodeRef targetAndChild = createNode("targetAndChild", TEST_TYPE_WITHOUT_ANYTHING, null); 224 225 NodeRef source = createNode("source", TEST_TYPE_WITH_ASSOCS, null); 226 nodeService.createAssociation(source, targetAndChild, TEST_ASSOC_NODE_ONE_ONE); 227 228 NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null); 229 nodeService.addChild(parent, targetAndChild, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "mandatoryChild")); 230 231 NodeRef aspected = createNode("aspectNode", TEST_TYPE_WITHOUT_ANYTHING, null); 232 nodeService.addAspect(aspected, TEST_ASPECT_WITH_ASSOC, null); 233 nodeService.createAssociation(aspected, targetAndChild, TEST_ASSOC_ASPECT_ONE_ONE); 234 235 checkIntegrityNoFailure(); 236 } 237 238 244 public void testCreateTargetOfAssocsWithMandatorySourcesMissing() throws Exception 245 { 246 logger.error("Method commented out: testCreateTargetOfAssocsWithMandatorySourcesMissing"); 251 } 252 253 258 public void testRemoveSourcesOfMandatoryAssocs() throws Exception 259 { 260 logger.error("Method commented out: testRemoveSourcesOfMandatoryAssocs"); 282 } 283 284 public void testDuplicateTargetAssocs() throws Exception 285 { 286 NodeRef parent = createNode("source", TEST_TYPE_WITH_CHILD_ASSOCS, null); 287 NodeRef child1 = createNode("child1", TEST_TYPE_WITHOUT_ANYTHING, null); 288 NodeRef child2 = createNode("child2", TEST_TYPE_WITHOUT_ANYTHING, null); 289 NodeRef child3 = createNode("child3", TEST_TYPE_WITHOUT_ANYTHING, null); 290 291 nodeService.addChild(parent, child3, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "mandatoryChild")); 293 294 nodeService.addChild(parent, child1, TEST_ASSOC_CHILD_ZEROMANY_ZEROMANY, QName.createQName(NAMESPACE, "dupli_cate")); 296 nodeService.addChild(parent, child2, TEST_ASSOC_CHILD_ZEROMANY_ZEROMANY, QName.createQName(NAMESPACE, "dupli_cate")); 297 298 checkIntegrityExpectFailure("Failed to detect duplicate association names", 1); 299 } 300 301 public void testCreateSourceOfAssocsWithMandatoryTargetsPresent() throws Exception 302 { 303 NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null); 304 NodeRef target = createNode("target", TEST_TYPE_WITHOUT_ANYTHING, null); 305 nodeService.createAssociation(source, target, TEST_ASSOC_NODE_ONE_ONE); 306 307 NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null); 308 NodeRef child = createNode("child", TEST_TYPE_WITHOUT_ANYTHING, null); 309 nodeService.addChild(parent, child, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one")); 310 311 NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null); 312 nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null); 313 NodeRef aspectTarget = createNode("aspectTarget", TEST_TYPE_WITHOUT_ANYTHING, null); 314 nodeService.createAssociation(aspectSource, aspectTarget, TEST_ASSOC_ASPECT_ONE_ONE); 315 316 checkIntegrityNoFailure(); 317 } 318 319 public void testCreateSourceOfAssocsWithMandatoryTargetsMissing() throws Exception 320 { 321 NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null); 322 323 NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null); 324 325 NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null); 326 nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null); 327 328 checkIntegrityExpectFailure("Failed to detect missing assoc targets", 3); 329 } 330 331 336 public void testRemoveTargetsOfMandatoryAssocs() throws Exception 337 { 338 logger.error("Method commented out: testRemoveTargetsOfMandatoryAssocs"); 360 } 361 362 public void testExcessTargetsOfOneToOneAssocs() throws Exception 363 { 364 NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null); 365 NodeRef target1 = createNode("target1", TEST_TYPE_WITHOUT_ANYTHING, null); 366 NodeRef target2 = createNode("target2", TEST_TYPE_WITHOUT_ANYTHING, null); 367 nodeService.createAssociation(source, target1, TEST_ASSOC_NODE_ONE_ONE); 368 nodeService.createAssociation(source, target2, TEST_ASSOC_NODE_ONE_ONE); 369 370 NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null); 371 NodeRef child1 = createNode("child1", TEST_TYPE_WITHOUT_ANYTHING, null); 372 NodeRef child2 = createNode("child2", TEST_TYPE_WITHOUT_ANYTHING, null); 373 nodeService.addChild(parent, child1, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one-first")); 374 nodeService.addChild(parent, child2, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one-second")); 375 376 NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null); 377 nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null); 378 NodeRef aspectTarget1 = createNode("aspectTarget1", TEST_TYPE_WITHOUT_ANYTHING, null); 379 NodeRef aspectTarget2 = createNode("aspectTarget2", TEST_TYPE_WITHOUT_ANYTHING, null); 380 nodeService.createAssociation(aspectSource, aspectTarget1, TEST_ASSOC_ASPECT_ONE_ONE); 381 nodeService.createAssociation(aspectSource, aspectTarget2, TEST_ASSOC_ASPECT_ONE_ONE); 382 383 checkIntegrityExpectFailure("Failed to detect excess target cardinality for one-to-one assocs", 3); 384 } 385 } 386 | Popular Tags |