KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > metadata > AbstractMetadataExtracterTest


1 /*
2  * Copyright (C) 2005 Jesper Steen Møller
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.metadata;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.Serializable JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
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 /**
38  * @see org.alfresco.repo.content.metadata.MetadataExtracter
39  * @see org.alfresco.repo.content.metadata.AbstractMetadataExtracter
40  *
41  * @author Jesper Steen Møller
42  */

43 public abstract class AbstractMetadataExtracterTest extends TestCase
44 {
45     private static ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
46     
47     protected static final String JavaDoc QUICK_TITLE = "The quick brown fox jumps over the lazy dog";
48     protected static final String JavaDoc QUICK_DESCRIPTION = "Gym class featuring a brown fox and lazy dog";
49     protected static final String JavaDoc QUICK_CREATOR = "Nevin Nollop";
50
51     protected MimetypeMap mimetypeMap;
52
53     protected abstract MetadataExtracter getExtracter();
54
55     /**
56      * Ensures that the temp locations are cleaned out before the tests start
57      */

58     @Override JavaDoc
59     public void setUp() throws Exception JavaDoc
60     {
61         this.mimetypeMap = (MimetypeMap) ctx.getBean("mimetypeService");
62         
63         // perform a little cleaning up
64
long now = System.currentTimeMillis();
65         TempFileProvider.TempFileCleanerJob.removeFiles(now);
66     }
67
68     /**
69      * Check that all objects are present
70      */

71     public void testSetUp() throws Exception JavaDoc
72     {
73         assertNotNull("MimetypeMap not present", mimetypeMap);
74         // check that the quick resources are available
75
File JavaDoc sourceFile = AbstractContentTransformerTest.loadQuickTestFile("txt");
76         assertNotNull("quick.* files should be available from Tests", sourceFile);
77     }
78     
79     protected void testExtractFromMimetype(String JavaDoc mimetype) throws Exception JavaDoc
80     {
81         Map JavaDoc<QName, Serializable JavaDoc> properties = extractFromMimetype(mimetype);
82         // check
83
testCommonMetadata(mimetype, properties);
84     }
85
86     protected Map JavaDoc<QName, Serializable JavaDoc> extractFromMimetype(String JavaDoc mimetype) throws Exception JavaDoc
87     {
88         Map JavaDoc<QName, Serializable JavaDoc> properties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
89         
90         // get the extension for the mimetype
91
String JavaDoc ext = mimetypeMap.getExtension(mimetype);
92
93         // attempt to get a source file for each mimetype
94
File JavaDoc sourceFile = AbstractContentTransformerTest.loadQuickTestFile(ext);
95         if (sourceFile == null)
96         {
97             throw new FileNotFoundException JavaDoc("No quick." + ext + " file found for test");
98         }
99
100         // construct a reader onto the source file
101
ContentReader sourceReader = new FileContentReader(sourceFile);
102         sourceReader.setMimetype(mimetype);
103         getExtracter().extract(sourceReader, properties);
104         return properties;
105     }
106
107     protected void testCommonMetadata(String JavaDoc mimetype, Map JavaDoc<QName, Serializable JavaDoc> 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