1*b1cdbd2cSJim Jagielski /************************************************************** 2*b1cdbd2cSJim Jagielski * 3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one 4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file 5*b1cdbd2cSJim Jagielski * distributed with this work for additional information 6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file 7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the 8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance 9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at 10*b1cdbd2cSJim Jagielski * 11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0 12*b1cdbd2cSJim Jagielski * 13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing, 14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an 15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the 17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations 18*b1cdbd2cSJim Jagielski * under the License. 19*b1cdbd2cSJim Jagielski * 20*b1cdbd2cSJim Jagielski *************************************************************/ 21*b1cdbd2cSJim Jagielski 22*b1cdbd2cSJim Jagielski package pvt.uno; 23*b1cdbd2cSJim Jagielski 24*b1cdbd2cSJim Jagielski import static org.openoffice.test.common.Testspace.*; 25*b1cdbd2cSJim Jagielski 26*b1cdbd2cSJim Jagielski import java.io.File; 27*b1cdbd2cSJim Jagielski 28*b1cdbd2cSJim Jagielski import org.junit.After; 29*b1cdbd2cSJim Jagielski import org.junit.AfterClass; 30*b1cdbd2cSJim Jagielski import org.junit.Before; 31*b1cdbd2cSJim Jagielski import org.junit.BeforeClass; 32*b1cdbd2cSJim Jagielski import org.junit.Rule; 33*b1cdbd2cSJim Jagielski import org.junit.Test; 34*b1cdbd2cSJim Jagielski import org.junit.runner.RunWith; 35*b1cdbd2cSJim Jagielski import org.openoffice.test.OpenOffice; 36*b1cdbd2cSJim Jagielski import org.openoffice.test.common.DataSheet; 37*b1cdbd2cSJim Jagielski import org.openoffice.test.common.FileProvider; 38*b1cdbd2cSJim Jagielski import org.openoffice.test.common.FileProvider.FileFilter; 39*b1cdbd2cSJim Jagielski import org.openoffice.test.common.FileProvider.FileRepeat; 40*b1cdbd2cSJim Jagielski import org.openoffice.test.common.FileProvider.FileRepos; 41*b1cdbd2cSJim Jagielski import org.openoffice.test.common.FileUtil; 42*b1cdbd2cSJim Jagielski import org.openoffice.test.common.Logger; 43*b1cdbd2cSJim Jagielski import org.openoffice.test.common.Testspace; 44*b1cdbd2cSJim Jagielski import org.openoffice.test.uno.UnoApp; 45*b1cdbd2cSJim Jagielski 46*b1cdbd2cSJim Jagielski import com.sun.star.beans.PropertyValue; 47*b1cdbd2cSJim Jagielski import com.sun.star.document.MacroExecMode; 48*b1cdbd2cSJim Jagielski import com.sun.star.lang.XComponent; 49*b1cdbd2cSJim Jagielski import com.sun.star.uno.UnoRuntime; 50*b1cdbd2cSJim Jagielski import com.sun.star.util.XCloseable; 51*b1cdbd2cSJim Jagielski 52*b1cdbd2cSJim Jagielski 53*b1cdbd2cSJim Jagielski @RunWith(FileProvider.class) 54*b1cdbd2cSJim Jagielski public class Conversion { 55*b1cdbd2cSJim Jagielski 56*b1cdbd2cSJim Jagielski @Rule 57*b1cdbd2cSJim Jagielski public Logger log = Logger.getLogger(this); 58*b1cdbd2cSJim Jagielski 59*b1cdbd2cSJim Jagielski @FileRepos 60*b1cdbd2cSJim Jagielski public static String repos = System.getProperty("conversion.repos", getDataPath("uno")); 61*b1cdbd2cSJim Jagielski @FileFilter 62*b1cdbd2cSJim Jagielski public static String filter = System.getProperty("conversion.filter", 63*b1cdbd2cSJim Jagielski "-f .*(doc|dot|odt|ott)$ writer_pdf_Export pdf " 64*b1cdbd2cSJim Jagielski + "-f .*(xls|xlt|ods|ots)$ calc_pdf_Export pdf " 65*b1cdbd2cSJim Jagielski + "-f .*(ppt|ppt|odp|otp)$ impress_pdf_Export pdf " 66*b1cdbd2cSJim Jagielski + "-f .*(doc|dot|docx|docm|dotx|dotm)$ writer8 odt " 67*b1cdbd2cSJim Jagielski + "-f .*(xls|xlt|xlsx|xltx|xlsm|xltm)$ calc8 ods " 68*b1cdbd2cSJim Jagielski + "-f .*(ppt|pot|pptx|pptm|potm|potx)$ impress8 odp " 69*b1cdbd2cSJim Jagielski + "-f .*(odt|ott)$ 'MS Word 97' doc " 70*b1cdbd2cSJim Jagielski + "-f .*(ods|ots)$ 'MS Excel 97' xls " 71*b1cdbd2cSJim Jagielski + "-f .*(odp|otp)$ 'MS PowerPoint 97' ppt"); 72*b1cdbd2cSJim Jagielski 73*b1cdbd2cSJim Jagielski @FileRepeat 74*b1cdbd2cSJim Jagielski public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8")); 75*b1cdbd2cSJim Jagielski 76*b1cdbd2cSJim Jagielski public static String clean = System.getProperty("conversion.clean", "file"); 77*b1cdbd2cSJim Jagielski 78*b1cdbd2cSJim Jagielski public static int nLevelInfo = Integer.parseInt(System.getProperty("conversion.limitationcheck", "0")); // Level info: starts from 1, 0 means no need for limitation check 79*b1cdbd2cSJim Jagielski 80*b1cdbd2cSJim Jagielski public static long nSleep = Long.parseLong(System.getProperty("conversion.sleep", "0")); // Sleep before loadComponentFromURL and storeToURL, millisecond 81*b1cdbd2cSJim Jagielski 82*b1cdbd2cSJim Jagielski private static OpenOffice aoo = new OpenOffice(); 83*b1cdbd2cSJim Jagielski 84*b1cdbd2cSJim Jagielski private static UnoApp app = null; 85*b1cdbd2cSJim Jagielski 86*b1cdbd2cSJim Jagielski private static DataSheet result; 87*b1cdbd2cSJim Jagielski 88*b1cdbd2cSJim Jagielski private static int counter = -1; 89*b1cdbd2cSJim Jagielski 90*b1cdbd2cSJim Jagielski @BeforeClass beforeClass()91*b1cdbd2cSJim Jagielski public static void beforeClass() throws Exception { 92*b1cdbd2cSJim Jagielski aoo.setUnoUrl(OpenOffice.DEFAULT_UNO_URL); 93*b1cdbd2cSJim Jagielski aoo.addArgs("-invisible", "-conversionmode", "-hidemenu", "-nofirststartwizard", "-headless", "-enableautomation"); 94*b1cdbd2cSJim Jagielski app = new UnoApp(aoo); 95*b1cdbd2cSJim Jagielski Testspace.prepareDataFile("limit_cfg.ini", aoo.getHome().toString()+"/program"); // Move limitation check file to installation dir 96*b1cdbd2cSJim Jagielski result = new DataSheet(getFile("output/" + Conversion.class.getName()+ ".xml")); 97*b1cdbd2cSJim Jagielski result.addRow("data", "File","Scenario", "No", "Time Consumed After Closing","Time Consumed After Saving","Time Consumed After Loading", "File Size", "Exported File Size"); 98*b1cdbd2cSJim Jagielski } 99*b1cdbd2cSJim Jagielski 100*b1cdbd2cSJim Jagielski @AfterClass afterClass()101*b1cdbd2cSJim Jagielski public static void afterClass() throws Exception { 102*b1cdbd2cSJim Jagielski app.close(); 103*b1cdbd2cSJim Jagielski } 104*b1cdbd2cSJim Jagielski 105*b1cdbd2cSJim Jagielski private String sourcePath = null; 106*b1cdbd2cSJim Jagielski private String targetFilterName = null; 107*b1cdbd2cSJim Jagielski private String targetExtName = null; 108*b1cdbd2cSJim Jagielski 109*b1cdbd2cSJim Jagielski private File sourceFile = null; 110*b1cdbd2cSJim Jagielski private File targetFile = null; 111*b1cdbd2cSJim Jagielski private String sourceFileUrl = null; 112*b1cdbd2cSJim Jagielski private String targetFileUrl = null; 113*b1cdbd2cSJim Jagielski 114*b1cdbd2cSJim Jagielski private String scenario = null; 115*b1cdbd2cSJim Jagielski private String sourceFileId = null; 116*b1cdbd2cSJim Jagielski private long loadTime = -1; 117*b1cdbd2cSJim Jagielski private long saveTime = -1; 118*b1cdbd2cSJim Jagielski private long closeTime = -1; 119*b1cdbd2cSJim Jagielski Conversion(String sourcePath, String targetFilterName, String targetExtName)120*b1cdbd2cSJim Jagielski public Conversion(String sourcePath, String targetFilterName, String targetExtName) { 121*b1cdbd2cSJim Jagielski super(); 122*b1cdbd2cSJim Jagielski this.sourcePath = sourcePath; 123*b1cdbd2cSJim Jagielski this.targetFilterName = targetFilterName; 124*b1cdbd2cSJim Jagielski this.targetExtName = targetExtName; 125*b1cdbd2cSJim Jagielski counter++; 126*b1cdbd2cSJim Jagielski } 127*b1cdbd2cSJim Jagielski 128*b1cdbd2cSJim Jagielski @Before before()129*b1cdbd2cSJim Jagielski public void before() throws Exception { 130*b1cdbd2cSJim Jagielski sourceFile = new File(sourcePath); 131*b1cdbd2cSJim Jagielski sourceFileUrl = FileUtil.getUrl(this.sourceFile); 132*b1cdbd2cSJim Jagielski targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName); 133*b1cdbd2cSJim Jagielski targetFileUrl = FileUtil.getUrl(this.targetFile); 134*b1cdbd2cSJim Jagielski 135*b1cdbd2cSJim Jagielski scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase(); 136*b1cdbd2cSJim Jagielski String pathSource = sourceFile.getCanonicalPath().replace("\\", "/"); 137*b1cdbd2cSJim Jagielski String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/"; 138*b1cdbd2cSJim Jagielski sourceFileId = pathSource.replace(pathRepos, ""); 139*b1cdbd2cSJim Jagielski log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]"); 140*b1cdbd2cSJim Jagielski app.start(); 141*b1cdbd2cSJim Jagielski } 142*b1cdbd2cSJim Jagielski 143*b1cdbd2cSJim Jagielski @After after()144*b1cdbd2cSJim Jagielski public void after() throws Exception{ 145*b1cdbd2cSJim Jagielski result.addRow("data", sourceFileId, scenario, counter % repeat , closeTime, saveTime, loadTime, sourceFile.length(), targetFile.length()); 146*b1cdbd2cSJim Jagielski log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]"); 147*b1cdbd2cSJim Jagielski if (closeTime < 0) { 148*b1cdbd2cSJim Jagielski app.close(); 149*b1cdbd2cSJim Jagielski } else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) { 150*b1cdbd2cSJim Jagielski app.close(); 151*b1cdbd2cSJim Jagielski } 152*b1cdbd2cSJim Jagielski } 153*b1cdbd2cSJim Jagielski propertyValue(String name, Object value)154*b1cdbd2cSJim Jagielski private PropertyValue propertyValue(String name, Object value) { 155*b1cdbd2cSJim Jagielski PropertyValue p = new PropertyValue(); 156*b1cdbd2cSJim Jagielski p.Name = name; 157*b1cdbd2cSJim Jagielski p.Value= value; 158*b1cdbd2cSJim Jagielski return p; 159*b1cdbd2cSJim Jagielski } 160*b1cdbd2cSJim Jagielski 161*b1cdbd2cSJim Jagielski 162*b1cdbd2cSJim Jagielski @Test(timeout=10 * 60000) testConversion()163*b1cdbd2cSJim Jagielski public void testConversion() throws Exception { 164*b1cdbd2cSJim Jagielski try { 165*b1cdbd2cSJim Jagielski if (nSleep > 0) 166*b1cdbd2cSJim Jagielski Thread.sleep(nSleep); 167*b1cdbd2cSJim Jagielski long start = System.currentTimeMillis(); 168*b1cdbd2cSJim Jagielski XComponent doc = app.loadDocumentFromURL(sourceFileUrl, 169*b1cdbd2cSJim Jagielski propertyValue("Hidden", true), 170*b1cdbd2cSJim Jagielski propertyValue("ReadOnly", true), 171*b1cdbd2cSJim Jagielski propertyValue("AsyncMode", false), 172*b1cdbd2cSJim Jagielski propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE), 173*b1cdbd2cSJim Jagielski propertyValue("LimitationCheckLevel", nLevelInfo)); 174*b1cdbd2cSJim Jagielski 175*b1cdbd2cSJim Jagielski loadTime = System.currentTimeMillis() - start; 176*b1cdbd2cSJim Jagielski 177*b1cdbd2cSJim Jagielski if (nSleep > 0) 178*b1cdbd2cSJim Jagielski Thread.sleep(nSleep); 179*b1cdbd2cSJim Jagielski app.saveDocumentToURL(doc, targetFileUrl, 180*b1cdbd2cSJim Jagielski propertyValue( "FilterName", targetFilterName), 181*b1cdbd2cSJim Jagielski propertyValue( "Overwrite", true)); 182*b1cdbd2cSJim Jagielski saveTime = System.currentTimeMillis() - start - nSleep; 183*b1cdbd2cSJim Jagielski XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc); 184*b1cdbd2cSJim Jagielski xCloseable.close(true); 185*b1cdbd2cSJim Jagielski closeTime = System.currentTimeMillis() - start - nSleep; 186*b1cdbd2cSJim Jagielski } catch (com.sun.star.task.ErrorCodeIOException e){ 187*b1cdbd2cSJim Jagielski int errCode = e.ErrCode; 188*b1cdbd2cSJim Jagielski if( 296 == errCode ) { // limitation check file 189*b1cdbd2cSJim Jagielski loadTime = -2; 190*b1cdbd2cSJim Jagielski saveTime = -2; 191*b1cdbd2cSJim Jagielski closeTime = -2; 192*b1cdbd2cSJim Jagielski } 193*b1cdbd2cSJim Jagielski 194*b1cdbd2cSJim Jagielski if( 3878 == errCode ) { // file is corrupt 195*b1cdbd2cSJim Jagielski loadTime = -3; 196*b1cdbd2cSJim Jagielski saveTime = -3; 197*b1cdbd2cSJim Jagielski closeTime = -3; 198*b1cdbd2cSJim Jagielski } 199*b1cdbd2cSJim Jagielski 200*b1cdbd2cSJim Jagielski throw e; 201*b1cdbd2cSJim Jagielski } 202*b1cdbd2cSJim Jagielski } 203*b1cdbd2cSJim Jagielski } 204