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.Testspace;
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.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("conversion_pvt"));
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 	private static OpenOffice aoo = new OpenOffice();
81 
82 	private static UnoApp app = null;
83 
84 	private static DataSheet result;
85 
86 	private static int counter = 0;
87 
88 	@BeforeClass
89 	public static void beforeClass() throws Exception {
90 		aoo.setUnoUrl(OpenOffice.DEFAULT_UNO_URL);
91 		aoo.addArgs("-invisible", "-conversionmode", "-hidemenu");
92 	    app = new UnoApp(aoo);
93 	    Testspace.prepareDataFile("limit_cfg.ini", aoo.getHome().toString()+"/program");	// Move limitation check file to installation dir
94 		result = new DataSheet(getFile("output/pvt_uno_conversion.xml"));
95 		result.addRow("data", "File","Scenario","File Size","Time Consumed After Closing","Time Consumed After Saving","Time Consumed After Loading");
96 	}
97 
98 	@AfterClass
99 	public static void afterClass() throws Exception {
100 		app.close();
101 	}
102 
103 	private String sourcePath = null;
104 	private String targetFilterName = null;
105 	private String targetExtName = null;
106 
107 	private File sourceFile = null;
108 	private File targetFile = null;
109 	private String sourceFileUrl = null;
110 	private String targetFileUrl = null;
111 
112 	private String scenario = null;
113 	private String sourceFileId = null;
114 	private long loadTime = -1;
115 	private long saveTime = -1;
116 	private long closeTime = -1;
117 
118 	public Conversion(String sourcePath, String targetFilterName, String targetExtName) {
119 		super();
120 		this.sourcePath = sourcePath;
121 		this.targetFilterName = targetFilterName;
122 		this.targetExtName = targetExtName;
123 		counter++;
124 	}
125 
126 	@Before
127 	public void before() throws Exception {
128 		sourceFile = new File(sourcePath);
129 		sourceFileUrl = FileUtil.getUrl(this.sourceFile);
130 		targetFile = getFile("classtemp/" + sourceFile.getName()+ "." + targetExtName);
131 		targetFileUrl = FileUtil.getUrl(this.targetFile);
132 
133 		scenario = FileUtil.getFileExtName(sourceFile.getName()).toLowerCase() + " to " + FileUtil.getFileExtName(targetFile.getName()).toLowerCase();
134 		String pathSource = sourceFile.getCanonicalPath().replace("\\", "/");
135 		String pathRepos = new File(repos).getCanonicalPath().replace("\\", "/") + "/";
136 		sourceFileId = pathSource.replace(pathRepos, "");
137 		log.info("Start [File: " + sourceFileId + "] [Size: " + (sourceFile.length() / 1024) + "KB] [Scenario: " + scenario + "]");
138 		app.start();
139 	}
140 
141 	@After
142 	public void after() throws Exception{
143 		result.addRow("data", sourceFileId, scenario, sourceFile.length(), closeTime, saveTime, loadTime);
144 		log.info("Result [After Closing: " + closeTime + "] [After Saving: " + saveTime + "] [After Loading: " + loadTime + "]");
145 		if (closeTime < 0) {
146 			app.close();
147 		} else if ("file".equalsIgnoreCase(clean) && counter % repeat == 0) {
148 			app.close();
149 		}
150 	}
151 
152 	private PropertyValue propertyValue(String name, Object value) {
153 		PropertyValue p = new PropertyValue();
154 		p.Name = name;
155 		p.Value= value;
156 		return p;
157 	}
158 
159 	@Test(timeout=10 * 60000)
160 	public void testConversion() throws Exception {
161 		// convert
162 		long start = System.currentTimeMillis();
163 		XComponent doc = app.loadDocumentFromURL(sourceFileUrl,
164 				propertyValue("Hidden", true),
165 				propertyValue("ReadOnly", true),
166 				propertyValue("AsyncMode", false),
167 				propertyValue("MacroExecutionMode", MacroExecMode.NEVER_EXECUTE),
168 				propertyValue("LimitationCheckLevel", nLevelInfo));
169 
170 		loadTime = System.currentTimeMillis() - start;
171 		app.saveDocumentToURL(doc, targetFileUrl,
172 				propertyValue( "FilterName", targetFilterName),
173 				propertyValue( "Overwrite", true));
174 		saveTime = System.currentTimeMillis() - start;
175 		XCloseable xCloseable = (XCloseable) UnoRuntime.queryInterface(XCloseable.class, doc);
176 		xCloseable.close(true);
177 		closeTime = System.currentTimeMillis() - start;
178 	}
179 
180 }
181