1 17 package org.alfresco.repo.content.metadata; 18 19 import java.io.File ; 20 import java.io.FileNotFoundException ; 21 import java.io.Serializable ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import junit.framework.TestCase; 26 27 import org.alfresco.model.ContentModel; 28 import org.alfresco.repo.content.MimetypeMap; 29 import org.alfresco.repo.content.filestore.FileContentReader; 30 import org.alfresco.repo.content.transform.AbstractContentTransformerTest; 31 import org.alfresco.service.cmr.repository.ContentReader; 32 import org.alfresco.service.namespace.QName; 33 import org.alfresco.util.ApplicationContextHelper; 34 import org.alfresco.util.TempFileProvider; 35 import org.springframework.context.ApplicationContext; 36 37 43 public abstract class AbstractMetadataExtracterTest extends TestCase 44 { 45 private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext(); 46 47 protected static final String QUICK_TITLE = "The quick brown fox jumps over the lazy dog"; 48 protected static final String QUICK_DESCRIPTION = "Gym class featuring a brown fox and lazy dog"; 49 protected static final String QUICK_CREATOR = "Nevin Nollop"; 50 51 protected MimetypeMap mimetypeMap; 52 53 protected abstract MetadataExtracter getExtracter(); 54 55 58 @Override 59 public void setUp() throws Exception 60 { 61 this.mimetypeMap = (MimetypeMap) ctx.getBean("mimetypeService"); 62 63 long now = System.currentTimeMillis(); 65 TempFileProvider.TempFileCleanerJob.removeFiles(now); 66 } 67 68 71 public void testSetUp() throws Exception 72 { 73 assertNotNull("MimetypeMap not present", mimetypeMap); 74 File sourceFile = AbstractContentTransformerTest.loadQuickTestFile("txt"); 76 assertNotNull("quick.* files should be available from Tests", sourceFile); 77 } 78 79 protected void testExtractFromMimetype(String mimetype) throws Exception 80 { 81 Map <QName, Serializable > properties = extractFromMimetype(mimetype); 82 testCommonMetadata(mimetype, properties); 84 } 85 86 protected Map <QName, Serializable > extractFromMimetype(String mimetype) throws Exception 87 { 88 Map <QName, Serializable > properties = new HashMap <QName, Serializable >(); 89 90 String ext = mimetypeMap.getExtension(mimetype); 92 93 File sourceFile = AbstractContentTransformerTest.loadQuickTestFile(ext); 95 if (sourceFile == null) 96 { 97 throw new FileNotFoundException ("No quick." + ext + " file found for test"); 98 } 99 100 ContentReader sourceReader = new FileContentReader(sourceFile); 102 sourceReader.setMimetype(mimetype); 103 getExtracter().extract(sourceReader, properties); 104 return properties; 105 } 106 107 protected void testCommonMetadata(String mimetype, Map <QName, Serializable > properties) 108 { 109 assertEquals( 110 "Property " + ContentModel.PROP_TITLE + " not found for mimetype " + mimetype, 111 QUICK_TITLE, properties.get(ContentModel.PROP_TITLE)); 112 assertEquals( 113 "Property " + ContentModel.PROP_DESCRIPTION + " not found for mimetype " + mimetype, 114 QUICK_DESCRIPTION, properties.get(ContentModel.PROP_DESCRIPTION)); 115 } 116 } 117 | Popular Tags |