merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / runner / convwatch / ConvWatch.java
blob94b04bf14206cba4f4b9f903e5863808d6e06ef4
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: ConvWatch.java,v $
10 * $Revision: 1.9 $
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.ConvWatchException;
34 import convwatch.ConvWatchCancelException;
35 import convwatch.FileHelper;
36 import convwatch.OfficePrint;
37 import convwatch.PRNCompare;
38 import convwatch.StatusHelper;
39 import helper.URLHelper;
40 import java.io.File;
42 public class ConvWatch
45 String getBuildID_FromFile(String _sInfoFile)
47 String sBuildID = "";
48 IniFile aIniFile = new IniFile(_sInfoFile);
49 if (aIniFile.is())
51 sBuildID = aIniFile.getValue("", "buildid");
53 return sBuildID;
55 /**
56 * Check if given document (_sAbsoluteInputFile) and it's postscript representation (_sAbsoluteReferenceFile) produce
57 * the same output like the StarOffice / OpenOffice.org which is accessable with XMultiServiceFactory.
58 * Here a simple graphically difference check is run through.
60 * Hint: In the OutputPath all needed files will create, there must
61 * be very much space. It's not possible to say how much.
62 * One page need up to 800kb as jpeg.
63 * Sample: If a document contains 2 pages, we need 2*800kb for prn
64 * output and 2*800kb for ps output and 2*800kb for it's difference
65 * output. So up to 4800kb or 4.8mb.
67 * RAM is need least 300mb. Will say, it's tested on a pc with 128mb RAM and 256mb swap.
69 * It's also absolutlly impossible to say, how much time this functions consume.
72 StatusHelper[] createPostscriptStartCheck(GraphicalTestArguments _aGTA,
73 String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
74 throws ConvWatchCancelException
76 // TODO: some more checks
78 if (! FileHelper.exists(_sAbsoluteInputFile))
80 throw new ConvWatchCancelException("createPostscriptStartCheck: Input file: " + _sAbsoluteInputFile + " does not exist.");
83 if (_sAbsoluteReferenceFile == null)
85 // we assume, that the prn file is near the document
86 String sAbsoluteInputFileNoSuffix = FileHelper.getNameNoSuffix(_sAbsoluteInputFile);
87 _sAbsoluteReferenceFile = sAbsoluteInputFileNoSuffix + ".prn";
90 String fs = System.getProperty("file.separator");
91 File aAbsoluteReferenceFile = new File(_sAbsoluteReferenceFile);
93 if (aAbsoluteReferenceFile.isDirectory())
95 String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
96 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
97 String sAbsoluteReferenceFileInfo = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".info";
98 _sAbsoluteReferenceFile = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".prn";
100 // Read the reference from the info file
101 String sRefBuildID = "";
102 if (FileHelper.exists(sAbsoluteReferenceFileInfo))
104 sRefBuildID = getBuildID_FromFile(sAbsoluteReferenceFileInfo);
106 _aGTA.setRefBuildID(sRefBuildID);
109 else
111 // java file has problems to check for directories, if the given directory doesn't exist.
112 String sName = FileHelper.getBasename(_sAbsoluteReferenceFile);
113 // thanks to Mircosoft, every document has a suffix, so if a name doesn't have a suffix, it must be a directory name
114 int nIdx = sName.indexOf('.');
115 if (nIdx == -1)
117 // must be a directory
118 throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference directory: '" + _sAbsoluteReferenceFile + "' does not exist.");
123 boolean bAbsoluteReferenceFile = true;
124 if (! FileHelper.exists(_sAbsoluteReferenceFile))
126 if (_aGTA.createDefaultReference())
128 GlobalLogWriter.get().println("Reference File doesn't exist, will create a default");
129 bAbsoluteReferenceFile = false;
131 else
133 throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist.");
137 FileHelper.makeDirectories("", _sOutputPath);
139 // runner.convwatch.compare();
141 String sAbsoluteInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
143 String sInputFile = FileHelper.getBasename(_sAbsoluteInputFile);
144 // System.out.println("InputFile: " + sInputFile);
146 String sInputFileNoSuffix = FileHelper.getNameNoSuffix(sInputFile);
147 // System.out.println("BasenameNoSuffix: " + sInputFileNoSuffix);
150 String sAbsoluteOutputFile = _sOutputPath + fs + sInputFile;
151 String sAbsoluteOutputFileURL = URLHelper.getFileURLFromSystemPath(sAbsoluteOutputFile);
153 String sReferenceFile = FileHelper.getBasename(_sAbsoluteReferenceFile);
154 String sReferenceFileNoSuffix = FileHelper.getNameNoSuffix(sReferenceFile);
155 String sPostScriptFile = sReferenceFileNoSuffix + ".ps";
156 // System.out.println("PostscriptFile: " + sPostScriptFile);
158 String sAbsolutePrintFile = _sOutputPath + fs + sPostScriptFile;
159 String sAbsolutePrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFile);
161 // System.out.println("AbsoluteInputFileURL: " + sAbsoluteInputFileURL);
162 // System.out.println("AbsoluteOutputFileURL: " + sAbsoluteOutputFileURL);
163 // System.out.println("AbsolutePrintFileURL: " + sAbsolutePrintFileURL);
165 // store and print the sAbsoluteInputFileURL file with StarOffice / OpenOffice.org
166 OfficePrint.printToFile(_aGTA, sAbsoluteInputFileURL, sAbsoluteOutputFileURL, sAbsolutePrintFileURL);
168 // wait(2);
170 if (! FileHelper.exists(sAbsolutePrintFile))
172 throw new ConvWatchCancelException("createPostscriptStartCheck: Printed file " + sAbsolutePrintFile + " does not exist.");
175 if (bAbsoluteReferenceFile == false)
177 // copy AbsolutePrintFile to AbsoluteReferenceFile
178 String sDestinationFile = sAbsolutePrintFile; // URLHelper.getSystemPathFromFileURL(...)
179 String sSourceFile = _sAbsoluteReferenceFile;
180 FileHelper.copy(sDestinationFile, sSourceFile);
181 // now the fix reference of the AbsoluteReferenceFile should exist.
182 if (! FileHelper.exists(_sAbsoluteReferenceFile))
184 throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist, after try to copy.");
188 PRNCompare a = new PRNCompare();
189 String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
190 String sReferencePath = FileHelper.getPath(_sAbsoluteReferenceFile);
191 // String sReferenceFile = FileHelper.getBasename(sAbsoluteReferenceFile);
193 // System.out.println("InputPath: " + sInputPath);
194 // System.out.println("sReferencePath: " + sReferencePath);
195 // System.out.println("sReferenceFile: " + sReferenceFile);
197 a.setInputPath( sInputPath );
198 a.setReferencePath( sReferencePath );
199 a.setOutputPath( _sOutputPath );
200 // a.setDocFile( "1_Gov.ppt");
201 a.setReferenceFile( sReferenceFile );
202 a.setPostScriptFile(sPostScriptFile );
203 if (_aGTA.printAllPages() == true)
205 a.setMaxPages(9999);
207 else
209 if (_aGTA.getMaxPages() > 0)
211 a.setMaxPages(_aGTA.getMaxPages());
213 if (_aGTA.getOnlyPages().length() != 0)
215 // we can't interpret the string of getOnlyPages() right without much logic, so print all pages here!
216 a.setMaxPages(9999);
220 a.setResolutionInDPI(_aGTA.getResolutionInDPI());
221 a.setBorderMove(_aGTA.getBorderMove());
222 a.setDocumentType(_aGTA.getDocumentType());
224 StatusHelper[] aList = a.compare();
226 _aGTA.setBorderMove(a.getBorderMove());
227 return aList;
230 // -----------------------------------------------------------------------------
231 // This creates a status for exact on document
232 static boolean createINIStatus(StatusHelper[] aList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID, String _sRefBuildID)
234 // Status
235 String fs = System.getProperty("file.separator");
236 String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
237 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
238 // String sHTMLFile = _sFilenamePrefix + sNameNoSuffix + ".html";
239 // HTMLOutputter HTMLoutput = HTMLOutputter.create(_sOutputPath, sHTMLFile, "", "");
240 // HTMLoutput.header(sNameNoSuffix);
241 // TODO: version info was fine
242 // HTMLoutput.checkSection(sBasename);
243 // Status end
245 String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
246 INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, "", "");
247 INIoutput.createHeader();
248 // TODO: version info was fine
250 INIoutput.writeSection("global");
251 INIoutput.writeValue("pages", String.valueOf(aList.length));
252 INIoutput.writeValue("buildid", _sBuildID);
253 INIoutput.writeValue("refbuildid", _sRefBuildID);
254 INIoutput.writeValue("diffdiff", "no");
255 INIoutput.writeValue("basename", sBasename);
257 boolean bResultIsOk = true; // result over all pages
258 for (int i=0;i<aList.length; i++)
260 INIoutput.writeSection("page" + String.valueOf(i + 1)); // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
261 aList[i].printStatus();
263 boolean bCurrentResult = true; // result over exact one page
265 int nCurrentDiffStatus = aList[i].nDiffStatus;
267 // check if the status is in a defined range
268 if (nCurrentDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES)
270 // ok.
272 else if (nCurrentDiffStatus == StatusHelper.DIFF_DIFFERENCES_FOUND && aList[i].nPercent < 5)
274 // ok.
276 else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS)
278 // ok.
280 else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND && aList[i].nPercent2 < 5)
282 // ok.
284 else
286 // failed.
287 bCurrentResult = false; // logic: nDiff==0 = true if there is no difference
290 // Status
291 // HTMLoutput.checkLine(aList[i], bCurrentResult);
292 INIoutput.checkLine(aList[i], bCurrentResult);
293 bResultIsOk &= bCurrentResult;
295 // Status
296 // HTMLoutput.close();
297 INIoutput.close();
298 return bResultIsOk;
301 // -----------------------------------------------------------------------------
303 static void createINIStatus_DiffDiff(StatusHelper[] aDiffDiffList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID)
305 // Status
306 String fs = System.getProperty("file.separator");
307 String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
308 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
309 String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
311 // HTMLOutputter HTMLoutput = HTMLOutputter.create(_sOutputPath, sHTMLFile, _sFilenamePrefix, "");
312 // HTMLoutput.header(sNameNoSuffix);
313 // HTMLoutput.checkDiffDiffSection(sBasename);
315 INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, _sFilenamePrefix, "");
316 INIoutput.createHeader();
317 // LLA? what if the are no values in the list? true or false;
318 INIoutput.writeSection("global");
319 INIoutput.writeValue("pages", String.valueOf(aDiffDiffList.length));
320 INIoutput.writeValue("buildid", _sBuildID);
321 INIoutput.writeValue("diffdiff", "yes");
322 INIoutput.writeValue("basename", sBasename);
324 for (int i=0;i<aDiffDiffList.length; i++)
326 INIoutput.writeSection("page" + String.valueOf(i + 1)); // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
327 boolean bCurrentResult = (aDiffDiffList[i].nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference
329 // HTMLoutput.checkDiffDiffLine(aDiffDiffList[i], bCurrentResult);
330 INIoutput.checkDiffDiffLine(aDiffDiffList[i], bCurrentResult);
332 // Status
333 // HTMLoutput.close();
334 INIoutput.close();
338 // -----------------------------------------------------------------------------
340 public static boolean check(GraphicalTestArguments _aGTA,
341 String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
342 throws ConvWatchCancelException, ConvWatchException
344 ConvWatch a = new ConvWatch();
345 StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);
346 DB.writeNumberOfPages(aList.length);
348 boolean bResultIsOk = createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());
350 if (! bResultIsOk)
352 // it could be that this will store in a DB, there are problems with '\'
353 String sErrorMessage = "Graphical compare failed with file ";
354 String sErrorFile = _sAbsoluteInputFile.replace('\\', '/');
355 sErrorMessage = sErrorMessage + "'" + sErrorFile + "'";
356 DB.writeErrorFile(sErrorFile);
357 throw new ConvWatchException(sErrorMessage);
359 return bResultIsOk;
362 // -----------------------------------------------------------------------------
363 public static boolean checkDiffDiff(GraphicalTestArguments _aGTA,
364 String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile,
365 String _sAbsoluteDiffPath)
366 throws ConvWatchCancelException, ConvWatchException
368 ConvWatch a = new ConvWatch();
369 _aGTA.setBorderMove(TriState.FALSE);
370 StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);
372 // Status
373 boolean bResultIsOk = createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());
375 StatusHelper[] aDiffDiffList = new StatusHelper[aList.length];
377 String fs = System.getProperty("file.separator");
379 boolean bDiffIsOk = true;
380 boolean bFoundAOldDiff = false;
382 PRNCompare aCompare = new PRNCompare();
383 // LLA? what if the are no values in the list? true or false;
384 for (int i=0;i<aList.length; i++)
386 String sOrigDiffName = aList[i].m_sDiffGfx;
387 String sDiffBasename = FileHelper.getBasename(sOrigDiffName);
389 String sNewDiffName = _sAbsoluteDiffPath + fs + sDiffBasename;
390 if (! FileHelper.exists(sNewDiffName))
392 GlobalLogWriter.get().println("checkDiffDiff: Old diff file: '" + sNewDiffName + "' does not exist." );
393 continue;
395 // String sNewDiffName = _sAbsoluteDiffPath + fs + sDiffBasename;
397 // make a simple difference between these both diff files.
398 String sSourcePath1 = FileHelper.getPath(sOrigDiffName);
399 String sSourceFile1 = sDiffBasename;
400 String sSourcePath2 = _sAbsoluteDiffPath;
401 String sSourceFile2 = sDiffBasename;
403 StatusHelper aCurrentStatus = aCompare.checkDiffDiff(_sOutputPath, sSourcePath1, sSourceFile1, sSourcePath2, sSourceFile2);
404 boolean bCurrentResult = (aCurrentStatus.nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference
405 bDiffIsOk &= bCurrentResult;
406 bFoundAOldDiff = true;
408 aDiffDiffList[i] = aCurrentStatus;
411 createINIStatus_DiffDiff(aDiffDiffList, "DiffDiff_", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID());
413 if (bFoundAOldDiff == false)
415 throw new ConvWatchCancelException("No old difference file found." );
417 if (! bDiffIsOk)
419 throw new ConvWatchException("Graphical difference compare failed with file '" + _sAbsoluteInputFile + "'");
421 return bDiffIsOk;
424 // public static void main( String[] argv )
425 // {
426 // PRNCompare a = new PRNCompare();
427 // a.setInputPath( "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
428 // a.setDocFile( "1_Gov.ppt");
429 // a.setReferencePath( "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
430 // a.setReferenceFile( "1_Gov.prn" );
432 // a.setOutputPath( "/tmp/convwatch_java");
433 // a.setPostScriptFile("1_Gov.ps" );
434 // }