1 17 package org.alfresco.repo.content.transform; 18 19 import java.util.Collections ; 20 import java.util.Map ; 21 22 import org.alfresco.repo.content.MimetypeMap; 23 import org.alfresco.repo.content.filestore.FileContentReader; 24 import org.alfresco.repo.content.filestore.FileContentWriter; 25 import org.alfresco.service.cmr.repository.ContentReader; 26 import org.alfresco.service.cmr.repository.ContentWriter; 27 import org.alfresco.service.cmr.repository.MimetypeService; 28 import org.alfresco.util.TempFileProvider; 29 30 35 public class ContentTransformerRegistryTest extends AbstractContentTransformerTest 36 { 37 private static final String A = MimetypeMap.MIMETYPE_TEXT_PLAIN; 38 private static final String B = MimetypeMap.MIMETYPE_XML; 39 private static final String C = MimetypeMap.MIMETYPE_WORD; 40 private static final String D = MimetypeMap.MIMETYPE_HTML; 41 42 43 private ContentTransformerRegistry registry; 44 45 private ContentTransformerRegistry dummyRegistry; 46 47 private ContentReader reader; 48 private ContentWriter writer; 49 50 53 public void setContentTransformerRegistry(ContentTransformerRegistry registry) 54 { 55 this.registry = registry; 56 } 57 58 @Override 59 public void onSetUpInTransaction() throws Exception 60 { 61 reader = new FileContentReader(TempFileProvider.createTempFile(getName(), ".txt")); 62 reader.setMimetype(A); 63 writer = new FileContentWriter(TempFileProvider.createTempFile(getName(), ".txt")); 64 writer.setMimetype(D); 65 66 byte[] bytes = new byte[256]; 67 for (int i = 0; i < 256; i++) 68 { 69 bytes[i] = (byte)i; 70 } 71 dummyRegistry = new ContentTransformerRegistry(mimetypeMap); 73 new DummyTransformer(mimetypeMap, dummyRegistry, A, B, 0.3, 10L); 75 new DummyTransformer(mimetypeMap, dummyRegistry, A, B, 0.6, 10L); 76 new DummyTransformer(mimetypeMap, dummyRegistry, A, C, 0.5, 10L); 77 new DummyTransformer(mimetypeMap, dummyRegistry, A, C, 1.0, 10L); 78 new DummyTransformer(mimetypeMap, dummyRegistry, B, C, 0.2, 10L); 79 new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L); 81 new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L); 82 new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 10L); new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L); 84 new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L); 85 } 86 87 90 public void testSetUp() throws Exception 91 { 92 super.testSetUp(); 93 assertNotNull(registry); 94 } 95 96 99 protected ContentTransformer getTransformer(String sourceMimetype, String targetMimetype) 100 { 101 return registry.getTransformer(sourceMimetype, targetMimetype); 102 } 103 104 public void testNullRetrieval() throws Exception 105 { 106 ContentTransformer transformer = null; 107 transformer = dummyRegistry.getTransformer(C, B); 108 assertNull("No transformer expected", transformer); 109 transformer = dummyRegistry.getTransformer(C, A); 110 assertNull("No transformer expected", transformer); 111 transformer = dummyRegistry.getTransformer(B, A); 112 assertNull("No transformer expected", transformer); 113 } 114 115 public void testSimpleRetrieval() throws Exception 116 { 117 ContentTransformer transformer = null; 118 transformer = dummyRegistry.getTransformer(B, C); 120 transformer = dummyRegistry.getTransformer(B, C); 121 assertNotNull("No transformer found", transformer); 122 assertEquals("Incorrect reliability", 0.2, transformer.getReliability(B, C)); 123 assertEquals("Incorrect reliability", 0.0, transformer.getReliability(C, B)); 124 } 125 126 131 public void testPerformanceRetrieval() throws Exception 132 { 133 ContentTransformer transformer1 = dummyRegistry.getTransformer(A, D); 135 assertEquals("Incorrect reliability", 1.0, transformer1.getReliability(A, D)); 136 assertEquals("Incorrect reliability", 0.0, transformer1.getReliability(D, A)); 137 assertEquals("Incorrect transformation time", 10L, transformer1.getTransformationTime()); 138 } 139 140 public void testScoredRetrieval() throws Exception 141 { 142 ContentTransformer transformer = null; 143 transformer = dummyRegistry.getTransformer(A, B); 145 assertNotNull("No transformer found", transformer); 146 assertEquals("Incorrect reliability", 0.6, transformer.getReliability(A, B)); 147 assertEquals("Incorrect reliability", 0.0, transformer.getReliability(B, A)); 148 transformer = dummyRegistry.getTransformer(A, C); 150 assertNotNull("No transformer found", transformer); 151 assertEquals("Incorrect reliability", 1.0, transformer.getReliability(A, C)); 152 assertEquals("Incorrect reliability", 0.0, transformer.getReliability(C, A)); 153 } 154 155 159 public void testExplicitTransformation() 160 { 161 AbstractContentTransformer dummyTransformer = new DummyTransformer( 162 mimetypeMap, 163 dummyRegistry, 164 MimetypeMap.MIMETYPE_FLASH, MimetypeMap.MIMETYPE_EXCEL, 165 1.0, 12345); 166 ContentTransformerRegistry.TransformationKey key = 168 new ContentTransformerRegistry.TransformationKey( 169 MimetypeMap.MIMETYPE_FLASH, MimetypeMap.MIMETYPE_EXCEL); 170 dummyTransformer.setExplicitTransformations(Collections.singletonList(key)); 171 dummyTransformer.register(); 173 174 ContentTransformer checkTransformer = dummyRegistry.getTransformer( 176 MimetypeMap.MIMETYPE_FLASH, MimetypeMap.MIMETYPE_EXCEL); 177 178 assertNotNull("No explicit transformer found", checkTransformer); 179 assertTrue("Expected explicit transformer", dummyTransformer == checkTransformer); 180 } 181 182 186 private static class DummyTransformer extends AbstractContentTransformer 187 { 188 private String sourceMimetype; 189 private String targetMimetype; 190 private double reliability; 191 private long transformationTime; 192 193 public DummyTransformer( 194 MimetypeService mimetypeService, 195 ContentTransformerRegistry registry, 196 String sourceMimetype, String targetMimetype, 197 double reliability, long transformationTime) 198 { 199 super.setMimetypeService(mimetypeService); 200 super.setRegistry(registry); 201 this.sourceMimetype = sourceMimetype; 202 this.targetMimetype = targetMimetype; 203 this.reliability = reliability; 204 this.transformationTime = transformationTime; 205 register(); 207 } 208 209 public double getReliability(String sourceMimetype, String targetMimetype) 210 { 211 if (this.sourceMimetype.equals(sourceMimetype) 212 && this.targetMimetype.equals(targetMimetype)) 213 { 214 return reliability; 215 } 216 else 217 { 218 return 0.0; 219 } 220 } 221 222 225 public void transformInternal( 226 ContentReader reader, 227 ContentWriter writer, 228 Map <String , Object > options) throws Exception 229 { 230 super.recordTime(transformationTime); 232 } 233 234 237 public synchronized long getTransformationTime() 238 { 239 return transformationTime; 240 } 241 } 242 } 243 | Popular Tags |