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