KickJava   Java API By Example, From Geeks To Geeks.

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


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.BufferedOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.UnsupportedEncodingException JavaDoc;
24 import java.io.Writer JavaDoc;
25 import java.util.Random JavaDoc;
26
27 import org.alfresco.error.AlfrescoRuntimeException;
28 import org.alfresco.repo.content.MimetypeMap;
29 import org.alfresco.repo.content.filestore.FileContentReader;
30 import org.alfresco.repo.content.filestore.FileContentWriter;
31 import org.alfresco.service.cmr.repository.ContentReader;
32 import org.alfresco.service.cmr.repository.ContentWriter;
33 import org.alfresco.util.TempFileProvider;
34
35 /**
36  * @see org.alfresco.repo.content.transform.StringExtractingContentTransformer
37  *
38  * @author Derek Hulley
39  */

40 public class StringExtractingContentTransformerTest extends AbstractContentTransformerTest
41 {
42     private static final String JavaDoc SOME_CONTENT;
43     static
44     {
45         // force the content to be encoded in a particular way, independently of the source file encoding
46
try
47         {
48             SOME_CONTENT = new String JavaDoc("azAz10!�$%^&*()\t\r\n".getBytes("UTF-8"), "MacDingbat");
49         }
50         catch (UnsupportedEncodingException JavaDoc e)
51         {
52             throw new AlfrescoRuntimeException("Encoding not recognised", e);
53         }
54     }
55     
56     private ContentTransformer transformer;
57     /** the final destination of transformations */
58     private ContentWriter targetWriter;
59     
60     @Override JavaDoc
61     public void onSetUpInTransaction() throws Exception JavaDoc
62     {
63         transformer = new StringExtractingContentTransformer();
64         targetWriter = new FileContentWriter(getTempFile());
65         targetWriter.setMimetype("text/plain");
66         targetWriter.setEncoding("UTF-8");
67     }
68     
69     protected ContentTransformer getTransformer(String JavaDoc sourceMimetype, String JavaDoc targetMimetype)
70     {
71         return transformer;
72     }
73
74     public void testSetUp() throws Exception JavaDoc
75     {
76         assertNotNull(transformer);
77     }
78     
79     /**
80      * @return Returns a new temp file
81      */

82     private File JavaDoc getTempFile()
83     {
84         return TempFileProvider.createTempFile(getName(), ".txt");
85     }
86     
87     /**
88      * Writes some content using the mimetype and encoding specified.
89      *
90      * @param mimetype
91      * @param encoding
92      * @return Returns a reader onto the newly written content
93      */

94     private ContentReader writeContent(String JavaDoc mimetype, String JavaDoc encoding)
95     {
96         ContentWriter writer = new FileContentWriter(getTempFile());
97         writer.setMimetype(mimetype);
98         writer.setEncoding(encoding);
99         // put content
100
writer.putContent(SOME_CONTENT);
101         // return a reader onto the new content
102
return writer.getReader();
103     }
104     
105     public void testDirectTransform() throws Exception JavaDoc
106     {
107         ContentReader reader = writeContent("text/plain", "MacDingbat");
108         
109         // check reliability
110
double reliability = transformer.getReliability(reader.getMimetype(), targetWriter.getMimetype());
111         assertEquals("Reliability incorrect", 1.0, reliability); // plain text to plain text is 100%
112

113         // transform
114
transformer.transform(reader, targetWriter);
115         
116         // get a reader onto the transformed content and check
117
ContentReader checkReader = targetWriter.getReader();
118         String JavaDoc checkContent = checkReader.getContentString();
119         assertEquals("Content check failed", SOME_CONTENT, checkContent);
120     }
121     
122     public void testInterTextTransform() throws Exception JavaDoc
123     {
124         ContentReader reader = writeContent("text/xml", "MacDingbat");
125         
126         // check reliability
127
double reliability = transformer.getReliability(reader.getMimetype(), targetWriter.getMimetype());
128         assertEquals("Reliability incorrect", 0.1, reliability); // markup to plain text not 100%
129

130         // transform
131
transformer.transform(reader, targetWriter);
132         
133         // get a reader onto the transformed content and check
134
ContentReader checkReader = targetWriter.getReader();
135         String JavaDoc checkContent = checkReader.getContentString();
136         assertEquals("Content check failed", SOME_CONTENT, checkContent);
137     }
138     
139     /**
140      * Generate a large file and then transform it using the text extractor.
141      * We are not creating super-large file (1GB) in order to test the transform
142      * as it takes too long to create the file in the first place. Rather,
143      * this test can be used during profiling to ensure that memory is not
144      * being consumed.
145      */

146     public void testLargeFileStreaming() throws Exception JavaDoc
147     {
148         File JavaDoc sourceFile = TempFileProvider.createTempFile(getName(), ".txt");
149         
150         int chars = 1000000; // a million characters should do the trick
151
Random JavaDoc random = new Random JavaDoc();
152         
153         Writer JavaDoc charWriter = new OutputStreamWriter JavaDoc(new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(sourceFile)));
154         for (int i = 0; i < chars; i++)
155         {
156             char next = (char)(random.nextDouble() * 93D + 32D);
157             charWriter.write(next);
158         }
159         charWriter.close();
160         
161         // get a reader and a writer
162
ContentReader reader = new FileContentReader(sourceFile);
163         reader.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
164         
165         File JavaDoc outputFile = TempFileProvider.createTempFile(getName(), ".txt");
166         ContentWriter writer = new FileContentWriter(outputFile);
167         writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
168         
169         // transform
170
transformer.transform(reader, writer);
171         
172         // delete files
173
sourceFile.delete();
174         outputFile.delete();
175     }
176 }
177
Popular Tags