KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.alfresco.repo.content.MimetypeMap;
26 import org.alfresco.repo.content.filestore.FileContentWriter;
27 import org.alfresco.repo.content.transform.ContentTransformerRegistry.TransformationKey;
28 import org.alfresco.service.cmr.repository.ContentReader;
29 import org.alfresco.service.cmr.repository.ContentWriter;
30 import org.alfresco.util.BaseAlfrescoTestCase;
31 import org.alfresco.util.TempFileProvider;
32 import org.alfresco.util.exec.RuntimeExec;
33 import org.apache.xml.security.transforms.TransformationException;
34
35 /**
36  * @see org.alfresco.repo.content.transform.RuntimeExecutableContentTransformer
37  *
38  * @author Derek Hulley
39  */

40 public class RuntimeExecutableContentTransformerTest extends BaseAlfrescoTestCase
41 {
42     private RuntimeExecutableContentTransformer transformer;
43     
44     @Override JavaDoc
45     protected void setUp() throws Exception JavaDoc
46     {
47         super.setUp();
48         
49         transformer = new RuntimeExecutableContentTransformer();
50         // the command to execute
51
RuntimeExec transformCommand = new RuntimeExec();
52         Map JavaDoc<String JavaDoc, String JavaDoc> commandMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>(5);
53         commandMap.put("Linux", "mv -f ${source} ${target}");
54         commandMap.put(".*", "cmd /c copy /Y \"${source}\" \"${target}\"");
55         transformCommand.setCommandMap(commandMap);
56         transformCommand.setErrorCodes("1, 2");
57         transformer.setTransformCommand(transformCommand);
58         transformer.setMimetypeService(serviceRegistry.getMimetypeService());
59         // set the explicit transformations
60
List JavaDoc<TransformationKey> explicitTranformations = new ArrayList JavaDoc<TransformationKey>(1);
61         explicitTranformations.add(
62                 new TransformationKey(MimetypeMap.MIMETYPE_TEXT_PLAIN, MimetypeMap.MIMETYPE_XML));
63         transformer.setExplicitTransformations(explicitTranformations);
64         
65         // initialise so that it doesn't score 0
66
transformer.register();
67     }
68
69     public void testCopyCommand() throws Exception JavaDoc
70     {
71         String JavaDoc content = "<A><B></B></A>";
72         // create the source
73
File JavaDoc sourceFile = TempFileProvider.createTempFile(getName() + "_", ".txt");
74         ContentWriter tempWriter = new FileContentWriter(sourceFile);
75         tempWriter.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
76         tempWriter.putContent(content);
77         ContentReader reader = tempWriter.getReader();
78         // create the target
79
File JavaDoc targetFile = TempFileProvider.createTempFile(getName() + "_", ".xml");
80         ContentWriter writer = new FileContentWriter(targetFile);
81         writer.setMimetype(MimetypeMap.MIMETYPE_XML);
82         
83         // do the transformation
84
transformer.transform(reader, writer); // no options on the copy
85

86         // make sure that the content was copied over
87
ContentReader checkReader = writer.getReader();
88         String JavaDoc checkContent = checkReader.getContentString();
89         assertEquals("Content not copied", content, checkContent);
90     }
91 }
92
Popular Tags