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 pvt.uno;
23 
24 import static org.openoffice.test.common.Testspace.*;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.PrintStream;
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.openoffice.test.OpenOffice;
38 import org.openoffice.test.common.FileProvider;
39 import org.openoffice.test.common.FileProvider.FileFilter;
40 import org.openoffice.test.common.FileProvider.FileRepeat;
41 import org.openoffice.test.common.FileProvider.FileRepos;
42 import org.openoffice.test.common.FileUtil;
43 import org.openoffice.test.common.Logger;
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.lang.XComponent;
50 import com.sun.star.uno.UnoRuntime;
51 import com.sun.star.util.XCloseable;
52 
53 
54 @RunWith(FileProvider.class)
55 public class Conversion {
56 
57 	@Rule
58 	public Logger log = Logger.getLogger(this);
59 
60 	@FileRepos
61 	public static String repos = System.getProperty("conversion.repos", getDataPath("conversion_pvt"));
62 	@FileFilter
63 	public static String filter = System.getProperty("conversion.filter", "-f .*\\.((doc)|(dot)|(odt)|(ott))$ writer_pdf_Export pdf "
64 			+ "-f .*\\.((xls)|(xlt)|(ods)|(ots))$ calc_pdf_Export pdf "
65 			+ "-f .*\\.((ppt)|(ppt)|(odp)|(otp))$ impress_pdf_Export pdf "
66 			+ "-f .*\\.((doc)|(dot)|(docx)|(docm)|(dotx)|(dotm))$ writer8 odt "
67 			+ "-f .*\\.((xls)|(xlt)|(xlsx)|(xltx)|(xlsm)|(xltm))$ calc8 ods "
68 			+ "-f .*\\.((ppt)|(pot)|(pptx)|(pptm)|(potm)|(potx))$ impress8 odp "
69 			+ "-f .*\\.((odt)|(ott))$ 'MS Word 97' doc "
70 			+ "-f .*\\.((ods)|(ots))$ 'MS Excel 97' xls "
71 			+ "-f .*\\.((odp)|(otp))$ 'MS PowerPoint 97' ppt");
72 
73 	@FileRepeat
74 	public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
75 
76 	public static String clean = System.getProperty("conversion.clean", "file");
77 
78 	private static UnoApp app = new UnoApp();
79 
80 	private static PrintStream result;
81 
82 	private static int counter = 0;
83 
84 	@BeforeClass
85 	public static void beforeClass() throws Exception {
86 		//Disable automation
87 		OpenOffice.getDefault().setAutomationPort(-1);
88 		OpenOffice.getDefault().addArgs("-invisible", "-conversionmode", "-headless", "-hidemenu");
89 
90 		File resultFile = Testspace.getFile("output/conversion.csv");
91 		resultFile.getParentFile().mkdirs();
92 		result = new PrintStream(new FileOutputStream(resultFile));
93 		result.println("File,Scenario,File Size,Time Consumed After Closing,Time Consumed After Saving,Time Consumed After Loading");
94 	}
95 
96 	@AfterClass
97 	public static void afterClass() throws Exception {
98 		result.close();
99 		app.close();
100 	}
101 
102 	private String sourcePath = null;
103 	private String targetFilterName = null;
104 	private String targetExtName = null;
105 
106 	private File sourceFile = null;
107 	private File targetFile = null;
108 	private String sourceFileUrl = null;
109 	private String targetFileUrl = null;
110 
111 	private String scenario = null;
112 	private String sourceFileId = null;
113 	private long loadTime = -1;
114 	private long saveTime = -1;
115 	private long closeTime = -1;
116 
117 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
118 		super();
119 		this.sourcePath = sourcePath;
120 		this.targetFilterName = targetFilterName;
121 		this.targetExtName = targetExtName;
122 		counter++;
123 	}
124 
125 	@Before
126 	public void before() throws Exception {
127 		sourceFile = new File(sourcePath);
128 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
129 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
130 		targetFileUrl = FileUtil.getUrl(this.targetFile);
131 
132 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
133 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
134 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
135 		sourceFileId = pathSource.replace(pathRepos, "");
136 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
137 		app.start();
138 	}
139 
140 	@After
141 	public void after() throws Exception{
142 		result.println(sourceFileId + "," + scenario + "," + sourceFile.length() + "," + closeTime + "," + saveTime + "," + loadTime);
143 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
144 		if (closeTime < 0) {
145 			app.close();
146 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
147 			app.close();
148 		}
149 	}
150 
151 	private PropertyValue propertyValue(String name, Object value) {
152 		PropertyValue p = new PropertyValue();
153 		p.Name = name;
154 		p.Value= value;
155 		return p;
156 	}
157 
158 	@Test(timeout=10 * 60000)
159 	public void testConversion() throws Exception {
160 		// convert
161 		long start = System.currentTimeMillis();
162 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
163 				propertyValue("Hidden", true),
164 				propertyValue("ReadOnly", true),
165 				propertyValue("AsyncMode", false),
166 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE));
167 
168 		loadTime = System.currentTimeMillis() - start;
169 		app.saveDocumentToURL(doc, targetFileUrl,
170 				propertyValue( "FilterName", targetFilterName),
171 				propertyValue( "Overwrite", true));
172 		saveTime = System.currentTimeMillis() - start;
173 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
174 		xCloseable.close(true);
175 		closeTime = System.currentTimeMillis() - start;
176 	}
177 
178 }
179