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 
283cb3fca6SLiu Zhe import org.junit.After;
293cb3fca6SLiu Zhe import org.junit.AfterClass;
303cb3fca6SLiu Zhe import org.junit.Before;
313cb3fca6SLiu Zhe import org.junit.BeforeClass;
323cb3fca6SLiu Zhe import org.junit.Rule;
333cb3fca6SLiu Zhe import org.junit.Test;
343cb3fca6SLiu Zhe import org.junit.runner.RunWith;
353cb3fca6SLiu Zhe import org.openoffice.test.OpenOffice;
36329fd865SLiu Zhe import org.openoffice.test.common.DataSheet;
37527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider;
38527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider.FileFilter;
395f863819SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepeat;
40527dbd07SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepos;
413cb3fca6SLiu Zhe import org.openoffice.test.common.FileUtil;
423cb3fca6SLiu Zhe import org.openoffice.test.common.Logger;
4375ed6c31SLiu Zhe import org.openoffice.test.common.Testspace;
443cb3fca6SLiu Zhe import org.openoffice.test.uno.UnoApp;
4575ed6c31SLiu Zhe 
463cb3fca6SLiu Zhe import com.sun.star.beans.PropertyValue;
473cb3fca6SLiu Zhe import com.sun.star.document.MacroExecMode;
483cb3fca6SLiu Zhe import com.sun.star.lang.XComponent;
493cb3fca6SLiu Zhe import com.sun.star.uno.UnoRuntime;
503cb3fca6SLiu Zhe import com.sun.star.util.XCloseable;
513cb3fca6SLiu Zhe 
523cb3fca6SLiu Zhe 
53527dbd07SLiu Zhe @RunWith(FileProvider.class)
543cb3fca6SLiu Zhe public class Conversion {
55527dbd07SLiu Zhe 
56f4092fc8SLiu Zhe 	@Rule
57f4092fc8SLiu Zhe 	public Logger log = Logger.getLogger(this);
58f4092fc8SLiu Zhe 
59527dbd07SLiu Zhe 	@FileRepos
6075ed6c31SLiu Zhe 	public static String repos = System.getProperty("conversion.repos", getDataPath("uno"));
61527dbd07SLiu Zhe 	@FileFilter
62329fd865SLiu Zhe 	public static String filter = System.getProperty("conversion.filter",
63329fd865SLiu Zhe 			  "-f .*(doc|dot|odt|ott)$ writer_pdf_Export pdf "
64329fd865SLiu Zhe 			+ "-f .*(xls|xlt|ods|ots)$ calc_pdf_Export pdf "
65329fd865SLiu Zhe 			+ "-f .*(ppt|ppt|odp|otp)$ impress_pdf_Export pdf "
66329fd865SLiu Zhe 			+ "-f .*(doc|dot|docx|docm|dotx|dotm)$ writer8 odt "
67329fd865SLiu Zhe 			+ "-f .*(xls|xlt|xlsx|xltx|xlsm|xltm)$ calc8 ods "
68329fd865SLiu Zhe 			+ "-f .*(ppt|pot|pptx|pptm|potm|potx)$ impress8 odp "
69329fd865SLiu Zhe 			+ "-f .*(odt|ott)$ 'MS Word 97' doc "
70329fd865SLiu Zhe 			+ "-f .*(ods|ots)$ 'MS Excel 97' xls "
71329fd865SLiu 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 
769b5f68a3SLiu Zhe 	public static String clean = System.getProperty("conversion.clean", "file");
774481f6d8SLinyi Li 
784481f6d8SLinyi Li 	public static int nLevelInfo = Integer.parseInt(System.getProperty("conversion.limitationcheck", "0"));	// Level info: starts from 1, 0 means no need for limitation check
799b5f68a3SLiu Zhe 
80*27723ce4SLinyi Li 	public static long nSleep = Long.parseLong(System.getProperty("conversion.sleep", "0"));	// Sleep before loadComponentFromURL and storeToURL, millisecond
818fd475f4SLinyi Li 
82329fd865SLiu Zhe 	private static OpenOffice aoo = new OpenOffice();
83527dbd07SLiu Zhe 
84329fd865SLiu Zhe 	private static UnoApp app = null;
85329fd865SLiu Zhe 
86329fd865SLiu Zhe 	private static DataSheet result;
873cb3fca6SLiu Zhe 
8825bd9146SLiu Zhe 	private static int counter = -1;
895f863819SLiu Zhe 
903cb3fca6SLiu Zhe 	@BeforeClass
beforeClass()913cb3fca6SLiu Zhe 	public static void beforeClass() throws Exception {
92329fd865SLiu Zhe 		aoo.setUnoUrl(OpenOffice.DEFAULT_UNO_URL);
93*27723ce4SLinyi Li 		aoo.addArgs("-invisible", "-conversionmode", "-hidemenu", "-nofirststartwizard", "-headless", "-enableautomation");
94329fd865SLiu Zhe 	    app = new UnoApp(aoo);
954481f6d8SLinyi Li 	    Testspace.prepareDataFile("limit_cfg.ini", aoo.getHome().toString()+"/program");	// Move limitation check file to installation dir
96ef94b85dSLinyi Li 		result = new DataSheet(getFile("output/" + Conversion.class.getName()+ ".xml"));
9775ed6c31SLiu Zhe 		result.addRow("data", "File","Scenario", "No", "Time Consumed After Closing","Time Consumed After Saving","Time Consumed After Loading", "File Size", "Exported File Size");
983cb3fca6SLiu Zhe 	}
993cb3fca6SLiu Zhe 
1003cb3fca6SLiu Zhe 	@AfterClass
afterClass()1013cb3fca6SLiu Zhe 	public static void afterClass() throws Exception {
102f4092fc8SLiu Zhe 		app.close();
1033cb3fca6SLiu Zhe 	}
1043cb3fca6SLiu Zhe 
105527dbd07SLiu Zhe 	private String sourcePath = null;
106527dbd07SLiu Zhe 	private String targetFilterName = null;
107527dbd07SLiu Zhe 	private String targetExtName = null;
108527dbd07SLiu Zhe 
1093cb3fca6SLiu Zhe 	private File sourceFile = null;
1103cb3fca6SLiu Zhe 	private File targetFile = null;
1113cb3fca6SLiu Zhe 	private String sourceFileUrl = null;
1123cb3fca6SLiu Zhe 	private String targetFileUrl = null;
113527dbd07SLiu Zhe 
114f4092fc8SLiu Zhe 	private String scenario = null;
115f4092fc8SLiu Zhe 	private String sourceFileId = null;
1163cb3fca6SLiu Zhe 	private long loadTime = -1;
1173cb3fca6SLiu Zhe 	private long saveTime = -1;
1183cb3fca6SLiu Zhe 	private long closeTime = -1;
1193cb3fca6SLiu Zhe 
Conversion(String sourcePath, String targetFilterName, String targetExtName)120527dbd07SLiu Zhe 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
1213cb3fca6SLiu Zhe 		super();
122527dbd07SLiu Zhe 		this.sourcePath = sourcePath;
1233cb3fca6SLiu Zhe 		this.targetFilterName = targetFilterName;
124527dbd07SLiu Zhe 		this.targetExtName = targetExtName;
1255f863819SLiu Zhe 		counter++;
126f4092fc8SLiu Zhe 	}
127f4092fc8SLiu Zhe 
128f4092fc8SLiu Zhe 	@Before
before()129f4092fc8SLiu Zhe 	public void before() throws Exception {
1309b5f68a3SLiu Zhe 		sourceFile = new File(sourcePath);
131527dbd07SLiu Zhe 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
132527dbd07SLiu Zhe 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
133527dbd07SLiu Zhe 		targetFileUrl = FileUtil.getUrl(this.targetFile);
134527dbd07SLiu Zhe 
135527dbd07SLiu Zhe 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
1369b5f68a3SLiu Zhe 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
1379b5f68a3SLiu Zhe 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
1389b5f68a3SLiu Zhe 		sourceFileId = pathSource.replace(pathRepos, "");
139f4092fc8SLiu Zhe 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
140f4092fc8SLiu Zhe 		app.start();
141f4092fc8SLiu Zhe 	}
142f4092fc8SLiu Zhe 
143f4092fc8SLiu Zhe 	@After
after()144f4092fc8SLiu Zhe 	public void after() throws Exception{
14575ed6c31SLiu Zhe 		result.addRow("data", sourceFileId, scenario, counter % repeat , closeTime, saveTime, loadTime, sourceFile.length(), targetFile.length());
146f4092fc8SLiu Zhe 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
1479b5f68a3SLiu Zhe 		if (closeTime < 0) {
1489b5f68a3SLiu Zhe 			app.close();
1499b5f68a3SLiu Zhe 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
150f4092fc8SLiu Zhe 			app.close();
151f4092fc8SLiu Zhe 		}
1523cb3fca6SLiu Zhe 	}
1539b5f68a3SLiu Zhe 
propertyValue(String name, Object value)1543cb3fca6SLiu Zhe 	private PropertyValue propertyValue(String name, Object value) {
1553cb3fca6SLiu Zhe 		PropertyValue p = new PropertyValue();
1563cb3fca6SLiu Zhe 		p.Name = name;
1573cb3fca6SLiu Zhe 		p.Value= value;
1583cb3fca6SLiu Zhe 		return p;
1593cb3fca6SLiu Zhe 	}
16075ed6c31SLiu Zhe 
1613cb3fca6SLiu Zhe 
162*27723ce4SLinyi Li 	@Test(timeout=10 * 60000)
testConversion()1633cb3fca6SLiu Zhe 	public void testConversion() throws Exception {
1648fd475f4SLinyi Li 		try {
16575ed6c31SLiu Zhe 			if (nSleep > 0)
16675ed6c31SLiu Zhe 				Thread.sleep(nSleep);
1678fd475f4SLinyi Li 			long start = System.currentTimeMillis();
1688fd475f4SLinyi Li 			XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
1698fd475f4SLinyi Li 					propertyValue("Hidden", true),
1708fd475f4SLinyi Li 					propertyValue("ReadOnly", true),
1718fd475f4SLinyi Li 					propertyValue("AsyncMode", false),
1728fd475f4SLinyi Li 					propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE),
1738fd475f4SLinyi Li 					propertyValue("LimitationCheckLevel", nLevelInfo));
1748fd475f4SLinyi Li 
1758fd475f4SLinyi Li 			loadTime = System.currentTimeMillis() - start;
1768fd475f4SLinyi Li 
17775ed6c31SLiu Zhe 			if (nSleep > 0)
17875ed6c31SLiu Zhe 				Thread.sleep(nSleep);
1798fd475f4SLinyi Li 			app.saveDocumentToURL(doc, targetFileUrl,
1808fd475f4SLinyi Li 					propertyValue( "FilterName", targetFilterName),
1818fd475f4SLinyi Li 					propertyValue( "Overwrite", true));
1828fd475f4SLinyi Li 			saveTime = System.currentTimeMillis() - start - nSleep;
1838fd475f4SLinyi Li 			XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
1848fd475f4SLinyi Li 			xCloseable.close(true);
1858fd475f4SLinyi Li 			closeTime = System.currentTimeMillis() - start - nSleep;
1868fd475f4SLinyi Li 		} catch (com.sun.star.task.ErrorCodeIOException e){
1878fd475f4SLinyi Li 			int errCode = e.ErrCode;
188*27723ce4SLinyi Li 			if( 296 == errCode ) {	// limitation check file
1898fd475f4SLinyi Li 				loadTime = -2;
1908fd475f4SLinyi Li 				saveTime = -2;
1918fd475f4SLinyi Li 				closeTime = -2;
1928fd475f4SLinyi Li 			}
1938fd475f4SLinyi Li 
194*27723ce4SLinyi Li 			if( 3878 == errCode ) {	// file is corrupt
195*27723ce4SLinyi Li 				loadTime = -3;
196*27723ce4SLinyi Li 				saveTime = -3;
197*27723ce4SLinyi Li 				closeTime = -3;
198*27723ce4SLinyi Li 			}
199*27723ce4SLinyi Li 
2008fd475f4SLinyi Li 			throw e;
2018fd475f4SLinyi Li 		}
2023cb3fca6SLiu Zhe 	}
2033cb3fca6SLiu Zhe }
204