KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > domain > hibernate > HibernateNodeTest


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.domain.hibernate;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import javax.transaction.UserTransaction JavaDoc;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.repo.domain.ChildAssoc;
31 import org.alfresco.repo.domain.Node;
32 import org.alfresco.repo.domain.NodeAssoc;
33 import org.alfresco.repo.domain.NodeKey;
34 import org.alfresco.repo.domain.NodeStatus;
35 import org.alfresco.repo.domain.PropertyValue;
36 import org.alfresco.repo.domain.Store;
37 import org.alfresco.repo.domain.StoreKey;
38 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
39 import org.alfresco.service.cmr.repository.StoreRef;
40 import org.alfresco.service.namespace.QName;
41 import org.alfresco.service.transaction.TransactionService;
42 import org.alfresco.util.BaseSpringTest;
43 import org.alfresco.util.GUID;
44
45 /**
46  * Test persistence and retrieval of Hibernate-specific implementations of the
47  * {@link org.alfresco.repo.domain.Node} interface
48  *
49  * @author Derek Hulley
50  */

51 public class HibernateNodeTest extends BaseSpringTest
52 {
53     private static final String JavaDoc TEST_NAMESPACE = "http://www.alfresco.org/test/HibernateNodeTest";
54     
55     private Store store;
56     
57     public HibernateNodeTest()
58     {
59     }
60     
61     protected void onSetUpInTransaction() throws Exception JavaDoc
62     {
63         store = new StoreImpl();
64         StoreKey storeKey = new StoreKey(StoreRef.PROTOCOL_WORKSPACE,
65                 "TestWorkspace@" + System.currentTimeMillis());
66         store.setKey(storeKey);
67         // persist so that it is present in the hibernate cache
68
getSession().save(store);
69     }
70     
71     protected void onTearDownInTransaction()
72     {
73         // force a flush to ensure that the database updates succeed
74
getSession().flush();
75         getSession().clear();
76     }
77
78     public void testSetUp() throws Exception JavaDoc
79     {
80         assertNotNull("Workspace not initialised", store);
81     }
82     
83     public void testGetStore() throws Exception JavaDoc
84     {
85         NodeKey key = new NodeKey("Random Protocol", "Random Identifier", "AAA");
86         // create the node status
87
NodeStatus nodeStatus = new NodeStatusImpl();
88         nodeStatus.setKey(key);
89         nodeStatus.setDeleted(false);
90         nodeStatus.setChangeTxnId("txn:123");
91         getSession().save(nodeStatus);
92         // create a new Node
93
Node node = new NodeImpl();
94         node.setKey(key);
95         node.setStore(store); // not meaningful as it contradicts the key
96
node.setTypeQName(ContentModel.TYPE_CONTAINER);
97         node.setStatus(nodeStatus);
98         // persist it
99
try
100         {
101             Serializable JavaDoc id = getSession().save(node);
102             fail("No store exists");
103         }
104         catch (Throwable JavaDoc e)
105         {
106             // expected
107
}
108         // this should not solve the problem
109
node.setStore(store);
110         // persist it
111
try
112         {
113             Serializable JavaDoc id = getSession().save(node);
114             fail("Setting store does not persist protocol and identifier attributes");
115         }
116         catch (Throwable JavaDoc e)
117         {
118             // expected
119
}
120         
121         // fix the key
122
key = new NodeKey(store.getKey().getProtocol(), store.getKey().getIdentifier(), "AAA");
123         node.setKey(key);
124         // now it should work
125
Serializable JavaDoc id = getSession().save(node);
126
127         // throw the reference away and get the a new one for the id
128
node = (Node) getSession().load(NodeImpl.class, id);
129         assertNotNull("Node not found", node);
130         // check that the store has been loaded
131
Store loadedStore = node.getStore();
132         assertNotNull("Store not present on node", loadedStore);
133         assertEquals("Incorrect store key", store, loadedStore);
134     }
135     
136     public void testNodeStatus()
137     {
138         NodeKey key = new NodeKey(store.getKey(), "AAA");
139         // create the node status
140
NodeStatus nodeStatus = new NodeStatusImpl();
141         nodeStatus.setKey(key);
142         nodeStatus.setDeleted(false);
143         nodeStatus.setChangeTxnId("txn:123");
144         getSession().save(nodeStatus);
145         
146         // it must be able to exist without the node
147
flushAndClear();
148         
149         // create a new Node
150
Node node = new NodeImpl();
151         node.setStore(store);
152         node.setKey(key);
153         node.setStore(store); // not meaningful as it contradicts the key
154
node.setTypeQName(ContentModel.TYPE_CONTAINER);
155         node.setStatus(nodeStatus);
156         Serializable JavaDoc id = getSession().save(node);
157         
158         // flush
159
flushAndClear();
160         
161         // is the status retrievable
162
node = (Node) getSession().get(NodeImpl.class, id);
163         nodeStatus = node.getStatus();
164         // change the values
165
nodeStatus.setChangeTxnId("txn:456");
166         nodeStatus.setDeleted(true);
167         // delete the node
168
getSession().delete(node);
169         
170         // flush
171
flushAndClear();
172     }
173
174     /**
175      * Check that properties can be persisted and retrieved
176      */

177     public void testProperties() throws Exception JavaDoc
178     {
179         NodeKey key = new NodeKey(store.getKey(), "AAA");
180         // create the node status
181
NodeStatus nodeStatus = new NodeStatusImpl();
182         nodeStatus.setKey(key);
183         nodeStatus.setDeleted(false);
184         nodeStatus.setChangeTxnId("txn:123");
185         getSession().save(nodeStatus);
186         // create a new Node
187
Node node = new NodeImpl();
188         node.setKey(key);
189         node.setTypeQName(ContentModel.TYPE_CONTAINER);
190         node.setStatus(nodeStatus);
191         // give it a property map
192
Map JavaDoc<QName, PropertyValue> propertyMap = new HashMap JavaDoc<QName, PropertyValue>(5);
193         QName propertyQName = QName.createQName("{}A");
194         PropertyValue propertyValue = new PropertyValue(DataTypeDefinition.TEXT, "AAA");
195         propertyMap.put(propertyQName, propertyValue);
196         node.getProperties().putAll(propertyMap);
197         // persist it
198
Serializable JavaDoc id = getSession().save(node);
199
200         // throw the reference away and get the a new one for the id
201
node = (Node) getSession().load(NodeImpl.class, id);
202         assertNotNull("Node not found", node);
203         // extract the Map
204
propertyMap = node.getProperties();
205         assertNotNull("Map not persisted", propertyMap);
206         // ensure that the value is present
207
assertNotNull("Property value not present in map", QName.createQName("{}A"));
208     }
209
210     /**
211      * Check that aspect qnames can be added and removed from a node and that they
212      * are persisted correctly
213      */

214     public void testAspects() throws Exception JavaDoc
215     {
216         NodeKey key = new NodeKey(store.getKey(), GUID.generate());
217         // create the node status
218
NodeStatus nodeStatus = new NodeStatusImpl();
219         nodeStatus.setKey(key);
220         nodeStatus.setDeleted(false);
221         nodeStatus.setChangeTxnId("txn:123");
222         getSession().save(nodeStatus);
223         // make a real node
224
Node node = new NodeImpl();
225         node.setKey(key);
226         node.setStore(store);
227         node.setTypeQName(ContentModel.TYPE_CMOBJECT);
228         node.setStatus(nodeStatus);
229         
230         // add some aspects
231
QName aspect1 = QName.createQName(TEST_NAMESPACE, "1");
232         QName aspect2 = QName.createQName(TEST_NAMESPACE, "2");
233         QName aspect3 = QName.createQName(TEST_NAMESPACE, "3");
234         QName aspect4 = QName.createQName(TEST_NAMESPACE, "4");
235         Set JavaDoc<QName> aspects = node.getAspects();
236         aspects.add(aspect1);
237         aspects.add(aspect2);
238         aspects.add(aspect3);
239         aspects.add(aspect4);
240         assertFalse("Set did not eliminate duplicate aspect qname", aspects.add(aspect4));
241         
242         // persist
243
Serializable JavaDoc id = getSession().save(node);
244         
245         // flush and clear
246
flushAndClear();
247         
248         // get node and check aspects
249
node = (Node) getSession().get(NodeImpl.class, id);
250         assertNotNull("Node not persisted", node);
251         aspects = node.getAspects();
252         assertEquals("Not all aspects persisted", 4, aspects.size());
253     }
254     
255     public void testNodeAssoc() throws Exception JavaDoc
256     {
257         NodeKey sourceKey = new NodeKey(store.getKey(), GUID.generate());
258         // make a source node
259
NodeStatus sourceNodeStatus = new NodeStatusImpl();
260         sourceNodeStatus.setKey(sourceKey);
261         sourceNodeStatus.setDeleted(false);
262         sourceNodeStatus.setChangeTxnId("txn:123");
263         getSession().save(sourceNodeStatus);
264         Node sourceNode = new NodeImpl();
265         sourceNode.setKey(sourceKey);
266         sourceNode.setStore(store);
267         sourceNode.setTypeQName(ContentModel.TYPE_CMOBJECT);
268         sourceNode.setStatus(sourceNodeStatus);
269         Serializable JavaDoc realNodeKey = getSession().save(sourceNode);
270         
271         // make a container node
272
NodeKey targetKey = new NodeKey(store.getKey(), GUID.generate());
273         NodeStatus targetNodeStatus = new NodeStatusImpl();
274         targetNodeStatus.setKey(targetKey);
275         targetNodeStatus.setDeleted(false);
276         targetNodeStatus.setChangeTxnId("txn:123");
277         getSession().save(targetNodeStatus);
278         Node targetNode = new NodeImpl();
279         targetNode.setKey(targetKey);
280         targetNode.setStore(store);
281         targetNode.setTypeQName(ContentModel.TYPE_CONTAINER);
282         targetNode.setStatus(targetNodeStatus);
283         Serializable JavaDoc containerNodeKey = getSession().save(targetNode);
284         
285         // create an association between them
286
NodeAssoc assoc = new NodeAssocImpl();
287         assoc.setTypeQName(QName.createQName("next"));
288         assoc.buildAssociation(sourceNode, targetNode);
289         getSession().save(assoc);
290         
291         // make another association between the same two nodes
292
assoc = new NodeAssocImpl();
293         assoc.setTypeQName(QName.createQName("helper"));
294         assoc.buildAssociation(sourceNode, targetNode);
295         getSession().save(assoc);
296         
297         // flush and clear the session
298
getSession().flush();
299         getSession().clear();
300         
301         // reload the source
302
sourceNode = (Node) getSession().get(NodeImpl.class, sourceKey);
303         assertNotNull("Source node not found", sourceNode);
304         // check that the associations are present
305
assertEquals("Expected exactly 2 target assocs", 2, sourceNode.getTargetNodeAssocs().size());
306         
307         // reload the target
308
targetNode = (Node) getSession().get(NodeImpl.class, targetKey);
309         assertNotNull("Target node not found", targetNode);
310         // check that the associations are present
311
assertEquals("Expected exactly 2 source assocs", 2, targetNode.getSourceNodeAssocs().size());
312     }
313
314     public void testChildAssoc() throws Exception JavaDoc
315     {
316         // make a content node
317
NodeKey key = new NodeKey(store.getKey(), GUID.generate());
318         NodeStatus contentNodeStatus = new NodeStatusImpl();
319         contentNodeStatus.setKey(key);
320         contentNodeStatus.setDeleted(false);
321         contentNodeStatus.setChangeTxnId("txn:123");
322         getSession().save(contentNodeStatus);
323         Node contentNode = new NodeImpl();
324         contentNode.setKey(key);
325         contentNode.setStore(store);
326         contentNode.setTypeQName(ContentModel.TYPE_CONTENT);
327         contentNode.setStatus(contentNodeStatus);
328         Serializable JavaDoc contentNodeKey = getSession().save(contentNode);
329
330         // make a container node
331
key = new NodeKey(store.getKey(), GUID.generate());
332         NodeStatus containerNodeStatus = new NodeStatusImpl();
333         containerNodeStatus.setKey(key);
334         containerNodeStatus.setDeleted(false);
335         containerNodeStatus.setChangeTxnId("txn:123");
336         getSession().save(containerNodeStatus);
337         Node containerNode = new NodeImpl();
338         containerNode.setKey(key);
339         containerNode.setStore(store);
340         containerNode.setTypeQName(ContentModel.TYPE_CONTAINER);
341         containerNode.setStatus(containerNodeStatus);
342         Serializable JavaDoc containerNodeKey = getSession().save(containerNode);
343         // create an association to the content
344
ChildAssoc assoc1 = new ChildAssocImpl();
345         assoc1.setIsPrimary(true);
346         assoc1.setTypeQName(QName.createQName(null, "type1"));
347         assoc1.setQname(QName.createQName(null, "number1"));
348         assoc1.buildAssociation(containerNode, contentNode);
349         getSession().save(assoc1);
350
351         // make another association between the same two parent and child nodes
352
ChildAssoc assoc2 = new ChildAssocImpl();
353         assoc2.setIsPrimary(true);
354         assoc2.setTypeQName(QName.createQName(null, "type1"));
355         assoc2.setQname(QName.createQName(null, "number2"));
356         assoc2.buildAssociation(containerNode, contentNode);
357         getSession().save(assoc2);
358         
359         assertFalse("Hashcode incorrent", assoc2.hashCode() == 0);
360         assertNotSame("Assoc equals failure", assoc1, assoc2);
361
362 // flushAndClear();
363

364         // reload the container
365
containerNode = (Node) getSession().get(NodeImpl.class, containerNodeKey);
366         assertNotNull("Node not found", containerNode);
367         // check
368
assertEquals("Expected exactly 2 children", 2, containerNode.getChildAssocs().size());
369         for (Iterator JavaDoc iterator = containerNode.getChildAssocs().iterator(); iterator.hasNext(); /**/)
370         {
371             ChildAssoc assoc = (ChildAssoc) iterator.next();
372             // the node id must be known
373
assertNotNull("Node not populated on assoc", assoc.getChild());
374             assertEquals("Node key on child assoc is incorrect", contentNodeKey,
375                     assoc.getChild().getKey());
376         }
377
378         // check that we can traverse the association from the child
379
Collection JavaDoc<ChildAssoc> parentAssocs = contentNode.getParentAssocs();
380         assertEquals("Expected exactly 2 parent assocs", 2, parentAssocs.size());
381         parentAssocs = new HashSet JavaDoc<ChildAssoc>(parentAssocs);
382         for (ChildAssoc assoc : parentAssocs)
383         {
384             // maintain inverse assoc sets
385
assoc.removeAssociation();
386             // remove the assoc
387
getSession().delete(assoc);
388         }
389         
390         // check that the child now has zero parents
391
parentAssocs = contentNode.getParentAssocs();
392         assertEquals("Expected exactly 0 parent assocs", 0, parentAssocs.size());
393     }
394     
395     /**
396      * Allows tracing of L2 cache
397      */

398     public void testCaching() throws Exception JavaDoc
399     {
400         NodeKey key = new NodeKey(store.getKey(), GUID.generate());
401         
402         // make a node
403
NodeStatus nodeStatus = new NodeStatusImpl();
404         nodeStatus.setKey(key);
405         nodeStatus.setDeleted(false);
406         nodeStatus.setChangeTxnId("txn:123");
407         getSession().save(nodeStatus);
408         Node node = new NodeImpl();
409         node.setKey(key);
410         node.setStore(store);
411         node.setTypeQName(ContentModel.TYPE_CONTENT);
412         node.setStatus(nodeStatus);
413         getSession().save(node);
414         
415         // add some aspects to the node
416
Set JavaDoc<QName> aspects = node.getAspects();
417         aspects.add(ContentModel.ASPECT_AUDITABLE);
418         
419         // add some properties
420
Map JavaDoc<QName, PropertyValue> properties = node.getProperties();
421         properties.put(ContentModel.PROP_NAME, new PropertyValue(DataTypeDefinition.TEXT, "ABC"));
422         
423         // check that the session hands back the same instance
424
Node checkNode = (Node) getSession().get(NodeImpl.class, key);
425         assertNotNull(checkNode);
426         assertTrue("Node retrieved was not same instance", checkNode == node);
427         
428         Set JavaDoc<QName> checkAspects = checkNode.getAspects();
429         assertTrue("Aspect set retrieved was not the same instance", checkAspects == aspects);
430         assertEquals("Incorrect number of aspects", 1, checkAspects.size());
431         QName checkQName = (QName) checkAspects.toArray()[0];
432         assertTrue("QName retrieved was not the same instance", checkQName == ContentModel.ASPECT_AUDITABLE);
433         
434         Map JavaDoc<QName, PropertyValue> checkProperties = checkNode.getProperties();
435         assertTrue("Propery map retrieved was not the same instance", checkProperties == properties);
436         assertTrue("Property not found", checkProperties.containsKey(ContentModel.PROP_NAME));
437 // assertTrue("Property value instance retrieved not the same", checkProperties)
438

439         flushAndClear();
440         // commit the transaction
441
setComplete();
442         endTransaction();
443         
444         TransactionService transactionService = (TransactionService) applicationContext.getBean("transactionComponent");
445         UserTransaction JavaDoc txn = transactionService.getUserTransaction();
446         try
447         {
448             txn.begin();
449             
450             // check that the L2 cache hands back the same instance
451
checkNode = (Node) getSession().get(NodeImpl.class, key);
452             assertNotNull(checkNode);
453             checkAspects = checkNode.getAspects();
454     
455 // assertTrue("Node retrieved was not same instance", checkNode == node);
456

457             txn.commit();
458         }
459         catch (Throwable JavaDoc e)
460         {
461             txn.rollback();
462         }
463         
464     }
465 }
Popular Tags