1 17 package org.alfresco.repo.content.replication; 18 19 import java.io.File ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Set ; 23 import java.util.concurrent.SynchronousQueue ; 24 import java.util.concurrent.ThreadPoolExecutor ; 25 import java.util.concurrent.TimeUnit ; 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 46 public class ReplicatingContentStoreTest extends AbstractContentReadWriteTest 47 { 48 private static final String SOME_CONTENT = "The No. 1 Ladies' Detective Agency"; 49 50 private ReplicatingContentStore replicatingStore; 51 private ContentStore primaryStore; 52 private List <ContentStore> secondaryStores; 53 54 @Override 55 public void setUp() throws Exception 56 { 57 super.setUp(); 58 59 File tempDir = TempFileProvider.getTempDir(); 60 String storeDir = tempDir.getAbsolutePath() + File.separatorChar + GUID.generate(); 62 primaryStore = new FileContentStore(storeDir); 63 secondaryStores = new ArrayList <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 80 public ContentStore getStore() 81 { 82 return replicatingStore; 83 } 84 85 89 private void checkForReplication(boolean inbound, boolean outbound, String contentUrl, String 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 114 private void checkForUrl(String contentUrl, boolean mustExist) 115 { 116 for (ContentStore store : secondaryStores) 118 { 119 Set <String > 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 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 133 { 134 replicatingStore.setOutbound(true); 135 136 ContentWriter writer = getWriter(); 138 writer.putContent(SOME_CONTENT); 139 String contentUrl = writer.getContentUrl(); 140 141 checkForReplication(false, true, contentUrl, SOME_CONTENT); 142 143 replicatingStore.delete(contentUrl); 145 checkForUrl(contentUrl, false); 146 } 147 148 public void testAsyncOutboundReplication() throws Exception 149 { 150 ThreadPoolExecutor tpe = new ThreadPoolExecutor (1, 1, 60L, TimeUnit.SECONDS, new SynchronousQueue <Runnable >()); 151 152 replicatingStore.setOutbound(true); 153 replicatingStore.setOutboundThreadPoolExecutor(tpe); 154 155 ContentWriter writer = getWriter(); 157 writer.putContent(SOME_CONTENT); 158 String contentUrl = writer.getContentUrl(); 159 160 synchronized(this) 162 { 163 this.wait(1000L); 164 } 165 166 checkForReplication(false, true, contentUrl, SOME_CONTENT); 167 168 replicatingStore.delete(contentUrl); 170 checkForUrl(contentUrl, false); 171 } 172 173 public void testInboundReplication() throws Exception 174 { 175 replicatingStore.setInbound(false); 176 177 ContentStore secondaryStore = secondaryStores.get(2); 179 ContentWriter writer = secondaryStore.getWriter(null, null); 180 writer.putContent(SOME_CONTENT); 181 String contentUrl = writer.getContentUrl(); 182 183 ContentReader reader = replicatingStore.getReader(contentUrl); 185 assertTrue("Reader must have been found in secondary store", reader.exists()); 186 187 replicatingStore.setInbound(true); 189 reader = replicatingStore.getReader(contentUrl); 190 191 checkForReplication(true, false, contentUrl, SOME_CONTENT); 193 } 194 } 195 | Popular Tags |