KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Collections JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
31  * @see org.alfresco.repo.content.transform.ContentTransformerRegistry
32  *
33  * @author Derek Hulley
34  */

35 public class ContentTransformerRegistryTest extends AbstractContentTransformerTest
36 {
37     private static final String JavaDoc A = MimetypeMap.MIMETYPE_TEXT_PLAIN;
38     private static final String JavaDoc B = MimetypeMap.MIMETYPE_XML;
39     private static final String JavaDoc C = MimetypeMap.MIMETYPE_WORD;
40     private static final String JavaDoc D = MimetypeMap.MIMETYPE_HTML;
41     
42     /** a real registry with real transformers */
43     private ContentTransformerRegistry registry;
44     /** a fake registry with fake transformers */
45     private ContentTransformerRegistry dummyRegistry;
46     
47     private ContentReader reader;
48     private ContentWriter writer;
49     
50     /**
51      * Allows dependency injection
52      */

53     public void setContentTransformerRegistry(ContentTransformerRegistry registry)
54     {
55         this.registry = registry;
56     }
57
58     @Override JavaDoc
59     public void onSetUpInTransaction() throws Exception JavaDoc
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         // create the dummyRegistry
72
dummyRegistry = new ContentTransformerRegistry(mimetypeMap);
73         // create some dummy transformers for reliability tests
74
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         // create some dummy transformers for speed tests
80
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); // the fast one
83
new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L);
84         new DummyTransformer(mimetypeMap, dummyRegistry, A, D, 1.0, 20L);
85     }
86
87     /**
88      * Checks that required objects are present
89      */

90     public void testSetUp() throws Exception JavaDoc
91     {
92         super.testSetUp();
93         assertNotNull(registry);
94     }
95
96     /**
97      * @return Returns the transformer provided by the <b>real</b> registry
98      */

99     protected ContentTransformer getTransformer(String JavaDoc sourceMimetype, String JavaDoc targetMimetype)
100     {
101         return registry.getTransformer(sourceMimetype, targetMimetype);
102     }
103
104     public void testNullRetrieval() throws Exception JavaDoc
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 JavaDoc
116     {
117         ContentTransformer transformer = null;
118         // B -> C expect 0.2
119
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     /**
127      * Force some equally reliant transformers to do some work and develop
128      * different average transformation times. Check that the registry
129      * copes with the new averages after a reset.
130      */

131     public void testPerformanceRetrieval() throws Exception JavaDoc
132     {
133         // A -> D expect 1.0, 10ms
134
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 JavaDoc
141     {
142         ContentTransformer transformer = null;
143         // A -> B expect 0.6
144
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         // A -> C expect 1.0
149
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     /**
156      * Set an explicit, and bizarre, transformation. Check that it is used.
157      *
158      */

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         // set an explicit transformation
167
ContentTransformerRegistry.TransformationKey key =
168                 new ContentTransformerRegistry.TransformationKey(
169                         MimetypeMap.MIMETYPE_FLASH, MimetypeMap.MIMETYPE_EXCEL);
170         dummyTransformer.setExplicitTransformations(Collections.singletonList(key));
171         // register again
172
dummyTransformer.register();
173         
174         // get the appropriate transformer for the bizarre mapping
175
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     /**
183      * Dummy transformer that does no transformation and scores exactly as it is
184      * told to in the constructor. It enables the tests to be sure of what to expect.
185      */

186     private static class DummyTransformer extends AbstractContentTransformer
187     {
188         private String JavaDoc sourceMimetype;
189         private String JavaDoc targetMimetype;
190         private double reliability;
191         private long transformationTime;
192         
193         public DummyTransformer(
194                 MimetypeService mimetypeService,
195                 ContentTransformerRegistry registry,
196                 String JavaDoc sourceMimetype, String JavaDoc 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
206
register();
207         }
208
209         public double getReliability(String JavaDoc sourceMimetype, String JavaDoc 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         /**
223          * Just notches up some average times
224          */

225         public void transformInternal(
226                 ContentReader reader,
227                 ContentWriter writer,
228                 Map JavaDoc<String JavaDoc, Object JavaDoc> options) throws Exception JavaDoc
229         {
230             // just update the transformation time
231
super.recordTime(transformationTime);
232         }
233
234         /**
235          * @return Returns the fixed dummy average transformation time
236          */

237         public synchronized long getTransformationTime()
238         {
239             return transformationTime;
240         }
241     }
242 }
243
Popular Tags