1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 */ 20 package testcase.uno.ffc; 21 22 import java.io.BufferedReader; 23 import java.io.File; 24 import java.io.FileReader; 25 import java.io.FilenameFilter; 26 import java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.HashMap; 29 import java.util.List; 30 import java.util.Map; 31 32 import junit.framework.Assert; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.BeforeClass; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.junit.runners.Parameterized; 41 import org.junit.runners.Parameterized.Parameters; 42 import org.openoffice.test.OpenOffice; 43 import org.openoffice.test.common.FileProvider; 44 import org.openoffice.test.common.FileProvider.FileRepos; 45 import org.openoffice.test.common.FileUtil; 46 import org.openoffice.test.common.Logger; 47 import org.openoffice.test.common.Testspace; 48 import org.openoffice.test.uno.UnoApp; 49 50 import com.sun.star.beans.PropertyValue; 51 import com.sun.star.document.MacroExecMode; 52 import com.sun.star.frame.XComponentLoader; 53 import com.sun.star.frame.XStorable; 54 import com.sun.star.io.IOException; 55 import com.sun.star.lang.IllegalArgumentException; 56 import com.sun.star.lang.XComponent; 57 import com.sun.star.uno.UnoRuntime; 58 /** 59 * Pls place a suite file in directory "suite" which is same level with test uno. like bewlow 60 * -suite 61 * -testuno 62 * The suite file content is like this format 63 * ftp://user:password@192.168.0.1/public/sample/testsample.doc 64 * .. 65 * .. 66 * ftp://user:password@192.168.0.1/public/sample/testsample2.doc 67 *This script is used to test FFC by UNO API 68 *It cover below scenario: 69 *MS2003/2010 format->ODF format 70 *New Saved ODF Format file -> MS 2003 Format 71 *New Saved ODF Format file -> PDF 72 * 73 */ 74 @RunWith(FileProvider.class) 75 public class FFCTest { 76 @Rule 77 public Logger log = Logger.getLogger(this, false); 78 79 private static final UnoApp app = new UnoApp(); 80 private static Map<String, String> filterMap = new HashMap<String, String>(); 81 @FileRepos 82 public static String suiteDir = "../suite/"; 83 private String fileURL = ""; 84 private String operateFilePath = ""; 85 private static boolean isSuiteFileExist = false; 86 private static Map<String, String> formatMap = new HashMap<String, String>(); 87 private static File testSpaceFile = Testspace.getFile(); 88 private boolean isSucceed = false; 89 private static String tempFolder = testSpaceFile.getAbsolutePath() + File.separator + "temp"; 90 // @Parameters 91 // public static Collection<String[]> data() throws Exception{ 92 // initMap(); 93 // ArrayList<String[]> list = new ArrayList<String[]>(); 94 // List<String> suitePathList = new ArrayList<String>(); 95 // FileReader fileReader = null; 96 // BufferedReader reader = null; 97 // File suites = new File(suiteDir); 98 // if (suites.exists() && suites.list().length > 0) { 99 // isSuiteFileExist = true; 100 // for(File file: suites.listFiles()){ 101 // if(FileUtil.getFileExtName(file.getName()).toLowerCase().equals("suite")){ 102 // suitePathList.add(file.getAbsolutePath()); 103 // } 104 // } 105 // try{ 106 // for (String suitePath : suitePathList) { 107 // fileReader = new FileReader(suitePath); 108 // reader = new BufferedReader(fileReader); 109 // String line = null; 110 // while((line = reader.readLine()) != null){ 111 // if (!"".equals(line)) { 112 // list.add(new String[]{line}); 113 // } 114 // } 115 // if(reader != null){ 116 // reader.close(); 117 // reader = null; 118 // } 119 // if(fileReader != null){ 120 // fileReader.close(); 121 // fileReader = null; 122 // } 123 // } 124 // 125 // }catch(Exception e){ 126 // throw new Exception("throw exception when read suite file. " + e.getMessage()); 127 // }finally{ 128 // try{ 129 // if(reader != null){ 130 // reader.close(); 131 // reader = null; 132 // } 133 // if(fileReader != null){ 134 // fileReader.close(); 135 // fileReader = null; 136 // } 137 // }catch(Exception io){ 138 // } 139 // } 140 // } else {// run files from ffc data directory 141 // File ffcDataHome = new File("data\\ffc"); 142 // getFileList(ffcDataHome, list); 143 // } 144 // 145 // return list; 146 // } 147 // 148 public static void getFileList(File dir, List<String[]> list) { 149 File[] files = dir.listFiles(new FilenameFilter() { 150 @Override 151 public boolean accept(File dir, String name) { 152 File file = new File(dir.getAbsolutePath() + File.separator + name); 153 String filename = new File(name).getName().toLowerCase(); 154 boolean accept; 155 if (file.isDirectory()) { 156 accept = true; 157 } else { 158 accept = filename.endsWith(".docx") 159 || filename.endsWith(".pptx") 160 || filename.endsWith(".xlsx") 161 || filename.endsWith(".ppt") 162 || filename.endsWith(".xls") 163 || filename.endsWith(".doc") ; 164 } 165 166 return accept; 167 } 168 169 }); 170 if (files == null) 171 return; 172 173 for (File file : files) { 174 if (file.isDirectory()) { 175 getFileList(file, list); 176 } else { 177 list.add(new String[] {file.getAbsolutePath().replace(dir.getParentFile().getAbsolutePath(), "").replace("\\", "/")}); 178 179 } 180 } 181 } 182 183 public FFCTest(String url) { 184 this.fileURL = url; 185 } 186 187 @BeforeClass 188 public static void init() { 189 initMap(); 190 //Disable automation 191 OpenOffice.getDefault().setAutomationPort(-1); 192 OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu"); 193 } 194 195 @Before 196 public void setUp() throws Exception { 197 // if (isSuiteFileExist) { 198 // operateFilePath = downloadFile(fileURL); 199 // } else { 200 // operateFilePath = Testspace.prepareData(fileURL); 201 // } 202 // 203 operateFilePath = Testspace.prepareData(fileURL); 204 app.start(); 205 } 206 @After 207 public void tearDown() throws Exception { 208 if (!isSucceed) { 209 FileUtil.copyFile(operateFilePath, Testspace.getFile("output").getAbsolutePath()); 210 app.close(); 211 } 212 } 213 214 215 @Test(timeout=1000*60*5) 216 public void exportTest() throws Exception { 217 //MS Office Format ->ODF 218 boolean flag = false; 219 220 String saveAsODF = exportAsODF(operateFilePath); 221 System.out.println("MS ->ODF finished"); 222 //ODF->MS 223 String savedMSFilePath = exportAsODF(saveAsODF); 224 System.out.println("ODF->MS Finished"); 225 File savedMSFile = new File(savedMSFilePath); 226 Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists()); 227 System.out.println("ODF->MS Finished"); 228 229 230 //Export ODF->PDF 231 exportAsPDF(saveAsODF); 232 System.out.println("ODF->PDF Finished"); 233 flag = true; 234 Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag); 235 isSucceed = true; 236 } 237 private String getSuffix(String file) { 238 String lowerCaseName = file.toLowerCase(); 239 String suffix = lowerCaseName.substring(lowerCaseName.lastIndexOf(".")); 240 return suffix; 241 } 242 /** 243 * return the Export ODF file path 244 * @throws IOException 245 * @throws IllegalArgumentException 246 */ 247 private String exportAsODF(String testFile) throws IOException, IllegalArgumentException { 248 XComponent document = loadSampleFile(testFile); 249 String suffix = getSuffix(testFile); 250 String filterName = filterMap.get(suffix); 251 PropertyValue[] lProperties = null; 252 lProperties = new PropertyValue[3]; 253 lProperties[0] = new PropertyValue(); 254 lProperties[0].Name = "FilterName"; 255 lProperties[0].Value = filterName; 256 lProperties[1] = new PropertyValue(); 257 lProperties[1].Name = "Overwrite"; 258 lProperties[1].Value = Boolean.TRUE; 259 lProperties[2] = new PropertyValue(); 260 lProperties[2].Name = "AsyncMode"; 261 lProperties[2].Value = new Boolean(false); 262 263 XStorable store = UnoRuntime.queryInterface(XStorable.class, document); 264 File file = new File(testFile); 265 String fileName = file.getName(); 266 String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + "." + formatMap.get(suffix);//TODO 267 store.storeAsURL(Testspace.getUrl(saveAsFilePath), lProperties); 268 app.closeDocument(document); 269 return saveAsFilePath; 270 } 271 272 private void exportAsPDF(String testFilePath) throws Exception { 273 XComponent xComponent = loadSampleFile(testFilePath); 274 XStorable xStorable = (XStorable) UnoRuntime.queryInterface( 275 XStorable.class, xComponent); 276 277 PropertyValue[] aMediaDescriptor = new PropertyValue[1]; 278 aMediaDescriptor[0] = new PropertyValue(); 279 aMediaDescriptor[0].Name = "FilterName"; 280 aMediaDescriptor[0].Value = "writer_pdf_Export"; 281 File file = new File(testFilePath); 282 String fileName = file.getName(); 283 String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + ".pdf" ; 284 // export to pdf 285 xStorable.storeToURL(Testspace.getUrl(saveAsFilePath), aMediaDescriptor); 286 287 // close this document 288 app.closeDocument(xComponent); 289 File pdfFile = new File(saveAsFilePath); 290 Assert.assertTrue("Verify sampe file " + testFilePath + " exprot to pdf!", pdfFile.exists()); 291 } 292 293 public static void initMap() { 294 filterMap.put(".doc", "writer8"); 295 filterMap.put(".docx", "writer8"); 296 filterMap.put(".odt", "MS Word 97"); 297 filterMap.put(".ppt", "impress8"); 298 filterMap.put(".pptx", "impress8"); 299 filterMap.put(".odp", "MS PowerPoint 97"); 300 filterMap.put(".xls", "calc8"); 301 filterMap.put(".xlsx", "calc8"); 302 filterMap.put(".ods", "MS Excel 97"); 303 304 formatMap.put(".doc", "odt"); 305 formatMap.put(".docx", "odt"); 306 formatMap.put(".odt", "doc"); 307 308 formatMap.put(".ppt", "odp"); 309 formatMap.put(".pptx", "odp"); 310 formatMap.put(".odp", "ppt"); 311 312 formatMap.put(".xls", "ods"); 313 formatMap.put(".xlsx", "ods"); 314 formatMap.put(".ods", "xls"); 315 } 316 private XComponent loadSampleFile(String filePath) throws IOException, IllegalArgumentException { 317 if (!"".equals(filePath)) { 318 PropertyValue[] loadProps = null; 319 if (filePath.endsWith("x")) {//ooxml sample file 320 loadProps = new PropertyValue[4]; 321 loadProps[0] = new PropertyValue(); 322 loadProps[0].Name = "Hidden"; 323 loadProps[0].Value = Boolean.TRUE; 324 loadProps[1] = new PropertyValue(); 325 loadProps[1].Name = "FilterName"; 326 String filePathLowCase = filePath.toLowerCase(); 327 if(filePathLowCase.endsWith("docx")) { 328 loadProps[1].Value = "MS Word 2007 XML"; 329 } 330 if(filePathLowCase.endsWith("pptx")){ 331 loadProps[1].Value = "MS PowerPoint 2007 XML"; 332 } 333 if(filePathLowCase.endsWith("xlsx")) { 334 loadProps[1].Value = "MS Excel 2007 XML"; 335 } 336 loadProps[2] = new PropertyValue(); 337 loadProps[2].Name = "ReadOnly"; 338 loadProps[2].Value = true; 339 loadProps[3] = new PropertyValue(); 340 loadProps[3].Name = "MacroExecutionMode"; 341 loadProps[3].Value = MacroExecMode.NEVER_EXECUTE; 342 } else { 343 loadProps = new PropertyValue[3]; 344 loadProps[0] = new PropertyValue(); 345 loadProps[0].Name = "Hidden"; 346 loadProps[0].Value = Boolean.TRUE; 347 loadProps[1] = new PropertyValue(); 348 loadProps[1].Name = "ReadOnly"; 349 loadProps[1].Value = Boolean.TRUE; 350 loadProps[2] = new PropertyValue(); 351 loadProps[2].Name = "AsyncMode"; 352 loadProps[2].Value = new Boolean(false); 353 } 354 355 String urlPath = Testspace.getUrl(filePath); 356 XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, app.getDesktop()); 357 return componentLoader.loadComponentFromURL(urlPath, "_blank", 0, loadProps); 358 } 359 return null; 360 } 361 /** 362 * the url is like this format: 363 * ftp://user:password@192.168.0.1/public/sample/testsample.doc 364 * @param url 365 * @return 366 */ 367 public String downloadFile(String url) { 368 File urlFile = new File( new File(url.replaceAll("%20", " ")).getName()); 369 370 File tempFolderFile = new File(tempFolder); 371 if (!tempFolderFile.exists()) { 372 tempFolderFile.mkdir(); 373 } 374 String testFile = testSpaceFile.getAbsolutePath() + File.separator + "temp" + File.separator + urlFile.getName(); 375 FileUtil.download(url, new File(testFile)); 376 return testFile; 377 } 378 } 379