xref: /AOO41X/test/testuno/source/pvt/uno/Conversion.java (revision 9dae0b27353374829b4671c7fdc49fc8f49db22d)
13e7cc3ecSLiu Zhe /**************************************************************
23e7cc3ecSLiu Zhe  *
33e7cc3ecSLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
43e7cc3ecSLiu Zhe  * or more contributor license agreements.  See the NOTICE file
53e7cc3ecSLiu Zhe  * distributed with this work for additional information
63e7cc3ecSLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
73e7cc3ecSLiu Zhe  * to you under the Apache License, Version 2.0 (the
83e7cc3ecSLiu Zhe  * "License"); you may not use this file except in compliance
93e7cc3ecSLiu Zhe  * with the License.  You may obtain a copy of the License at
103e7cc3ecSLiu Zhe  *
113e7cc3ecSLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
123e7cc3ecSLiu Zhe  *
133e7cc3ecSLiu Zhe  * Unless required by applicable law or agreed to in writing,
143e7cc3ecSLiu Zhe  * software distributed under the License is distributed on an
153e7cc3ecSLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163e7cc3ecSLiu Zhe  * KIND, either express or implied.  See the License for the
173e7cc3ecSLiu Zhe  * specific language governing permissions and limitations
183e7cc3ecSLiu Zhe  * under the License.
193e7cc3ecSLiu Zhe  *
203e7cc3ecSLiu Zhe  *************************************************************/
213e7cc3ecSLiu Zhe 
223e7cc3ecSLiu Zhe package pvt.uno;
233e7cc3ecSLiu Zhe 
243e7cc3ecSLiu Zhe import static org.openoffice.test.common.Testspace.*;
253e7cc3ecSLiu Zhe 
263e7cc3ecSLiu Zhe import java.io.File;
273e7cc3ecSLiu Zhe import java.io.FileOutputStream;
283e7cc3ecSLiu Zhe import java.io.PrintStream;
293e7cc3ecSLiu Zhe 
303e7cc3ecSLiu Zhe import org.junit.After;
313e7cc3ecSLiu Zhe import org.junit.AfterClass;
323e7cc3ecSLiu Zhe import org.junit.Before;
333e7cc3ecSLiu Zhe import org.junit.BeforeClass;
343e7cc3ecSLiu Zhe import org.junit.Rule;
353e7cc3ecSLiu Zhe import org.junit.Test;
363e7cc3ecSLiu Zhe import org.junit.runner.RunWith;
373e7cc3ecSLiu Zhe import org.openoffice.test.OpenOffice;
383e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider;
393e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider.FileFilter;
405f863819SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepeat;
413e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider.FileRepos;
423e7cc3ecSLiu Zhe import org.openoffice.test.common.FileUtil;
433e7cc3ecSLiu Zhe import org.openoffice.test.common.Logger;
443e7cc3ecSLiu Zhe import org.openoffice.test.common.Testspace;
453e7cc3ecSLiu Zhe import org.openoffice.test.uno.UnoApp;
463e7cc3ecSLiu Zhe 
473e7cc3ecSLiu Zhe import com.sun.star.beans.PropertyValue;
483e7cc3ecSLiu Zhe import com.sun.star.document.MacroExecMode;
493e7cc3ecSLiu Zhe import com.sun.star.lang.XComponent;
503e7cc3ecSLiu Zhe import com.sun.star.uno.UnoRuntime;
513e7cc3ecSLiu Zhe import com.sun.star.util.XCloseable;
523e7cc3ecSLiu Zhe 
533e7cc3ecSLiu Zhe 
543e7cc3ecSLiu Zhe @RunWith(FileProvider.class)
553e7cc3ecSLiu Zhe public class Conversion {
563e7cc3ecSLiu Zhe 
573e7cc3ecSLiu Zhe 	@Rule
583e7cc3ecSLiu Zhe 	public Logger log = Logger.getLogger(this);
593e7cc3ecSLiu Zhe 
603e7cc3ecSLiu Zhe 	@FileRepos
613e7cc3ecSLiu Zhe 	public static String repos = getDataFile("pvt_conversion").getAbsolutePath();
623e7cc3ecSLiu Zhe 
633e7cc3ecSLiu Zhe 	@FileFilter
645f863819SLiu Zhe 	public static String filter = System.getProperty("conversion.filter", "-f .*\\.((doc)|(dot)|(odt)|(ott))$ writer_pdf_Export pdf "
653e7cc3ecSLiu Zhe 			+ "-f .*\\.((xls)|(xlt)|(ods)|(ots))$ calc_pdf_Export pdf "
663e7cc3ecSLiu Zhe 			+ "-f .*\\.((ppt)|(ppt)|(odp)|(otp))$ impress_pdf_Export pdf "
673e7cc3ecSLiu Zhe 			+ "-f .*\\.((doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$ writer8 odt "
683e7cc3ecSLiu Zhe 			+ "-f .*\\.((xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$ calc8 ods "
693e7cc3ecSLiu Zhe 			+ "-f .*\\.((ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$ impress8 odp "
703e7cc3ecSLiu Zhe 			+ "-f .*\\.((odt)|(ott))$ 'MS Word 97' doc "
713e7cc3ecSLiu Zhe 			+ "-f .*\\.((ods)|(ots))$ 'MS Excel 97' xls "
725f863819SLiu Zhe 			+ "-f .*\\.((odp)|(otp))$ 'MS PowerPoint 97' ppt");
735f863819SLiu Zhe 
745f863819SLiu Zhe 	@FileRepeat
75*9dae0b27SLinyi Li 	public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
763e7cc3ecSLiu Zhe 
773e7cc3ecSLiu Zhe 	private static UnoApp app = new UnoApp();
783e7cc3ecSLiu Zhe 
793e7cc3ecSLiu Zhe 	private static PrintStream result;
803e7cc3ecSLiu Zhe 
815f863819SLiu Zhe 	private static int counter = 0;
825f863819SLiu Zhe 
833e7cc3ecSLiu Zhe 	@BeforeClass
843e7cc3ecSLiu Zhe 	public static void beforeClass() throws Exception {
853e7cc3ecSLiu Zhe 		//Disable automation
863e7cc3ecSLiu Zhe 		OpenOffice.getDefault().setAutomationPort(-1);
873e7cc3ecSLiu Zhe 		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
883e7cc3ecSLiu Zhe 
893e7cc3ecSLiu Zhe 		File resultFile = Testspace.getFile("output/conversion.csv");
903e7cc3ecSLiu Zhe 		resultFile.getParentFile().mkdirs();
913e7cc3ecSLiu Zhe 		result = new PrintStream(new FileOutputStream(resultFile));
923e7cc3ecSLiu Zhe 		result.println("File,Scenario,After Close,After Save,After Load");
933e7cc3ecSLiu Zhe 	}
943e7cc3ecSLiu Zhe 
953e7cc3ecSLiu Zhe 	@AfterClass
963e7cc3ecSLiu Zhe 	public static void afterClass() throws Exception {
973e7cc3ecSLiu Zhe 		result.close();
983e7cc3ecSLiu Zhe 		app.close();
993e7cc3ecSLiu Zhe 	}
1003e7cc3ecSLiu Zhe 
1013e7cc3ecSLiu Zhe 	private String sourcePath = null;
1023e7cc3ecSLiu Zhe 	private String targetFilterName = null;
1033e7cc3ecSLiu Zhe 	private String targetExtName = null;
1043e7cc3ecSLiu Zhe 
1053e7cc3ecSLiu Zhe 	private File sourceFile = null;
1063e7cc3ecSLiu Zhe 	private File targetFile = null;
1073e7cc3ecSLiu Zhe 	private String sourceFileUrl = null;
1083e7cc3ecSLiu Zhe 	private String targetFileUrl = null;
1093e7cc3ecSLiu Zhe 
1103e7cc3ecSLiu Zhe 	private String scenario = null;
1113e7cc3ecSLiu Zhe 	private String sourceFileId = null;
1123e7cc3ecSLiu Zhe 	private long loadTime = -1;
1133e7cc3ecSLiu Zhe 	private long saveTime = -1;
1143e7cc3ecSLiu Zhe 	private long closeTime = -1;
1153e7cc3ecSLiu Zhe 
1163e7cc3ecSLiu Zhe 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
1173e7cc3ecSLiu Zhe 		super();
1183e7cc3ecSLiu Zhe 		this.sourcePath = sourcePath;
1193e7cc3ecSLiu Zhe 		this.targetFilterName = targetFilterName;
1203e7cc3ecSLiu Zhe 		this.targetExtName = targetExtName;
1215f863819SLiu Zhe 		counter++;
1223e7cc3ecSLiu Zhe 	}
1233e7cc3ecSLiu Zhe 
1243e7cc3ecSLiu Zhe 	@Before
1253e7cc3ecSLiu Zhe 	public void before() throws Exception {
1263e7cc3ecSLiu Zhe 		sourceFile = prepareDataFile(sourcePath);
1273e7cc3ecSLiu Zhe 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
1283e7cc3ecSLiu Zhe 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
1293e7cc3ecSLiu Zhe 		targetFileUrl = FileUtil.getUrl(this.targetFile);
1303e7cc3ecSLiu Zhe 
1313e7cc3ecSLiu Zhe 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
1323e7cc3ecSLiu Zhe 		sourceFileId = sourceFile.getAbsolutePath().replace(new File(repos).getAbsolutePath(), "").replace("\\", "/");
1333e7cc3ecSLiu Zhe 
1343e7cc3ecSLiu Zhe 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
1353e7cc3ecSLiu Zhe 		app.start();
1363e7cc3ecSLiu Zhe 	}
1373e7cc3ecSLiu Zhe 
1383e7cc3ecSLiu Zhe 	@After
1393e7cc3ecSLiu Zhe 	public void after() throws Exception{
1403e7cc3ecSLiu Zhe 		result.println(sourceFileId + "," + scenario + "," + closeTime + "," + saveTime + "," + loadTime);
1413e7cc3ecSLiu Zhe 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
142*9dae0b27SLinyi Li 		if (counter % repeat == 0) {
1433e7cc3ecSLiu Zhe 			app.close();
1443e7cc3ecSLiu Zhe 		}
1453e7cc3ecSLiu Zhe 	}
1463e7cc3ecSLiu Zhe 
1473e7cc3ecSLiu Zhe 	private PropertyValue propertyValue(String name, Object value) {
1483e7cc3ecSLiu Zhe 		PropertyValue p = new PropertyValue();
1493e7cc3ecSLiu Zhe 		p.Name = name;
1503e7cc3ecSLiu Zhe 		p.Value= value;
1513e7cc3ecSLiu Zhe 		return p;
1523e7cc3ecSLiu Zhe 	}
1533e7cc3ecSLiu Zhe 
1543e7cc3ecSLiu Zhe 	@Test(timeout=10 * 60000)
1553e7cc3ecSLiu Zhe 	public void testConversion() throws Exception {
1563e7cc3ecSLiu Zhe 		// convert
1573e7cc3ecSLiu Zhe 		long start = System.currentTimeMillis();
1583e7cc3ecSLiu Zhe 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
1593e7cc3ecSLiu Zhe 				propertyValue("Hidden", true),
1603e7cc3ecSLiu Zhe 				propertyValue("ReadOnly", true),
1613e7cc3ecSLiu Zhe 				propertyValue("AsyncMode", false),
1623e7cc3ecSLiu Zhe 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE));
1633e7cc3ecSLiu Zhe 
1643e7cc3ecSLiu Zhe 		loadTime = System.currentTimeMillis() - start;
1653e7cc3ecSLiu Zhe 		app.saveDocumentToURL(doc, targetFileUrl,
1663e7cc3ecSLiu Zhe 				propertyValue( "FilterName", targetFilterName),
1673e7cc3ecSLiu Zhe 				propertyValue( "Overwrite", true));
1683e7cc3ecSLiu Zhe 		saveTime = System.currentTimeMillis() - start;
1693e7cc3ecSLiu Zhe 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
1703e7cc3ecSLiu Zhe 		xCloseable.close(true);
1713e7cc3ecSLiu Zhe 		closeTime = System.currentTimeMillis() - start;
1723e7cc3ecSLiu Zhe 	}
1735f863819SLiu Zhe 
1743e7cc3ecSLiu Zhe }
175