Index: testuno/source/testcase/uno/ffc/FFCTest.java =================================================================== --- testuno/source/testcase/uno/ffc/FFCTest.java (revision 0) +++ testuno/source/testcase/uno/ffc/FFCTest.java (working copy) @@ -0,0 +1,304 @@ +/************************************************************** + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package testcase.uno.ffc; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import junit.framework.Assert; + +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.junit.runners.Parameterized.Parameters; +import org.openoffice.test.common.FileUtil; +import org.openoffice.test.common.Testspace; +import org.openoffice.test.uno.UnoApp; + +import com.sun.star.beans.PropertyValue; +import com.sun.star.document.MacroExecMode; +import com.sun.star.frame.XComponentLoader; +import com.sun.star.frame.XStorable; +import com.sun.star.io.IOException; +import com.sun.star.lang.IllegalArgumentException; +import com.sun.star.lang.XComponent; +import com.sun.star.uno.UnoRuntime; +/** + * Pls place a suite file in directory "suite" which is same level with test uno. like bewlow + * -suite + * -testuno + * The suite file content is like this format + * ftp://user:password@192.168.0.1/public/sample/testsample.doc + * .. + * .. + * ftp://user:password@192.168.0.1/public/sample/testsample2.doc + *This script is used to test FFC by UNO API + *It cover below scenario: + *MS2003/2010 format->ODF format + *New Saved ODF Format file -> MS 2003 Format + *New Saved ODF Format file -> PDF + * + */ +@RunWith(Parameterized.class) +public class FFCTest { + + private static final UnoApp app = new UnoApp(); + private static Map filterMap = new HashMap(); + private static String suiteDir = "../suite"; + private String fileURL = ""; + private String operateFilePath = ""; + + private static Map formatMap = new HashMap(); + private static File testSpaceFile = Testspace.getFile(); + + private static String tempFolder = testSpaceFile.getAbsolutePath() + File.separator + "temp"; + @Parameters + public static Collection data() throws Exception{ + initMap(); + ArrayList list = new ArrayList(); + List suitePathList = new ArrayList(); + FileReader fileReader = null; + BufferedReader reader = null; + File suites = new File(suiteDir); + for(File file: suites.listFiles()){ + if(FileUtil.getFileExtName(file.getName()).toLowerCase().equals("suite")){ + suitePathList.add(file.getAbsolutePath()); + } + } + + try{ + for (String suitePath : suitePathList) { + fileReader = new FileReader(suitePath); + reader = new BufferedReader(fileReader); + String line = null; + while((line = reader.readLine()) != null){ + if (!"".equals(line)) { + list.add(new String[]{line}); + } + + } + } + + }catch(Exception e){ + throw new Exception("throw exception when read suite file. " + e.getMessage()); + }finally{ + try{ + if(reader != null){ + reader.close(); + reader = null; + } + if(fileReader != null){ + fileReader.close(); + fileReader = null; + } + }catch(Exception io){ + } + } + return list; + } + + public FFCTest(String url) { + this.fileURL = url; + } + + @BeforeClass + public static void cleanTemp() { + FileUtil.deleteFile(tempFolder); + } + + @Before + public void setUp() throws Exception { + operateFilePath = downloadFile(fileURL); + + app.start(); + } + @After + public void tearDown() throws Exception { + operateFilePath = ""; + app.close(); + } + + + @Test + public void exportTest() throws Exception { + //MS Office Format ->ODF + boolean flag = false; + String saveAsODF = exportAsODF(operateFilePath); + //ODF->MS + String savedMSFilePath = exportAsODF(saveAsODF); + File savedMSFile = new File(savedMSFilePath); + Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists()); + flag = true; + Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag); + //Export ODF->PDF + exportAsPDF(saveAsODF); + } + private String getSuffix(String file) { + String lowerCaseName = file.toLowerCase(); + String suffix = lowerCaseName.substring(lowerCaseName.lastIndexOf(".")); + return suffix; + } + /** + * return the Export ODF file path + * @throws IOException + * @throws IllegalArgumentException + */ + private String exportAsODF(String testFile) throws IOException, IllegalArgumentException { + XComponent document = loadSampleFile(testFile); + String suffix = getSuffix(testFile); + String filterName = filterMap.get(suffix); + PropertyValue[] lProperties = null; + lProperties = new PropertyValue[3]; + lProperties[0] = new PropertyValue(); + lProperties[0].Name = "FilterName"; + lProperties[0].Value = filterName; + lProperties[1] = new PropertyValue(); + lProperties[1].Name = "Overwrite"; + lProperties[1].Value = Boolean.TRUE; + lProperties[2] = new PropertyValue(); + lProperties[2].Name = "AsyncMode"; + lProperties[2].Value = new Boolean(false); + + XStorable store = UnoRuntime.queryInterface(XStorable.class, document); + File file = new File(testFile); + String fileName = file.getName(); + String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + "." + formatMap.get(suffix);//TODO + store.storeAsURL(Testspace.getUrl(saveAsFilePath), lProperties); + app.closeDocument(document); + return saveAsFilePath; + } + + private void exportAsPDF(String testFilePath) throws Exception { + XComponent xComponent = loadSampleFile(testFilePath); + XStorable xStorable = (XStorable) UnoRuntime.queryInterface( + XStorable.class, xComponent); + + PropertyValue[] aMediaDescriptor = new PropertyValue[1]; + aMediaDescriptor[0] = new PropertyValue(); + aMediaDescriptor[0].Name = "FilterName"; + aMediaDescriptor[0].Value = "writer_pdf_Export"; + File file = new File(testFilePath); + String fileName = file.getName(); + String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + ".pdf" ; + // export to pdf + xStorable.storeToURL(Testspace.getUrl(saveAsFilePath), aMediaDescriptor); + + // close this document + app.closeDocument(xComponent); + File pdfFile = new File(saveAsFilePath); + Assert.assertTrue("Verify sampe file " + testFilePath + " exprot to pdf!", pdfFile.exists()); + } + + public static void initMap() { + filterMap.put(".doc", "writer8"); + filterMap.put(".docx", "writer8"); + filterMap.put(".odt", "MS Word 97"); + filterMap.put(".ppt", "impress8"); + filterMap.put(".pptx", "impress8"); + filterMap.put(".odp", "MS PowerPoint 97"); + filterMap.put(".xls", "calc8"); + filterMap.put(".xlsx", "calc8"); + filterMap.put(".ods", "MS Excel 97"); + + formatMap.put(".doc", "odt"); + formatMap.put(".docx", "odt"); + formatMap.put(".odt", "doc"); + + formatMap.put(".ppt", "odp"); + formatMap.put(".pptx", "odp"); + formatMap.put(".odp", "ppt"); + + formatMap.put(".xls", "ods"); + formatMap.put(".xlsx", "ods"); + formatMap.put(".ods", "xls"); + } + private XComponent loadSampleFile(String filePath) throws IOException, IllegalArgumentException { + if (!"".equals(filePath)) { + PropertyValue[] loadProps = null; + if (filePath.endsWith("x")) {//ooxml sample file + loadProps = new PropertyValue[4]; + loadProps[0] = new PropertyValue(); + loadProps[0].Name = "Hidden"; + loadProps[0].Value = Boolean.TRUE; + loadProps[1] = new PropertyValue(); + loadProps[1].Name = "FilterName"; + String filePathLowCase = filePath.toLowerCase(); + if(filePathLowCase.endsWith("docx")) { + loadProps[1].Value = "MS Word 2007 XML"; + } + if(filePathLowCase.endsWith("pptx")){ + loadProps[1].Value = "MS PowerPoint 2007 XML"; + } + if(filePathLowCase.endsWith("xlsx")) { + loadProps[1].Value = "MS Excel 2007 XML"; + } + loadProps[2] = new PropertyValue(); + loadProps[2].Name = "ReadOnly"; + loadProps[2].Value = true; + loadProps[3] = new PropertyValue(); + loadProps[3].Name = "MacroExecutionMode"; + loadProps[3].Value = MacroExecMode.NEVER_EXECUTE; + } else { + loadProps = new PropertyValue[3]; + loadProps[0] = new PropertyValue(); + loadProps[0].Name = "Hidden"; + loadProps[0].Value = Boolean.TRUE; + loadProps[1] = new PropertyValue(); + loadProps[1].Name = "ReadOnly"; + loadProps[1].Value = Boolean.TRUE; + loadProps[2] = new PropertyValue(); + loadProps[2].Name = "AsyncMode"; + loadProps[2].Value = new Boolean(false); + } + + String urlPath = Testspace.getUrl(filePath); + XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, app.getDesktop()); + return componentLoader.loadComponentFromURL(urlPath, "_blank", 0, loadProps); + } + return null; + } + /** + * the url is like this format: + * ftp://user:password@192.168.0.1/public/sample/testsample.doc + * @param url + * @return + */ + public String downloadFile(String url) { + File urlFile = new File( new File(url.replaceAll("%20", " ")).getName()); + + File tempFolderFile = new File(tempFolder); + if (!tempFolderFile.exists()) { + tempFolderFile.mkdir(); + } + String testFile = testSpaceFile.getAbsolutePath() + File.separator + "temp" + File.separator + urlFile.getName(); + FileUtil.download(url, new File(testFile)); + return testFile; + } + +}