KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > node > ConcurrentNodeServiceTest


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;
18
19 import java.io.InputStream JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import javax.transaction.UserTransaction JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.alfresco.repo.dictionary.DictionaryDAO;
27 import org.alfresco.repo.dictionary.M2Model;
28 import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
29 import org.alfresco.repo.security.authentication.AuthenticationComponent;
30 import org.alfresco.service.ServiceRegistry;
31 import org.alfresco.service.cmr.repository.ChildAssociationRef;
32 import org.alfresco.service.cmr.repository.NodeRef;
33 import org.alfresco.service.cmr.repository.NodeService;
34 import org.alfresco.service.cmr.repository.StoreRef;
35 import org.alfresco.service.cmr.search.ResultSet;
36 import org.alfresco.service.cmr.search.SearchService;
37 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver;
38 import org.alfresco.service.namespace.NamespacePrefixResolver;
39 import org.alfresco.service.namespace.NamespaceService;
40 import org.alfresco.service.namespace.QName;
41 import org.alfresco.service.transaction.TransactionService;
42 import org.alfresco.util.ApplicationContextHelper;
43 import org.apache.lucene.index.IndexWriter;
44 import org.springframework.context.ApplicationContext;
45
46 public class ConcurrentNodeServiceTest extends TestCase
47 {
48     public static final String JavaDoc NAMESPACE = "http://www.alfresco.org/test/BaseNodeServiceTest";
49     public static final String JavaDoc TEST_PREFIX = "test";
50     public static final QName TYPE_QNAME_TEST_CONTENT = QName.createQName(NAMESPACE, "content");
51     public static final QName ASPECT_QNAME_TEST_TITLED = QName.createQName(NAMESPACE, "titled");
52     public static final QName PROP_QNAME_TEST_TITLE = QName.createQName(NAMESPACE, "title");
53     public static final QName PROP_QNAME_TEST_MIMETYPE = QName.createQName(NAMESPACE, "mimetype");
54
55     static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
56
57     private NodeService nodeService;
58     private TransactionService transactionService;
59     private NodeRef rootNodeRef;
60     private FullTextSearchIndexer luceneFTS;
61     
62     private AuthenticationComponent authenticationComponent;
63
64     public ConcurrentNodeServiceTest()
65     {
66         super();
67     }
68
69     protected void setUp() throws Exception JavaDoc
70     {
71         DictionaryDAO dictionaryDao = (DictionaryDAO) ctx.getBean("dictionaryDAO");
72         // load the system model
73
ClassLoader JavaDoc cl = BaseNodeServiceTest.class.getClassLoader();
74         InputStream JavaDoc modelStream = cl.getResourceAsStream("alfresco/model/systemModel.xml");
75         assertNotNull(modelStream);
76         M2Model model = M2Model.createModel(modelStream);
77         dictionaryDao.putModel(model);
78         // load the test model
79
modelStream = cl.getResourceAsStream("org/alfresco/repo/node/BaseNodeServiceTest_model.xml");
80         assertNotNull(modelStream);
81         model = M2Model.createModel(modelStream);
82         dictionaryDao.putModel(model);
83
84         nodeService = (NodeService) ctx.getBean("dbNodeService");
85         transactionService = (TransactionService) ctx.getBean("transactionComponent");
86         luceneFTS = (FullTextSearchIndexer) ctx.getBean("LuceneFullTextSearchIndexer");
87         this.authenticationComponent = (AuthenticationComponent)ctx.getBean("authenticationComponent");
88         
89         this.authenticationComponent.setSystemUserAsCurrentUser();
90
91         // create a first store directly
92
UserTransaction JavaDoc tx = transactionService.getUserTransaction();
93         tx.begin();
94         StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
95         rootNodeRef = nodeService.getRootNode(storeRef);
96         tx.commit();
97     }
98
99     @Override JavaDoc
100     protected void tearDown() throws Exception JavaDoc
101     {
102         authenticationComponent.clearCurrentSecurityContext();
103         super.tearDown();
104     }
105     
106     protected Map JavaDoc<QName, ChildAssociationRef> buildNodeGraph() throws Exception JavaDoc
107     {
108         return BaseNodeServiceTest.buildNodeGraph(nodeService, rootNodeRef);
109     }
110
111     protected Map JavaDoc<QName, ChildAssociationRef> commitNodeGraph() throws Exception JavaDoc
112     {
113         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
114         tx.begin();
115         Map JavaDoc<QName, ChildAssociationRef> answer = buildNodeGraph();
116         tx.commit();
117
118         return null;// answer;
119
}
120
121     public void testConcurrent() throws Exception JavaDoc
122     {
123         luceneFTS.pause();
124         IndexWriter.COMMIT_LOCK_TIMEOUT = 100000;
125         int count = 10;
126         int repeats = 10;
127
128         Map JavaDoc<QName, ChildAssociationRef> assocRefs = commitNodeGraph();
129         Thread JavaDoc runner = null;
130
131         for (int i = 0; i < count; i++)
132         {
133             runner = new Nester("Concurrent-" + i, runner, repeats);
134         }
135         if (runner != null)
136         {
137             runner.start();
138
139             try
140             {
141                 runner.join();
142             }
143             catch (InterruptedException JavaDoc e)
144             {
145                 e.printStackTrace();
146             }
147         }
148
149         SearchService searcher = (SearchService) ctx.getBean(ServiceRegistry.SEARCH_SERVICE.getLocalName());
150         assertEquals(2 * ((count * repeats) + 1), searcher.selectNodes(rootNodeRef, "/*", null,
151                 getNamespacePrefixReolsver(""), false).size());
152         ResultSet results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/*\"");
153         // n6 has root aspect - there are three things at the root level in the
154
// index
155
assertEquals(3 * ((count * repeats) + 1), results.length());
156         results.close();
157     }
158
159     /**
160      * Daemon thread
161      */

162     private class Nester extends Thread JavaDoc
163     {
164         Thread JavaDoc waiter;
165
166         int repeats;
167
168         Nester(String JavaDoc name, Thread JavaDoc waiter, int repeats)
169         {
170             super(name);
171             this.setDaemon(true);
172             this.waiter = waiter;
173             this.repeats = repeats;
174         }
175
176         public void run()
177         {
178             authenticationComponent.setSystemUserAsCurrentUser();
179             
180             if (waiter != null)
181             {
182                 waiter.start();
183             }
184             try
185             {
186                 System.out.println("Start " + this.getName());
187                 for (int i = 0; i < repeats; i++)
188                 {
189                     Map JavaDoc<QName, ChildAssociationRef> assocRefs = commitNodeGraph();
190                     System.out.println(" " + this.getName() + " " + i);
191                 }
192                 System.out.println("End " + this.getName());
193             }
194             catch (Exception JavaDoc e)
195             {
196                 e.printStackTrace();
197                 System.exit(12);
198             }
199             if (waiter != null)
200             {
201                 try
202                 {
203                     waiter.join();
204                 }
205                 catch (InterruptedException JavaDoc e)
206                 {
207                 }
208             }
209         }
210
211     }
212
213     private NamespacePrefixResolver getNamespacePrefixReolsver(String JavaDoc defaultURI)
214     {
215         DynamicNamespacePrefixResolver nspr = new DynamicNamespacePrefixResolver(null);
216         nspr.registerNamespace(NamespaceService.SYSTEM_MODEL_PREFIX, NamespaceService.SYSTEM_MODEL_1_0_URI);
217         nspr.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI);
218         nspr.registerNamespace(NamespaceService.APP_MODEL_PREFIX, NamespaceService.APP_MODEL_1_0_URI);
219         nspr.registerNamespace("namespace", "namespace");
220         nspr.registerNamespace(NamespaceService.DEFAULT_PREFIX, defaultURI);
221         return nspr;
222     }
223 }
224
Popular Tags