1 17 package org.alfresco.repo.content.transform; 18 19 import java.io.BufferedOutputStream ; 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.UnsupportedEncodingException ; 24 import java.io.Writer ; 25 import java.util.Random ; 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 40 public class StringExtractingContentTransformerTest extends AbstractContentTransformerTest 41 { 42 private static final String SOME_CONTENT; 43 static 44 { 45 try 47 { 48 SOME_CONTENT = new String ("azAz10!�$%^&*()\t\r\n".getBytes("UTF-8"), "MacDingbat"); 49 } 50 catch (UnsupportedEncodingException e) 51 { 52 throw new AlfrescoRuntimeException("Encoding not recognised", e); 53 } 54 } 55 56 private ContentTransformer transformer; 57 58 private ContentWriter targetWriter; 59 60 @Override 61 public void onSetUpInTransaction() throws Exception 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 sourceMimetype, String targetMimetype) 70 { 71 return transformer; 72 } 73 74 public void testSetUp() throws Exception 75 { 76 assertNotNull(transformer); 77 } 78 79 82 private File getTempFile() 83 { 84 return TempFileProvider.createTempFile(getName(), ".txt"); 85 } 86 87 94 private ContentReader writeContent(String mimetype, String encoding) 95 { 96 ContentWriter writer = new FileContentWriter(getTempFile()); 97 writer.setMimetype(mimetype); 98 writer.setEncoding(encoding); 99 writer.putContent(SOME_CONTENT); 101 return writer.getReader(); 103 } 104 105 public void testDirectTransform() throws Exception 106 { 107 ContentReader reader = writeContent("text/plain", "MacDingbat"); 108 109 double reliability = transformer.getReliability(reader.getMimetype(), targetWriter.getMimetype()); 111 assertEquals("Reliability incorrect", 1.0, reliability); 113 transformer.transform(reader, targetWriter); 115 116 ContentReader checkReader = targetWriter.getReader(); 118 String checkContent = checkReader.getContentString(); 119 assertEquals("Content check failed", SOME_CONTENT, checkContent); 120 } 121 122 public void testInterTextTransform() throws Exception 123 { 124 ContentReader reader = writeContent("text/xml", "MacDingbat"); 125 126 double reliability = transformer.getReliability(reader.getMimetype(), targetWriter.getMimetype()); 128 assertEquals("Reliability incorrect", 0.1, reliability); 130 transformer.transform(reader, targetWriter); 132 133 ContentReader checkReader = targetWriter.getReader(); 135 String checkContent = checkReader.getContentString(); 136 assertEquals("Content check failed", SOME_CONTENT, checkContent); 137 } 138 139 146 public void testLargeFileStreaming() throws Exception 147 { 148 File sourceFile = TempFileProvider.createTempFile(getName(), ".txt"); 149 150 int chars = 1000000; Random random = new Random (); 152 153 Writer charWriter = new OutputStreamWriter (new BufferedOutputStream (new FileOutputStream (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 ContentReader reader = new FileContentReader(sourceFile); 163 reader.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 164 165 File outputFile = TempFileProvider.createTempFile(getName(), ".txt"); 166 ContentWriter writer = new FileContentWriter(outputFile); 167 writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN); 168 169 transformer.transform(reader, writer); 171 172 sourceFile.delete(); 174 outputFile.delete(); 175 } 176 } 177 | Popular Tags |