Update ooo320-m1
[ooovba.git] / qadevOOo / runner / convwatch / GraphicalDifferenceCheck.java
blob2a2720cdc953151eeca61b9b3386c790503f823e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: GraphicalDifferenceCheck.java,v $
10 * $Revision: 1.8.8.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package convwatch;
33 import convwatch.GraphicalTestArguments;
34 import convwatch.OfficePrint;
35 import convwatch.ConvWatchCancelException;
36 import convwatch.FileHelper;
37 import java.io.File;
39 import helper.URLHelper;
40 import com.sun.star.lang.XComponent;
41 import com.sun.star.frame.XStorable;
42 import com.sun.star.beans.PropertyValue;
43 import com.sun.star.lang.XServiceInfo;
44 import com.sun.star.uno.UnoRuntime;
46 public class GraphicalDifferenceCheck
48 private static void showVersion()
50 // DEBUG only
51 if (FileHelper.isDebugEnabled())
53 GlobalLogWriter.get().println("");
54 GlobalLogWriter.get().println("+##############################+");
55 GlobalLogWriter.get().println("##### THIS IS CONVWATCH #####");
56 GlobalLogWriter.get().println("##### Debug Version 1.0015 #####");
57 GlobalLogWriter.get().println("+##############################+");
58 GlobalLogWriter.get().println("");
62 /**
63 * Creates references form documents used by the graphical difference check
65 * @param _sInputPath the original document path
66 * @param _sReferencePath the directory where the document will print as file or export as pdf.
68 * @throws ConvWatchException if there are problems, see message
70 * Stops rest, if one creation of reference fails.
72 public static void createReferences(String _sInputPath, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
74 //!
75 // System.out.println("createReferences() InputPath: " + _sInputPath + " refpath: " + _sReferencePath);
76 showVersion();
77 File aInputPath = new File(_sInputPath);
79 // System.out.println("Inputpath in file: " + aInputPath.getAbsolutePath());
80 //!
81 // if (aInputPath.exists())
82 // {
83 // System.out.println("Inputpath exists");
84 // }
85 // else
86 // {
87 // System.out.println("Inputpath doesn't exists");
88 // return;
89 // }
91 if (aInputPath.isDirectory())
93 String fs = System.getProperty("file.separator");
95 String sRemovePath = aInputPath.getAbsolutePath();
96 // a whole directory
98 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
99 for (int i=0;i<aList.length;i++)
101 String sEntry = (String)aList[i];
102 String sNewReferencePath = _sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
103 createOneReferenceFile(sEntry, sNewReferencePath, _aGTA);
106 else
109 // System.out.println("No directory.");
110 createOneReferenceFile(_sInputPath, _sReferencePath, _aGTA);
116 * Creates a reference for a single document used by the graphical difference check
118 * @param _sInputFile the original document
119 * @param _sReferencePath the directory where the document will print as file or export as pdf.
121 * @throws ConvWatchException if the are problems, see containing message
123 public static boolean createOneReferenceFile(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
125 showVersion();
126 if (_aGTA != null)
128 _aGTA.setInputFile(_sInputFile);
130 return OfficePrint.buildReference(_aGTA, _sReferencePath, _sInputFile);
134 * Check if a reference exist
136 * @param _sInputFile the original document
137 * @param _sReferencePath the directory where the document will print as file or export as pdf.
139 * @throws ConvWatchException if the are problems, see containing message
141 public static boolean isReferenceExists(String _sInputFile, String _sReferencePath, GraphicalTestArguments _aGTA)
143 return OfficePrint.isReferenceExists(_aGTA, _sReferencePath, _sInputFile);
148 * Used for the comparance of graphical differences.
149 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
151 * @param _sInputPath the original document path
152 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
153 * needed very much disk space (up to 10MB per page).
154 * The path _sOutputPath must be writeable.
155 * @param _sReferencePath the directory where the document will print as file or export as pdf.
156 * @param _GTA Helper class for lot of parameter to control the office.
158 * Disadvantage: stops rest if one test file has a problem.
160 public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
162 return check(_sInputPath, _sOutputPath, _sReferencePath, null, _aGTA);
166 * Used for the comparance of graphical differences.
167 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
169 * @param _sInputPath the original document path
170 * @param _sReferencePath the directory where the document will print as file or export as pdf.
171 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
172 * needed very much disk space (up to 10MB per page).
173 * The path _sOutputPath must be writeable.
174 * @param _sDiffPath Path to older differences.
175 * @param _GTA Helper class for lot of parameter to control the office.
178 * Stops all, if one creation of reference fails
180 public static boolean check(String _sInputPath, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
182 showVersion();
184 boolean bOk = true;
186 File aInputPath = new File(_sInputPath);
187 if (aInputPath.isDirectory())
189 String fs = System.getProperty("file.separator");
190 // a whole directory
191 Object[] aList = DirectoryHelper.traverse(_sInputPath, FileHelper.getFileFilter(), _aGTA.includeSubDirectories());
192 if (aList.length != 0)
194 for (int i=0;i<aList.length;i++)
196 String sEntry = (String)aList[i];
197 String sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, _sInputPath);
198 String sNewReferencePath = _sReferencePath;
199 String sNewOutputPath = _sOutputPath;
200 String sNewDiffPath = _sDiffPath;
201 if (sNewSubDir.length() > 0)
203 if (sNewReferencePath != null)
205 sNewReferencePath = sNewReferencePath + fs + sNewSubDir;
208 sNewOutputPath = sNewOutputPath + fs + sNewSubDir;
209 if (sNewDiffPath != null)
211 sNewDiffPath = sNewDiffPath + fs + sNewSubDir;
214 bOk &= checkOneFile(sEntry, sNewOutputPath, sNewReferencePath, sNewDiffPath, _aGTA);
218 else
220 bOk = /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath, _sOutputPath, _sReferencePath, _sDiffPath, _aGTA);
222 return bOk;
226 * Used for the comparance of graphical differences.
227 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
229 * The path _sOutputPath must be writeable
231 public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, GraphicalTestArguments _aGTA) throws ConvWatchException
233 return checkOneFile( _sInputFile, _sOutputPath, _sReferencePath, null, _aGTA);
238 * Used for the comparance of graphical differences.
239 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
241 * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
242 * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
243 * will be compared with the (same named) difference document from a earlier comparance.
245 * The path _sOutputPath must be writeable
247 public static boolean checkOneFile(String _sInputFile, String _sOutputPath, String _sReferencePath, String _sDiffPath, GraphicalTestArguments _aGTA ) throws ConvWatchException
249 showVersion();
250 if (_aGTA != null)
252 _aGTA.setInputFile(_sInputFile);
255 boolean bOk = false;
256 if (_sDiffPath != null)
258 // check with an old diff
259 bOk = convwatch.ConvWatch.checkDiffDiff(_aGTA, _sOutputPath, _sInputFile, _sReferencePath, _sDiffPath);
261 else
263 // one file
264 bOk = convwatch.ConvWatch.check(_aGTA, _sOutputPath, _sInputFile, _sReferencePath);
266 return bOk;
270 * Instead of providing a saved document for graphical comparance a StarOffice xComponent
271 * will be saved and afterwards compared.
273 * @param xComponent the test document to be compared as StarOffice component
274 * @param _sOutputPath Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
275 * These documents need sufficient disk space (up to 10MB per page).
276 * A directory structure will be created, which is a mirrored from input path.
278 * @param resultDocName Name by which the xComponent shall be saved as OpenOffice.org XML document.
279 * If provided without suffix, the suffix will be derived from the export filter.
280 * @param _sReferencePath the directory where the document will print as file or export as pdf.
281 * @param _GTA Helper class for lot of parameter to control the office.
283 public static boolean checkOneFile(XComponent xComponent, String _sOutputPath, String _resultDocName, String _sReferencePath, GraphicalTestArguments _aGTA ) throws ConvWatchException
285 showVersion();
287 // one file
288 String sInputFile;
289 sInputFile = createInputFile(xComponent, _sOutputPath, _resultDocName);
290 sInputFile = FileHelper.getSystemPathFromFileURL(sInputFile);
291 return convwatch.ConvWatch.check(_aGTA, _sOutputPath, sInputFile, _sReferencePath);
295 // LLA: old! /**
296 // LLA: old! * Returns 'true' if a reference document on the specific output path exists.
297 // LLA: old! * The name of the document is corresponding to the input document, which can be
298 // LLA: old! * provided by a single name or path.
299 // LLA: old! *
300 // LLA: old! * @param inputPath the original document name (possibly including path)
301 // LLA: old! * @param referencePath the directory where the reference document will be stored
302 // LLA: old!
303 // LLA: old! */
304 // LLA: old! public static boolean isReferencExistent(String inputDocumentPath, String referencePath)
305 // LLA: old! {
306 // LLA: old! // isolate the document name
307 // LLA: old! if(inputDocumentPath.indexOf(File.separator) != -1)
308 // LLA: old! inputDocumentPath = inputDocumentPath.substring(inputDocumentPath.lastIndexOf(File.separator) + 1, inputDocumentPath.length());
309 // LLA: old!
310 // LLA: old! // exchange any arbitray suffix against the refence suffix (.prn)
311 // LLA: old! if(inputDocumentPath.indexOf('.') != -1)
312 // LLA: old! inputDocumentPath = inputDocumentPath.substring(0, inputDocumentPath.lastIndexOf('.'));
313 // LLA: old! inputDocumentPath = inputDocumentPath + ".prn";
314 // LLA: old! System.out.println("GraphicalDifference CheckReferenceDocument: " + inputDocumentPath);
315 // LLA: old!
316 // LLA: old! File refFile = new File(referencePath + inputDocumentPath);
317 // LLA: old! if(refFile.exists()){
318 // LLA: old! return true;
319 // LLA: old! }else
320 // LLA: old! return false;
321 // LLA: old! }
324 private static String createInputFile(XComponent xComponent, String _sOutputPath, String resultDocName)
325 throws ConvWatchCancelException
328 // find the adequate XML StarOffice output filter to save the document and adequate suffix
329 StringBuffer suffix = new StringBuffer();
330 String exportFilter = getXMLOutputFilterforXComponent(xComponent, suffix);
331 if(resultDocName == null)
332 resultDocName = "OOoTestDocument";
333 if(resultDocName.indexOf('.') == -1)
334 resultDocName = suffix.insert(0, resultDocName).toString();
336 // create a result URL for storing the office document
337 String resultURL = URLHelper.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath) + resultDocName);
339 XStorable xStorable = null;
340 xStorable = (com.sun.star.frame.XStorable)UnoRuntime.queryInterface(com.sun.star.frame.XStorable.class, xComponent);
341 if(xStorable == null)
343 throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
346 PropertyValue pvFilterName = new PropertyValue("FilterName", -1, exportFilter, com.sun.star.beans.PropertyState.getDefault());
347 PropertyValue pvOverwrite = new PropertyValue("Overwrite", -1, new Boolean(true), com.sun.star.beans.PropertyState.getDefault());
351 xStorable.storeAsURL(resultURL, new PropertyValue[]{pvFilterName, pvOverwrite});
353 catch (com.sun.star.io.IOException e)
355 // wrap IOException
356 throw new ConvWatchCancelException("Wrap IOException caught, " + e.getMessage());
359 GlobalLogWriter.get().println("Saving XComponent as " + resultURL);
361 return resultURL;
365 private static String getXMLOutputFilterforXComponent(XComponent xComponent, StringBuffer suffix){
366 XServiceInfo xSI = (XServiceInfo) UnoRuntime.queryInterface(XServiceInfo.class, xComponent);
367 if (xSI.supportsService("com.sun.star.text.TextDocument")){
368 resetBuffer(suffix, ".sxw");
369 return "swriter: StarOffice XML (Writer)";
370 }else if (xSI.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
371 resetBuffer(suffix, ".sxc");
372 return "scalc: StarOffice XML (Calc)";
373 }else if (xSI.supportsService("com.sun.star.presentation.PresentationDocument")){
374 resetBuffer(suffix, ".sxi");
375 return "simpress: StarOffice XML (Impress)";
376 }else if(xSI.supportsService("com.sun.star.drawing.DrawingDocument")){
377 resetBuffer(suffix, ".sxd");
378 return "sdraw: StarOffice XML (Draw)";
379 }else if (xSI.supportsService("com.sun.star.formula.FormulaProperties")){
380 resetBuffer(suffix, ".sxm");
381 return "smath: StarOffice XML (Math)";
383 return null;
386 private static StringBuffer resetBuffer(StringBuffer sb, String suffix)
388 if(sb != null)
390 sb.replace(0, sb.length(), suffix);
392 return sb;
395 private static String ensureEndingFileSep(String s)
397 if(s != null && !s.equals("") && !s.endsWith(File.separator))
399 s = s.trim() + File.separator;
401 else
403 if(s == null)
405 s = "";
409 return s;