KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > content > transform > AbstractContentTransformerTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
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.transform;
18
19 import java.io.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.util.List JavaDoc;
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 /**
33  * Provides a base set of tests for testing
34  * {@link org.alfresco.repo.content.transform.ContentTransformer}
35  * implementations.
36  *
37  * @author Derek Hulley
38  */

39 public abstract class AbstractContentTransformerTest extends BaseSpringTest
40 {
41     private static String JavaDoc QUICK_CONTENT = "The quick brown fox jumps over the lazy dog";
42     private static String JavaDoc[] QUICK_WORDS = new String JavaDoc[] {
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     /**
53      * Fetches a transformer to test for a given transformation. The transformer
54      * does not <b>have</b> to be reliable for the given format - if it isn't
55      * then it will be ignored.
56      *
57      * @param sourceMimetype the sourceMimetype to be tested
58      * @param targetMimetype the targetMimetype to be tested
59      * @return Returns the <tt>ContentTranslators</tt> that will be tested by
60      * the methods implemented in this class. A null return value is
61      * acceptable if the source and target mimetypes are not of interest.
62      */

63     protected abstract ContentTransformer getTransformer(String JavaDoc sourceMimetype, String JavaDoc targetMimetype);
64
65     /**
66      * Ensures that the temp locations are cleaned out before the tests start
67      */

68     @Override JavaDoc
69     protected void onSetUpInTransaction() throws Exception JavaDoc
70     {
71         // perform a little cleaning up
72
long now = System.currentTimeMillis();
73         TempFileProvider.TempFileCleanerJob.removeFiles(now);
74     }
75
76     /**
77      * Check that all objects are present
78      */

79     public void testSetUp() throws Exception JavaDoc
80     {
81         assertNotNull("MimetypeMap not present", mimetypeMap);
82         // check that the quick resources are available
83
File JavaDoc sourceFile = AbstractContentTransformerTest.loadQuickTestFile("txt");
84         assertNotNull(sourceFile);
85     }
86     
87     /**
88      * Helper method to load one of the "The quick brown fox" files from the
89      * classpath.
90      *
91      * @param extension the extension of the file required
92      * @return Returns a test resource loaded from the classpath or <tt>null</tt> if
93      * no resource could be found.
94      * @throws IOException
95      */

96     public static File JavaDoc loadQuickTestFile(String JavaDoc extension) throws IOException JavaDoc
97     {
98         URL JavaDoc url = AbstractContentTransformerTest.class.getClassLoader().getResource("quick/quick." + extension);
99         if (url == null)
100         {
101             return null;
102         }
103         File JavaDoc file = new File JavaDoc(url.getFile());
104         if (!file.exists())
105         {
106             return null;
107         }
108         return file;
109     }
110
111     /**
112      * Tests the full range of transformations available on the
113      * {@link #getTransformer(String, String) transformer} subject to the
114      * {@link org.alfresco.util.test.QuickFileTest available test files}
115      * and the {@link ContentTransformer#getReliability(String, String) reliability} of
116      * the {@link #getTransformer(String, String) transformer} itself.
117      * <p>
118      * Each transformation is repeated several times, with a transformer being
119      * {@link #getTransformer(String, String) requested} for each transformation. In the
120      * case where optimizations are being done around the selection of the most
121      * appropriate transformer, different transformers could be used during the iteration
122      * process.
123      */

124     public void testAllConversions() throws Exception JavaDoc
125     {
126         // get all mimetypes
127
List JavaDoc<String JavaDoc> mimetypes = mimetypeMap.getMimetypes();
128         for (String JavaDoc sourceMimetype : mimetypes)
129         {
130             // attempt to get a source file for each mimetype
131
String JavaDoc sourceExtension = mimetypeMap.getExtension(sourceMimetype);
132             File JavaDoc sourceFile = AbstractContentTransformerTest.loadQuickTestFile(sourceExtension);
133             if (sourceFile == null)
134             {
135                 continue; // no test file available for that extension
136
}
137             
138             // attempt to convert to every other mimetype
139
for (String JavaDoc targetMimetype : mimetypes)
140             {
141                 ContentWriter targetWriter = null;
142                 // construct a reader onto the source file
143
ContentReader sourceReader = new FileContentReader(sourceFile);
144
145                 // perform the transformation several times so that we get a good idea of performance
146
int count = 0;
147                 for (int i = 0; i < 5; i++)
148                 {
149                     // must we test the transformation?
150
ContentTransformer transformer = getTransformer(sourceMimetype, targetMimetype);
151                     if (transformer == null)
152                     {
153                         break; // test is not required
154
}
155                     else if (transformer.getReliability(sourceMimetype, targetMimetype) <= 0.0)
156                     {
157                         break; // not reliable for this transformation
158
}
159                     
160                     // make a writer for the target file
161
String JavaDoc targetExtension = mimetypeMap.getExtension(targetMimetype);
162                     File JavaDoc targetFile = TempFileProvider.createTempFile(
163                             getClass().getSimpleName() + "_" + getName() + "_" + sourceExtension + "_",
164                             "." + targetExtension);
165                     targetWriter = new FileContentWriter(targetFile);
166                     
167                     // do the transformation
168
sourceReader.setMimetype(sourceMimetype);
169                     targetWriter.setMimetype(targetMimetype);
170                     transformer.transform(sourceReader.getReader(), targetWriter);
171                     
172                     // if the target format is any type of text, then it must contain the 'quick' phrase
173
if (targetMimetype.equals(MimetypeMap.MIMETYPE_TEXT_PLAIN))
174                     {
175                         ContentReader targetReader = targetWriter.getReader();
176                         String JavaDoc 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 JavaDoc checkContent = targetReader.getContentString();
187                         // essentially check that FTS indexing can use the conversion properly
188
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                     // increment count
199
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