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; 27*7bf9ba33SDongJun 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/"; 89*7bf9ba33SDongJun Zong private String runedScenarios = ""; 90eba4d44aSLiu Zhe 91eba4d44aSLiu Zhe public FFCTest(String url) { 92eba4d44aSLiu Zhe this.fileURL = url; 93eba4d44aSLiu Zhe } 94eba4d44aSLiu Zhe 95eba4d44aSLiu Zhe @BeforeClass 96eba4d44aSLiu Zhe public static void init() { 97eba4d44aSLiu Zhe initMap(); 98*7bf9ba33SDongJun Zong File failedDirec = Testspace.getFile(failedFilesDir); 99*7bf9ba33SDongJun 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 108*7bf9ba33SDongJun Zong 109eba4d44aSLiu Zhe failedDirec.mkdirs(); 110*7bf9ba33SDongJun Zong 111eba4d44aSLiu Zhe } 112eba4d44aSLiu Zhe 113eba4d44aSLiu Zhe @Before 114eba4d44aSLiu Zhe public void setUp() throws Exception { 115eba4d44aSLiu Zhe operateFilePath = Testspace.prepareData(fileURL); 116eba4d44aSLiu Zhe 117*7bf9ba33SDongJun Zong runedScenarios = ""; 118eba4d44aSLiu Zhe app.start(); 119eba4d44aSLiu Zhe } 120eba4d44aSLiu Zhe @After 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"); 125*7bf9ba33SDongJun Zong String failedMessage = fileURL +"\r\n" + runedScenarios; 126*7bf9ba33SDongJun 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) 1392b9e17edSLi Feng Wang public void exportTest() throws Exception { 1402b9e17edSLi Feng Wang //MS Office Format ->ODF 1412b9e17edSLi Feng Wang boolean flag = false; 1422b9e17edSLi Feng Wang 1432b9e17edSLi Feng Wang String saveAsODF = exportAsODF(operateFilePath); 1442b9e17edSLi Feng Wang System.out.println("MS ->ODF finished"); 145*7bf9ba33SDongJun Zong runedScenarios = "MS ->ODF finished" + "\r\n"; 1462b9e17edSLi Feng Wang //ODF->MS 1472b9e17edSLi Feng Wang String savedMSFilePath = exportAsODF(saveAsODF); 1482b9e17edSLi Feng Wang File savedMSFile = new File(savedMSFilePath); 1492b9e17edSLi Feng Wang Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists()); 150*7bf9ba33SDongJun Zong 1512b9e17edSLi Feng Wang System.out.println("ODF->MS Finished"); 1522b9e17edSLi Feng Wang 153*7bf9ba33SDongJun Zong runedScenarios = runedScenarios + "ODF->MS Finished" + "\r\n"; 1542b9e17edSLi Feng Wang //Export ODF->PDF 1552b9e17edSLi Feng Wang exportAsPDF(saveAsODF); 1562b9e17edSLi Feng Wang System.out.println("ODF->PDF Finished"); 157*7bf9ba33SDongJun Zong runedScenarios = runedScenarios + "ODF->PDF Finished" + "\r\n"; 1582b9e17edSLi Feng Wang flag = true; 1592b9e17edSLi Feng Wang Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag); 1602b9e17edSLi Feng Wang isSucceed = true; 1612b9e17edSLi Feng Wang } 162eba4d44aSLiu Zhe private String getSuffix(String file) { 163eba4d44aSLiu Zhe String lowerCaseName = file.toLowerCase(); 164eba4d44aSLiu Zhe String suffix = lowerCaseName.substring(lowerCaseName.lastIndexOf(".")); 165eba4d44aSLiu Zhe return suffix; 166eba4d44aSLiu Zhe } 167eba4d44aSLiu Zhe /** 168916c15c1SDongJun Zong * return the Export ODF file path 169eba4d44aSLiu Zhe * @throws IOException 170eba4d44aSLiu Zhe * @throws IllegalArgumentException 171eba4d44aSLiu Zhe */ 172eba4d44aSLiu Zhe private String exportAsODF(String testFile) throws IOException, IllegalArgumentException { 173eba4d44aSLiu Zhe XComponent document = loadSampleFile(testFile); 174916c15c1SDongJun Zong try { 175916c15c1SDongJun Zong Thread.sleep(2000); 176916c15c1SDongJun Zong } catch (InterruptedException e) { 177916c15c1SDongJun Zong e.printStackTrace(); 178916c15c1SDongJun Zong } 179eba4d44aSLiu Zhe String suffix = getSuffix(testFile); 180eba4d44aSLiu Zhe String filterName = filterMap.get(suffix); 181eba4d44aSLiu Zhe PropertyValue[] lProperties = null; 182eba4d44aSLiu Zhe lProperties = new PropertyValue[3]; 183eba4d44aSLiu Zhe lProperties[0] = new PropertyValue(); 184eba4d44aSLiu Zhe lProperties[0].Name = "FilterName"; 185eba4d44aSLiu Zhe lProperties[0].Value = filterName; 186eba4d44aSLiu Zhe lProperties[1] = new PropertyValue(); 187eba4d44aSLiu Zhe lProperties[1].Name = "Overwrite"; 188eba4d44aSLiu Zhe lProperties[1].Value = Boolean.TRUE; 189eba4d44aSLiu Zhe lProperties[2] = new PropertyValue(); 190eba4d44aSLiu Zhe lProperties[2].Name = "AsyncMode"; 191eba4d44aSLiu Zhe lProperties[2].Value = new Boolean(false); 192eba4d44aSLiu Zhe 193eba4d44aSLiu Zhe XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document); 194eba4d44aSLiu Zhe File file = new File(testFile); 195eba4d44aSLiu Zhe String fileName = file.getName(); 196eba4d44aSLiu Zhe String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + "." + formatMap.get(suffix);//TODO 197eba4d44aSLiu Zhe store.storeAsURL(Testspace.getUrl(saveAsFilePath), lProperties); 198916c15c1SDongJun Zong try { 199916c15c1SDongJun Zong Thread.sleep(3000); 200916c15c1SDongJun Zong } catch (InterruptedException e) { 201916c15c1SDongJun Zong e.printStackTrace(); 202916c15c1SDongJun Zong } 203eba4d44aSLiu Zhe app.closeDocument(document); 204916c15c1SDongJun Zong try { 205916c15c1SDongJun Zong Thread.sleep(2000); 206916c15c1SDongJun Zong } catch (InterruptedException e) { 207916c15c1SDongJun Zong e.printStackTrace(); 208916c15c1SDongJun Zong } 209eba4d44aSLiu Zhe return saveAsFilePath; 210eba4d44aSLiu Zhe } 211eba4d44aSLiu Zhe 212eba4d44aSLiu Zhe private void exportAsPDF(String testFilePath) throws Exception { 213eba4d44aSLiu Zhe XComponent xComponent = loadSampleFile(testFilePath); 214eba4d44aSLiu Zhe XStorable xStorable = (XStorable) UnoRuntime.queryInterface( 215eba4d44aSLiu Zhe XStorable.class, xComponent); 216eba4d44aSLiu Zhe 217eba4d44aSLiu Zhe PropertyValue[] aMediaDescriptor = new PropertyValue[1]; 218eba4d44aSLiu Zhe aMediaDescriptor[0] = new PropertyValue(); 219eba4d44aSLiu Zhe aMediaDescriptor[0].Name = "FilterName"; 220eba4d44aSLiu Zhe aMediaDescriptor[0].Value = "writer_pdf_Export"; 221eba4d44aSLiu Zhe File file = new File(testFilePath); 222eba4d44aSLiu Zhe String fileName = file.getName(); 223eba4d44aSLiu Zhe String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + ".pdf" ; 224eba4d44aSLiu Zhe // export to pdf 225eba4d44aSLiu Zhe xStorable.storeToURL(Testspace.getUrl(saveAsFilePath), aMediaDescriptor); 226916c15c1SDongJun Zong try { 227916c15c1SDongJun Zong Thread.sleep(5000); 228916c15c1SDongJun Zong } catch (InterruptedException e) { 229916c15c1SDongJun Zong e.printStackTrace(); 230916c15c1SDongJun Zong } 231eba4d44aSLiu Zhe // close this document 232eba4d44aSLiu Zhe app.closeDocument(xComponent); 233eba4d44aSLiu Zhe File pdfFile = new File(saveAsFilePath); 234eba4d44aSLiu Zhe Assert.assertTrue("Verify sampe file " + testFilePath + " exprot to pdf!", pdfFile.exists()); 235eba4d44aSLiu Zhe } 236eba4d44aSLiu Zhe 237eba4d44aSLiu Zhe public static void initMap() { 238eba4d44aSLiu Zhe filterMap.put(".doc", "writer8"); 239eba4d44aSLiu Zhe filterMap.put(".docx", "writer8"); 240eba4d44aSLiu Zhe filterMap.put(".odt", "MS Word 97"); 241eba4d44aSLiu Zhe filterMap.put(".ppt", "impress8"); 242eba4d44aSLiu Zhe filterMap.put(".pptx", "impress8"); 243eba4d44aSLiu Zhe filterMap.put(".odp", "MS PowerPoint 97"); 244eba4d44aSLiu Zhe filterMap.put(".xls", "calc8"); 245eba4d44aSLiu Zhe filterMap.put(".xlsx", "calc8"); 246eba4d44aSLiu Zhe filterMap.put(".ods", "MS Excel 97"); 247eba4d44aSLiu Zhe 248eba4d44aSLiu Zhe formatMap.put(".doc", "odt"); 249eba4d44aSLiu Zhe formatMap.put(".docx", "odt"); 250eba4d44aSLiu Zhe formatMap.put(".odt", "doc"); 251eba4d44aSLiu Zhe 252eba4d44aSLiu Zhe formatMap.put(".ppt", "odp"); 253eba4d44aSLiu Zhe formatMap.put(".pptx", "odp"); 254eba4d44aSLiu Zhe formatMap.put(".odp", "ppt"); 255eba4d44aSLiu Zhe 256eba4d44aSLiu Zhe formatMap.put(".xls", "ods"); 257eba4d44aSLiu Zhe formatMap.put(".xlsx", "ods"); 258eba4d44aSLiu Zhe formatMap.put(".ods", "xls"); 259eba4d44aSLiu Zhe } 260eba4d44aSLiu Zhe private XComponent loadSampleFile(String filePath) throws IOException, IllegalArgumentException { 261eba4d44aSLiu Zhe if (!"".equals(filePath)) { 262eba4d44aSLiu Zhe PropertyValue[] loadProps = null; 263eba4d44aSLiu Zhe if (filePath.endsWith("x")) {//ooxml sample file 264eba4d44aSLiu Zhe loadProps = new PropertyValue[4]; 265eba4d44aSLiu Zhe loadProps[0] = new PropertyValue(); 266eba4d44aSLiu Zhe loadProps[0].Name = "Hidden"; 267eba4d44aSLiu Zhe loadProps[0].Value = Boolean.TRUE; 268eba4d44aSLiu Zhe loadProps[1] = new PropertyValue(); 269eba4d44aSLiu Zhe loadProps[1].Name = "FilterName"; 270eba4d44aSLiu Zhe String filePathLowCase = filePath.toLowerCase(); 271eba4d44aSLiu Zhe if(filePathLowCase.endsWith("docx")) { 272eba4d44aSLiu Zhe loadProps[1].Value = "MS Word 2007 XML"; 273eba4d44aSLiu Zhe } 274eba4d44aSLiu Zhe if(filePathLowCase.endsWith("pptx")){ 275eba4d44aSLiu Zhe loadProps[1].Value = "MS PowerPoint 2007 XML"; 276eba4d44aSLiu Zhe } 277eba4d44aSLiu Zhe if(filePathLowCase.endsWith("xlsx")) { 278eba4d44aSLiu Zhe loadProps[1].Value = "MS Excel 2007 XML"; 279eba4d44aSLiu Zhe } 280eba4d44aSLiu Zhe loadProps[2] = new PropertyValue(); 281eba4d44aSLiu Zhe loadProps[2].Name = "ReadOnly"; 282eba4d44aSLiu Zhe loadProps[2].Value = true; 283eba4d44aSLiu Zhe loadProps[3] = new PropertyValue(); 284eba4d44aSLiu Zhe loadProps[3].Name = "MacroExecutionMode"; 285eba4d44aSLiu Zhe loadProps[3].Value = MacroExecMode.NEVER_EXECUTE; 286eba4d44aSLiu Zhe } else { 287eba4d44aSLiu Zhe loadProps = new PropertyValue[3]; 288eba4d44aSLiu Zhe loadProps[0] = new PropertyValue(); 289eba4d44aSLiu Zhe loadProps[0].Name = "Hidden"; 290eba4d44aSLiu Zhe loadProps[0].Value = Boolean.TRUE; 291eba4d44aSLiu Zhe loadProps[1] = new PropertyValue(); 292eba4d44aSLiu Zhe loadProps[1].Name = "ReadOnly"; 293eba4d44aSLiu Zhe loadProps[1].Value = Boolean.TRUE; 294eba4d44aSLiu Zhe loadProps[2] = new PropertyValue(); 295eba4d44aSLiu Zhe loadProps[2].Name = "AsyncMode"; 296eba4d44aSLiu Zhe loadProps[2].Value = new Boolean(false); 297eba4d44aSLiu Zhe } 298eba4d44aSLiu Zhe 299eba4d44aSLiu Zhe String urlPath = Testspace.getUrl(filePath); 300eba4d44aSLiu Zhe XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, app.getDesktop()); 301eba4d44aSLiu Zhe return componentLoader.loadComponentFromURL(urlPath, "_blank", 0, loadProps); 302eba4d44aSLiu Zhe } 303eba4d44aSLiu Zhe return null; 304eba4d44aSLiu Zhe } 305eba4d44aSLiu Zhe /** 306eba4d44aSLiu Zhe * the url is like this format: 307eba4d44aSLiu Zhe * ftp://user:password@192.168.0.1/public/sample/testsample.doc 308eba4d44aSLiu Zhe * @param url 309eba4d44aSLiu Zhe * @return 310eba4d44aSLiu Zhe */ 311eba4d44aSLiu Zhe public String downloadFile(String url) { 312eba4d44aSLiu Zhe File urlFile = new File( new File(url.replaceAll("%20", " ")).getName()); 313eba4d44aSLiu Zhe 314eba4d44aSLiu Zhe File tempFolderFile = new File(tempFolder); 315eba4d44aSLiu Zhe if (!tempFolderFile.exists()) { 316eba4d44aSLiu Zhe tempFolderFile.mkdir(); 317eba4d44aSLiu Zhe } 318eba4d44aSLiu Zhe String testFile = testSpaceFile.getAbsolutePath() + File.separator + "temp" + File.separator + urlFile.getName(); 319eba4d44aSLiu Zhe FileUtil.download(url, new File(testFile)); 320eba4d44aSLiu Zhe return testFile; 321eba4d44aSLiu Zhe } 322eba4d44aSLiu Zhe 323eba4d44aSLiu Zhe 324eba4d44aSLiu Zhe 325eba4d44aSLiu Zhe } 326