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