KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > cleanup > ContentStoreCleanerTest


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.content.cleanup;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.List JavaDoc;
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 /**
39  * @see org.alfresco.repo.content.cleanup.ContentStoreCleaner
40  *
41  * @author Derek Hulley
42  */

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 JavaDoc<String JavaDoc> deletedUrls;
51     
52     @Override JavaDoc
53     public void setUp() throws Exception JavaDoc
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         // we need a store
61
store = new FileContentStore(TempFileProvider.getTempDir().getAbsolutePath());
62         // and a listener
63
listener = new DummyCleanerListener();
64         // initialise record of deleted URLs
65
deletedUrls = new ArrayList JavaDoc<String JavaDoc>(5);
66         
67         // construct the test cleaner
68
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 JavaDoc
77     {
78         cleaner.setProtectDays(0);
79         // add some content to the store
80
ContentWriter writer = store.getWriter(null, null);
81         writer.putContent("ABC");
82         String JavaDoc contentUrl = writer.getContentUrl();
83         
84         // fire the cleaner
85
cleaner.execute();
86         
87         // the content should have disappeared as it is not in the database
88
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 JavaDoc
93     {
94         cleaner.setProtectDays(1);
95         // add some content to the store
96
ContentWriter writer = store.getWriter(null, null);
97         writer.putContent("ABC");
98         String JavaDoc contentUrl = writer.getContentUrl();
99         
100         // fire the cleaner
101
cleaner.execute();
102         
103         // the content should have disappeared as it is not in the database
104
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