KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > search > SearchServiceTest


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;
18
19 import javax.transaction.UserTransaction JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.alfresco.model.ContentModel;
24 import org.alfresco.repo.security.authentication.AuthenticationComponent;
25 import org.alfresco.repo.security.authentication.MutableAuthenticationDao;
26 import org.alfresco.service.ServiceRegistry;
27 import org.alfresco.service.cmr.repository.NodeRef;
28 import org.alfresco.service.cmr.repository.NodeService;
29 import org.alfresco.service.cmr.repository.StoreRef;
30 import org.alfresco.service.cmr.search.LimitBy;
31 import org.alfresco.service.cmr.search.PermissionEvaluationMode;
32 import org.alfresco.service.cmr.search.ResultSet;
33 import org.alfresco.service.cmr.search.SearchParameters;
34 import org.alfresco.service.cmr.search.SearchService;
35 import org.alfresco.service.cmr.security.AuthenticationService;
36 import org.alfresco.service.cmr.security.PermissionService;
37 import org.alfresco.service.namespace.QName;
38 import org.alfresco.service.transaction.TransactionService;
39 import org.alfresco.util.ApplicationContextHelper;
40 import org.springframework.context.ApplicationContext;
41
42 public class SearchServiceTest extends TestCase
43 {
44     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
45
46     private AuthenticationComponent authenticationComponent;
47
48     private AuthenticationService authenticationService;
49
50     private MutableAuthenticationDao authenticationDAO;
51
52     private UserTransaction JavaDoc tx;
53
54     private SearchService pubSearchService;
55
56     private NodeRef rootNodeRef;
57
58     private NodeRef n1;
59
60     private NodeRef n2;
61
62     private NodeRef n3;
63
64     private NodeRef n4;
65
66     private NodeRef n6;
67
68     private NodeRef n5;
69
70     private NodeRef n7;
71
72     private NodeRef n8;
73
74     private NodeRef n9;
75
76     private NodeRef n10;
77
78     private NodeService nodeService;
79
80     private PermissionService pubPermissionService;
81
82     public SearchServiceTest()
83     {
84         super();
85     }
86
87     public void setUp() throws Exception JavaDoc
88     {
89         nodeService = (NodeService) ctx.getBean("dbNodeService");
90         authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponentImpl");
91         authenticationService = (AuthenticationService) ctx.getBean("authenticationService");
92         authenticationDAO = (MutableAuthenticationDao) ctx.getBean("alfDaoImpl");
93         pubSearchService = (SearchService) ctx.getBean("SearchService");
94         pubPermissionService = (PermissionService) ctx.getBean("PermissionService");
95
96         this.authenticationComponent.setSystemUserAsCurrentUser();
97
98         TransactionService transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE
99                 .getLocalName());
100         tx = transactionService.getUserTransaction();
101         tx.begin();
102
103         if (!authenticationDAO.userExists("andy"))
104         {
105             authenticationService.createAuthentication("andy", "andy".toCharArray());
106         }
107
108         if (!authenticationDAO.userExists("admin"))
109         {
110             authenticationService.createAuthentication("admin", "admin".toCharArray());
111         }
112
113         if (!authenticationDAO.userExists("administrator"))
114         {
115             authenticationService.createAuthentication("administrator", "administrator".toCharArray());
116         }
117
118         StoreRef storeRef = nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
119         rootNodeRef = nodeService.getRootNode(storeRef);
120
121         n1 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}01"),
122                 ContentModel.TYPE_CONTAINER).getChildRef();
123         pubPermissionService.setPermission(n1, "andy", "Read", true);
124         n2 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}02"),
125                 ContentModel.TYPE_CONTAINER).getChildRef();
126         pubPermissionService.setPermission(n2, "andy", "Read", true);
127         n3 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}03"),
128                 ContentModel.TYPE_CONTAINER).getChildRef();
129         pubPermissionService.setPermission(n3, "andy", "Read", true);
130         n4 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}04"),
131                 ContentModel.TYPE_CONTAINER).getChildRef();
132         pubPermissionService.setPermission(n4, "andy", "Read", true);
133         n5 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}05"),
134                 ContentModel.TYPE_CONTAINER).getChildRef();
135         pubPermissionService.setPermission(n5, "andy", "Read", true);
136         n6 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}06"),
137                 ContentModel.TYPE_CONTAINER).getChildRef();
138         n7 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}07"),
139                 ContentModel.TYPE_CONTAINER).getChildRef();
140         n8 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}08"),
141                 ContentModel.TYPE_CONTAINER).getChildRef();
142         n9 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}09"),
143                 ContentModel.TYPE_CONTAINER).getChildRef();
144         n10 = nodeService.createNode(rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}10"),
145                 ContentModel.TYPE_CONTAINER).getChildRef();
146
147     }
148
149     @Override JavaDoc
150     protected void tearDown() throws Exception JavaDoc
151     {
152         authenticationComponent.clearCurrentSecurityContext();
153         tx.rollback();
154         super.tearDown();
155     }
156
157     public void testAdmim()
158     {
159         authenticationComponent.setCurrentUser("admin");
160         SearchParameters sp = new SearchParameters();
161         sp.setLanguage(SearchService.LANGUAGE_LUCENE);
162         sp.setQuery("PATH:\"//*\"");
163         sp.addStore(rootNodeRef.getStoreRef());
164         ResultSet results = pubSearchService.query(sp);
165         assertEquals(results.length(), 10);
166         assertNotNull(results.getResultSetMetaData());
167         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
168         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
169         results.close();
170
171         sp.setLimitBy(LimitBy.FINAL_SIZE);
172         sp.setLimit(20);
173         results = pubSearchService.query(sp);
174         assertEquals(results.length(), 10);
175         assertNotNull(results.getResultSetMetaData());
176         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
177         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
178         results.close();
179
180         sp.setLimitBy(LimitBy.FINAL_SIZE);
181         sp.setLimit(10);
182         results = pubSearchService.query(sp);
183         assertEquals(results.length(), 10);
184         assertNotNull(results.getResultSetMetaData());
185         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
186         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
187         results.close();
188
189         sp.setLimitBy(LimitBy.FINAL_SIZE);
190         sp.setLimit(9);
191         results = pubSearchService.query(sp);
192         assertEquals(results.length(), 9);
193         assertNotNull(results.getResultSetMetaData());
194         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.FINAL_SIZE);
195         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
196         results.close();
197         
198         sp.setLimitBy(LimitBy.FINAL_SIZE);
199         sp.setLimit(5);
200         results = pubSearchService.query(sp);
201         assertEquals(results.length(), 5);
202         assertNotNull(results.getResultSetMetaData());
203         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.FINAL_SIZE);
204         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
205         results.close();
206     }
207
208     public void testAndy()
209     {
210         authenticationComponent.setCurrentUser("andy");
211         SearchParameters sp = new SearchParameters();
212         sp.setLanguage(SearchService.LANGUAGE_LUCENE);
213         sp.setQuery("PATH:\"//*\"");
214         sp.addStore(rootNodeRef.getStoreRef());
215         ResultSet results = pubSearchService.query(sp);
216         assertEquals(results.length(), 5);
217         assertNotNull(results.getResultSetMetaData());
218         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
219         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
220         results.close();
221
222         sp.setLimitBy(LimitBy.FINAL_SIZE);
223         sp.setLimit(20);
224         results = pubSearchService.query(sp);
225         assertEquals(results.length(), 5);
226         assertNotNull(results.getResultSetMetaData());
227         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
228         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
229         results.close();
230
231         sp.setLimitBy(LimitBy.FINAL_SIZE);
232         sp.setLimit(5);
233         results = pubSearchService.query(sp);
234         assertEquals(results.length(), 5);
235         assertNotNull(results.getResultSetMetaData());
236         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.UNLIMITED);
237         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
238         results.close();
239
240         sp.setLimitBy(LimitBy.FINAL_SIZE);
241         sp.setLimit(4);
242         results = pubSearchService.query(sp);
243         assertEquals(results.length(), 4);
244         assertNotNull(results.getResultSetMetaData());
245         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.FINAL_SIZE);
246         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
247         results.close();
248         
249         sp.setLimitBy(LimitBy.FINAL_SIZE);
250         sp.setLimit(2);
251         results = pubSearchService.query(sp);
252         assertEquals(results.length(), 2);
253         assertNotNull(results.getResultSetMetaData());
254         assertEquals(results.getResultSetMetaData().getLimitedBy(), LimitBy.FINAL_SIZE);
255         assertEquals(results.getResultSetMetaData().getPermissionEvaluationMode(), PermissionEvaluationMode.EAGER);
256         results.close();
257     }
258 }
259
Popular Tags