KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > impl > lucene > LuceneCategoryTest


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.search.impl.lucene;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Arrays JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Random JavaDoc;
24
25 import javax.transaction.UserTransaction JavaDoc;
26
27 import junit.framework.TestCase;
28
29 import org.alfresco.model.ContentModel;
30 import org.alfresco.repo.dictionary.DictionaryDAO;
31 import org.alfresco.repo.dictionary.M2Aspect;
32 import org.alfresco.repo.dictionary.M2Model;
33 import org.alfresco.repo.dictionary.M2Property;
34 import org.alfresco.repo.search.impl.lucene.fts.FullTextSearchIndexer;
35 import org.alfresco.repo.search.transaction.LuceneIndexLock;
36 import org.alfresco.service.ServiceRegistry;
37 import org.alfresco.service.cmr.dictionary.DictionaryService;
38 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
39 import org.alfresco.service.cmr.repository.ChildAssociationRef;
40 import org.alfresco.service.cmr.repository.NodeRef;
41 import org.alfresco.service.cmr.repository.NodeService;
42 import org.alfresco.service.cmr.repository.StoreRef;
43 import org.alfresco.service.cmr.search.CategoryService;
44 import org.alfresco.service.cmr.search.ResultSet;
45 import org.alfresco.service.cmr.search.ResultSetRow;
46 import org.alfresco.service.cmr.search.SearchService;
47 import org.alfresco.service.namespace.DynamicNamespacePrefixResolver;
48 import org.alfresco.service.namespace.NamespacePrefixResolver;
49 import org.alfresco.service.namespace.NamespaceService;
50 import org.alfresco.service.namespace.QName;
51 import org.alfresco.service.transaction.TransactionService;
52 import org.alfresco.util.ApplicationContextHelper;
53 import org.springframework.context.ApplicationContext;
54
55 public class LuceneCategoryTest extends TestCase
56 {
57     private ServiceRegistry serviceRegistry;
58     
59     static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
60     NodeService nodeService;
61     DictionaryService dictionaryService;
62     LuceneIndexLock luceneIndexLock;
63     private NodeRef rootNodeRef;
64     private NodeRef n1;
65     private NodeRef n2;
66     private NodeRef n3;
67     private NodeRef n4;
68     private NodeRef n6;
69     private NodeRef n5;
70     private NodeRef n7;
71     private NodeRef n8;
72     private NodeRef n9;
73     private NodeRef n10;
74     private NodeRef n11;
75     private NodeRef n12;
76     private NodeRef n13;
77     private NodeRef n14;
78     
79     private NodeRef catContainer;
80     private NodeRef catRoot;
81     private NodeRef catACBase;
82     private NodeRef catACOne;
83     private NodeRef catACTwo;
84     private NodeRef catACThree;
85     private FullTextSearchIndexer luceneFTS;
86     private DictionaryDAO dictionaryDAO;
87     private String JavaDoc TEST_NAMESPACE = "http://www.alfresco.org/test/lucenecategorytest";
88     private QName regionCategorisationQName;
89     private QName assetClassCategorisationQName;
90     private QName investmentRegionCategorisationQName;
91     private QName marketingRegionCategorisationQName;
92     private NodeRef catRBase;
93     private NodeRef catROne;
94     private NodeRef catRTwo;
95     private NodeRef catRThree;
96     private SearchService searcher;
97     private LuceneIndexerAndSearcher indexerAndSearcher;
98
99     private CategoryService categoryService;
100
101     public LuceneCategoryTest()
102     {
103         super();
104     }
105
106     public LuceneCategoryTest(String JavaDoc arg0)
107     {
108         super(arg0);
109     }
110
111     public void setUp() throws Exception JavaDoc
112     {
113         nodeService = (NodeService)ctx.getBean("dbNodeService");
114         luceneIndexLock = (LuceneIndexLock)ctx.getBean("luceneIndexLock");
115         dictionaryService = (DictionaryService)ctx.getBean("dictionaryService");
116         luceneFTS = (FullTextSearchIndexer) ctx.getBean("LuceneFullTextSearchIndexer");
117         dictionaryDAO = (DictionaryDAO) ctx.getBean("dictionaryDAO");
118         searcher = (SearchService) ctx.getBean("searchService");
119         indexerAndSearcher = (LuceneIndexerAndSearcher) ctx.getBean("luceneIndexerAndSearcherFactory");
120         categoryService = (CategoryService) ctx.getBean("categoryService");
121         serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
122         
123         createTestTypes();
124         
125         TransactionService transactionService = serviceRegistry.getTransactionService();
126         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
127         tx.begin();
128         
129         StoreRef storeRef = nodeService.createStore(
130                 StoreRef.PROTOCOL_WORKSPACE,
131                 "Test_" + System.currentTimeMillis());
132         rootNodeRef = nodeService.getRootNode(storeRef);
133         
134         n1 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}one"), ContentModel.TYPE_CONTAINER).getChildRef();
135         nodeService.setProperty(n1, QName.createQName("{namespace}property-1"), "value-1");
136         n2 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}two"), ContentModel.TYPE_CONTAINER).getChildRef();
137         nodeService.setProperty(n2, QName.createQName("{namespace}property-1"), "value-1");
138         nodeService.setProperty(n2, QName.createQName("{namespace}property-2"), "value-2");
139         n3 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}three"), ContentModel.TYPE_CONTAINER).getChildRef();
140         n4 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}four"), ContentModel.TYPE_CONTAINER).getChildRef();
141         n5 = nodeService.createNode(n1, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}five"), ContentModel.TYPE_CONTAINER).getChildRef();
142         n6 = nodeService.createNode(n1, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}six"), ContentModel.TYPE_CONTAINER).getChildRef();
143         n7 = nodeService.createNode(n2, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}seven"), ContentModel.TYPE_CONTAINER).getChildRef();
144         n8 = nodeService.createNode(n2, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}eight-2"), ContentModel.TYPE_CONTAINER).getChildRef();
145         n9 = nodeService.createNode(n5, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}nine"), ContentModel.TYPE_CONTAINER).getChildRef();
146         n10 = nodeService.createNode(n5, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}ten"), ContentModel.TYPE_CONTAINER).getChildRef();
147         n11 = nodeService.createNode(n5, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}eleven"), ContentModel.TYPE_CONTAINER).getChildRef();
148         n12 = nodeService.createNode(n5, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}twelve"), ContentModel.TYPE_CONTAINER).getChildRef();
149         n13 = nodeService.createNode(n12, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}thirteen"), ContentModel.TYPE_CONTAINER).getChildRef();
150         n14 = nodeService.createNode(n13, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}fourteen"), ContentModel.TYPE_CONTAINER).getChildRef();
151         
152         nodeService.addChild(rootNodeRef, n8, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}eight-0"));
153         nodeService.addChild(n1, n8, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}eight-1"));
154         nodeService.addChild(n2, n13, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}link"));
155         
156         nodeService.addChild(n1, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
157         nodeService.addChild(n2, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
158         nodeService.addChild(n5, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
159         nodeService.addChild(n6, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
160         nodeService.addChild(n12, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
161         nodeService.addChild(n13, n14, ContentModel.ASSOC_CHILDREN, QName.createQName("{namespace}common"));
162         
163         // Categories
164

165         catContainer = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "categoryContainer"), ContentModel.TYPE_CONTAINER).getChildRef();
166         catRoot = nodeService.createNode(catContainer, ContentModel.ASSOC_CHILDREN, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "categoryRoot"), ContentModel.TYPE_CATEGORYROOT).getChildRef();
167        
168        
169         
170         catRBase = nodeService.createNode(catRoot, ContentModel.ASSOC_CATEGORIES, QName.createQName(TEST_NAMESPACE, "Region"), ContentModel.TYPE_CATEGORY).getChildRef();
171         catROne = nodeService.createNode(catRBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "Europe"), ContentModel.TYPE_CATEGORY).getChildRef();
172         catRTwo = nodeService.createNode(catRBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "RestOfWorld"), ContentModel.TYPE_CATEGORY).getChildRef();
173         catRThree = nodeService.createNode(catRTwo, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "US"), ContentModel.TYPE_CATEGORY).getChildRef();
174         
175         nodeService.addChild(catRoot, catRBase, ContentModel.ASSOC_CATEGORIES, QName.createQName(TEST_NAMESPACE, "InvestmentRegion"));
176         nodeService.addChild(catRoot, catRBase, ContentModel.ASSOC_CATEGORIES, QName.createQName(TEST_NAMESPACE, "MarketingRegion"));
177         
178         
179         catACBase = nodeService.createNode(catRoot, ContentModel.ASSOC_CATEGORIES, QName.createQName(TEST_NAMESPACE, "AssetClass"), ContentModel.TYPE_CATEGORY).getChildRef();
180         catACOne = nodeService.createNode(catACBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "Fixed"), ContentModel.TYPE_CATEGORY).getChildRef();
181         catACTwo = nodeService.createNode(catACBase, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "Equity"), ContentModel.TYPE_CATEGORY).getChildRef();
182         catACThree = nodeService.createNode(catACTwo, ContentModel.ASSOC_SUBCATEGORIES, QName.createQName(TEST_NAMESPACE, "SpecialEquity"), ContentModel.TYPE_CATEGORY).getChildRef();
183         
184         
185        
186         nodeService.addAspect(n1, assetClassCategorisationQName, createMap("assetClass", catACBase));
187         nodeService.addAspect(n1, regionCategorisationQName, createMap("region", catRBase));
188         
189         nodeService.addAspect(n2, assetClassCategorisationQName, createMap("assetClass", catACOne));
190         nodeService.addAspect(n3, assetClassCategorisationQName, createMap("assetClass", catACOne));
191         nodeService.addAspect(n4, assetClassCategorisationQName, createMap("assetClass", catACOne));
192         nodeService.addAspect(n5, assetClassCategorisationQName, createMap("assetClass", catACOne));
193         nodeService.addAspect(n6, assetClassCategorisationQName, createMap("assetClass", catACOne));
194         
195         nodeService.addAspect(n7, assetClassCategorisationQName, createMap("assetClass", catACTwo));
196         nodeService.addAspect(n8, assetClassCategorisationQName, createMap("assetClass", catACTwo));
197         nodeService.addAspect(n9, assetClassCategorisationQName, createMap("assetClass", catACTwo));
198         nodeService.addAspect(n10, assetClassCategorisationQName, createMap("assetClass", catACTwo));
199         nodeService.addAspect(n11, assetClassCategorisationQName, createMap("assetClass", catACTwo));
200         
201         nodeService.addAspect(n12, assetClassCategorisationQName, createMap("assetClass", catACOne, catACTwo));
202         nodeService.addAspect(n13, assetClassCategorisationQName, createMap("assetClass", catACOne, catACTwo, catACThree));
203         nodeService.addAspect(n14, assetClassCategorisationQName, createMap("assetClass", catACOne, catACTwo));
204         
205         nodeService.addAspect(n2, regionCategorisationQName, createMap("region", catROne));
206         nodeService.addAspect(n3, regionCategorisationQName, createMap("region", catROne));
207         nodeService.addAspect(n4, regionCategorisationQName, createMap("region", catRTwo));
208         nodeService.addAspect(n5, regionCategorisationQName, createMap("region", catRTwo));
209         
210         nodeService.addAspect(n5, investmentRegionCategorisationQName, createMap("investmentRegion", catRBase));
211         nodeService.addAspect(n5, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
212         nodeService.addAspect(n6, investmentRegionCategorisationQName, createMap("investmentRegion", catRBase));
213         nodeService.addAspect(n7, investmentRegionCategorisationQName, createMap("investmentRegion", catRBase));
214         nodeService.addAspect(n8, investmentRegionCategorisationQName, createMap("investmentRegion", catRBase));
215         nodeService.addAspect(n9, investmentRegionCategorisationQName, createMap("investmentRegion", catRBase));
216         nodeService.addAspect(n10, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
217         nodeService.addAspect(n11, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
218         nodeService.addAspect(n12, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
219         nodeService.addAspect(n13, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
220         nodeService.addAspect(n14, marketingRegionCategorisationQName, createMap("marketingRegion", catRBase));
221         
222         tx.commit();
223     }
224     
225     private HashMap JavaDoc<QName, Serializable JavaDoc> createMap(String JavaDoc name, NodeRef[] nodeRefs)
226     {
227         HashMap JavaDoc<QName, Serializable JavaDoc> map = new HashMap JavaDoc<QName, Serializable JavaDoc>();
228         Serializable JavaDoc value = (Serializable JavaDoc) Arrays.asList(nodeRefs);
229         map.put(QName.createQName(TEST_NAMESPACE, name), value);
230         return map;
231     }
232     
233     private HashMap JavaDoc<QName, Serializable JavaDoc> createMap(String JavaDoc name, NodeRef nodeRef)
234     {
235         return createMap(name, new NodeRef[]{nodeRef});
236     }
237     
238     private HashMap JavaDoc<QName, Serializable JavaDoc> createMap(String JavaDoc name, NodeRef nodeRef1, NodeRef nodeRef2)
239     {
240         return createMap(name, new NodeRef[]{nodeRef1, nodeRef2});
241     }
242     
243     private HashMap JavaDoc<QName, Serializable JavaDoc> createMap(String JavaDoc name, NodeRef nodeRef1, NodeRef nodeRef2, NodeRef nodeRef3)
244     {
245         return createMap(name, new NodeRef[]{nodeRef1, nodeRef2, nodeRef3});
246     }
247     
248     private void createTestTypes()
249     {
250         M2Model model = M2Model.createModel("test:lucenecategory");
251         model.createNamespace(TEST_NAMESPACE, "test");
252         model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
253         model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
254         
255         regionCategorisationQName = QName.createQName(TEST_NAMESPACE, "Region");
256         M2Aspect generalCategorisation = model.createAspect("test:" + regionCategorisationQName.getLocalName());
257         generalCategorisation.setParentName("cm:" + ContentModel.ASPECT_CLASSIFIABLE.getLocalName());
258         M2Property genCatProp = generalCategorisation.createProperty("test:region");
259         genCatProp.setIndexed(true);
260         genCatProp.setIndexedAtomically(true);
261         genCatProp.setMandatory(true);
262         genCatProp.setMultiValued(true);
263         genCatProp.setStoredInIndex(true);
264         genCatProp.setTokenisedInIndex(true);
265         genCatProp.setType("d:" + DataTypeDefinition.CATEGORY.getLocalName());
266         
267         assetClassCategorisationQName = QName.createQName(TEST_NAMESPACE, "AssetClass");
268         M2Aspect assetClassCategorisation = model.createAspect("test:" + assetClassCategorisationQName.getLocalName());
269         assetClassCategorisation.setParentName("cm:" + ContentModel.ASPECT_CLASSIFIABLE.getLocalName());
270         M2Property acProp = assetClassCategorisation.createProperty("test:assetClass");
271         acProp.setIndexed(true);
272         acProp.setIndexedAtomically(true);
273         acProp.setMandatory(true);
274         acProp.setMultiValued(true);
275         acProp.setStoredInIndex(true);
276         acProp.setTokenisedInIndex(true);
277         acProp.setType("d:" + DataTypeDefinition.CATEGORY.getLocalName());
278         
279         investmentRegionCategorisationQName = QName.createQName(TEST_NAMESPACE, "InvestmentRegion");
280         M2Aspect investmentRegionCategorisation = model.createAspect("test:" + investmentRegionCategorisationQName.getLocalName());
281         investmentRegionCategorisation.setParentName("cm:" + ContentModel.ASPECT_CLASSIFIABLE.getLocalName());
282         M2Property irProp = investmentRegionCategorisation.createProperty("test:investmentRegion");
283         irProp.setIndexed(true);
284         irProp.setIndexedAtomically(true);
285         irProp.setMandatory(true);
286         irProp.setMultiValued(true);
287         irProp.setStoredInIndex(true);
288         irProp.setTokenisedInIndex(true);
289         irProp.setType("d:" + DataTypeDefinition.CATEGORY.getLocalName());
290         
291         marketingRegionCategorisationQName = QName.createQName(TEST_NAMESPACE, "MarketingRegion");
292         M2Aspect marketingRegionCategorisation = model.createAspect("test:" + marketingRegionCategorisationQName.getLocalName());
293         marketingRegionCategorisation.setParentName("cm:" + ContentModel.ASPECT_CLASSIFIABLE.getLocalName());
294         M2Property mrProp = marketingRegionCategorisation.createProperty("test:marketingRegion");
295         mrProp.setIndexed(true);
296         mrProp.setIndexedAtomically(true);
297         mrProp.setMandatory(true);
298         mrProp.setMultiValued(true);
299         mrProp.setStoredInIndex(true);
300         mrProp.setTokenisedInIndex(true);
301         mrProp.setType("d:" + DataTypeDefinition.CATEGORY.getLocalName());
302
303         dictionaryDAO.putModel(model);
304     }
305     
306     private void buildBaseIndex()
307     {
308         LuceneIndexerImpl indexer = LuceneIndexerImpl.getUpdateIndexer(rootNodeRef.getStoreRef(), "delta" + System.currentTimeMillis() + "_" + (new Random JavaDoc().nextInt()), indexerAndSearcher);
309         indexer.setNodeService(nodeService);
310         indexer.setLuceneIndexLock(luceneIndexLock);
311         indexer.setDictionaryService(dictionaryService);
312         indexer.setLuceneFullTextSearchIndexer(luceneFTS);
313         //indexer.clearIndex();
314
indexer.createNode(new ChildAssociationRef(null, null, null, rootNodeRef));
315         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, rootNodeRef, QName.createQName("{namespace}one"), n1));
316         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, rootNodeRef, QName.createQName("{namespace}two"), n2));
317         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, rootNodeRef, QName.createQName("{namespace}three"), n3));
318         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, rootNodeRef, QName.createQName("{namespace}four"), n4));
319         
320         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, rootNodeRef, QName.createQName("{namespace}categoryContainer"), catContainer));
321         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CHILDREN, catContainer, QName.createQName("{cat}categoryRoot"), catRoot));
322         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, catRoot, QName.createQName(TEST_NAMESPACE, "AssetClass"), catACBase));
323         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catACBase, QName.createQName(TEST_NAMESPACE, "Fixed"), catACOne));
324         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catACBase, QName.createQName(TEST_NAMESPACE, "Equity"), catACTwo));
325         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catACTwo, QName.createQName(TEST_NAMESPACE, "SpecialEquity"), catACThree));
326         
327         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, catRoot, QName.createQName(TEST_NAMESPACE, "Region"), catRBase));
328         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catRBase, QName.createQName(TEST_NAMESPACE, "Europe"), catROne));
329         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catRBase, QName.createQName(TEST_NAMESPACE, "RestOfWorld"), catRTwo));
330         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_SUBCATEGORIES, catRTwo, QName.createQName(TEST_NAMESPACE, "US"), catRThree));
331         
332         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n1, QName.createQName("{namespace}five"), n5));
333         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n1, QName.createQName("{namespace}six"), n6));
334         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n2, QName.createQName("{namespace}seven"), n7));
335         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n2, QName.createQName("{namespace}eight"), n8));
336         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n5, QName.createQName("{namespace}nine"), n9));
337         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n5, QName.createQName("{namespace}ten"), n10));
338         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n5, QName.createQName("{namespace}eleven"), n11));
339         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n5, QName.createQName("{namespace}twelve"), n12));
340         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n12, QName.createQName("{namespace}thirteen"), n13));
341         indexer.createNode(new ChildAssociationRef(ContentModel.ASSOC_CATEGORIES, n13, QName.createQName("{namespace}fourteen"), n14));
342         indexer.prepare();
343         indexer.commit();
344     }
345
346     
347     public void testMulti() throws Exception JavaDoc
348     {
349         TransactionService transactionService = serviceRegistry.getTransactionService();
350         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
351         tx.begin();
352         buildBaseIndex();
353         
354         LuceneSearcherImpl searcher = LuceneSearcherImpl.getSearcher(rootNodeRef.getStoreRef(), indexerAndSearcher);
355         
356         searcher.setNodeService(nodeService);
357         searcher.setDictionaryService(dictionaryService);
358         searcher.setNamespacePrefixResolver(getNamespacePrefixReolsver(""));
359         ResultSet results;
360         
361         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"//*\" AND (PATH:\"/test:AssetClass/test:Equity/member\" PATH:\"/test:MarketingRegion/member\")", null, null);
362         //printPaths(results);
363
assertEquals(9, results.length());
364         results.close();
365         tx.rollback();
366     }
367     
368     public void testBasic() throws Exception JavaDoc
369     {
370         TransactionService transactionService = serviceRegistry.getTransactionService();
371         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
372         tx.begin();
373         buildBaseIndex();
374         
375         LuceneSearcherImpl searcher = LuceneSearcherImpl.getSearcher(rootNodeRef.getStoreRef(), indexerAndSearcher);
376         
377         searcher.setNodeService(nodeService);
378         searcher.setDictionaryService(dictionaryService);
379         searcher.setNamespacePrefixResolver(getNamespacePrefixReolsver(""));
380         ResultSet results;
381         
382         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:MarketingRegion\"", null, null);
383         //printPaths(results);
384
assertEquals(1, results.length());
385         results.close();
386         
387         
388         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:MarketingRegion//member\"", null, null);
389         //printPaths(results);
390
assertEquals(6, results.length());
391         results.close();
392         
393         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer\"", null, null);
394         assertEquals(1, results.length());
395         results.close();
396         
397         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot\"", null, null);
398         assertEquals(1, results.length());
399         results.close();
400         
401         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot\"", null, null);
402         assertEquals(1, results.length());
403         results.close();
404         
405         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass\"", null, null);
406         assertEquals(1, results.length());
407         results.close();
408         
409         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/member\" ", null, null);
410         assertEquals(1, results.length());
411         results.close();
412         
413         
414         
415         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/test:Fixed\"", null, null);
416         assertEquals(1, results.length());
417         results.close();
418         
419         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/test:Equity\"", null, null);
420         assertEquals(1, results.length());
421         results.close();
422         
423         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass\"", null, null);
424         assertEquals(1, results.length());
425         results.close();
426         
427         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Fixed\"", null, null);
428         assertEquals(1, results.length());
429         results.close();
430         
431         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity\"", null, null);
432         assertEquals(1, results.length());
433         results.close();
434         
435         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:*\"", null, null);
436         assertEquals(2, results.length());
437         results.close();
438         
439         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass//test:*\"", null, null);
440         assertEquals(3, results.length());
441         results.close();
442         
443         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Fixed/member\"", null, null);
444         //printPaths(results);
445
assertEquals(8, results.length());
446         results.close();
447         
448         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/member\"", null, null);
449         //printPaths(results);
450
assertEquals(8, results.length());
451         results.close();
452         
453         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/test:SpecialEquity/member//.\"", null, null);
454         assertEquals(1, results.length());
455         results.close();
456         
457         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/test:SpecialEquity/member//*\"", null, null);
458         assertEquals(0, results.length());
459         results.close();
460         
461         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/test:SpecialEquity/member\"", null, null);
462         assertEquals(1, results.length());
463         results.close();
464         
465         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "+PATH:\"/test:AssetClass/test:Equity/member\" AND +PATH:\"/test:AssetClass/test:Fixed/member\"", null, null);
466         //printPaths(results);
467
assertEquals(3, results.length());
468         results.close();
469         
470         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/member\" PATH:\"/test:AssetClass/test:Fixed/member\"", null, null);
471         //printPaths(results);
472
assertEquals(13, results.length());
473         results.close();
474         
475         // Region
476

