1eba4d44aSLiu Zhe /************************************************************** 2eba4d44aSLiu Zhe * 3eba4d44aSLiu Zhe * Licensed to the Apache Software Foundation (ASF) under one 4eba4d44aSLiu Zhe * or more contributor license agreements. See the NOTICE file 5eba4d44aSLiu Zhe * distributed with this work for additional information 6eba4d44aSLiu Zhe * regarding copyright ownership. The ASF licenses this file 7eba4d44aSLiu Zhe * to you under the Apache License, Version 2.0 (the 8eba4d44aSLiu Zhe * "License"); you may not use this file except in compliance 9eba4d44aSLiu Zhe * with the License. You may obtain a copy of the License at 10eba4d44aSLiu Zhe * 11eba4d44aSLiu Zhe * http://www.apache.org/licenses/LICENSE-2.0 12eba4d44aSLiu Zhe * 13eba4d44aSLiu Zhe * Unless required by applicable law or agreed to in writing, 14eba4d44aSLiu Zhe * software distributed under the License is distributed on an 15eba4d44aSLiu Zhe * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16eba4d44aSLiu Zhe * KIND, either express or implied. See the License for the 17eba4d44aSLiu Zhe * specific language governing permissions and limitations 18eba4d44aSLiu Zhe * under the License. 19eba4d44aSLiu Zhe */ 200afc47faSDongJun Zong package fvt.uno.ffc; 21eba4d44aSLiu Zhe 22eba4d44aSLiu Zhe import java.io.File; 23eba4d44aSLiu Zhe import java.io.FilenameFilter; 24eba4d44aSLiu Zhe import java.util.HashMap; 25eba4d44aSLiu Zhe import java.util.List; 26eba4d44aSLiu Zhe import java.util.Map; 277bf9ba33SDongJun Zong import java.util.logging.Level; 28eba4d44aSLiu Zhe 29eba4d44aSLiu Zhe import junit.framework.Assert; 30eba4d44aSLiu Zhe 31eba4d44aSLiu Zhe import org.junit.After; 32eba4d44aSLiu Zhe import org.junit.Before; 33eba4d44aSLiu Zhe import org.junit.BeforeClass; 342b9e17edSLi Feng Wang import org.junit.Ignore; 35eba4d44aSLiu Zhe import org.junit.Rule; 36eba4d44aSLiu Zhe import org.junit.Test; 37eba4d44aSLiu Zhe import org.junit.runner.RunWith; 38eba4d44aSLiu Zhe import org.openoffice.test.OpenOffice; 39eba4d44aSLiu Zhe import org.openoffice.test.common.FileProvider; 40eba4d44aSLiu Zhe import org.openoffice.test.common.FileProvider.FileRepos; 41eba4d44aSLiu Zhe import org.openoffice.test.common.FileUtil; 42eba4d44aSLiu Zhe import org.openoffice.test.common.Logger; 43eba4d44aSLiu Zhe import org.openoffice.test.common.SystemUtil; 44eba4d44aSLiu Zhe import org.openoffice.test.common.Testspace; 45eba4d44aSLiu Zhe import org.openoffice.test.uno.UnoApp; 46eba4d44aSLiu Zhe 47eba4d44aSLiu Zhe import com.sun.star.beans.PropertyValue; 48eba4d44aSLiu Zhe import com.sun.star.document.MacroExecMode; 49eba4d44aSLiu Zhe import com.sun.star.frame.XComponentLoader; 50eba4d44aSLiu Zhe import com.sun.star.frame.XStorable; 51eba4d44aSLiu Zhe import com.sun.star.io.IOException; 52eba4d44aSLiu Zhe import com.sun.star.lang.IllegalArgumentException; 53eba4d44aSLiu Zhe import com.sun.star.lang.XComponent; 54eba4d44aSLiu Zhe import com.sun.star.uno.UnoRuntime; 55eba4d44aSLiu Zhe /** 56eba4d44aSLiu Zhe * Pls place a suite file in directory "suite" which is same level with test uno. like bewlow 57eba4d44aSLiu Zhe * -suite 58eba4d44aSLiu Zhe * -testuno 59eba4d44aSLiu Zhe * The suite file content is like this format 60eba4d44aSLiu Zhe * ftp://user:password@192.168.0.1/public/sample/testsample.doc 61eba4d44aSLiu Zhe * .. 62eba4d44aSLiu Zhe * .. 63eba4d44aSLiu Zhe * ftp://user:password@192.168.0.1/public/sample/testsample2.doc 64eba4d44aSLiu Zhe *This script is used to test FFC by UNO API 65eba4d44aSLiu Zhe *It cover below scenario: 66eba4d44aSLiu Zhe *MS2003/2010 format->ODF format 67eba4d44aSLiu Zhe *New Saved ODF Format file -> MS 2003 Format 68eba4d44aSLiu Zhe *New Saved ODF Format file -> PDF 69eba4d44aSLiu Zhe * 70eba4d44aSLiu Zhe */ 712b9e17edSLi Feng Wang @RunWith(FileProvider.class) 72eba4d44aSLiu Zhe public class FFCTest { 73eba4d44aSLiu Zhe @Rule 74eba4d44aSLiu Zhe public Logger log = Logger.getLogger(this, false); 75eba4d44aSLiu Zhe 764da5b138SDongJun Zong 774da5b138SDongJun Zong private static UnoApp app = null; 78eba4d44aSLiu Zhe private static Map<String, String> filterMap = new HashMap<String, String>(); 79eba4d44aSLiu Zhe @FileRepos 802b9e17edSLi Feng Wang public static String suiteDir = "../suite/"; 81eba4d44aSLiu Zhe private String fileURL = ""; 82eba4d44aSLiu Zhe private String operateFilePath = ""; 83eba4d44aSLiu Zhe private static Map<String, String> formatMap = new HashMap<String, String>(); 84eba4d44aSLiu Zhe private static File testSpaceFile = Testspace.getFile(); 85eba4d44aSLiu Zhe private boolean isSucceed = false; 86eba4d44aSLiu Zhe private static String tempFolder = testSpaceFile.getAbsolutePath() + File.separator + "temp"; 87eba4d44aSLiu Zhe 882b9e17edSLi Feng Wang private static String failedFilesDir = "output/failedSampleFiles/"; 897bf9ba33SDongJun Zong private String runedScenarios = ""; 90eba4d44aSLiu Zhe FFCTest(String url)91eba4d44aSLiu Zhe public FFCTest(String url) { 92eba4d44aSLiu Zhe this.fileURL = url; 93eba4d44aSLiu Zhe } 94eba4d44aSLiu Zhe 95eba4d44aSLiu Zhe @BeforeClass init()96eba4d44aSLiu Zhe public static void init() { 97eba4d44aSLiu Zhe initMap(); 987bf9ba33SDongJun Zong File failedDirec = Testspace.getFile(failedFilesDir); 997bf9ba33SDongJun Zong FileUtil.deleteFile(failedDirec); 100eba4d44aSLiu Zhe //Disable automation 1014da5b138SDongJun Zong 1024da5b138SDongJun Zong OpenOffice defaultOpenOffice = new OpenOffice(); 1034da5b138SDongJun Zong defaultOpenOffice.addArgs("-nofirststartwizard", "-norestore", "-quickstart=no"); 1044da5b138SDongJun Zong defaultOpenOffice.setUnoUrl(OpenOffice.DEFAULT_UNO_URL); 1054da5b138SDongJun Zong defaultOpenOffice.addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu"); 1064da5b138SDongJun Zong app = new UnoApp(defaultOpenOffice); 107eba4d44aSLiu Zhe 1087bf9ba33SDongJun Zong 109eba4d44aSLiu Zhe failedDirec.mkdirs(); 1107bf9ba33SDongJun Zong 111eba4d44aSLiu Zhe } 112eba4d44aSLiu Zhe 113eba4d44aSLiu Zhe @Before setUp()114eba4d44aSLiu Zhe public void setUp() throws Exception { 115eba4d44aSLiu Zhe operateFilePath = Testspace.prepareData(fileURL); 116eba4d44aSLiu Zhe 1177bf9ba33SDongJun Zong runedScenarios = ""; 118eba4d44aSLiu Zhe app.start(); 119eba4d44aSLiu Zhe } 120eba4d44aSLiu Zhe @After tearDown()121eba4d44aSLiu Zhe public void tearDown() throws Exception { 122eba4d44aSLiu Zhe if (!isSucceed) { 123eba4d44aSLiu Zhe FileUtil.copyFile(operateFilePath, Testspace.getFile(failedFilesDir).getAbsolutePath()); 124eba4d44aSLiu Zhe FileUtil.appendStringToFile( Testspace.getFile(failedFilesDir + File.separator + "failedFiles.files").getAbsolutePath(), fileURL +"\r\n"); 1257bf9ba33SDongJun Zong String failedMessage = fileURL +"\r\n" + runedScenarios; 1267bf9ba33SDongJun Zong FileUtil.appendStringToFile( Testspace.getFile(failedFilesDir + File.separator + "failedFiles_scenarios.files").getAbsolutePath(), failedMessage); 127eba4d44aSLiu Zhe app.close(); 128eba4d44aSLiu Zhe SystemUtil.killProcess("WerFault.*"); 129eba4d44aSLiu Zhe SystemUtil.sleep(2); 130eba4d44aSLiu Zhe SystemUtil.killProcess("EQNEDT32.*"); 131eba4d44aSLiu Zhe //WerFault.exe 132eba4d44aSLiu Zhe //EQNEDT32.EXE 133eba4d44aSLiu Zhe } 134eba4d44aSLiu Zhe } 135eba4d44aSLiu Zhe 136eba4d44aSLiu Zhe 137eba4d44aSLiu Zhe 1382b9e17edSLi Feng Wang @Test(timeout=1000*60*10) 139*bb8589ebSLi Feng Wang @Ignore exportTest()1402b9e17edSLi Feng Wang public void exportTest() throws Exception { 1412b9e17edSLi Feng Wang //MS Office Format ->ODF 1422b9e17edSLi Feng Wang boolean flag = false; 1432b9e17edSLi Feng Wang 1442b9e17edSLi Feng Wang String saveAsODF = exportAsODF(operateFilePath); 1452b9e17edSLi Feng Wang System.out.println("MS ->ODF finished"); 1467bf9ba33SDongJun Zong runedScenarios = "MS ->ODF finished" + "\r\n"; 1472b9e17edSLi Feng Wang //ODF->MS 1482b9e17edSLi Feng Wang String savedMSFilePath = exportAsODF(saveAsODF); 1492b9e17edSLi Feng Wang File savedMSFile = new File(savedMSFilePath); 1502b9e17edSLi Feng Wang Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists()); 1517bf9ba33SDongJun Zong 1522b9e17edSLi Feng Wang System.out.println("ODF->MS Finished"); 1532b9e17edSLi Feng Wang 1547bf9ba33SDongJun Zong runedScenarios = runedScenarios + "ODF->MS Finished" + "\r\n"; 1552b9e17edSLi Feng Wang //Export ODF->PDF 1562b9e17edSLi Feng Wang exportAsPDF(saveAsODF); 1572b9e17edSLi Feng Wang System.out.println("ODF->PDF Finished"); 1587bf9ba33SDongJun Zong runedScenarios = runedScenarios + "ODF->PDF Finished" + "\r\n"; 1592b9e17edSLi Feng Wang flag = true; 1602b9e17edSLi Feng Wang Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag); 1612b9e17edSLi Feng Wang isSucceed = true; 1622b9e17edSLi Feng Wang } getSuffix(String file)163eba4d44aSLiu Zhe private String getSuffix(String file) { 164eba4d44aSLiu Zhe String lowerCaseName = file.toLowerCase(); 165eba4d44aSLiu Zhe String suffix = lowerCaseName.substring(lowerCaseName.lastIndexOf(".")); 166eba4d44aSLiu Zhe return suffix; 167eba4d44aSLiu Zhe } 168eba4d44aSLiu Zhe /** 169916c15c1SDongJun Zong * return the Export ODF file path 170eba4d44aSLiu Zhe * @throws IOException 171eba4d44aSLiu Zhe * @throws IllegalArgumentException 172eba4d44aSLiu Zhe */ exportAsODF(String testFile)173eba4d44aSLiu Zhe private String exportAsODF(String testFile) throws IOException, IllegalArgumentException { 174eba4d44aSLiu Zhe XComponent document = loadSampleFile(testFile); 175916c15c1SDongJun Zong try { 176916c15c1SDongJun Zong Thread.sleep(2000); 177916c15c1SDongJun Zong } catch (InterruptedException e) { 178916c15c1SDongJun Zong e.printStackTrace(); 179916c15c1SDongJun Zong } 180eba4d44aSLiu Zhe String suffix = getSuffix(testFile); 181eba4d44aSLiu Zhe String filterName = filterMap.get(suffix); 182eba4d44aSLiu Zhe PropertyValue[] lProperties = null; 183eba4d44aSLiu Zhe lProperties = new PropertyValue[3]; 184eba4d44aSLiu Zhe lProperties[0] = new PropertyValue(); 185eba4d44aSLiu Zhe lProperties[0].Name = "FilterName"; 186eba4d44aSLiu Zhe lProperties[0].Value = filterName; 187eba4d44aSLiu Zhe lProperties[1] = new PropertyValue(); 188eba4d44aSLiu Zhe lProperties[1].Name = "Overwrite"; 189eba4d44aSLiu Zhe lProperties[1].Value = Boolean.TRUE; 190eba4d44aSLiu Zhe lProperties[2] = new PropertyValue(); 191eba4d44aSLiu Zhe lProperties[2].Name = "AsyncMode"; 192eba4d44aSLiu Zhe lProperties[2].Value = new Boolean(false); 193eba4d44aSLiu Zhe 194eba4d44aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 195eba4d44aSLiu Zhe File file = new File(testFile); 196eba4d44aSLiu Zhe String fileName = file.getName(); 197eba4d44aSLiu Zhe String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + "." + formatMap.get(suffix);//TODO 198eba4d44aSLiu Zhe store.storeAsURL(Testspace.getUrl(saveAsFilePath), lProperties); 199916c15c1SDongJun Zong try { 200916c15c1SDongJun Zong Thread.sleep(3000); 201916c15c1SDongJun Zong } catch (InterruptedException e) { 202916c15c1SDongJun Zong e.printStackTrace(); 203916c15c1SDongJun Zong } 204eba4d44aSLiu Zhe app.closeDocument(document); 205916c15c1SDongJun Zong try { 206916c15c1SDongJun Zong Thread.sleep(2000); 207916c15c1SDongJun Zong } catch (InterruptedException e) { 208916c15c1SDongJun Zong e.printStackTrace(); 209916c15c1SDongJun Zong } 210eba4d44aSLiu Zhe return saveAsFilePath; 211eba4d44aSLiu Zhe } 212eba4d44aSLiu Zhe exportAsPDF(String testFilePath)213eba4d44aSLiu Zhe private void exportAsPDF(String testFilePath) throws Exception { 214eba4d44aSLiu Zhe XComponent xComponent = loadSampleFile(testFilePath); 215eba4d44aSLiu Zhe XStorable xStorable = (XStorable) UnoRuntime.queryInterface( 216eba4d44aSLiu Zhe XStorable.class, xComponent); 217eba4d44aSLiu Zhe 218eba4d44aSLiu Zhe PropertyValue[] aMediaDescriptor = new PropertyValue[1]; 219eba4d44aSLiu Zhe aMediaDescriptor[0] = new PropertyValue(); 220eba4d44aSLiu Zhe aMediaDescriptor[0].Name = "FilterName"; 221eba4d44aSLiu Zhe aMediaDescriptor[0].Value = "writer_pdf_Export"; 222eba4d44aSLiu Zhe File file = new File(testFilePath); 223eba4d44aSLiu Zhe String fileName = file.getName(); 224eba4d44aSLiu Zhe String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + ".pdf" ; 225eba4d44aSLiu Zhe // export to pdf 226eba4d44aSLiu Zhe xStorable.storeToURL(Testspace.getUrl(saveAsFilePath), aMediaDescriptor); 227916c15c1SDongJun Zong try { 228916c15c1SDongJun Zong Thread.sleep(5000); 229916c15c1SDongJun Zong } catch (InterruptedException e) { 230916c15c1SDongJun Zong e.printStackTrace(); 231916c15c1SDongJun Zong } 232eba4d44aSLiu Zhe // close this document 233eba4d44aSLiu Zhe app.closeDocument(xComponent); 234eba4d44aSLiu Zhe File pdfFile = new File(saveAsFilePath); 235eba4d44aSLiu Zhe Assert.assertTrue("Verify sampe file " + testFilePath + " exprot to pdf!", pdfFile.exists()); 236eba4d44aSLiu Zhe } 237eba4d44aSLiu Zhe initMap()238eba4d44aSLiu Zhe public static void initMap() { 239eba4d44aSLiu Zhe filterMap.put(".doc", "writer8"); 240eba4d44aSLiu Zhe filterMap.put(".docx", "writer8"); 241eba4d44aSLiu Zhe filterMap.put(".odt", "MS Word 97"); 242eba4d44aSLiu Zhe filterMap.put(".ppt", "impress8"); 243eba4d44aSLiu Zhe filterMap.put(".pptx", "impress8"); 244eba4d44aSLiu Zhe filterMap.put(".odp", "MS PowerPoint 97"); 245eba4d44aSLiu Zhe filterMap.put(".xls", "calc8"); 246eba4d44aSLiu Zhe filterMap.put(".xlsx", "calc8"); 247eba4d44aSLiu Zhe filterMap.put(".ods", "MS Excel 97"); 248eba4d44aSLiu Zhe 249eba4d44aSLiu Zhe formatMap.put(".doc", "odt"); 250eba4d44aSLiu Zhe formatMap.put(".docx", "odt"); 251eba4d44aSLiu Zhe formatMap.put(".odt", "doc"); 252eba4d44aSLiu Zhe 253eba4d44aSLiu Zhe formatMap.put(".ppt", "odp"); 254eba4d44aSLiu Zhe formatMap.put(".pptx", "odp"); 255eba4d44aSLiu Zhe formatMap.put(".odp", "ppt"); 256eba4d44aSLiu Zhe 257eba4d44aSLiu Zhe formatMap.put(".xls", "ods"); 258eba4d44aSLiu Zhe formatMap.put(".xlsx", "ods"); 259eba4d44aSLiu Zhe formatMap.put(".ods", "xls"); 260eba4d44aSLiu Zhe } loadSampleFile(String filePath)261eba4d44aSLiu Zhe private XComponent loadSampleFile(String filePath) throws IOException, IllegalArgumentException { 262eba4d44aSLiu Zhe if (!"".equals(filePath)) { 263eba4d44aSLiu Zhe PropertyValue[] loadProps = null; 264eba4d44aSLiu Zhe if (filePath.endsWith("x")) {//ooxml sample file 265eba4d44aSLiu Zhe loadProps = new PropertyValue[4]; 266eba4d44aSLiu Zhe loadProps[0] = new PropertyValue(); 267eba4d44aSLiu Zhe loadProps[0].Name = "Hidden"; 268eba4d44aSLiu Zhe loadProps[0].Value = Boolean.TRUE; 269eba4d44aSLiu Zhe loadProps[1] = new PropertyValue(); 270eba4d44aSLiu Zhe loadProps[1].Name = "FilterName"; 271eba4d44aSLiu Zhe String filePathLowCase = filePath.toLowerCase(); 272eba4d44aSLiu Zhe if(filePathLowCase.endsWith("docx")) { 273eba4d44aSLiu Zhe loadProps[1].Value = "MS Word 2007 XML"; 274eba4d44aSLiu Zhe } 275eba4d44aSLiu Zhe if(filePathLowCase.endsWith("pptx")){ 276eba4d44aSLiu Zhe loadProps[1].Value = "MS PowerPoint 2007 XML"; 277eba4d44aSLiu Zhe } 278eba4d44aSLiu Zhe if(filePathLowCase.endsWith("xlsx")) { 279eba4d44aSLiu Zhe loadProps[1].Value = "MS Excel 2007 XML"; 280eba4d44aSLiu Zhe } 281eba4d44aSLiu Zhe loadProps[2] = new PropertyValue(); 282eba4d44aSLiu Zhe loadProps[2].Name = "ReadOnly"; 283eba4d44aSLiu Zhe loadProps[2].Value = true; 284eba4d44aSLiu Zhe loadProps[3] = new PropertyValue(); 285eba4d44aSLiu Zhe loadProps[3].Name = "MacroExecutionMode"; 286eba4d44aSLiu Zhe loadProps[3].Value = MacroExecMode.NEVER_EXECUTE; 287eba4d44aSLiu Zhe } else { 288eba4d44aSLiu Zhe loadProps = new PropertyValue[3]; 289eba4d44aSLiu Zhe loadProps[0] = new PropertyValue(); 290eba4d44aSLiu Zhe loadProps[0].Name = "Hidden"; 291eba4d44aSLiu Zhe loadProps[0].Value = Boolean.TRUE; 292eba4d44aSLiu Zhe loadProps[1] = new PropertyValue(); 293eba4d44aSLiu Zhe loadProps[1].Name = "ReadOnly"; 294eba4d44aSLiu Zhe loadProps[1].Value = Boolean.TRUE; 295eba4d44aSLiu Zhe loadProps[2] = new PropertyValue(); 296eba4d44aSLiu Zhe loadProps[2].Name = "AsyncMode"; 297eba4d44aSLiu Zhe loadProps[2].Value = new Boolean(false); 298eba4d44aSLiu Zhe } 299eba4d44aSLiu Zhe 300eba4d44aSLiu Zhe String urlPath = Testspace.getUrl(filePath); 301eba4d44aSLiu Zhe XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, app.getDesktop()); 302eba4d44aSLiu Zhe return componentLoader.loadComponentFromURL(urlPath, "_blank", 0, loadProps); 303eba4d44aSLiu Zhe } 304eba4d44aSLiu Zhe return null; 305eba4d44aSLiu Zhe } 306eba4d44aSLiu Zhe /** 307eba4d44aSLiu Zhe * the url is like this format: 308eba4d44aSLiu Zhe * ftp://user:password@192.168.0.1/public/sample/testsample.doc 309eba4d44aSLiu Zhe * @param url 310eba4d44aSLiu Zhe * @return 311eba4d44aSLiu Zhe */ downloadFile(String url)312eba4d44aSLiu Zhe public String downloadFile(String url) { 313eba4d44aSLiu Zhe File urlFile = new File( new File(url.replaceAll("%20", " ")).getName()); 314eba4d44aSLiu Zhe 315eba4d44aSLiu Zhe File tempFolderFile = new File(tempFolder); 316eba4d44aSLiu Zhe if (!tempFolderFile.exists()) { 317eba4d44aSLiu Zhe tempFolderFile.mkdir(); 318eba4d44aSLiu Zhe } 319eba4d44aSLiu Zhe String testFile = testSpaceFile.getAbsolutePath() + File.separator + "temp" + File.separator + urlFile.getName(); 320eba4d44aSLiu Zhe FileUtil.download(url, new File(testFile)); 321eba4d44aSLiu Zhe return testFile; 322eba4d44aSLiu Zhe } 323eba4d44aSLiu Zhe 324eba4d44aSLiu Zhe 325eba4d44aSLiu Zhe 326eba4d44aSLiu Zhe } 327