View | Details | Raw Unified | Return to issue 120849
Collapse All | Expand All

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

Return to issue 120849