KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > replication > ReplicatingContentStoreTest


1 /*
2  * Copyright (C) 2006 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.replication;
18
19 import java.io.File JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23 import java.util.concurrent.SynchronousQueue JavaDoc;
24 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
25 import java.util.concurrent.TimeUnit JavaDoc;
26
27 import org.alfresco.repo.content.AbstractContentReadWriteTest;
28 import org.alfresco.repo.content.ContentStore;
29 import org.alfresco.repo.content.filestore.FileContentStore;
30 import org.alfresco.repo.transaction.DummyTransactionService;
31 import org.alfresco.service.cmr.repository.ContentReader;
32 import org.alfresco.service.cmr.repository.ContentWriter;
33 import org.alfresco.util.GUID;
34 import org.alfresco.util.TempFileProvider;
35
36 /**
37  * Tests read and write functionality for the replicating store.
38  * <p>
39  * By default, replication is off for both the inbound and outbound
40  * replication. Specific tests change this.
41  *
42  * @see org.alfresco.repo.content.replication.ReplicatingContentStore
43  *
44  * @author Derek Hulley
45  */

46 public class ReplicatingContentStoreTest extends AbstractContentReadWriteTest
47 {
48     private static final String JavaDoc SOME_CONTENT = "The No. 1 Ladies' Detective Agency";
49     
50     private ReplicatingContentStore replicatingStore;
51     private ContentStore primaryStore;
52     private List JavaDoc<ContentStore> secondaryStores;
53     
54     @Override JavaDoc
55     public void setUp() throws Exception JavaDoc
56     {
57         super.setUp();
58         
59         File JavaDoc tempDir = TempFileProvider.getTempDir();
60         // create a primary file store
61
String JavaDoc storeDir = tempDir.getAbsolutePath() + File.separatorChar + GUID.generate();
62         primaryStore = new FileContentStore(storeDir);
63         // create some secondary file stores
64
secondaryStores = new ArrayList JavaDoc<ContentStore>(3);
65         for (int i = 0; i < 3; i++)
66         {
67             storeDir = tempDir.getAbsolutePath() + File.separatorChar + GUID.generate();
68             ContentStore store = new FileContentStore(storeDir);
69             secondaryStores.add(store);
70         }
71         replicatingStore = new ReplicatingContentStore();
72         replicatingStore.setTransactionService(new DummyTransactionService());
73         replicatingStore.setPrimaryStore(primaryStore);
74         replicatingStore.setSecondaryStores(secondaryStores);
75         replicatingStore.setOutbound(false);
76         replicatingStore.setInbound(false);
77     }
78
79     @Override JavaDoc
80     public ContentStore getStore()
81     {
82         return replicatingStore;
83     }
84     
85     /**
86      * Performs checks necessary to ensure the proper replication of content for the given
87      * URL
88      */

89     private void checkForReplication(boolean inbound, boolean outbound, String JavaDoc contentUrl, String JavaDoc content)
90     {
91         if (inbound)
92         {
93             ContentReader reader = primaryStore.getReader(contentUrl);
94             assertTrue("Content was not replicated into the primary store", reader.exists());
95             assertEquals("The replicated content was incorrect", content, reader.getContentString());
96         }
97         if (outbound)
98         {
99             for (ContentStore store : secondaryStores)
100             {
101                 ContentReader reader = store.getReader(contentUrl);
102                 assertTrue("Content was not replicated out to the secondary stores within a second", reader.exists());
103                 assertEquals("The replicated content was incorrect", content, reader.getContentString());
104             }
105         }
106     }
107     
108     /**
109      * Checks that the url is present in each of the stores
110      *
111      * @param contentUrl
112      * @param mustExist true if the content must exist, false if it must <b>not</b> exist
113      */

114     private void checkForUrl(String JavaDoc contentUrl, boolean mustExist)
115     {
116         // check that the URL is present for each of the stores
117
for (ContentStore store : secondaryStores)
118         {
119             Set JavaDoc<String JavaDoc> urls = store.getUrls();
120             assertTrue("URL of new content not present in store", urls.contains(contentUrl) == mustExist);
121         }
122     }
123     
124     public void testNoReplication() throws Exception JavaDoc
125     {
126         ContentWriter writer = getWriter();
127         writer.putContent(SOME_CONTENT);
128         
129         checkForReplication(false, false, writer.getContentUrl(), SOME_CONTENT);
130     }
131     
132     public void testOutboundReplication() throws Exception JavaDoc
133     {
134         replicatingStore.setOutbound(true);
135         
136         // write some content
137
ContentWriter writer = getWriter();
138         writer.putContent(SOME_CONTENT);
139         String JavaDoc contentUrl = writer.getContentUrl();
140         
141         checkForReplication(false, true, contentUrl, SOME_CONTENT);
142         
143         // check for outbound deletes
144
replicatingStore.delete(contentUrl);
145         checkForUrl(contentUrl, false);
146     }
147     
148     public void testAsyncOutboundReplication() throws Exception JavaDoc
149     {
150         ThreadPoolExecutor JavaDoc tpe = new ThreadPoolExecutor JavaDoc(1, 1, 60L, TimeUnit.SECONDS, new SynchronousQueue JavaDoc<Runnable JavaDoc>());
151         
152         replicatingStore.setOutbound(true);
153         replicatingStore.setOutboundThreadPoolExecutor(tpe);
154         
155         // write some content
156
ContentWriter writer = getWriter();
157         writer.putContent(SOME_CONTENT);
158         String JavaDoc contentUrl = writer.getContentUrl();
159         
160         // wait for a second
161
synchronized(this)
162         {
163             this.wait(1000L);
164         }
165         
166         checkForReplication(false, true, contentUrl, SOME_CONTENT);
167         
168         // check for outbound deletes
169
replicatingStore.delete(contentUrl);
170         checkForUrl(contentUrl, false);
171     }
172     
173     public void testInboundReplication() throws Exception JavaDoc
174     {
175         replicatingStore.setInbound(false);
176         
177         // pick a secondary store and write some content to it
178
ContentStore secondaryStore = secondaryStores.get(2);
179         ContentWriter writer = secondaryStore.getWriter(null, null);
180         writer.putContent(SOME_CONTENT);
181         String JavaDoc contentUrl = writer.getContentUrl();
182         
183         // get a reader from the replicating store
184
ContentReader reader = replicatingStore.getReader(contentUrl);
185         assertTrue("Reader must have been found in secondary store", reader.exists());
186         
187         // set inbound replication on and repeat
188
replicatingStore.setInbound(true);
189         reader = replicatingStore.getReader(contentUrl);
190         
191         // this time, it must have been replicated to the primary store
192
checkForReplication(true, false, contentUrl, SOME_CONTENT);
193     }
194 }
195
Popular Tags