KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
21
22 import junit.framework.TestCase;
23
24 import org.alfresco.repo.content.AbstractContentStore;
25 import org.alfresco.repo.content.ContentStore;
26 import org.alfresco.repo.content.filestore.FileContentStore;
27 import org.alfresco.service.cmr.repository.ContentWriter;
28 import org.alfresco.util.GUID;
29 import org.alfresco.util.TempFileProvider;
30
31 /**
32  * Tests the content store replicator.
33  *
34  * @see org.alfresco.repo.content.replication.ContentStoreReplicator
35  *
36  * @author Derek Hulley
37  */

38 public class ContentStoreReplicatorTest extends TestCase
39 {
40     private static final String JavaDoc SOME_CONTENT = "The No. 1 Ladies' Detective Agency";
41     
42     private ContentStoreReplicator replicator;
43     private ContentStore sourceStore;
44     private ContentStore targetStore;
45     
46     @Override JavaDoc
47     public void setUp() throws Exception JavaDoc
48     {
49         super.setUp();
50         
51         File JavaDoc tempDir = TempFileProvider.getTempDir();
52         // create the source file store
53
String JavaDoc storeDir = tempDir.getAbsolutePath() + File.separatorChar + getName() + File.separatorChar + GUID.generate();
54         sourceStore = new FileContentStore(storeDir);
55         // create the target file store
56
storeDir = tempDir.getAbsolutePath() + File.separatorChar + getName() + File.separatorChar + GUID.generate();
57         targetStore = new FileContentStore(storeDir);
58         
59         // create the replicator
60
replicator = new ContentStoreReplicator();
61         replicator.setSourceStore(sourceStore);
62         replicator.setTargetStore(targetStore);
63         replicator.setRunContinuously(false); // replicate once
64
replicator.setWaitTime(0);
65     }
66     
67     /**
68      * Creates a source with some files and replicates in a single pass, checking the results.
69      */

70     public void testSinglePassReplication() throws Exception JavaDoc
71     {
72         ContentWriter writer = sourceStore.getWriter(null, null);
73         writer.putContent("123");
74         
75         // replicate
76
replicator.start();
77         
78         // wait a second
79
synchronized(this)
80         {
81             this.wait(1000L);
82         }
83         
84         assertTrue("Target store doesn't have content added to source",
85                 targetStore.exists(writer.getContentUrl()));
86         
87         // this was a single pass, so now more replication should be done
88
writer = sourceStore.getWriter(null, null);
89         writer.putContent("456");
90
91         // wait a second
92
synchronized(this)
93         {
94             this.wait(1000L);
95         }
96         
97         assertFalse("Replication should have been single-pass",
98                 targetStore.exists(writer.getContentUrl()));
99     }
100     
101     /**
102      * Adds content to the source while the replicator is going as fast as possible.
103      * Just to make it more interesting, the content is sometimes put in the target
104      * store as well.
105      * <p>
106      * Afterwards, some content is removed from the the target.
107      * <p>
108      * Then, finally, a check is performed to ensure that the source and target are
109      * in synch.
110      */

111     public void testContinuousReplication() throws Exception JavaDoc
112     {
113         replicator.setRunContinuously(true);
114         replicator.setWaitTime(0L);
115         replicator.start();
116         
117         String JavaDoc duplicateUrl = AbstractContentStore.createNewUrl();
118         // start the replicator - it won't wait between iterations
119
for (int i = 0; i < 10; i++)
120         {
121             // put some content into both the target and source
122
duplicateUrl = AbstractContentStore.createNewUrl();
123             ContentWriter duplicateTargetWriter = targetStore.getWriter(null, duplicateUrl);
124             ContentWriter duplicateSourceWriter = sourceStore.getWriter(null, duplicateUrl);
125             duplicateTargetWriter.putContent("Duplicate Target Content: " + i);
126             duplicateSourceWriter.putContent(duplicateTargetWriter.getReader());
127             
128             for (int j = 0; j < 100; j++)
129             {
130                 // write content
131
ContentWriter writer = sourceStore.getWriter(null, null);
132                 writer.putContent("Repeated put: " + j);
133             }
134         }
135         
136         // remove the last duplicated URL from the target
137
targetStore.delete(duplicateUrl);
138         
139         // allow time for the replicator to catch up
140
synchronized(this)
141         {
142             this.wait(1000L);
143         }
144         
145         // check that we have an exact match of URLs
146
Set JavaDoc<String JavaDoc> sourceUrls = sourceStore.getUrls();
147         Set JavaDoc<String JavaDoc> targetUrls = targetStore.getUrls();
148         
149         sourceUrls.containsAll(targetUrls);
150         targetUrls.contains(sourceUrls);
151     }
152 }
153
Popular Tags