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.SystemUtil;
39 import org.openoffice.test.common.Testspace;
40 import org.openoffice.test.common.FileProvider.FileFilter;
41 import org.openoffice.test.common.FileProvider.FileRepeat;
42 import org.openoffice.test.common.FileProvider.FileRepos;
43 import org.openoffice.test.common.FileUtil;
44 import org.openoffice.test.common.Logger;
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",
64 			  "-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 = Integer.parseInt(System.getProperty("conversion.repeat", "8"));
76 
77 	public static String clean = System.getProperty("conversion.clean", "file");
78 
79 	public static int nLevelInfo = Integer.parseInt(System.getProperty("conversion.limitationcheck", "0"));	// Level info: starts from 1, 0 means no need for limitation check
80 
81 	public static int nSleep = Integer.parseInt(System.getProperty("conversion.sleep", "3000"));	// Sleep before loadComponentFromURL and storeToURL
82 
83 	private static OpenOffice aoo = new OpenOffice();
84 
85 	private static UnoApp app = null;
86 
87 	private static DataSheet result;
88 
89 	private static int counter = 0;
90 
91 	@BeforeClass
92 	public static void beforeClass() throws Exception {
93 		aoo.setUnoUrl(OpenOffice.DEFAULT_UNO_URL);
94 		aoo.addArgs("-invisible", "-conversionmode", "-hidemenu", "-nofirststartwizard");
95 	    app = new UnoApp(aoo);
96 	    Testspace.prepareDataFile("limit_cfg.ini", aoo.getHome().toString()+"/program");	// Move limitation check file to installation dir
97 		result = new DataSheet(getFile("output/pvt_uno_conversion.xml"));
98 		result.addRow("data", "File","Scenario","File Size","No","Time Consumed After Closing","Time Consumed After Saving","Time Consumed After Loading");
99 	}
100 
101 	@AfterClass
102 	public static void afterClass() throws Exception {
103 		app.close();
104 	}
105 
106 	private String sourcePath = null;
107 	private String targetFilterName = null;
108 	private String targetExtName = null;
109 
110 	private File sourceFile = null;
111 	private File targetFile = null;
112 	private String sourceFileUrl = null;
113 	private String targetFileUrl = null;
114 
115 	private String scenario = null;
116 	private String sourceFileId = null;
117 	private long loadTime = -1;
118 	private long saveTime = -1;
119 	private long closeTime = -1;
120 
121 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
122 		super();
123 		this.sourcePath = sourcePath;
124 		this.targetFilterName = targetFilterName;
125 		this.targetExtName = targetExtName;
126 		counter++;
127 	}
128 
129 	@Before
130 	public void before() throws Exception {
131 		sourceFile = new File(sourcePath);
132 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
133 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
134 		targetFileUrl = FileUtil.getUrl(this.targetFile);
135 
136 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
137 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
138 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
139 		sourceFileId = pathSource.replace(pathRepos, "");
140 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
141 		app.start();
142 	}
143 
144 	@After
145 	public void after() throws Exception{
146 		result.addRow("data", sourceFileId, scenario, sourceFile.length(), counter % repeat , closeTime, saveTime, loadTime);
147 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
148 		if (closeTime < 0) {
149 			app.close();
150 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
151 			app.close();
152 		}
153 	}
154 
155 	private PropertyValue propertyValue(String name, Object value) {
156 		PropertyValue p = new PropertyValue();
157 		p.Name = name;
158 		p.Value= value;
159 		return p;
160 	}
161 
162 	@Test(timeout=10 * 60000)
163 	public void testConversion() throws Exception {
164 		try {
165 			// convert
166 			SystemUtil.sleep(nSleep);	// Sleep before loadComponentFromURL
167 			long start = System.currentTimeMillis();
168 			XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
169 					propertyValue("Hidden", true),
170 					propertyValue("ReadOnly", true),
171 					propertyValue("AsyncMode", false),
172 					propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE),
173 					propertyValue("LimitationCheckLevel", nLevelInfo));
174 
175 			loadTime = System.currentTimeMillis() - start;
176 
177 			SystemUtil.sleep(nSleep);	// Sleep before storeToURL
178 			app.saveDocumentToURL(doc, targetFileUrl,
179 					propertyValue( "FilterName", targetFilterName),
180 					propertyValue( "Overwrite", true));
181 			saveTime = System.currentTimeMillis() - start - nSleep;
182 			XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
183 			xCloseable.close(true);
184 			closeTime = System.currentTimeMillis() - start - nSleep;
185 		} catch (com.sun.star.task.ErrorCodeIOException e){
186 			int errCode = e.ErrCode;
187 			if( 296 == errCode ) {
188 				loadTime = -2;
189 				saveTime = -2;
190 				closeTime = -2;
191 			}
192 
193 			throw e;
194 		}
195 	}
196 }
197