1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.URL ; 22 import java.util.List ; 23 24 import org.alfresco.repo.content.MimetypeMap; 25 import org.alfresco.repo.content.filestore.FileContentReader; 26 import org.alfresco.repo.content.filestore.FileContentWriter; 27 import org.alfresco.service.cmr.repository.ContentReader; 28 import org.alfresco.service.cmr.repository.ContentWriter; 29 import org.alfresco.util.BaseSpringTest; 30 import org.alfresco.util.TempFileProvider; 31 32 39 public abstract class AbstractContentTransformerTest extends BaseSpringTest 40 { 41 private static String QUICK_CONTENT = "The quick brown fox jumps over the lazy dog"; 42 private static String [] QUICK_WORDS = new String [] { 43 "quick", "brown", "fox", "jumps", "lazy", "dog"}; 44 45 protected MimetypeMap mimetypeMap; 46 47 public final void setMimetypeMap(MimetypeMap mimetypeMap) 48 { 49 this.mimetypeMap = mimetypeMap; 50 } 51 52 63 protected abstract ContentTransformer getTransformer(String sourceMimetype, String targetMimetype); 64 65 68 @Override 69 protected void onSetUpInTransaction() throws Exception 70 { 71 long now = System.currentTimeMillis(); 73 TempFileProvider.TempFileCleanerJob.removeFiles(now); 74 } 75 76 79 public void testSetUp() throws Exception 80 { 81 assertNotNull("MimetypeMap not present", mimetypeMap); 82 File sourceFile = AbstractContentTransformerTest.loadQuickTestFile("txt"); 84 assertNotNull(sourceFile); 85 } 86 87 96 public static File loadQuickTestFile(String extension) throws IOException 97 { 98 URL url = AbstractContentTransformerTest.class.getClassLoader().getResource("quick/quick." + extension); 99 if (url == null) 100 { 101 return null; 102 } 103 File file = new File (url.getFile()); 104 if (!file.exists()) 105 { 106 return null; 107 } 108 return file; 109 } 110 111 124 public void testAllConversions() throws Exception 125 { 126 List <String > mimetypes = mimetypeMap.getMimetypes(); 128 for (String sourceMimetype : mimetypes) 129 { 130 String sourceExtension = mimetypeMap.getExtension(sourceMimetype); 132 File sourceFile = AbstractContentTransformerTest.loadQuickTestFile(sourceExtension); 133 if (sourceFile == null) 134 { 135 continue; } 137 138 for (String targetMimetype : mimetypes) 140 { 141 ContentWriter targetWriter = null; 142 ContentReader sourceReader = new FileContentReader(sourceFile); 144 145 int count = 0; 147 for (int i = 0; i < 5; i++) 148 { 149 ContentTransformer transformer = getTransformer(sourceMimetype, targetMimetype); 151 if (transformer == null) 152 { 153 break; } 155 else if (transformer.getReliability(sourceMimetype, targetMimetype) <= 0.0) 156 { 157 break; } 159 160 String targetExtension = mimetypeMap.getExtension(targetMimetype); 162 File targetFile = TempFileProvider.createTempFile( 163 getClass().getSimpleName() + "_" + getName() + "_" + sourceExtension + "_", 164 "." + targetExtension); 165 targetWriter = new FileContentWriter(targetFile); 166 167 sourceReader.setMimetype(sourceMimetype); 169 targetWriter.setMimetype(targetMimetype); 170 transformer.transform(sourceReader.getReader(), targetWriter); 171 172 if (targetMimetype.equals(MimetypeMap.MIMETYPE_TEXT_PLAIN)) 174 { 175 ContentReader targetReader = targetWriter.getReader(); 176 String checkContent = targetReader.getContentString(); 177 assertTrue("Quick phrase not present in document converted to text: \n" + 178 " transformer: " + transformer + "\n" + 179 " source: " + sourceReader + "\n" + 180 " target: " + targetWriter, 181 checkContent.contains(QUICK_CONTENT)); 182 } 183 else if (targetMimetype.startsWith(StringExtractingContentTransformer.PREFIX_TEXT)) 184 { 185 ContentReader targetReader = targetWriter.getReader(); 186 String checkContent = targetReader.getContentString(); 187 for (int word = 0; word < QUICK_WORDS.length; word++) 189 { 190 assertTrue("Quick phrase word not present in document converted to text: \n" + 191 " transformer: " + transformer + "\n" + 192 " source: " + sourceReader + "\n" + 193 " target: " + targetWriter + "\n" + 194 " word: " + word, 195 checkContent.contains(QUICK_WORDS[word])); 196 } 197 } 198 count++; 200 } 201 202 if (logger.isDebugEnabled()) 203 { 204 logger.debug("Transformation performed " + count + " time: " + 205 sourceMimetype + " --> " + targetMimetype + "\n" + 206 " source: " + sourceReader + "\n" + 207 " target: " + targetWriter + "\n" + 208 " transformer: " + getTransformer(sourceMimetype, targetMimetype)); 209 } 210 } 211 } 212 } 213 } 214 | Popular Tags |