Many a times I was struggling when trying to run test cases those comes bundled with Alfresco. Got very less help on forums to setup environment for it and create Custom Unit test cases.
Attached Test Case runs with Alfresco version 3.3.5 and 4.0.2
If You are running this Test Case in eclipse, please add external class folder (given below ) into libraries section.
Alfresco_Home/tomcat/shared/classes and
Alfresco_Home/tomcat/webapps/alfresco/WEB-INF/classes
There are three different approaches I have used to demonstrate multiple ways to implement search in your application.
package com.search;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchParameters;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.ApplicationContextHelper;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
public class CustomSearchServiceTest{
private final static Logger logger = Logger.getLogger(CustomSearchServiceTest.class);
private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
private AuthenticationComponent authenticationComponent;
private SearchService pubSearchService;
private TransactionService transactionService;
@Before
public void setUp() throws Exception {
authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
pubSearchService = (SearchService) ctx.getBean("SearchService");
transactionService = (TransactionService) ctx.getBean(ServiceRegistry.TRANSACTION_SERVICE.getLocalName());
}
@After
public void tearDown() throws Exception {
logger.debug("tearDown");
}
@Test
public void testSearchParametersApproach() {
logger.debug("testApproachFirst");
String luceneQuery = "( TYPE:\"content\" AND (@cm\\:name:\"*Alfresco*\"))";
SearchParameters sp = new SearchParameters();
sp.setLanguage(SearchService.LANGUAGE_LUCENE);
sp.setQuery(luceneQuery);
sp.addStore(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE);
authenticationComponent.setSystemUserAsCurrentUser();
ResultSet results = pubSearchService.query(sp);
logger.debug("results :::: " + results);
try {
if (results != null && results.length() > 0) {
logger.debug("nodeRef results 0 ::::" + results.getNodeRef(0));
}
} finally {
if (results != null) {
results.close();
}
}
}
@Test
public void testRunAsWorkApproach() {
logger.debug("testApproachSecond");
RunAsWork indexRunAs = new RunAsWork() {
public NodeRef doWork() {
NodeRef nodeRef = null;
try {
String luceneQuery = "( TYPE:\"content\" AND (@cm\\:name:\"*Alfresco*\"))";
ResultSet resultSet = pubSearchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_LUCENE, luceneQuery);
if (resultSet != null && resultSet.length() > 0) {
nodeRef = resultSet.getNodeRef(0);
}
if (resultSet != null) {
resultSet.close();
}
logger.debug("nodeRef results 0 ::::" + nodeRef);
} catch (Exception e) {
e.printStackTrace();
}
return nodeRef;
}
};
NodeRef results = AuthenticationUtil.runAs(indexRunAs, AuthenticationUtil.getAdminUserName());
}
@Test
public void testRetryingTransactionHelperApproach() {
logger.debug("testApproachThird");
authenticationComponent.setSystemUserAsCurrentUser();
RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
RetryingTransactionCallback callback = new RetryingTransactionCallback() {
public NodeRef execute() throws Throwable {
NodeRef nodeRef = null;
String luceneQuery = "( TYPE:\"content\" AND (@cm\\:name:\"*Alfresco*\"))";
ResultSet resultSet = pubSearchService.query(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, SearchService.LANGUAGE_LUCENE, luceneQuery);
if (resultSet != null && resultSet.length() > 0) {
nodeRef = resultSet.getNodeRef(0);
}
if (resultSet != null) {
resultSet.close();
}
return nodeRef;
}
};
NodeRef results = txnHelper.doInTransaction(callback, false, true);
logger.debug("nodeRef results ::::" + results);
}
}
No comments:
Post a Comment