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 
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Rule;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.openoffice.test.OpenOffice;
36 import org.openoffice.test.common.DataSheet;
37 import org.openoffice.test.common.FileProvider;
38 import org.openoffice.test.common.FileProvider.FileFilter;
39 import org.openoffice.test.common.FileProvider.FileRepeat;
40 import org.openoffice.test.common.FileProvider.FileRepos;
41 import org.openoffice.test.common.FileUtil;
42 import org.openoffice.test.common.Logger;
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(FileProvider.class)
53 public class Conversion {
54 
55 	@Rule
56 	public Logger log = Logger.getLogger(this);
57 
58 	@FileRepos
59 	public static String repos = System.getProperty("conversion.repos", getDataPath("conversion_pvt"));
60 	@FileFilter
61 	public static String filter = System.getProperty("conversion.filter",
62 			  "-f .*(doc|dot|odt|ott)$ writer_pdf_Export pdf "
63 			+ "-f .*(xls|xlt|ods|ots)$ calc_pdf_Export pdf "
64 			+ "-f .*(ppt|ppt|odp|otp)$ impress_pdf_Export pdf "
65 			+ "-f .*(doc|dot|docx|docm|dotx|dotm)$ writer8 odt "
66 			+ "-f .*(xls|xlt|xlsx|xltx|xlsm|xltm)$ calc8 ods "
67 			+ "-f .*(ppt|pot|pptx|pptm|potm|potx)$ impress8 odp "
68 			+ "-f .*(odt|ott)$ 'MS Word 97' doc "
69 			+ "-f .*(ods|ots)$ 'MS Excel 97' xls "
70 			+ "-f .*(odp|otp)$ 'MS PowerPoint 97' ppt");
71 
72 	@FileRepeat
73 	public static int repeat = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
74 
75 	public static String clean = System.getProperty("conversion.clean", "file");
76 
77 	private static OpenOffice aoo = new OpenOffice();
78 
79 	private static UnoApp app = null;
80 
81 	private static DataSheet result;
82 
83 	private static int counter = 0;
84 
85 	@BeforeClass
86 	public static void beforeClass() throws Exception {
87 		aoo.setUnoUrl(OpenOffice.DEFAULT_UNO_URL);
88 		aoo.addArgs("-invisible", "-conversionmode", "-hidemenu");
89 	    app = new UnoApp(aoo);
90 		result = new DataSheet(getFile("output/pvt_uno_conversion.xml"));
91 		result.addRow("data", "File","Scenario","File Size","Time Consumed After Closing","Time Consumed After Saving","Time Consumed After Loading");
92 	}
93 
94 	@AfterClass
95 	public static void afterClass() throws Exception {
96 		app.close();
97 	}
98 
99 	private String sourcePath = null;
100 	private String targetFilterName = null;
101 	private String targetExtName = null;
102 
103 	private File sourceFile = null;
104 	private File targetFile = null;
105 	private String sourceFileUrl = null;
106 	private String targetFileUrl = null;
107 
108 	private String scenario = null;
109 	private String sourceFileId = null;
110 	private long loadTime = -1;
111 	private long saveTime = -1;
112 	private long closeTime = -1;
113 
114 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
115 		super();
116 		this.sourcePath = sourcePath;
117 		this.targetFilterName = targetFilterName;
118 		this.targetExtName = targetExtName;
119 		counter++;
120 	}
121 
122 	@Before
123 	public void before() throws Exception {
124 		sourceFile = new File(sourcePath);
125 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
126 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
127 		targetFileUrl = FileUtil.getUrl(this.targetFile);
128 
129 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
130 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
131 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
132 		sourceFileId = pathSource.replace(pathRepos, "");
133 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
134 		app.start();
135 	}
136 
137 	@After
138 	public void after() throws Exception{
139 		result.addRow("data", sourceFileId, scenario, sourceFile.length(), closeTime, saveTime, loadTime);
140 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
141 		if (closeTime < 0) {
142 			app.close();
143 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
144 			app.close();
145 		}
146 	}
147 
148 	private PropertyValue propertyValue(String name, Object value) {
149 		PropertyValue p = new PropertyValue();
150 		p.Name = name;
151 		p.Value= value;
152 		return p;
153 	}
154 
155 	@Test(timeout=10 * 60000)
156 	public void testConversion() throws Exception {
157 
158 		int nLevelInfo = Integer.parseInt(System.getProperty("conversion.limitationcheck", "0"));	// Level info: starts from 1, 0 means no need for limitation check
159 
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 				propertyValue("LimitationCheckLevel", nLevelInfo));
168 
169 		loadTime = System.currentTimeMillis() - start;
170 		app.saveDocumentToURL(doc, targetFileUrl,
171 				propertyValue( "FilterName", targetFilterName),
172 				propertyValue( "Overwrite", true));
173 		saveTime = System.currentTimeMillis() - start;
174 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
175 		xCloseable.close(true);
176 		closeTime = System.currentTimeMillis() - start;
177 	}
178 
179 }
180