xref: /trunk/test/testuno/source/pvt/uno/Conversion.java (revision 42a976b737d4e2b88cfbe2fe1cd610fe957bdc84)
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 
283e7cc3ecSLiu Zhe import org.junit.After;
293e7cc3ecSLiu Zhe import org.junit.AfterClass;
303e7cc3ecSLiu Zhe import org.junit.Before;
313e7cc3ecSLiu Zhe import org.junit.BeforeClass;
323e7cc3ecSLiu Zhe import org.junit.Rule;
333e7cc3ecSLiu Zhe import org.junit.Test;
343e7cc3ecSLiu Zhe import org.junit.runner.RunWith;
353e7cc3ecSLiu Zhe import org.openoffice.test.OpenOffice;
36329fd865SLiu Zhe import org.openoffice.test.common.DataSheet;
373e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider;
383e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider.FileFilter;
395f863819SLiu Zhe import org.openoffice.test.common.FileProvider.FileRepeat;
403e7cc3ecSLiu Zhe import org.openoffice.test.common.FileProvider.FileRepos;
413e7cc3ecSLiu Zhe import org.openoffice.test.common.FileUtil;
423e7cc3ecSLiu Zhe import org.openoffice.test.common.Logger;
4375ed6c31SLiu Zhe import org.openoffice.test.common.Testspace;
443e7cc3ecSLiu Zhe import org.openoffice.test.uno.UnoApp;
4575ed6c31SLiu Zhe 
463e7cc3ecSLiu Zhe import com.sun.star.beans.PropertyValue;
473e7cc3ecSLiu Zhe import com.sun.star.document.MacroExecMode;
483e7cc3ecSLiu Zhe import com.sun.star.lang.XComponent;
493e7cc3ecSLiu Zhe import com.sun.star.uno.UnoRuntime;
503e7cc3ecSLiu Zhe import com.sun.star.util.XCloseable;
513e7cc3ecSLiu Zhe 
523e7cc3ecSLiu Zhe 
533e7cc3ecSLiu Zhe @RunWith(FileProvider.class)
543e7cc3ecSLiu Zhe public class Conversion {
553e7cc3ecSLiu Zhe 
563e7cc3ecSLiu Zhe     @Rule
573e7cc3ecSLiu Zhe     public Logger log = Logger.getLogger(this);
583e7cc3ecSLiu Zhe 
593e7cc3ecSLiu Zhe     @FileRepos
6075ed6c31SLiu Zhe     public static String repos = System.getProperty("conversion.repos", getDataPath("uno"));
613e7cc3ecSLiu 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");
725f863819SLiu Zhe 
735f863819SLiu Zhe     @FileRepeat
749dae0b27SLinyi Li     public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
753e7cc3ecSLiu Zhe 
769b5f68a3SLiu Zhe     public static String clean = System.getProperty("conversion.clean", "file");
779b5f68a3SLiu Zhe 
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
794481f6d8SLinyi Li 
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();
833e7cc3ecSLiu Zhe 
84329fd865SLiu Zhe     private static UnoApp app = null;
85329fd865SLiu Zhe 
86329fd865SLiu Zhe     private static DataSheet result;
873e7cc3ecSLiu Zhe 
8825bd9146SLiu Zhe     private static int counter = -1;
895f863819SLiu Zhe 
903e7cc3ecSLiu Zhe     @BeforeClass
beforeClass()913e7cc3ecSLiu 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");
983e7cc3ecSLiu Zhe     }
993e7cc3ecSLiu Zhe 
1003e7cc3ecSLiu Zhe     @AfterClass
afterClass()1013e7cc3ecSLiu Zhe     public static void afterClass() throws Exception {
1023e7cc3ecSLiu Zhe         app.close();
1033e7cc3ecSLiu Zhe     }
1043e7cc3ecSLiu Zhe 
1053e7cc3ecSLiu Zhe     private String sourcePath = null;
1063e7cc3ecSLiu Zhe     private String targetFilterName = null;
1073e7cc3ecSLiu Zhe     private String targetExtName = null;
1083e7cc3ecSLiu Zhe 
1093e7cc3ecSLiu Zhe     private File sourceFile = null;
1103e7cc3ecSLiu Zhe     private File targetFile = null;
1113e7cc3ecSLiu Zhe     private String sourceFileUrl = null;
1123e7cc3ecSLiu Zhe     private String targetFileUrl = null;
1133e7cc3ecSLiu Zhe 
1143e7cc3ecSLiu Zhe     private String scenario = null;
1153e7cc3ecSLiu Zhe     private String sourceFileId = null;
1163e7cc3ecSLiu Zhe     private long loadTime = -1;
1173e7cc3ecSLiu Zhe     private long saveTime = -1;
1183e7cc3ecSLiu Zhe     private long closeTime = -1;
1193e7cc3ecSLiu Zhe 
Conversion(String sourcePath, String targetFilterName, String targetExtName)1203e7cc3ecSLiu Zhe     public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
1213e7cc3ecSLiu Zhe         super();
1223e7cc3ecSLiu Zhe         this.sourcePath = sourcePath;
1233e7cc3ecSLiu Zhe         this.targetFilterName = targetFilterName;
1243e7cc3ecSLiu Zhe         this.targetExtName = targetExtName;
1255f863819SLiu Zhe         counter++;
1263e7cc3ecSLiu Zhe     }
1273e7cc3ecSLiu Zhe 
1283e7cc3ecSLiu Zhe     @Before
before()1293e7cc3ecSLiu Zhe     public void before() throws Exception {
1309b5f68a3SLiu Zhe         sourceFile = new File(sourcePath);
1313e7cc3ecSLiu Zhe         sourceFileUrl = FileUtil.getUrl(this.sourceFile);
1323e7cc3ecSLiu Zhe         targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
1333e7cc3ecSLiu Zhe         targetFileUrl = FileUtil.getUrl(this.targetFile);
1343e7cc3ecSLiu Zhe 
1353e7cc3ecSLiu 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, "");
1393e7cc3ecSLiu Zhe         log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
1403e7cc3ecSLiu Zhe         app.start();
1413e7cc3ecSLiu Zhe     }
1423e7cc3ecSLiu Zhe 
1433e7cc3ecSLiu Zhe     @After
after()1443e7cc3ecSLiu Zhe     public void after() throws Exception{
14575ed6c31SLiu Zhe         result.addRow("data", sourceFileId, scenario, counter % repeat , closeTime, saveTime, loadTime, sourceFile.length(), targetFile.length());
1463e7cc3ecSLiu 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) {
1503e7cc3ecSLiu Zhe             app.close();
1513e7cc3ecSLiu Zhe         }
1523e7cc3ecSLiu Zhe     }
1533e7cc3ecSLiu Zhe 
propertyValue(String name, Object value)1543e7cc3ecSLiu Zhe     private PropertyValue propertyValue(String name, Object value) {
1553e7cc3ecSLiu Zhe         PropertyValue p = new PropertyValue();
1563e7cc3ecSLiu Zhe         p.Name = name;
1573e7cc3ecSLiu Zhe         p.Value= value;
1583e7cc3ecSLiu Zhe         return p;
1593e7cc3ecSLiu Zhe     }
1603e7cc3ecSLiu Zhe 
16175ed6c31SLiu Zhe 
162*27723ce4SLinyi Li     @Test(timeout=10 * 60000)
testConversion()1633e7cc3ecSLiu Zhe     public void testConversion() throws Exception {
1648fd475f4SLinyi Li         try {
16575ed6c31SLiu Zhe             if (nSleep > 0)
16675ed6c31SLiu Zhe                 Thread.sleep(nSleep);
1673e7cc3ecSLiu Zhe             long start = System.currentTimeMillis();
1683e7cc3ecSLiu Zhe             XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
1693e7cc3ecSLiu Zhe                     propertyValue("Hidden", true),
1703e7cc3ecSLiu Zhe                     propertyValue("ReadOnly", true),
1713e7cc3ecSLiu Zhe                     propertyValue("AsyncMode", false),
172a1693044SLinyi Li                     propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE),
173a1693044SLinyi Li                     propertyValue("LimitationCheckLevel", nLevelInfo));
1743e7cc3ecSLiu Zhe 
1753e7cc3ecSLiu Zhe             loadTime = System.currentTimeMillis() - start;
1768fd475f4SLinyi Li 
17775ed6c31SLiu Zhe             if (nSleep > 0)
17875ed6c31SLiu Zhe                 Thread.sleep(nSleep);
1793e7cc3ecSLiu Zhe             app.saveDocumentToURL(doc, targetFileUrl,
1803e7cc3ecSLiu Zhe                     propertyValue( "FilterName", targetFilterName),
1813e7cc3ecSLiu Zhe                     propertyValue( "Overwrite", true));
1828fd475f4SLinyi Li             saveTime = System.currentTimeMillis() - start - nSleep;
1833e7cc3ecSLiu Zhe             XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
1843e7cc3ecSLiu Zhe             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;
1923e7cc3ecSLiu Zhe             }
1935f863819SLiu Zhe 
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         }
2028fd475f4SLinyi Li     }
2033e7cc3ecSLiu Zhe }
204