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  *************************************************************/
21 
22 package testcase.uno.pvt;
23 
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.PrintStream;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 
30 import org.junit.After;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.junit.runners.Parameterized;
38 import org.junit.runners.Parameterized.Parameters;
39 import org.openoffice.test.OpenOffice;
40 import org.openoffice.test.common.FileUtil;
41 import org.openoffice.test.common.Logger;
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.lang.XComponent;
48 import com.sun.star.uno.UnoRuntime;
49 import com.sun.star.util.XCloseable;
50 
51 
52 @RunWith(Parameterized.class)
53 public class Conversion {
54 	@Rule
55 	public Logger log = Logger.getLogger(this);
56 
57 	public static String repos = System.getProperty("user.home") + "/pvt_conversion_data";
58 
59 	public static String[] params = {
60 		".*\\.((doc)|(dot)|(odt)|(ott))$", "writer_pdf_Export", "pdf",
61 		".*\\.((xls)|(xlt)|(ods)|(ots))$", "calc_pdf_Export", "pdf",
62 		".*\\.((ppt)|(ppt)|(odp)|(otp))$", "impress_pdf_Export", "pdf",
63 		".*\\.((doc)|(dot))$", "writer8", "odt",
64 		".*\\.((xls)|(xlt))$", "calc8", "ods",
65 		".*\\.((ppt)|(ppt))$", "impress8", "odp",
66 		".*\\.((odt)|(ott))$", "MS Word 97", "doc",
67 		".*\\.((ods)|(ots))$", "MS Excel 97", "xls",
68 		".*\\.((odp)|(otp))$", "MS PowerPoint 97", "ppt",
69 		};
70 
71 	private static UnoApp app = new UnoApp();
72 	private static PrintStream result;
73 	@Parameters
74 	public static Collection<Object[]> data() {
75 		File dir = new File(repos);
76 		ArrayList<Object[]> list = new ArrayList<Object[]>();
77 		collect(dir, list);
78 		return list;
79 	}
80 
81 	/**
82 	 * @param dir
83 	 * @param list
84 	 */
85 	public static void collect(File dir, ArrayList<Object[]> list) {
86 		File[] files = dir.listFiles();
87 		if (files == null)
88 			return;
89 
90 		for (File file : files) {
91 			if (file.isDirectory()) {
92 				collect(file, list);
93 			} else {
94 				String fileName = file.getName().toLowerCase();
95 				for (int i = 0; i < params.length;) {
96 					String pattern = params[i++];
97 					String targetFilterName = params[i++];
98 					String targetExtName = params[i++];
99 					if (pattern != null && fileName.matches(pattern)) {
100 						Object[] data = {file, Testspace.getFile("classtemp/" + file.getName()+ "." + targetExtName), targetFilterName};
101 						list.add(data);
102 					}
103 				}
104 			}
105 		}
106 	}
107 
108 	@BeforeClass
109 	public static void beforeClass() throws Exception {
110 		//Disable automation
111 		OpenOffice.getDefault().setAutomationPort(-1);
112 		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
113 
114 		File resultFile = Testspace.getFile("output/conversion.csv");
115 		resultFile.getParentFile().mkdirs();
116 		result = new PrintStream(new FileOutputStream(resultFile));
117 		result.println("File,Scenario,After Close,After Save,After Load");
118 	}
119 
120 	@AfterClass
121 	public static void afterClass() throws Exception {
122 		result.close();
123 	}
124 
125 	@Before
126 	public void before() throws Exception {
127 		app.start();
128 	}
129 
130 	@After
131 	public void after() throws Exception{
132 		String scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
133 		result.println(sourceFile.getName() + "," + scenario + "," + closeTime + "," + saveTime + "," + loadTime);
134 		if (closeTime < 0) {
135 			log.warning("Exception occurs during " + sourceFile.getName() + "->" + targetFile.getName());
136 			app.close();
137 		}
138 	}
139 
140 	private File sourceFile = null;
141 	private File targetFile = null;
142 	private String sourceFileUrl = null;
143 	private String targetFileUrl = null;
144 	private String targetFilterName = null;
145 	private long loadTime = -1;
146 	private long saveTime = -1;
147 	private long closeTime = -1;
148 
149 	public Conversion(File sourceFile, File targetFile, String targetFilterName) {
150 		super();
151 		this.sourceFile = sourceFile;
152 		this.targetFile = targetFile;
153 		this.targetFilterName = targetFilterName;
154 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
155 		targetFileUrl = FileUtil.getUrl(this.targetFile);
156 	}
157 
158 	private PropertyValue propertyValue(String name, Object value) {
159 		PropertyValue p = new PropertyValue();
160 		p.Name = name;
161 		p.Value= value;
162 		return p;
163 	}
164 
165 	@Test
166 	public void testConversion() throws Exception {
167 		// convert
168 		long start = System.currentTimeMillis();
169 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
170 				propertyValue("Hidden", true),
171 				propertyValue("ReadOnly", true),
172 				propertyValue("AsyncMode", false),
173 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE));
174 
175 		loadTime = System.currentTimeMillis() - start;
176 		app.saveDocumentToURL(doc, targetFileUrl,
177 				propertyValue( "FilterName", targetFilterName),
178 				propertyValue( "Overwrite", true));
179 		saveTime = System.currentTimeMillis() - start;
180 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
181 		xCloseable.close(true);
182 		closeTime = System.currentTimeMillis() - start;
183 	}
184 }
185