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