Advertisement
shmoula

Untitled

Jun 15th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.18 KB | None | 0 0
  1. package cz.shmoula.test;
  2.  
  3. import java.util.List;
  4.  
  5. import junit.framework.Test;
  6. import junit.framework.TestCase;
  7. import junit.framework.TestSuite;
  8.  
  9. import org.alfresco.model.ContentModel;
  10. import org.alfresco.repo.security.authentication.AuthenticationUtil;
  11. import org.alfresco.service.ServiceRegistry;
  12. import org.alfresco.service.cmr.model.FileFolderService;
  13. import org.alfresco.service.cmr.model.FileInfo;
  14. import org.alfresco.service.cmr.repository.NodeRef;
  15. import org.alfresco.service.cmr.repository.NodeService;
  16. import org.alfresco.service.cmr.repository.StoreRef;
  17. import org.alfresco.service.cmr.search.ResultSet;
  18. import org.alfresco.service.cmr.search.SearchParameters;
  19. import org.alfresco.service.cmr.search.SearchService;
  20. import org.alfresco.service.namespace.QName;
  21. import org.alfresco.util.ApplicationContextHelper;
  22. import org.springframework.context.ApplicationContext;
  23.  
  24. /**
  25.  * Testcase for weird behaviour of searchService testing
  26.  * @author vbalak
  27.  *
  28.  */
  29. public class SearchServiceTest extends TestCase {
  30.     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
  31.    
  32.     private ServiceRegistry serviceRegistry;
  33.     private NodeService nodeService;
  34.     private SearchService searchService;
  35.     private FileFolderService fileFolderService;
  36.    
  37.     private static final String parentRef = "workspace://SpacesStore/9a1262e0-8467-459d-bc52-60c3cb99b58b";
  38.    
  39.     private NodeRef testingNodeRef;
  40.    
  41.    
  42.     public SearchServiceTest(String name) {
  43.         super(name);
  44.     }
  45.    
  46.     @Override
  47.     protected void setUp() throws Exception {
  48.         serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);
  49.         nodeService = serviceRegistry.getNodeService();
  50.         searchService = serviceRegistry.getSearchService();
  51.         fileFolderService = serviceRegistry.getFileFolderService();
  52.        
  53.         AuthenticationUtil.setRunAsUserSystem();
  54.        
  55.         createTestingContent();
  56.     }
  57.    
  58.     @Override
  59.     protected void tearDown() throws Exception {
  60.         removeTestingContent();
  61.        
  62.         AuthenticationUtil.clearCurrentSecurityContext();
  63.     }
  64.    
  65.     /**
  66.      * Creates testing content node under predefined space (see parentRef)
  67.      */
  68.     private void createTestingContent() {
  69.         NodeRef parentNodeRef = new NodeRef(parentRef);
  70.         QName pubWebContent = QName.createQName("http://www.alfresco.org/model/pub/0.1", "webContent");
  71.        
  72.         FileInfo fileInfo = fileFolderService.create(parentNodeRef, "TestingNode", pubWebContent);
  73.         testingNodeRef = fileInfo.getNodeRef();
  74.     }
  75.    
  76.     /**
  77.      * Removes testing content, if exists
  78.      */
  79.     private void removeTestingContent() {
  80.         if(testingNodeRef != null) {
  81.             nodeService.addAspect(testingNodeRef, ContentModel.ASPECT_TEMPORARY, null);
  82.             nodeService.deleteNode(testingNodeRef);
  83.         }
  84.     }
  85.    
  86.     /**
  87.      * Sets boolean property on testing node
  88.      * @param toProcess
  89.      */
  90.     private void setToProcessProperty(boolean toProcess) {
  91.         QName pubToProcess = QName.createQName("http://www.alfresco.org/model/pub/0.1", "toProcess");
  92.         nodeService.setProperty(testingNodeRef, pubToProcess, toProcess);
  93.     }
  94.    
  95.    
  96.     /**
  97.      * First test - right after node creation - there should be 0 items for both true and false
  98.      */
  99.     @org.junit.Test
  100.     public void testAfterCreation() {
  101.         assertEquals(0, getProcessableNodes("true").size());
  102.         assertEquals(0, getProcessableNodes("false").size());
  103.     }
  104.    
  105.     @org.junit.Test
  106.     public void testFalseProperty() {
  107.         setToProcessProperty(false);
  108.        
  109.         assertEquals(0, getProcessableNodes("true").size());
  110.         assertEquals(1, getProcessableNodes("false").size());
  111.     }
  112.    
  113.     @org.junit.Test
  114.     public void testTrueProperty() {
  115.         setToProcessProperty(true);
  116.        
  117.         assertEquals(1, getProcessableNodes("true").size());
  118.         assertEquals(0, getProcessableNodes("false").size());
  119.     }
  120.    
  121.     @org.junit.Test
  122.     public void testBothProperties() {
  123.         setToProcessProperty(false);
  124.        
  125.         assertEquals(0, getProcessableNodes("true").size());
  126.         assertEquals(1, getProcessableNodes("false").size());
  127.    
  128.         setToProcessProperty(true);
  129.        
  130.         assertEquals(1, getProcessableNodes("true").size());
  131.         assertEquals(0, getProcessableNodes("false").size());
  132.     }
  133.  
  134.     public static Test suite() {
  135.         TestSuite suite = new TestSuite();
  136.         suite.addTest(new SearchServiceTest("testAfterCreation"));
  137.         suite.addTest(new SearchServiceTest("testFalseProperty"));
  138.         suite.addTest(new SearchServiceTest("testTrueProperty"));
  139.         suite.addTest(new SearchServiceTest("testBothProperties"));
  140.         return suite;
  141.     }
  142.    
  143.    
  144.     /**
  145.      * Search for webContent nodes in specified space
  146.      * @param toProcess is a String, just to ensure what is really inside query
  147.      * @return collection of NodeRefs
  148.      */
  149.     private List<NodeRef> getProcessableNodes(String toProcess) {
  150.         List<NodeRef> nodeRefs = null;
  151.  
  152.         String query = "+PARENT:\"" + parentRef + "\""
  153.                 + " +TYPE:\"mispub:webContent\""
  154.                 + " + @mispub\\:toProcess:" + toProcess;
  155.  
  156.         SearchParameters sp = new SearchParameters();
  157.         StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
  158.         sp.addStore(storeRef);
  159.         sp.setLanguage(SearchService.LANGUAGE_LUCENE);
  160.  
  161.         sp.setQuery(query);
  162.  
  163.         ResultSet result = searchService.query(sp);
  164.        
  165.         try {
  166.             if (result != null)
  167.                 nodeRefs = result.getNodeRefs();
  168.         } finally {
  169.             result.close();
  170.         }
  171.  
  172.         return nodeRefs;
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement