KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > db > DbNodeServiceImplTest


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.db;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.transaction.UserTransaction JavaDoc;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.repo.content.MimetypeMap;
28 import org.alfresco.repo.domain.NodeStatus;
29 import org.alfresco.repo.node.BaseNodeServiceTest;
30 import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
31 import org.alfresco.repo.transaction.TransactionUtil;
32 import org.alfresco.repo.transaction.TransactionUtil.TransactionWork;
33 import org.alfresco.service.cmr.repository.ChildAssociationRef;
34 import org.alfresco.service.cmr.repository.ContentData;
35 import org.alfresco.service.cmr.repository.NodeRef;
36 import org.alfresco.service.cmr.repository.NodeService;
37 import org.alfresco.service.namespace.QName;
38 import org.alfresco.service.transaction.TransactionService;
39
40 /**
41  * @see org.alfresco.repo.node.db.DbNodeServiceImpl
42  *
43  * @author Derek Hulley
44  */

45 public class DbNodeServiceImplTest extends BaseNodeServiceTest
46 {
47     private TransactionService txnService;
48     private NodeDaoService nodeDaoService;
49     
50     protected NodeService getNodeService()
51     {
52         return (NodeService) applicationContext.getBean("NodeService");
53     }
54
55     @Override JavaDoc
56     protected void onSetUpInTransaction() throws Exception JavaDoc
57     {
58         super.onSetUpInTransaction();
59         txnService = (TransactionService) applicationContext.getBean("transactionComponent");
60         nodeDaoService = (NodeDaoService) applicationContext.getBean("nodeDaoService");
61     }
62
63     /**
64      * Deletes a child node and then iterates over the children of the parent node,
65      * getting the QName. This caused some issues after we did some optimization
66      * using lazy loading of the associations.
67      */

68     public void testLazyLoadIssue() throws Exception JavaDoc
69     {
70         Map JavaDoc<QName, ChildAssociationRef> assocRefs = buildNodeGraph();
71         // commit results
72
setComplete();
73         endTransaction();
74
75         UserTransaction JavaDoc userTransaction = txnService.getUserTransaction();
76         
77         try
78         {
79             userTransaction.begin();
80             
81             ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
82             NodeRef n6Ref = n6pn8Ref.getParentRef();
83             NodeRef n8Ref = n6pn8Ref.getChildRef();
84             
85             // delete n8
86
nodeService.deleteNode(n8Ref);
87             
88             // get the parent children
89
List JavaDoc<ChildAssociationRef> assocs = nodeService.getChildAssocs(n6Ref);
90             for (ChildAssociationRef assoc : assocs)
91             {
92                 // just checking
93
}
94             
95             userTransaction.commit();
96         }
97         catch(Exception JavaDoc e)
98         {
99             try { userTransaction.rollback(); } catch (IllegalStateException JavaDoc ee) {}
100             throw e;
101         }
102     }
103     
104     /**
105      * Checks that the node status changes correctly during:
106      * <ul>
107      * <li>creation</li>
108      * <li>property changes</li>
109      * <li>aspect changes</li>
110      * <li>moving</li>
111      * <li>deletion</li>
112      * </ul>
113      */

114     public void testNodeStatus() throws Exception JavaDoc
115     {
116         Map JavaDoc<QName, ChildAssociationRef> assocRefs = buildNodeGraph();
117         // get the node to play with
118
ChildAssociationRef n6pn8Ref = assocRefs.get(QName.createQName(BaseNodeServiceTest.NAMESPACE, "n6_p_n8"));
119         final NodeRef n6Ref = n6pn8Ref.getParentRef();
120         final NodeRef n8Ref = n6pn8Ref.getChildRef();
121         final Map JavaDoc<QName, Serializable JavaDoc> properties = nodeService.getProperties(n6Ref);
122
123         // commit results
124
setComplete();
125         endTransaction();
126
127         // change property - check status
128
TransactionWork<Object JavaDoc> changePropertiesWork = new TransactionWork<Object JavaDoc>()
129         {
130             public Object JavaDoc doWork()
131             {
132                 nodeService.setProperty(n6Ref, ContentModel.PROP_CREATED, new Date JavaDoc());
133                 return null;
134             }
135         };
136         executeAndCheck(n6Ref, changePropertiesWork);
137         
138         // add an aspect
139
TransactionWork<Object JavaDoc> addAspectWork = new TransactionWork<Object JavaDoc>()
140         {
141             public Object JavaDoc doWork()
142             {
143                 nodeService.addAspect(n6Ref, ASPECT_QNAME_TEST_MARKER, null);
144                 return null;
145             }
146         };
147         executeAndCheck(n6Ref, addAspectWork);
148         
149         // remove an aspect
150
TransactionWork<Object JavaDoc> removeAspectWork = new TransactionWork<Object JavaDoc>()
151         {
152             public Object JavaDoc doWork()
153             {
154                 nodeService.removeAspect(n6Ref, ASPECT_QNAME_TEST_MARKER);
155                 return null;
156             }
157         };
158         executeAndCheck(n6Ref, removeAspectWork);
159         
160         // move the node
161
TransactionWork<Object JavaDoc> moveNodeWork = new TransactionWork<Object JavaDoc>()
162         {
163             public Object JavaDoc doWork()
164             {
165                 nodeService.moveNode(
166                         n6Ref,
167                         rootNodeRef,
168                         ASSOC_TYPE_QNAME_TEST_CHILDREN,
169                         QName.createQName(NAMESPACE, "moved"));
170                 return null;
171             }
172         };
173         executeAndCheck(n6Ref, moveNodeWork);
174         
175         // delete the node
176
TransactionWork<Object JavaDoc> deleteNodeWork = new TransactionWork<Object JavaDoc>()
177         {
178             public Object JavaDoc doWork()
179             {
180                 nodeService.deleteNode(n6Ref);
181                 return null;
182             }
183         };
184         executeAndCheck(n6Ref, deleteNodeWork);
185         
186         // check cascade-deleted nodes
187
TransactionWork<Object JavaDoc> checkCascadeWork = new TransactionWork<Object JavaDoc>()
188         {
189             public Object JavaDoc doWork()
190             {
191                 // check n6
192
NodeStatus n6Status = nodeDaoService.getNodeStatus(
193                         n6Ref.getStoreRef().getProtocol(),
194                         n6Ref.getStoreRef().getIdentifier(),
195                         n6Ref.getId());
196                 if (!n6Status.isDeleted())
197                 {
198                     throw new RuntimeException JavaDoc("Deleted node does not have deleted status");
199                 }
200                 // n8 is a primary child - it should be deleted too
201
NodeStatus n8Status = nodeDaoService.getNodeStatus(
202                         n8Ref.getStoreRef().getProtocol(),
203                         n8Ref.getStoreRef().getIdentifier(),
204                         n8Ref.getId());
205                 if (!n8Status.isDeleted())
206                 {
207                     throw new RuntimeException JavaDoc("Cascade-deleted node does not have deleted status");
208                 }
209                 return null;
210             }
211         };
212         TransactionUtil.executeInUserTransaction(txnService, checkCascadeWork);
213         
214         // check node recreation
215
TransactionWork<Object JavaDoc> checkRecreateWork = new TransactionWork<Object JavaDoc>()
216         {
217             public Object JavaDoc doWork()
218             {
219                 properties.put(ContentModel.PROP_STORE_PROTOCOL, n6Ref.getStoreRef().getProtocol());
220                 properties.put(ContentModel.PROP_STORE_IDENTIFIER, n6Ref.getStoreRef().getIdentifier());
221                 properties.put(ContentModel.PROP_NODE_UUID, n6Ref.getId());
222
223                 // recreate n6
224
nodeService.createNode(
225                         rootNodeRef,
226                         ASSOC_TYPE_QNAME_TEST_CHILDREN,
227                         QName.createQName(NAMESPACE, "recreated-n6"),
228                         ContentModel.TYPE_CONTAINER,
229                         properties);
230                 return null;
231             }
232         };
233         TransactionUtil.executeInUserTransaction(txnService, checkRecreateWork);
234     }
235     
236     private void executeAndCheck(NodeRef nodeRef, TransactionWork<Object JavaDoc> work) throws Exception JavaDoc
237     {
238         UserTransaction JavaDoc txn = txnService.getUserTransaction();
239         txn.begin();
240         
241         NodeRef.Status currentStatus = nodeService.getNodeStatus(nodeRef);
242         assertNotNull(currentStatus);
243         String JavaDoc currentTxnId = AlfrescoTransactionSupport.getTransactionId();
244         assertNotNull(currentTxnId);
245         assertNotSame(currentTxnId, currentStatus.getChangeTxnId());
246         try
247         {
248             work.doWork();
249             // get the status
250
NodeRef.Status newStatus = nodeService.getNodeStatus(nodeRef);
251             assertNotNull(newStatus);
252             // check
253
assertEquals("Change didn't update status", currentTxnId, newStatus.getChangeTxnId());
254             txn.commit();
255         }
256         catch (Exception JavaDoc e)
257         {
258             try { txn.rollback(); } catch (Throwable JavaDoc ee) {}
259             throw e;
260         }
261     }
262     
263     /**
264      * Checks that the string_value retrieval against a property type is working
265      */

266     public void testGetContentDataStringValues() throws Exception JavaDoc
267     {
268         ContentData contentData = new ContentData("abc", MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, null);
269         // put this in as a random property
270
nodeService.setProperty(
271                 rootNodeRef,
272                 QName.createQName(NAMESPACE, "random"),
273                 contentData);
274         // get a list of all content values
275
List JavaDoc<String JavaDoc> contentDataStrings = nodeDaoService.getContentDataStrings();
276         assertNotNull(contentDataStrings);
277         assertTrue("ContentData not represented as a String in results",
278                 contentDataStrings.contains(contentData.toString()));
279     }
280 }
281
Popular Tags