1 17 package org.alfresco.repo.content.cleanup; 18 19 import java.util.ArrayList ; 20 import java.util.Collections ; 21 import java.util.List ; 22 23 import org.alfresco.repo.content.ContentStore; 24 import org.alfresco.repo.content.filestore.FileContentStore; 25 import org.alfresco.repo.node.db.NodeDaoService; 26 import org.alfresco.service.ServiceRegistry; 27 import org.alfresco.service.cmr.dictionary.DictionaryService; 28 import org.alfresco.service.cmr.repository.ContentIOException; 29 import org.alfresco.service.cmr.repository.ContentReader; 30 import org.alfresco.service.cmr.repository.ContentWriter; 31 import org.alfresco.service.transaction.TransactionService; 32 import org.alfresco.util.ApplicationContextHelper; 33 import org.alfresco.util.TempFileProvider; 34 import org.springframework.context.ApplicationContext; 35 36 import junit.framework.TestCase; 37 38 43 public class ContentStoreCleanerTest extends TestCase 44 { 45 private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 46 47 private ContentStoreCleaner cleaner; 48 private ContentStore store; 49 private ContentStoreCleanerListener listener; 50 private List <String > deletedUrls; 51 52 @Override 53 public void setUp() throws Exception 54 { 55 ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean("ServiceRegistry"); 56 TransactionService transactionService = serviceRegistry.getTransactionService(); 57 DictionaryService dictionaryService = serviceRegistry.getDictionaryService(); 58 NodeDaoService nodeDaoService = (NodeDaoService) ctx.getBean("nodeDaoService"); 59 60 store = new FileContentStore(TempFileProvider.getTempDir().getAbsolutePath()); 62 listener = new DummyCleanerListener(); 64 deletedUrls = new ArrayList <String >(5); 66 67 cleaner = new ContentStoreCleaner(); 69 cleaner.setTransactionService(transactionService); 70 cleaner.setDictionaryService(dictionaryService); 71 cleaner.setNodeDaoService(nodeDaoService); 72 cleaner.setStores(Collections.singletonList(store)); 73 cleaner.setListeners(Collections.singletonList(listener)); 74 } 75 76 public void testImmediateRemoval() throws Exception 77 { 78 cleaner.setProtectDays(0); 79 ContentWriter writer = store.getWriter(null, null); 81 writer.putContent("ABC"); 82 String contentUrl = writer.getContentUrl(); 83 84 cleaner.execute(); 86 87 assertFalse("Unprotected content was not deleted", store.exists(contentUrl)); 89 assertTrue("Content listener was not called with deletion", deletedUrls.contains(contentUrl)); 90 } 91 92 public void testProtectedRemoval() throws Exception 93 { 94 cleaner.setProtectDays(1); 95 ContentWriter writer = store.getWriter(null, null); 97 writer.putContent("ABC"); 98 String contentUrl = writer.getContentUrl(); 99 100 cleaner.execute(); 102 103 assertTrue("Protected content was deleted", store.exists(contentUrl)); 105 assertFalse("Content listener was called with deletion of protected URL", deletedUrls.contains(contentUrl)); 106 } 107 108 private class DummyCleanerListener implements ContentStoreCleanerListener 109 { 110 public void beforeDelete(ContentReader reader) throws ContentIOException 111 { 112 deletedUrls.add(reader.getContentUrl()); 113 } 114 } 115 } 116 | Popular Tags |