2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import convwatch
.GraphicalTestArguments
;
22 import convwatch
.OfficePrint
;
23 import convwatch
.ConvWatchCancelException
;
24 import convwatch
.FileHelper
;
27 import helper
.URLHelper
;
28 import com
.sun
.star
.lang
.XComponent
;
29 import com
.sun
.star
.frame
.XStorable
;
30 import com
.sun
.star
.beans
.PropertyValue
;
31 import com
.sun
.star
.lang
.XServiceInfo
;
32 import com
.sun
.star
.uno
.UnoRuntime
;
34 public class GraphicalDifferenceCheck
36 private static void showVersion()
39 if (FileHelper
.isDebugEnabled())
41 GlobalLogWriter
.get().println("");
42 GlobalLogWriter
.get().println("+##############################+");
43 GlobalLogWriter
.get().println("##### THIS IS CONVWATCH #####");
44 GlobalLogWriter
.get().println("##### Debug Version 1.0015 #####");
45 GlobalLogWriter
.get().println("+##############################+");
46 GlobalLogWriter
.get().println("");
51 * Creates references form documents used by the graphical difference check
53 * @param _sInputPath the original document path
54 * @param _sReferencePath the directory where the document will print as file or export as pdf.
56 * @throws ConvWatchException if there are problems, see message
58 * Stops rest, if one creation of reference fails.
60 public static void createReferences(String _sInputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
63 File aInputPath
= new File(_sInputPath
);
65 if (aInputPath
.isDirectory())
67 String fs
= System
.getProperty("file.separator");
69 aInputPath
.getAbsolutePath();
71 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
72 for (int i
=0;i
<aList
.length
;i
++)
74 String sEntry
= (String
)aList
[i
];
75 String sNewReferencePath
= _sReferencePath
+ fs
+ FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
76 createOneReferenceFile(sEntry
, sNewReferencePath
, _aGTA
);
81 // System.out.println("No directory.");
82 createOneReferenceFile(_sInputPath
, _sReferencePath
, _aGTA
);
88 * Creates a reference for a single document used by the graphical difference check
90 * @param _sInputFile the original document
91 * @param _sReferencePath the directory where the document will print as file or export as pdf.
93 * @throws ConvWatchException if the are problems, see containing message
95 public static boolean createOneReferenceFile(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
100 _aGTA
.setInputFile(_sInputFile
);
102 return OfficePrint
.buildReference(_aGTA
, _sReferencePath
, _sInputFile
);
106 * Check if a reference exist
108 * @param _sInputFile the original document
109 * @param _sReferencePath the directory where the document will print as file or export as pdf.
111 public static boolean isReferenceExists(String _sInputFile
, String _sReferencePath
, GraphicalTestArguments _aGTA
)
113 return OfficePrint
.isReferenceExists(_aGTA
, _sReferencePath
, _sInputFile
);
118 * Used for the comparance of graphical differences.
119 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
121 * @param _sInputPath the original document path
122 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
123 * needed very much disk space (up to 10MB per page).
124 * The path _sOutputPath must be writeable.
125 * @param _sReferencePath the directory where the document will print as file or export as pdf.
126 * @param _aGTA Helper class for lot of parameter to control the office.
128 * Disadvantage: stops rest if one test file has a problem.
130 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
132 return check(_sInputPath
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
136 * Used for the comparance of graphical differences.
137 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
139 * @param _sInputPath the original document path
140 * @param _sReferencePath the directory where the document will print as file or export as pdf.
141 * @param _sOutputPath path where the same directory structure of the given input path will create. All the result documents
142 * needed very much disk space (up to 10MB per page).
143 * The path _sOutputPath must be writeable.
144 * @param _sDiffPath Path to older differences.
145 * @param _aGTA Helper class for lot of parameter to control the office.
148 * Stops all, if one creation of reference fails
150 public static boolean check(String _sInputPath
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
156 File aInputPath
= new File(_sInputPath
);
157 if (aInputPath
.isDirectory())
159 String fs
= System
.getProperty("file.separator");
161 Object
[] aList
= DirectoryHelper
.traverse(_sInputPath
, FileHelper
.getFileFilter(), _aGTA
.includeSubDirectories());
162 if (aList
.length
!= 0)
164 for (int i
=0;i
<aList
.length
;i
++)
166 String sEntry
= (String
)aList
[i
];
167 String sNewSubDir
= FileHelper
.removeFirstDirectorysAndBasenameFrom(sEntry
, _sInputPath
);
168 String sNewReferencePath
= _sReferencePath
;
169 String sNewOutputPath
= _sOutputPath
;
170 String sNewDiffPath
= _sDiffPath
;
171 if (sNewSubDir
.length() > 0)
173 if (sNewReferencePath
!= null)
175 sNewReferencePath
= sNewReferencePath
+ fs
+ sNewSubDir
;
178 sNewOutputPath
= sNewOutputPath
+ fs
+ sNewSubDir
;
179 if (sNewDiffPath
!= null)
181 sNewDiffPath
= sNewDiffPath
+ fs
+ sNewSubDir
;
184 bOk
&= checkOneFile(sEntry
, sNewOutputPath
, sNewReferencePath
, sNewDiffPath
, _aGTA
);
190 bOk
= /* GraphicalDifferenceCheck.*/ checkOneFile(_sInputPath
, _sOutputPath
, _sReferencePath
, _sDiffPath
, _aGTA
);
196 * Used for the comparance of graphical differences.
197 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
199 * The path _sOutputPath must be writeable
201 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
203 return checkOneFile( _sInputFile
, _sOutputPath
, _sReferencePath
, null, _aGTA
);
208 * Used for the comparance of graphical differences.
209 * Method compares one document (_sInputFile) with an older document of the same name in the provided directory (_sReferencePath).
211 * For scenarios, where a difference is known and further changes are of interest, differences itself can be compared.
212 * This functionality is provided by the difference path parameter (_sDiffPath). If set, the difference of the current comparance (between input and reference),
213 * will be compared with the (same named) difference document from a earlier comparance.
215 * The path _sOutputPath must be writeable
217 public static boolean checkOneFile(String _sInputFile
, String _sOutputPath
, String _sReferencePath
, String _sDiffPath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
222 _aGTA
.setInputFile(_sInputFile
);
226 if (_sDiffPath
!= null)
228 // check with an old diff
229 bOk
= convwatch
.ConvWatch
.checkDiffDiff(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
, _sDiffPath
);
234 bOk
= convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, _sInputFile
, _sReferencePath
);
240 * Instead of providing a saved document for graphical comparance a StarOffice xComponent
241 * will be saved and afterwards compared.
243 * @param xComponent the test document to be compared as StarOffice component
244 * @param _sOutputPath Path where test results are supposed to been saved. The path _sOutputPath must be writeable.
245 * These documents need sufficient disk space (up to 10MB per page).
246 * A directory structure will be created, which is a mirrored from input path.
248 * @param _resultDocName Name by which the xComponent shall be saved as OpenOffice.org XML document.
249 * If provided without suffix, the suffix will be derived from the export filter.
250 * @param _sReferencePath the directory where the document will print as file or export as pdf.
251 * @param _aGTA Helper class for lot of parameter to control the office.
253 public static boolean checkOneFile(XComponent xComponent
, String _sOutputPath
, String _resultDocName
, String _sReferencePath
, GraphicalTestArguments _aGTA
) throws ConvWatchException
259 sInputFile
= createInputFile(xComponent
, _sOutputPath
, _resultDocName
);
260 sInputFile
= FileHelper
.getSystemPathFromFileURL(sInputFile
);
261 return convwatch
.ConvWatch
.check(_aGTA
, _sOutputPath
, sInputFile
, _sReferencePath
);
264 private static String
createInputFile(XComponent xComponent
, String _sOutputPath
, String resultDocName
)
265 throws ConvWatchCancelException
268 // find the adequate XML StarOffice output filter to save the document and adequate suffix
269 StringBuffer suffix
= new StringBuffer();
270 String exportFilter
= getXMLOutputFilterforXComponent(xComponent
, suffix
);
271 if(resultDocName
== null)
272 resultDocName
= "OOoTestDocument";
273 if(resultDocName
.indexOf('.') == -1)
274 resultDocName
= suffix
.insert(0, resultDocName
).toString();
276 // create a result URL for storing the office document
277 String resultURL
= URLHelper
.getFileURLFromSystemPath(ensureEndingFileSep(_sOutputPath
) + resultDocName
);
279 XStorable xStorable
= null;
280 xStorable
= UnoRuntime
.queryInterface(com
.sun
.star
.frame
.XStorable
.class, xComponent
);
281 if(xStorable
== null)
283 throw new ConvWatchCancelException("com.sun.star.frame.XStorable could not be instantiated from the office.");
286 PropertyValue pvFilterName
= new PropertyValue("FilterName", -1, exportFilter
, com
.sun
.star
.beans
.PropertyState
.getDefault());
287 PropertyValue pvOverwrite
= new PropertyValue("Overwrite", -1, new Boolean(true), com
.sun
.star
.beans
.PropertyState
.getDefault());
291 xStorable
.storeAsURL(resultURL
, new PropertyValue
[]{pvFilterName
, pvOverwrite
});
293 catch (com
.sun
.star
.io
.IOException e
)
296 throw new ConvWatchCancelException("Wrap IOException caught, " + e
.getMessage());
299 GlobalLogWriter
.get().println("Saving XComponent as " + resultURL
);
305 private static String
getXMLOutputFilterforXComponent(XComponent xComponent
, StringBuffer suffix
){
306 XServiceInfo xSI
= UnoRuntime
.queryInterface(XServiceInfo
.class, xComponent
);
307 if (xSI
.supportsService("com.sun.star.text.TextDocument")){
308 resetBuffer(suffix
, ".sxw");
309 return "swriter: StarOffice XML (Writer)";
310 }else if (xSI
.supportsService("com.sun.star.sheet.SpreadsheetDocument")){
311 resetBuffer(suffix
, ".sxc");
312 return "scalc: StarOffice XML (Calc)";
313 }else if (xSI
.supportsService("com.sun.star.presentation.PresentationDocument")){
314 resetBuffer(suffix
, ".sxi");
315 return "simpress: StarOffice XML (Impress)";
316 }else if(xSI
.supportsService("com.sun.star.drawing.DrawingDocument")){
317 resetBuffer(suffix
, ".sxd");
318 return "sdraw: StarOffice XML (Draw)";
319 }else if (xSI
.supportsService("com.sun.star.formula.FormulaProperties")){
320 resetBuffer(suffix
, ".sxm");
321 return "smath: StarOffice XML (Math)";
326 private static StringBuffer
resetBuffer(StringBuffer sb
, String suffix
)
330 sb
.replace(0, sb
.length(), suffix
);
335 private static String
ensureEndingFileSep(String s
)
337 if(s
!= null && !s
.equals("") && !s
.endsWith(File
.separator
))
339 s
= s
.trim() + File
.separator
;