incubator-ooo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From liu...@apache.org
Subject svn commit: r1383810 - in /incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc: ./ FFCTest.java
Date Wed, 12 Sep 2012 07:13:09 GMT
Author: liuzhe
Date: Wed Sep 12 07:13:09 2012
New Revision: 1383810

URL: http://svn.apache.org/viewvc?rev=1383810&view=rev
Log:
#120849 - [testuno]FFC test by UNO AP
Patch By: Zong Dong Jun <zongdj001@gmail.com>
Review By: Liu Zhe <aliuzhe@gmail.com>

Added:
    incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/
    incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/FFCTest.java

Added: incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/FFCTest.java
URL: http://svn.apache.org/viewvc/incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/FFCTest.java?rev=1383810&view=auto
==============================================================================
--- incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/FFCTest.java (added)
+++ incubator/ooo/trunk/test/testuno/source/testcase/uno/ffc/FFCTest.java Wed Sep 12 07:13:09
2012
@@ -0,0 +1,378 @@
+/**************************************************************
+ * 
+ * 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.io.FilenameFilter;
+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.Rule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+import org.openoffice.test.OpenOffice;
+import org.openoffice.test.common.FileProvider;
+import org.openoffice.test.common.FileProvider.FileRepos;
+import org.openoffice.test.common.FileUtil;
+import org.openoffice.test.common.Logger;
+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(FileProvider.class)
+public class FFCTest {
+	@Rule
+	public Logger log = Logger.getLogger(this, false);
+
+	private static final UnoApp app = new UnoApp();
+	private static Map<String, String> filterMap = new HashMap<String, String>();
+	@FileRepos
+	public static String suiteDir = "../suite/";
+	private String fileURL = "";
+	private String operateFilePath = "";
+	private static boolean isSuiteFileExist = false;
+	private static Map<String, String> formatMap = new HashMap<String, String>();
+	private static File testSpaceFile = Testspace.getFile();
+	private boolean isSucceed = false;
+	private static String  tempFolder = testSpaceFile.getAbsolutePath() + File.separator + "temp";
+//	@Parameters
+//	public static Collection<String[]>  data() throws Exception{
+//		initMap();
+//		ArrayList<String[]> list = new ArrayList<String[]>();
+//		List<String> suitePathList = new ArrayList<String>();
+//		FileReader fileReader = null;
+//		BufferedReader reader = null;
+//		File suites = new File(suiteDir);
+//		if (suites.exists() && suites.list().length > 0) {
+//			isSuiteFileExist = true;
+//			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});
+//						}
+//					} 
+//					if(reader != null){
+//						reader.close();
+//						reader = null;
+//					}
+//					if(fileReader != null){
+//						fileReader.close();
+//						fileReader = null;					
+//					}
+//				}
+//			
+//			}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){
+//				}
+//			}
+//		} else {// run files from ffc data directory
+//			File ffcDataHome = new File("data\\ffc");
+//			getFileList(ffcDataHome, list);
+//		}
+//	
+//		return list;
+//	}
+//	
+	public static void getFileList(File dir, List<String[]> list) {
+		File[] files = dir.listFiles(new FilenameFilter() {
+			@Override
+			public boolean accept(File dir, String name) {
+				File file =  new File(dir.getAbsolutePath() + File.separator + name);
+				String filename = new File(name).getName().toLowerCase(); 
+				boolean accept;
+				if (file.isDirectory()) {
+					accept = true;
+				} else {
+					accept =  filename.endsWith(".docx")
+					|| filename.endsWith(".pptx")
+					|| filename.endsWith(".xlsx")
+					|| filename.endsWith(".ppt")
+					|| filename.endsWith(".xls")
+					|| 	filename.endsWith(".doc") ;
+				}
+				
+				return accept;
+			}
+			
+		});
+		if (files == null)
+			return;
+
+		for (File file : files) {
+			if (file.isDirectory()) {
+				getFileList(file, list);
+			} else {
+				list.add(new String[] {file.getAbsolutePath().replace(dir.getParentFile().getAbsolutePath(),
"").replace("\\", "/")});
+			
+			}
+		}
+	}
+	
+	public FFCTest(String url) {
+		this.fileURL = url;
+	}
+	
+	@BeforeClass
+	public static void init() {
+		initMap();
+		//Disable automation
+		OpenOffice.getDefault().setAutomationPort(-1);
+		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
+	}
+	
+	@Before
+	public void setUp() throws Exception {	
+//		if (isSuiteFileExist) {
+//			operateFilePath = downloadFile(fileURL);
+//		} else {
+//			operateFilePath = Testspace.prepareData(fileURL);
+//		}
+//		
+		operateFilePath = Testspace.prepareData(fileURL);
+		app.start();	
+	}
+	@After
+	public void tearDown() throws Exception {			
+		if (!isSucceed) {
+			FileUtil.copyFile(operateFilePath, Testspace.getFile("output").getAbsolutePath());
+			app.close();
+		}
+	}
+
+	
+	@Test(timeout=1000*60*5)
+	public void exportTest() throws Exception {
+		//MS Office Format ->ODF
+		boolean flag = false;
+		
+		String saveAsODF = exportAsODF(operateFilePath);
+		System.out.println("MS ->ODF finished");
+		//ODF->MS
+		String savedMSFilePath = exportAsODF(saveAsODF); 
+		System.out.println("ODF->MS Finished");
+		File savedMSFile = new File(savedMSFilePath);
+		Assert.assertTrue("FFC Test for file : "+ savedMSFilePath, savedMSFile.exists());
+		System.out.println("ODF->MS Finished");
+		
+
+		//Export ODF->PDF
+		exportAsPDF(saveAsODF);
+		System.out.println("ODF->PDF Finished");
+		flag = true;
+		Assert.assertTrue("FFC Test for file : "+ operateFilePath, flag);
+		isSucceed = true;
+	}
+	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;
+	}
+}



Mime
View raw message