Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package cz.shmoula.test;
- import java.util.List;
- import junit.framework.Test;
- import junit.framework.TestCase;
- import junit.framework.TestSuite;
- import org.alfresco.model.ContentModel;
- import org.alfresco.repo.security.authentication.AuthenticationUtil;
- import org.alfresco.service.ServiceRegistry;
- import org.alfresco.service.cmr.model.FileFolderService;
- import org.alfresco.service.cmr.model.FileInfo;
- import org.alfresco.service.cmr.repository.NodeRef;
- import org.alfresco.service.cmr.repository.NodeService;
- 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.namespace.QName;
- import org.alfresco.util.ApplicationContextHelper;
- import org.springframework.context.ApplicationContext;
- /**
- * Testcase for weird behaviour of searchService testing
- * @author vbalak
- *
- */
- public class SearchServiceTest extends TestCase {
- private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
- private ServiceRegistry serviceRegistry;
- private NodeService nodeService;
- private SearchService searchService;
- private FileFolderService fileFolderService;
- private static final String parentRef = "workspace://SpacesStore/9a1262e0-8467-459d-bc52-60c3cb99b58b";
- private NodeRef testingNodeRef;
- public SearchServiceTest(String name) {
- super(name);
- }
- @Override
- protected void setUp() throws Exception {
- serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
- nodeService = serviceRegistry.getNodeService();
- searchService = serviceRegistry.getSearchService();
- fileFolderService = serviceRegistry.getFileFolderService();
- AuthenticationUtil.setRunAsUserSystem();
- createTestingContent();
- }
- @Override
- protected void tearDown() throws Exception {
- removeTestingContent();
- AuthenticationUtil.clearCurrentSecurityContext();
- }
- /**
- * Creates testing content node under predefined space (see parentRef)
- */
- private void createTestingContent() {
- NodeRef parentNodeRef = new NodeRef(parentRef);
- QName pubWebContent = QName.createQName("http://www.alfresco.org/model/pub/0.1", "webContent");
- FileInfo fileInfo = fileFolderService.create(parentNodeRef, "TestingNode", pubWebContent);
- testingNodeRef = fileInfo.getNodeRef();
- }
- /**
- * Removes testing content, if exists
- */
- private void removeTestingContent() {
- if(testingNodeRef != null) {
- nodeService.addAspect(testingNodeRef, ContentModel.ASPECT_TEMPORARY, null);
- nodeService.deleteNode(testingNodeRef);
- }
- }
- /**
- * Sets boolean property on testing node
- * @param toProcess
- */
- private void setToProcessProperty(boolean toProcess) {
- QName pubToProcess = QName.createQName("http://www.alfresco.org/model/pub/0.1", "toProcess");
- nodeService.setProperty(testingNodeRef, pubToProcess, toProcess);
- }
- /**
- * First test - right after node creation - there should be 0 items for both true and false
- */
- @org.junit.Test
- public void testAfterCreation() {
- assertEquals(0, getProcessableNodes("true").size());
- assertEquals(0, getProcessableNodes("false").size());
- }
- @org.junit.Test
- public void testFalseProperty() {
- setToProcessProperty(false);
- assertEquals(0, getProcessableNodes("true").size());
- assertEquals(1, getProcessableNodes("false").size());
- }
- @org.junit.Test
- public void testTrueProperty() {
- setToProcessProperty(true);
- assertEquals(1, getProcessableNodes("true").size());
- assertEquals(0, getProcessableNodes("false").size());
- }
- @org.junit.Test
- public void testBothProperties() {
- setToProcessProperty(false);
- assertEquals(0, getProcessableNodes("true").size());
- assertEquals(1, getProcessableNodes("false").size());
- setToProcessProperty(true);
- assertEquals(1, getProcessableNodes("true").size());
- assertEquals(0, getProcessableNodes("false").size());
- }
- public static Test suite() {
- TestSuite suite = new TestSuite();
- suite.addTest(new SearchServiceTest("testAfterCreation"));
- suite.addTest(new SearchServiceTest("testFalseProperty"));
- suite.addTest(new SearchServiceTest("testTrueProperty"));
- suite.addTest(new SearchServiceTest("testBothProperties"));
- return suite;
- }
- /**
- * Search for webContent nodes in specified space
- * @param toProcess is a String, just to ensure what is really inside query
- * @return collection of NodeRefs
- */
- private List<NodeRef> getProcessableNodes(String toProcess) {
- List<NodeRef> nodeRefs = null;
- String query = "+PARENT:\"" + parentRef + "\""
- + " +TYPE:\"mispub:webContent\""
- + " + @mispub\\:toProcess:" + toProcess;
- SearchParameters sp = new SearchParameters();
- StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
- sp.addStore(storeRef);
- sp.setLanguage(SearchService.LANGUAGE_LUCENE);
- sp.setQuery(query);
- ResultSet result = searchService.query(sp);
- try {
- if (result != null)
- nodeRefs = result.getNodeRefs();
- } finally {
- result.close();
- }
- return nodeRefs;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement