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