KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > integrity > IntegrityTest


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.node.integrity;
18
19 import java.io.InputStream JavaDoc;
20
21 import javax.transaction.UserTransaction JavaDoc;
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 /**
43  * Attempts to build faulty node structures in order to test integrity.
44  * <p>
45  * The entire application context is loaded as is, but the integrity fail-
46  * mode is set to throw an exception.
47  *
48  * TODO: Role name restrictions must be checked
49  *
50  * @author Derek Hulley
51  */

52 public class IntegrityTest extends TestCase
53 {
54     private static Log logger = LogFactory.getLog(IntegrityTest.class);
55     
56     public static final String JavaDoc NAMESPACE = "http://www.alfresco.org/test/IntegrityTest";
57     public static final String JavaDoc 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 JavaDoc txn;
91     private AuthenticationComponent authenticationComponent;
92     
93     public void setUp() throws Exception JavaDoc
94     {
95         DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
96         ClassLoader JavaDoc cl = BaseNodeServiceTest.class.getClassLoader();
97         // load the test model
98
InputStream JavaDoc 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); // we want to count the correct number of errors
108

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         // begin a transaction
116
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 JavaDoc
134     {
135         authenticationComponent.clearCurrentSecurityContext();
136         txn.rollback();
137     }
138
139     /**
140      * Create a node of the given type, and hanging off the root node
141      */

142     private NodeRef createNode(String JavaDoc 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 JavaDoc
154     {
155         integrityChecker.checkIntegrity();
156     }
157     
158     /**
159      *
160      * @param failureMsg the fail message if an integrity exception doesn't occur
161      * @param expectedCount the expected number of integrity failures, or -1 to ignore
162      */

163     private void checkIntegrityExpectFailure(String JavaDoc 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 JavaDoc
180     {
181         assertNotNull("Static IntegrityChecker not created", integrityChecker);
182     }
183     
184     public void testCreateWithoutProperties() throws Exception JavaDoc
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 JavaDoc
191     {
192         NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_PROPERTIES, allProperties);
193         checkIntegrityNoFailure();
194     }
195     
196     public void testMandatoryPropertiesRemoved() throws Exception JavaDoc
197     {
198         NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_PROPERTIES, allProperties);
199         
200         // remove all the properties
201
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 JavaDoc
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 JavaDoc
215     {
216         NodeRef nodeRef = createNode("abc", TEST_TYPE_WITH_ASPECT, allProperties);
217         checkIntegrityNoFailure();
218     }
219
220     public void testCreateTargetOfAssocsWithMandatorySourcesPresent() throws Exception JavaDoc
221     {
222         // this is the target of 3 assoc types where the source cardinality is 1..1
223
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     /**
239      * TODO: The dictionary support for the reverse lookup of mandatory associations will
240      * allow this method to go in
241      * <p>
242      * <b>Does nothing</b>.
243      */

244     public void testCreateTargetOfAssocsWithMandatorySourcesMissing() throws Exception JavaDoc
245     {
246 // // this is the target of 3 associations where the source cardinality is 1..1
247
// NodeRef target = createNode("abc", TEST_TYPE_WITHOUT_ANYTHING, null);
248
//
249
// checkIntegrityExpectFailure("Failed to detect missing mandatory assoc sources", 3);
250
logger.error("Method commented out: testCreateTargetOfAssocsWithMandatorySourcesMissing");
251     }
252
253     /**
254      * TODO: Reactivate once cascade delete notifications are back on
255      * <p>
256      * <b>Does nothing</b>.
257      */

258     public void testRemoveSourcesOfMandatoryAssocs() throws Exception JavaDoc
259     {
260 // // this is the target of 3 assoc types where the source cardinality is 1..1
261
// NodeRef targetAndChild = createNode("targetAndChild", TEST_TYPE_WITHOUT_ANYTHING, null);
262
//
263
// NodeRef source = createNode("source", TEST_TYPE_WITH_ASSOCS, null);
264
// nodeService.createAssociation(source, targetAndChild, TEST_ASSOC_NODE_ONE_ONE);
265
//
266
// NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
267
// nodeService.addChild(parent, targetAndChild, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "mandatoryChild"));
268
//
269
// NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
270
// nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
271
// nodeService.createAssociation(aspectSource, targetAndChild, TEST_ASSOC_ASPECT_ONE_ONE);
272
//
273
// checkIntegrityNoFailure();
274
//
275
// // remove source nodes
276
// nodeService.deleteNode(source);
277
// nodeService.deleteNode(parent);
278
// nodeService.deleteNode(aspectSource);
279
//
280
// checkIntegrityExpectFailure("Failed to detect removal of mandatory assoc sources", 3);
281
logger.error("Method commented out: testRemoveSourcesOfMandatoryAssocs");
282     }
283     
284     public void testDuplicateTargetAssocs() throws Exception JavaDoc
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         // satisfy the one-to-one
292
nodeService.addChild(parent, child3, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "mandatoryChild"));
293         
294         // create the non-duplicate assocs
295
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 JavaDoc
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 JavaDoc
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     /**
332      * TODO: Reactivate once cascade delete notifications are back on
333      * <p>
334      * <b>Does nothing</b>.
335      */

336     public void testRemoveTargetsOfMandatoryAssocs() throws Exception JavaDoc
337     {
338 // NodeRef source = createNode("abc", TEST_TYPE_WITH_ASSOCS, null);
339
// NodeRef target = createNode("target", TEST_TYPE_WITHOUT_ANYTHING, null);
340
// nodeService.createAssociation(source, target, TEST_ASSOC_NODE_ONE_ONE);
341
//
342
// NodeRef parent = createNode("parent", TEST_TYPE_WITH_CHILD_ASSOCS, null);
343
// NodeRef child = createNode("child", TEST_TYPE_WITHOUT_ANYTHING, null);
344
// nodeService.addChild(parent, child, TEST_ASSOC_CHILD_ONE_ONE, QName.createQName(NAMESPACE, "one-to-one"));
345
//
346
// NodeRef aspectSource = createNode("aspectSource", TEST_TYPE_WITHOUT_ANYTHING, null);
347
// nodeService.addAspect(aspectSource, TEST_ASPECT_WITH_ASSOC, null);
348
// NodeRef aspectTarget = createNode("aspectTarget", TEST_TYPE_WITHOUT_ANYTHING, null);
349
// nodeService.createAssociation(aspectSource, aspectTarget, TEST_ASSOC_ASPECT_ONE_ONE);
350
//
351
// checkIntegrityNoFailure();
352
//
353
// // remove target nodes
354
// nodeService.deleteNode(target);
355
// nodeService.deleteNode(child);
356
// nodeService.deleteNode(aspectTarget);
357
//
358
// checkIntegrityExpectFailure("Failed to detect removal of mandatory assoc targets", 3);
359
logger.error("Method commented out: testRemoveTargetsOfMandatoryAssocs");
360     }
361
362     public void testExcessTargetsOfOneToOneAssocs() throws Exception JavaDoc
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