1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
30 import convwatch
.GraphicalTestArguments
;
31 import convwatch
.OfficePrint
;
32 import convwatch
.ConvWatchCancelException
;
33 import convwatch
.FileHelper
;
36 import helper
.URLHelper
;
37 import com
.sun
.star
.lang
.XComponent
;
38 import com
.sun
.star
.frame
.XStorable
;
39 import com
.sun
.star
.beans
.PropertyValue
;
40 import com
.sun
.star
.lang
.XServiceInfo
;
41 import com
.sun
.star
.uno
.UnoRuntime
;
43 public class GraphicalDifferenceCheck
45 private static void showVersion()
48 if (FileHelper
.isDebugEnabled())
50 GlobalLogWriter
.get().println("");
51 GlobalLogWriter
.get().println("+##############################+");
52 GlobalLogWriter
.get().println("##### THIS IS CONVWATCH #####");
53 GlobalLogWriter
.get().println("##### Debug Version 1.0015 #####");
54 GlobalLogWriter
.get().println("+##############################+");
55 GlobalLogWriter
.get().println("");
60 * Creates references form documents used by the graphical difference check
62 * @param _sInputPath the original document path
63 * @param _sReferencePath the directory where the document will print as file or export as pdf.
65 * @throws ConvWatchException if there are problems, see message
67 * Stops rest, if one creation of reference fails.
69 public static void createReferences(String _sInputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
72 // System.out.println("createReferences() InputPath: " + _sInputPath + " refpath: " + _sReferencePath);
74 File aInputPath
= new File(_sInputPath
);
76 // System.out.println("Inputpath in file: " + aInputPath.getAbsolutePath());
78 // if (aInputPath.exists())
80 // System.out.println("Inputpath exists");
84 // System.out.println("Inputpath doesn't exists");
88 if (aInputPath
.isDirectory())
90 String fs
= System
.getProperty("file.separator");
92 String sRemovePath
= aInputPath
.getAbsolutePath();
95 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
96 for (int i
=0;i
<aList
.length
;i
++)
98 String sEntry
= (String
)aList
[i
];
99 String sNewReferencePath
= _sReferencePath
+ fs
+ FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
100 createOneReferenceFile(sEntry
, sNewReferencePath
, _aGTA
);
106 // System.out.println("No directory.");
107 createOneReferenceFile(_sInputPath
, _sReferencePath
, _aGTA
);
113 * Creates a reference for a single document used by the graphical difference check
115 * @param _sInputFile the original document
116 * @param _sReferencePath the directory where the document will print as file or export as pdf.
118 * @throws ConvWatchException if the are problems, see containing message
120 public static boolean createOneReferenceFile(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
125 _aGTA
.setInputFile(_sInputFile
);
127 return OfficePrint
.buildReference(_aGTA
, _sReferencePath
, _sInputFile
);
131 * Check if a reference exist
133 * @param _sInputFile the original document
134 * @param _sReferencePath the directory where the document will print as file or export as pdf.
136 * @throws ConvWatchException if the are problems, see containing message
138 public static boolean isReferenceExists(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
)
140 return OfficePrint
.isReferenceExists(_aGTA
, _sReferencePath
, _sInputFile
);
145 * Used for the comparance of graphical differences.
146 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
148 * @param _sInputPath the original document path
149 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
150 * needed very much disk space (up to 10MB per page).
151 * The path _sOutputPath must be writeable.
152 * @param _sReferencePath the directory where the document will print as file or export as pdf.
153 * @param _GTA Helper class for lot of parameter to control the office.
155 * Disadvantage: stops rest if one test file has a problem.
157 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
159 return check(_sInputPath
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
163 * Used for the comparance of graphical differences.
164 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
166 * @param _sInputPath the original document path
167 * @param _sReferencePath the directory where the document will print as file or export as pdf.
168 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
169 * needed very much disk space (up to 10MB per page).
170 * The path _sOutputPath must be writeable.
171 * @param _sDiffPath Path to older differences.
172 * @param _GTA Helper class for lot of parameter to control the office.
175 * Stops all, if one creation of reference fails
177 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
183 File aInputPath
= new File(_sInputPath
);
184 if (aInputPath
.isDirectory())
186 String fs
= System
.getProperty("file.separator");
188 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
189 if (aList
.length
!= 0)
191 for (int i
=0;i
<aList
.length
;i
++)
193 String sEntry
= (String
)aList
[i
];
194 String sNewSubDir
= FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
195 String sNewReferencePath
= _sReferencePath
;
196 String sNewOutputPath
= _sOutputPath
;
197 String sNewDiffPath
= _sDiffPath
;
198 if (sNewSubDir
.length() > 0)
200 if (sNewReferencePath
!= null)
202 sNewReferencePath
= sNewReferencePath
+ fs
+ sNewSubDir
;
205 sNewOutputPath
= sNewOutputPath
+ fs
+ sNewSubDir
;
206 if (sNewDiffPath
!= null)
208 sNewDiffPath
= sNewDiffPath
+ fs
+ sNewSubDir
;
211 bOk
&= checkOneFile(sEntry
, sNewOutputPath
, sNewReferencePath
, sNewDiffPath
, _aGTA
);
217 bOk
= /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath
, _sOutputPath
, _sReferencePath
, _sDiffPath
, _aGTA
);
223 * Used for the comparance of graphical differences.
224 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
226 * The path _sOutputPath must be writeable
228 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
230 return checkOneFile( _sInputFile
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
235 * Used for the comparance of graphical differences.
236 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
238 * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
239 * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
240 * will be compared with the (same named) difference document from a earlier comparance.
242 * The path _sOutputPath must be writeable
244 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
249 _aGTA
.setInputFile(_sInputFile
);
253 if (_sDiffPath
!= null)
255 // check with an old diff
256 bOk
= convwatch
.ConvWatch
.checkDiffDiff(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
, _sDiffPath
);
261 bOk
= convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
);
267 * Instead of providing a saved document for graphical comparance a StarOffice xComponent
268 * will be saved and afterwards compared.
270 * @param xComponent the test document to be compared as StarOffice component
271 * @param _sOutputPath Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
272 * These documents need sufficient disk space (up to 10MB per page).
273 * A directory structure will be created, which is a mirrored from input path.
275 * @param resultDocName Name by which the xComponent shall be saved as OpenOffice.org XML document.
276 * If provided without suffix, the suffix will be derived from the export filter.
277 * @param _sReferencePath the directory where the document will print as file or export as pdf.
278 * @param _GTA Helper class for lot of parameter to control the office.
280 public static boolean checkOneFile(XComponent xComponent
, String _sOutputPath
, String _resultDocName
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
286 sInputFile
= createInputFile(xComponent
, _sOutputPath
, _resultDocName
);
287 sInputFile
= FileHelper
.getSystemPathFromFileURL(sInputFile
);
288 return convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, sInputFile
, _sReferencePath
);
293 // LLA: old! * Returns 'true' if a reference document on the specific output path exists.
294 // LLA: old! * The name of the document is corresponding to the input document, which can be
295 // LLA: old! * provided by a single name or path.
297 // LLA: old! * @param inputPath the original document name (possibly including path)
298 // LLA: old! * @param referencePath the directory where the reference document will be stored
301 // LLA: old! public static boolean isReferencExistent(String inputDocumentPath, String referencePath)
303 // LLA: old! // isolate the document name
304 // LLA: old! if(inputDocumentPath.indexOf(File.separator) != -1)
305 // LLA: old! inputDocumentPath = inputDocumentPath.substring(inputDocumentPath.lastIndexOf(File.separator) + 1, inputDocumentPath.length());
307 // LLA: old! // exchange any arbitray suffix against the refence suffix (.prn)
308 // LLA: old! if(inputDocumentPath.indexOf('.') != -1)
309 // LLA: old! inputDocumentPath = inputDocumentPath.substring(0, inputDocumentPath.lastIndexOf('.'));
310 // LLA: old! inputDocumentPath = inputDocumentPath + ".prn";
311 // LLA: old! System.out.println("GraphicalDifference CheckReferenceDocument: " + inputDocumentPath);
313 // LLA: old! File refFile = new File(referencePath + inputDocumentPath);
314 // LLA: old! if(refFile.exists()){
315 // LLA: old! return true;
317 // LLA: old! return false;
321 private static String
createInputFile(XComponent xComponent
, String _sOutputPath
, String resultDocName
)
322 throws ConvWatchCancelException
325 // find the adequate XML StarOffice output filter to save the document and adequate suffix
326 StringBuffer suffix
= new StringBuffer();
327 String exportFilter
= getXMLOutputFilterforXComponent(xComponent
, suffix
);
328 if(resultDocName
== null)
329 resultDocName
= "OOoTestDocument";
330 if(resultDocName
.indexOf('.') == -1)
331 resultDocName
= suffix
.insert(0, resultDocName
).toString();
333 // create a result URL for storing the office document
334 String resultURL
= URLHelper
.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath
) + resultDocName
);
336 XStorable xStorable
= null;
337 xStorable
= (com
.sun
.star
.frame
.XStorable
)UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XStorable
.class, xComponent
);
338 if(xStorable
== null)
340 throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
343 PropertyValue pvFilterName
= new PropertyValue("FilterName", -1, exportFilter
, com
.sun
.star
.beans
.PropertyState
.getDefault());
344 PropertyValue pvOverwrite
= new PropertyValue("Overwrite", -1, new Boolean(true), com
.sun
.star
.beans
.PropertyState
.getDefault());
348 xStorable
.storeAsURL(resultURL
, new PropertyValue
[]{pvFilterName
, pvOverwrite
});
350 catch (com
.sun
.star
.io
.IOException e
)
353 throw new ConvWatchCancelException("Wrap IOException caught, " + e
.getMessage());
356 GlobalLogWriter
.get().println("Saving XComponent as " + resultURL
);
362 private static String
getXMLOutputFilterforXComponent(XComponent xComponent
, StringBuffer suffix
){
363 XServiceInfo xSI
= (XServiceInfo
) UnoRuntime
.queryInterface(XServiceInfo
.class, xComponent
);
364 if (xSI
.supportsService("com.sun.star.text.TextDocument")){
365 resetBuffer(suffix
, ".sxw");
366 return "swriter: StarOffice XML (Writer)";
367 }else if (xSI
.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
368 resetBuffer(suffix
, ".sxc");
369 return "scalc: StarOffice XML (Calc)";
370 }else if (xSI
.supportsService("com.sun.star.presentation.PresentationDocument")){
371 resetBuffer(suffix
, ".sxi");
372 return "simpress: StarOffice XML (Impress)";
373 }else if(xSI
.supportsService("com.sun.star.drawing.DrawingDocument")){
374 resetBuffer(suffix
, ".sxd");
375 return "sdraw: StarOffice XML (Draw)";
376 }else if (xSI
.supportsService("com.sun.star.formula.FormulaProperties")){
377 resetBuffer(suffix
, ".sxm");
378 return "smath: StarOffice XML (Math)";
383 private static StringBuffer
resetBuffer(StringBuffer sb
, String suffix
)
387 sb
.replace(0, sb
.length(), suffix
);
392 private static String
ensureEndingFileSep(String s
)
394 if(s
!= null && !s
.equals("") && !s
.endsWith(File
.separator
))
396 s
= s
.trim() + File
.separator
;