1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 package pvt.uno;
23 
24 import static org.openoffice.test.common.Testspace.*;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.PrintStream;
29 
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.openoffice.test.OpenOffice;
38 import org.openoffice.test.common.FileProvider;
39 import org.openoffice.test.common.FileProvider.FileFilter;
40 import org.openoffice.test.common.FileProvider.FileRepos;
41 import org.openoffice.test.common.FileUtil;
42 import org.openoffice.test.common.Logger;
43 import org.openoffice.test.common.Testspace;
44 import org.openoffice.test.uno.UnoApp;
45 
46 import com.sun.star.beans.PropertyValue;
47 import com.sun.star.document.MacroExecMode;
48 import com.sun.star.lang.XComponent;
49 import com.sun.star.uno.UnoRuntime;
50 import com.sun.star.util.XCloseable;
51 
52 
53 @RunWith(FileProvider.class)
54 public class Conversion {
55 
56 	@Rule
57 	public Logger log = Logger.getLogger(this);
58 
59 	@FileRepos
60 	public static String repos = getDataFile("pvt_conversion").getAbsolutePath();
61 
62 	@FileFilter
63 	public static String filter = "-f .*\\.((doc)|(dot)|(odt)|(ott))$ writer_pdf_Export pdf "
64 			+ "-f .*\\.((xls)|(xlt)|(ods)|(ots))$ calc_pdf_Export pdf "
65 			+ "-f .*\\.((ppt)|(ppt)|(odp)|(otp))$ impress_pdf_Export pdf "
66 			+ "-f .*\\.((doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$ writer8 odt "
67 			+ "-f .*\\.((xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$ calc8 ods "
68 			+ "-f .*\\.((ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$ impress8 odp "
69 			+ "-f .*\\.((odt)|(ott))$ 'MS Word 97' doc "
70 			+ "-f .*\\.((ods)|(ots))$ 'MS Excel 97' xls "
71 			+ "-f .*\\.((odp)|(otp))$ 'MS PowerPoint 97' ppt";
72 
73 	private static UnoApp app = new UnoApp();
74 
75 	private static PrintStream result;
76 
77 	@BeforeClass
78 	public static void beforeClass() throws Exception {
79 		//Disable automation
80 		OpenOffice.getDefault().setAutomationPort(-1);
81 		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
82 
83 		File resultFile = Testspace.getFile("output/conversion.csv");
84 		resultFile.getParentFile().mkdirs();
85 		result = new PrintStream(new FileOutputStream(resultFile));
86 		result.println("File,Scenario,After Close,After Save,After Load");
87 	}
88 
89 	@AfterClass
90 	public static void afterClass() throws Exception {
91 		result.close();
92 		app.close();
93 	}
94 
95 	private String sourcePath = null;
96 	private String targetFilterName = null;
97 	private String targetExtName = null;
98 
99 	private File sourceFile = null;
100 	private File targetFile = null;
101 	private String sourceFileUrl = null;
102 	private String targetFileUrl = null;
103 
104 	private String scenario = null;
105 	private String sourceFileId = null;
106 	private long loadTime = -1;
107 	private long saveTime = -1;
108 	private long closeTime = -1;
109 
110 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
111 		super();
112 		this.sourcePath = sourcePath;
113 		this.targetFilterName = targetFilterName;
114 		this.targetExtName = targetExtName;
115 	}
116 
117 	@Before
118 	public void before() throws Exception {
119 		sourceFile = prepareDataFile(sourcePath);
120 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
121 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
122 		targetFileUrl = FileUtil.getUrl(this.targetFile);
123 
124 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
125 		sourceFileId = sourceFile.getAbsolutePath().replace(new File(repos).getAbsolutePath(), "").replace("\\", "/");
126 
127 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
128 		app.start();
129 	}
130 
131 	@After
132 	public void after() throws Exception{
133 		result.println(sourceFileId + "," + scenario + "," + closeTime + "," + saveTime + "," + loadTime);
134 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
135 		if (closeTime < 0) {
136 			app.close();
137 		}
138 	}
139 
140 	private PropertyValue propertyValue(String name, Object value) {
141 		PropertyValue p = new PropertyValue();
142 		p.Name = name;
143 		p.Value= value;
144 		return p;
145 	}
146 
147 	@Test(timeout=10 * 60000)
148 	public void testConversion() throws Exception {
149 		// convert
150 		long start = System.currentTimeMillis();
151 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
152 				propertyValue("Hidden", true),
153 				propertyValue("ReadOnly", true),
154 				propertyValue("AsyncMode", false),
155 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE));
156 
157 		loadTime = System.currentTimeMillis() - start;
158 		app.saveDocumentToURL(doc, targetFileUrl,
159 				propertyValue( "FilterName", targetFilterName),
160 				propertyValue( "Overwrite", true));
161 		saveTime = System.currentTimeMillis() - start;
162 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
163 		xCloseable.close(true);
164 		closeTime = System.currentTimeMillis() - start;
165 	}
166 }
167