xref: /trunk/test/testuno/source/fvt/uno/ffc/FFCTest.java (revision 7bf9ba33)
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 
91 	public FFCTest(String url) {
92 		this.fileURL = url;
93 	}
94 
95 	@BeforeClass
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
114 	public void setUp() throws Exception {
115 		operateFilePath =  Testspace.prepareData(fileURL);
116 
117 		runedScenarios = "";
118 		app.start();
119 	}
120 	@After
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 	public void exportTest() throws Exception {
140 		//MS Office Format ->ODF
141 		boolean flag = false;
142 
143 		String saveAsODF = exportAsODF(operateFilePath);
144 		System.out.println("MS ->ODF finished");
145 		runedScenarios = "MS ->ODF finished" + "\r\n";
146 		//ODF->MS
147 		String savedMSFilePath = exportAsODF(saveAsODF);
148 		File savedMSFile = new File(savedMSFilePath);
149 		Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists());
150 
151 		System.out.println("ODF->MS Finished");
152 
153 		runedScenarios = runedScenarios + "ODF->MS Finished" + "\r\n";
154 		//Export ODF->PDF
155 		exportAsPDF(saveAsODF);
156 		System.out.println("ODF->PDF Finished");
157 		runedScenarios = runedScenarios + "ODF->PDF Finished" + "\r\n";
158 		flag = true;
159 		Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag);
160 		isSucceed = true;
161 	}
162 	private String getSuffix(String file) {
163 		String lowerCaseName = file.toLowerCase();
164 		String suffix = lowerCaseName.substring(lowerCaseName.lastIndexOf("."));
165 		return suffix;
166 	}
167 	/**
168 	 * return the Export ODF file path
169 	 * @throws IOException
170 	 * @throws IllegalArgumentException
171 	 */
172 	private String exportAsODF(String testFile) throws IOException, IllegalArgumentException {
173 		XComponent document = loadSampleFile(testFile);
174 		try {
175 			Thread.sleep(2000);
176 		} catch (InterruptedException e) {
177 			e.printStackTrace();
178 		}
179 		String suffix = getSuffix(testFile);
180 		String filterName = filterMap.get(suffix);
181 		PropertyValue[] lProperties = null;
182 		lProperties = new PropertyValue[3];
183 		lProperties[0] = new PropertyValue();
184 		lProperties[0].Name = "FilterName";
185 		lProperties[0].Value = filterName;
186 		lProperties[1] = new PropertyValue();
187 		lProperties[1].Name = "Overwrite";
188 		lProperties[1].Value = Boolean.TRUE;
189 		lProperties[2] = new PropertyValue();
190 		lProperties[2].Name = "AsyncMode";
191 		lProperties[2].Value = new Boolean(false);
192 
193 		XStorable store = (XStorable) UnoRuntime.queryInterface(XStorable.class, document);
194 		File file = new File(testFile);
195 		String fileName = file.getName();
196 		String saveAsFilePath = file.getParentFile().getAbsolutePath() + File.separator + fileName + "." + formatMap.get(suffix);//TODO
197 		store.storeAsURL(Testspace.getUrl(saveAsFilePath), lProperties);
198 		try {
199 			Thread.sleep(3000);
200 		} catch (InterruptedException e) {
201 			e.printStackTrace();
202 		}
203 		app.closeDocument(document);
204 		try {
205 			Thread.sleep(2000);
206 		} catch (InterruptedException e) {
207 			e.printStackTrace();
208 		}
209 		return saveAsFilePath;
210 	}
211 
212 	private void exportAsPDF(String testFilePath) throws Exception {
213 		XComponent xComponent = loadSampleFile(testFilePath);
214 		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
215 				XStorable.class, xComponent);
216 
217 		PropertyValue[] aMediaDescriptor = new PropertyValue[1];
218 		aMediaDescriptor[0] = new PropertyValue();
219 		aMediaDescriptor[0].Name = "FilterName";
220 		aMediaDescriptor[0].Value = "writer_pdf_Export";
221 		File file = new File(testFilePath);
222 		String fileName = file.getName();
223 		String saveAsFilePath =  file.getParentFile().getAbsolutePath() + File.separator + fileName + ".pdf" ;
224 		// export to pdf
225 		xStorable.storeToURL(Testspace.getUrl(saveAsFilePath), aMediaDescriptor);
226 		try {
227 			Thread.sleep(5000);
228 		} catch (InterruptedException e) {
229 			e.printStackTrace();
230 		}
231 		// close this document
232 		app.closeDocument(xComponent);
233 		File pdfFile = new File(saveAsFilePath);
234 		Assert.assertTrue("Verify sampe file " + testFilePath + " exprot to pdf!", pdfFile.exists());
235 	}
236 
237 	public static void initMap() {
238 		filterMap.put(".doc", "writer8");
239 		filterMap.put(".docx", "writer8");
240 		filterMap.put(".odt", "MS Word 97");
241 		filterMap.put(".ppt", "impress8");
242 		filterMap.put(".pptx", "impress8");
243 		filterMap.put(".odp", "MS PowerPoint 97");
244 		filterMap.put(".xls", "calc8");
245 		filterMap.put(".xlsx", "calc8");
246 		filterMap.put(".ods", "MS Excel 97");
247 
248 		formatMap.put(".doc", "odt");
249 		formatMap.put(".docx", "odt");
250 		formatMap.put(".odt", "doc");
251 
252 		formatMap.put(".ppt", "odp");
253 		formatMap.put(".pptx", "odp");
254 		formatMap.put(".odp", "ppt");
255 
256 		formatMap.put(".xls", "ods");
257 		formatMap.put(".xlsx", "ods");
258 		formatMap.put(".ods", "xls");
259 	}
260 	private XComponent loadSampleFile(String filePath) throws IOException, IllegalArgumentException {
261 		if (!"".equals(filePath)) {
262 			PropertyValue[] loadProps = null;
263 			if (filePath.endsWith("x")) {//ooxml sample file
264 				loadProps = new PropertyValue[4];
265 				loadProps[0] = new PropertyValue();
266 				loadProps[0].Name = "Hidden";
267 				loadProps[0].Value = Boolean.TRUE;
268 				loadProps[1] = new PropertyValue();
269 				loadProps[1].Name = "FilterName";
270 				String filePathLowCase = filePath.toLowerCase();
271 				if(filePathLowCase.endsWith("docx")) {
272 					loadProps[1].Value = "MS Word 2007 XML";
273 				}
274 				if(filePathLowCase.endsWith("pptx")){
275 					loadProps[1].Value = "MS PowerPoint 2007 XML";
276 				}
277 				if(filePathLowCase.endsWith("xlsx")) {
278 					loadProps[1].Value = "MS Excel 2007 XML";
279 				}
280 				loadProps[2] = new PropertyValue();
281 				loadProps[2].Name = "ReadOnly";
282 				loadProps[2].Value = true;
283 				loadProps[3] = new PropertyValue();
284 				loadProps[3].Name = "MacroExecutionMode";
285 				loadProps[3].Value = MacroExecMode.NEVER_EXECUTE;
286 			} else {
287 				loadProps = new PropertyValue[3];
288 				loadProps[0] = new PropertyValue();
289 				loadProps[0].Name = "Hidden";
290 				loadProps[0].Value = Boolean.TRUE;
291 				loadProps[1] = new PropertyValue();
292 				loadProps[1].Name = "ReadOnly";
293 				loadProps[1].Value = Boolean.TRUE;
294 				loadProps[2] = new PropertyValue();
295 				loadProps[2].Name = "AsyncMode";
296 				loadProps[2].Value = new Boolean(false);
297 			}
298 
299 			String urlPath = Testspace.getUrl(filePath);
300 			XComponentLoader componentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, app.getDesktop());
301 			return componentLoader.loadComponentFromURL(urlPath, "_blank", 0, loadProps);
302 		}
303 		return null;
304 	}
305 	/**
306 	 * the url is like this format:
307 	 * ftp://user:password@192.168.0.1/public/sample/testsample.doc
308 	 * @param url
309 	 * @return
310 	 */
311 	public String downloadFile(String url) {
312 		File urlFile = new File( new File(url.replaceAll("%20", " ")).getName());
313 
314 		File tempFolderFile = new File(tempFolder);
315 		if (!tempFolderFile.exists()) {
316 			tempFolderFile.mkdir();
317 		}
318 		String testFile = testSpaceFile.getAbsolutePath() + File.separator + "temp" + File.separator + urlFile.getName();
319 		FileUtil.download(url, new File(testFile));
320 		return testFile;
321 	}
322 
323 
324 
325 }
326