13cb3fca6SLiu Zhe /**************************************************************
23cb3fca6SLiu Zhe  *
33cb3fca6SLiu Zhe  * Licensed to the Apache Software Foundation (ASF) under one
43cb3fca6SLiu Zhe  * or more contributor license agreements.  See the NOTICE file
53cb3fca6SLiu Zhe  * distributed with this work for additional information
63cb3fca6SLiu Zhe  * regarding copyright ownership.  The ASF licenses this file
73cb3fca6SLiu Zhe  * to you under the Apache License, Version 2.0 (the
83cb3fca6SLiu Zhe  * "License"); you may not use this file except in compliance
93cb3fca6SLiu Zhe  * with the License.  You may obtain a copy of the License at
103cb3fca6SLiu Zhe  *
113cb3fca6SLiu Zhe  *   http://www.apache.org/licenses/LICENSE-2.0
123cb3fca6SLiu Zhe  *
133cb3fca6SLiu Zhe  * Unless required by applicable law or agreed to in writing,
143cb3fca6SLiu Zhe  * software distributed under the License is distributed on an
153cb3fca6SLiu Zhe  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163cb3fca6SLiu Zhe  * KIND, either express or implied.  See the License for the
173cb3fca6SLiu Zhe  * specific language governing permissions and limitations
183cb3fca6SLiu Zhe  * under the License.
193cb3fca6SLiu Zhe  *
203cb3fca6SLiu Zhe  *************************************************************/
213cb3fca6SLiu Zhe 
223e7cc3ecSLiu Zhe package pvt.uno;
233cb3fca6SLiu Zhe 
24527dbd07SLiu Zhe import static org.openoffice.test.common.Testspace.*;
25527dbd07SLiu Zhe 
263cb3fca6SLiu Zhe import java.io.File;
273cb3fca6SLiu Zhe import java.io.FileOutputStream;
283cb3fca6SLiu Zhe import java.io.PrintStream;
293cb3fca6SLiu Zhe 
303cb3fca6SLiu Zhe import org.junit.After;
313cb3fca6SLiu Zhe import org.junit.AfterClass;
323cb3fca6SLiu Zhe import org.junit.Before;
333cb3fca6SLiu Zhe import org.junit.BeforeClass;
343cb3fca6SLiu Zhe import org.junit.Rule;
353cb3fca6SLiu Zhe import org.junit.Test;
363cb3fca6SLiu Zhe import org.junit.runner.RunWith;
373cb3fca6SLiu Zhe import org.openoffice.test.OpenOffice;
38527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider;
39527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider.FileFilter;
405f863819SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepeat;
41527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepos;
423cb3fca6SLiu Zhe import org.openoffice.test.common.FileUtil;
433cb3fca6SLiu Zhe import org.openoffice.test.common.Logger;
443cb3fca6SLiu Zhe import org.openoffice.test.common.Testspace;
453cb3fca6SLiu Zhe import org.openoffice.test.uno.UnoApp;
463cb3fca6SLiu Zhe 
473cb3fca6SLiu Zhe import com.sun.star.beans.PropertyValue;
483cb3fca6SLiu Zhe import com.sun.star.document.MacroExecMode;
493cb3fca6SLiu Zhe import com.sun.star.lang.XComponent;
503cb3fca6SLiu Zhe import com.sun.star.uno.UnoRuntime;
513cb3fca6SLiu Zhe import com.sun.star.util.XCloseable;
523cb3fca6SLiu Zhe 
533cb3fca6SLiu Zhe 
54527dbd07SLiu Zhe @RunWith(FileProvider.class)
553cb3fca6SLiu Zhe public class Conversion {
56527dbd07SLiu Zhe 
57f4092fc8SLiu Zhe 	@Rule
58f4092fc8SLiu Zhe 	public Logger log = Logger.getLogger(this);
59f4092fc8SLiu Zhe 
60527dbd07SLiu Zhe 	@FileRepos
61*9b5f68a3SLiu Zhe 	public static String repos = System.getProperty("conversion.repos", getDataPath("conversion_pvt"));
62527dbd07SLiu Zhe 	@FileFilter
635f863819SLiu Zhe 	public static String filter = System.getProperty("conversion.filter", "-f .*\\.((doc)|(dot)|(odt)|(ott))$ writer_pdf_Export pdf "
64527dbd07SLiu Zhe 			+ "-f .*\\.((xls)|(xlt)|(ods)|(ots))$ calc_pdf_Export pdf "
65527dbd07SLiu Zhe 			+ "-f .*\\.((ppt)|(ppt)|(odp)|(otp))$ impress_pdf_Export pdf "
66527dbd07SLiu Zhe 			+ "-f .*\\.((doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$ writer8 odt "
67527dbd07SLiu Zhe 			+ "-f .*\\.((xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$ calc8 ods "
68527dbd07SLiu Zhe 			+ "-f .*\\.((ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$ impress8 odp "
69527dbd07SLiu Zhe 			+ "-f .*\\.((odt)|(ott))$ 'MS Word 97' doc "
70527dbd07SLiu Zhe 			+ "-f .*\\.((ods)|(ots))$ 'MS Excel 97' xls "
715f863819SLiu Zhe 			+ "-f .*\\.((odp)|(otp))$ 'MS PowerPoint 97' ppt");
723cb3fca6SLiu Zhe 
735f863819SLiu Zhe 	@FileRepeat
749dae0b27SLinyi Li 	public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
755f863819SLiu Zhe 
76*9b5f68a3SLiu Zhe 	public static String clean = System.getProperty("conversion.clean", "file");
77*9b5f68a3SLiu Zhe 
783cb3fca6SLiu Zhe 	private static UnoApp app = new UnoApp();
79527dbd07SLiu Zhe 
803cb3fca6SLiu Zhe 	private static PrintStream result;
813cb3fca6SLiu Zhe 
825f863819SLiu Zhe 	private static int counter = 0;
835f863819SLiu Zhe 
843cb3fca6SLiu Zhe 	@BeforeClass
853cb3fca6SLiu Zhe 	public static void beforeClass() throws Exception {
863cb3fca6SLiu Zhe 		//Disable automation
873cb3fca6SLiu Zhe 		OpenOffice.getDefault().setAutomationPort(-1);
883cb3fca6SLiu Zhe 		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
893cb3fca6SLiu Zhe 
903cb3fca6SLiu Zhe 		File resultFile = Testspace.getFile("output/conversion.csv");
913cb3fca6SLiu Zhe 		resultFile.getParentFile().mkdirs();
923cb3fca6SLiu Zhe 		result = new PrintStream(new FileOutputStream(resultFile));
93*9b5f68a3SLiu Zhe 		result.println("File,Scenario,File Size,Time Consumed After Closing,Time Consumed After Saving,Time Consumed After Loading");
943cb3fca6SLiu Zhe 	}
953cb3fca6SLiu Zhe 
963cb3fca6SLiu Zhe 	@AfterClass
973cb3fca6SLiu Zhe 	public static void afterClass() throws Exception {
983cb3fca6SLiu Zhe 		result.close();
99f4092fc8SLiu Zhe 		app.close();
1003cb3fca6SLiu Zhe 	}
1013cb3fca6SLiu Zhe 
102527dbd07SLiu Zhe 	private String sourcePath = null;
103527dbd07SLiu Zhe 	private String targetFilterName = null;
104527dbd07SLiu Zhe 	private String targetExtName = null;
105527dbd07SLiu Zhe 
1063cb3fca6SLiu Zhe 	private File sourceFile = null;
1073cb3fca6SLiu Zhe 	private File targetFile = null;
1083cb3fca6SLiu Zhe 	private String sourceFileUrl = null;
1093cb3fca6SLiu Zhe 	private String targetFileUrl = null;
110527dbd07SLiu Zhe 
111f4092fc8SLiu Zhe 	private String scenario = null;
112f4092fc8SLiu Zhe 	private String sourceFileId = null;
1133cb3fca6SLiu Zhe 	private long loadTime = -1;
1143cb3fca6SLiu Zhe 	private long saveTime = -1;
1153cb3fca6SLiu Zhe 	private long closeTime = -1;
1163cb3fca6SLiu Zhe 
117527dbd07SLiu Zhe 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
1183cb3fca6SLiu Zhe 		super();
119527dbd07SLiu Zhe 		this.sourcePath = sourcePath;
1203cb3fca6SLiu Zhe 		this.targetFilterName = targetFilterName;
121527dbd07SLiu Zhe 		this.targetExtName = targetExtName;
1225f863819SLiu Zhe 		counter++;
123f4092fc8SLiu Zhe 	}
124f4092fc8SLiu Zhe 
125f4092fc8SLiu Zhe 	@Before
126f4092fc8SLiu Zhe 	public void before() throws Exception {
127*9b5f68a3SLiu Zhe 		sourceFile = new File(sourcePath);
128527dbd07SLiu Zhe 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
129527dbd07SLiu Zhe 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
130527dbd07SLiu Zhe 		targetFileUrl = FileUtil.getUrl(this.targetFile);
131527dbd07SLiu Zhe 
132527dbd07SLiu Zhe 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
133*9b5f68a3SLiu Zhe 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
134*9b5f68a3SLiu Zhe 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
135*9b5f68a3SLiu Zhe 		sourceFileId = pathSource.replace(pathRepos, "");
136f4092fc8SLiu Zhe 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
137f4092fc8SLiu Zhe 		app.start();
138f4092fc8SLiu Zhe 	}
139f4092fc8SLiu Zhe 
140f4092fc8SLiu Zhe 	@After
141f4092fc8SLiu Zhe 	public void after() throws Exception{
142*9b5f68a3SLiu Zhe 		result.println(sourceFileId + "," + scenario + "," + sourceFile.length() + "," + closeTime + "," + saveTime + "," + loadTime);
143f4092fc8SLiu Zhe 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
144*9b5f68a3SLiu Zhe 		if (closeTime < 0) {
145*9b5f68a3SLiu Zhe 			app.close();
146*9b5f68a3SLiu Zhe 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
147f4092fc8SLiu Zhe 			app.close();
148f4092fc8SLiu Zhe 		}
1493cb3fca6SLiu Zhe 	}
150*9b5f68a3SLiu Zhe 
1513cb3fca6SLiu Zhe 	private PropertyValue propertyValue(String name, Object value) {
1523cb3fca6SLiu Zhe 		PropertyValue p = new PropertyValue();
1533cb3fca6SLiu Zhe 		p.Name = name;
1543cb3fca6SLiu Zhe 		p.Value= value;
1553cb3fca6SLiu Zhe 		return p;
1563cb3fca6SLiu Zhe 	}
1573cb3fca6SLiu Zhe 
158527dbd07SLiu Zhe 	@Test(timeout=10 * 60000)
1593cb3fca6SLiu Zhe 	public void testConversion() throws Exception {
1603cb3fca6SLiu Zhe 		// convert
1613cb3fca6SLiu Zhe 		long start = System.currentTimeMillis();
1623cb3fca6SLiu Zhe 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
1633cb3fca6SLiu Zhe 				propertyValue("Hidden", true),
1643cb3fca6SLiu Zhe 				propertyValue("ReadOnly", true),
1653cb3fca6SLiu Zhe 				propertyValue("AsyncMode", false),
1663cb3fca6SLiu Zhe 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE));
1673cb3fca6SLiu Zhe 
1683cb3fca6SLiu Zhe 		loadTime = System.currentTimeMillis() - start;
1693cb3fca6SLiu Zhe 		app.saveDocumentToURL(doc, targetFileUrl,
1703cb3fca6SLiu Zhe 				propertyValue( "FilterName", targetFilterName),
1713cb3fca6SLiu Zhe 				propertyValue( "Overwrite", true));
1723cb3fca6SLiu Zhe 		saveTime = System.currentTimeMillis() - start;
1733cb3fca6SLiu Zhe 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
1743cb3fca6SLiu Zhe 		xCloseable.close(true);
1753cb3fca6SLiu Zhe 		closeTime = System.currentTimeMillis() - start;
1763cb3fca6SLiu Zhe 	}
1775f863819SLiu Zhe 
1783cb3fca6SLiu Zhe }
179