477         assertEquals(4, nodeService.getChildAssocs(catRoot).size());
478         
479         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:Region\"", null, null);
480         //printPaths(results);
481
assertEquals(1, results.length());
482         results.close();
483         
484         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:Region/member\"", null, null);
485         //printPaths(results);
486
assertEquals(1, results.length());
487         results.close();
488         
489         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:Region/test:Europe/member\"", null, null);
490         //printPaths(results);
491
assertEquals(2, results.length());
492         results.close();
493         
494         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:Region/test:RestOfWorld/member\"", null, null);
495         //printPaths(results);
496
assertEquals(2, results.length());
497         results.close();
498         
499         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:Region//member\"", null, null);
500         //printPaths(results);
501
assertEquals(5, results.length());
502         results.close();
503         
504         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:InvestmentRegion//member\"", null, null);
505         //printPaths(results);
506
assertEquals(5, results.length());
507         results.close();
508         
509         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:MarketingRegion//member\"", null, null);
510         //printPaths(results);
511
assertEquals(6, results.length());
512         results.close();
513         
514         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "+PATH:\"/test:AssetClass/test:Fixed/member\" AND +PATH:\"/test:Region/test:Europe/member\"", null, null);
515         //printPaths(results);
516
assertEquals(2, results.length());
517         results.close();
518         
519         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "+PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/test:Fixed/member\" AND +PATH:\"/cm:categoryContainer/cm:categoryRoot/test:Region/test:Europe/member\"", null, null);
520         //printPaths(results);
521
assertEquals(2, results.length());
522         results.close();
523         
524         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/test:AssetClass/test:Equity/member\" PATH:\"/test:MarketingRegion/member\"", null, null);
525         //printPaths(results);
526
assertEquals(9, results.length());
527         results.close();
528         tx.rollback();
529     }
530     
531     public void testCategoryServiceImpl() throws Exception JavaDoc
532     {
533         TransactionService transactionService = serviceRegistry.getTransactionService();
534         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
535         tx.begin();
536         buildBaseIndex();
537         
538         LuceneSearcherImpl searcher = LuceneSearcherImpl.getSearcher(rootNodeRef.getStoreRef(), indexerAndSearcher);
539         
540         searcher.setNodeService(nodeService);
541         searcher.setDictionaryService(dictionaryService);
542         searcher.setNamespacePrefixResolver(getNamespacePrefixReolsver(""));
543         
544         ResultSet
545         
546         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/*\" ", null, null);
547         assertEquals(3, results.length());
548         results.close();
549         
550         results = searcher.query(rootNodeRef.getStoreRef(), "lucene", "PATH:\"/cm:categoryContainer/cm:categoryRoot/test:AssetClass/member\" ", null, null);
551         assertEquals(1, results.length());
552         results.close();
553         
554         LuceneCategoryServiceImpl impl = new LuceneCategoryServiceImpl();
555         impl.setNodeService(nodeService);
556         impl.setNamespacePrefixResolver(getNamespacePrefixReolsver(""));
557         impl.setIndexerAndSearcher(indexerAndSearcher);
558         impl.setDictionaryService(dictionaryService);
559         
560         Collection JavaDoc<ChildAssociationRef>
561         result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.IMMEDIATE);
562         assertEquals(1, result.size());
563        
564         
565         result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.IMMEDIATE);
566         assertEquals(3, result.size());
567        
568         
569         result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE);
570         assertEquals(2, result.size());
571         
572         
573         result = impl.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.ANY);
574         assertEquals(18, result.size());
575         
576         
577         result = impl.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.ANY);
578         assertEquals(21, result.size());
579        
580         
581         result = impl.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY);
582         assertEquals(3, result.size());
583         
584         
585         result = impl.getClassifications(rootNodeRef.getStoreRef());
586         assertEquals(4, result.size());
587         
588         
589         result = impl.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE);
590         assertEquals(2, result.size());
591       
592         
593         Collection JavaDoc<QName> aspects = impl.getClassificationAspects();
594         assertEquals(6, aspects.size());
595        
596         tx.rollback();
597     }
598     
599     private NamespacePrefixResolver getNamespacePrefixReolsver(String JavaDoc defaultURI)
600     {
601         DynamicNamespacePrefixResolver nspr = new DynamicNamespacePrefixResolver(null);
602         nspr.registerNamespace(NamespaceService.CONTENT_MODEL_PREFIX, NamespaceService.CONTENT_MODEL_1_0_URI);
603         nspr.registerNamespace("namespace", "namespace");
604         nspr.registerNamespace("test", TEST_NAMESPACE);
605         nspr.registerNamespace(NamespaceService.DEFAULT_PREFIX, defaultURI);
606         return nspr;
607     }
608     
609     public void testCategoryService() throws Exception JavaDoc
610     {
611         TransactionService transactionService = serviceRegistry.getTransactionService();
612         UserTransaction JavaDoc tx = transactionService.getUserTransaction();
613         tx.begin();
614         buildBaseIndex();
615         assertEquals(1, categoryService.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.IMMEDIATE).size());
616         assertEquals(2, categoryService.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.IMMEDIATE).size());
617         assertEquals(3, categoryService.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.IMMEDIATE).size());
618         assertEquals(18, categoryService.getChildren(catACBase , CategoryService.Mode.MEMBERS, CategoryService.Depth.ANY).size());
619         assertEquals(3, categoryService.getChildren(catACBase , CategoryService.Mode.SUB_CATEGORIES, CategoryService.Depth.ANY).size());
620         assertEquals(21, categoryService.getChildren(catACBase , CategoryService.Mode.ALL, CategoryService.Depth.ANY).size());
621         assertEquals(4, categoryService.getClassifications(rootNodeRef.getStoreRef()).size());
622         assertEquals(2, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE).size());
623         assertEquals(3, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.ANY).size());
624         assertEquals(6, categoryService.getClassificationAspects().size());
625         assertEquals(2, categoryService.getRootCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass")).size());
626
627         NodeRef newRoot = categoryService.createRootCategory(rootNodeRef.getStoreRef(),QName.createQName(TEST_NAMESPACE, "AssetClass"), "Fruit");
628         tx.commit();
629         tx = transactionService.getUserTransaction();
630         tx.begin();
631         assertEquals(3, categoryService.getRootCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass")).size());
632         assertEquals(3, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE).size());
633         assertEquals(4, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.ANY).size());
634      
635         NodeRef newCat = categoryService.createCategory(newRoot, "Banana");
636         tx.commit();
637         tx = transactionService.getUserTransaction();
638         tx.begin();
639         assertEquals(3, categoryService.getRootCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass")).size());
640         assertEquals(3, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE).size());
641         assertEquals(5, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.ANY).size());
642      
643         categoryService.deleteCategory(newCat);
644         tx.commit();
645         tx = transactionService.getUserTransaction();
646         tx.begin();
647         assertEquals(3, categoryService.getRootCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass")).size());
648         assertEquals(3, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE).size());
649         assertEquals(4, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.ANY).size());
650      
651         categoryService.deleteCategory(newRoot);
652         tx.commit();
653         tx = transactionService.getUserTransaction();
654         tx.begin();
655         assertEquals(2, categoryService.getRootCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass")).size());
656         assertEquals(2, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.IMMEDIATE).size());
657         assertEquals(3, categoryService.getCategories(rootNodeRef.getStoreRef(), QName.createQName(TEST_NAMESPACE, "AssetClass"), CategoryService.Depth.ANY).size());
658      
659         
660         tx.rollback();
661     }
662     
663     private int getTotalScore(ResultSet results)
664     {
665         int totalScore = 0;
666         for(ResultSetRow row: results)
667         {
668             totalScore += row.getScore();
669         }
670         return totalScore;
671     }
672 }
673
Popular Tags