1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 package convwatch;
29 
30 import convwatch.ConvWatchException;
31 import convwatch.ConvWatchCancelException;
32 import convwatch.FileHelper;
33 import convwatch.OfficePrint;
34 import convwatch.PRNCompare;
35 import convwatch.StatusHelper;
36 import helper.URLHelper;
37 import java.io.File;
38 
39 public class ConvWatch
40 {
41 
42     String getBuildID_FromFile(String _sInfoFile)
43     {
44         String sBuildID = "";
45         IniFile aIniFile = new IniFile(_sInfoFile);
46         if (aIniFile.is())
47         {
48             sBuildID = aIniFile.getValue("", "buildid");
49         }
50         return sBuildID;
51     }
52     /**
53      * Check if given document (_sAbsoluteInputFile) and it's postscript representation (_sAbsoluteReferenceFile) produce
54      * the same output like the StarOffice / OpenOffice.org which is accessable with XMultiServiceFactory.
55      * Here a simple graphically difference check is run through.
56      *
57      * Hint: In the OutputPath all needed files will create, there must
58      * be very much space. It's not possible to say how much.
59      * One page need up to 800kb as jpeg.
60      * Sample: If a document contains 2 pages, we need 2*800kb for prn
61      * output and 2*800kb for ps output and 2*800kb for it's difference
62      * output. So up to 4800kb or 4.8mb.
63      *
64      * RAM is need least 300mb. Will say, it's tested on a pc with 128mb RAM and 256mb swap.
65      *
66      * It's also absolutlly impossible to say, how much time this functions consume.
67      */
68 
69     StatusHelper[] createPostscriptStartCheck(GraphicalTestArguments _aGTA,
70                                               String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
71         throws ConvWatchCancelException
72         {
73 //  TODO: some more checks
74 
75             if (! FileHelper.exists(_sAbsoluteInputFile))
76             {
77                 throw new ConvWatchCancelException("createPostscriptStartCheck: Input file: " + _sAbsoluteInputFile + " does not exist.");
78             }
79 
80             if (_sAbsoluteReferenceFile == null)
81             {
82                 // we assume, that the prn file is near the document
83                 String sAbsoluteInputFileNoSuffix = FileHelper.getNameNoSuffix(_sAbsoluteInputFile);
84                 _sAbsoluteReferenceFile = sAbsoluteInputFileNoSuffix + ".prn";
85             }
86 
87             String fs = System.getProperty("file.separator");
88             File aAbsoluteReferenceFile = new File(_sAbsoluteReferenceFile);
89 
90             if (aAbsoluteReferenceFile.isDirectory())
91             {
92                 String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
93                 String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
94                 String sAbsoluteReferenceFileInfo = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".info";
95                 _sAbsoluteReferenceFile = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".prn";
96 
97                 // Read the reference from the info file
98                 String sRefBuildID = "";
99                 if (FileHelper.exists(sAbsoluteReferenceFileInfo))
100                 {
101                     sRefBuildID = getBuildID_FromFile(sAbsoluteReferenceFileInfo);
102                 }
103                 _aGTA.setRefBuildID(sRefBuildID);
104 
105             }
106             else
107             {
108                 // java file has problems to check for directories, if the given directory doesn't exist.
109                 String sName = FileHelper.getBasename(_sAbsoluteReferenceFile);
110                 // thanks to Mircosoft, every document has a suffix, so if a name doesn't have a suffix, it must be a directory name
111                 int nIdx = sName.indexOf('.');
112                 if (nIdx == -1)
113                 {
114                     // must be a directory
115                     throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference directory: '" + _sAbsoluteReferenceFile + "' does not exist.");
116                 }
117             }
118 
119 
120             boolean bAbsoluteReferenceFile = true;
121             if (! FileHelper.exists(_sAbsoluteReferenceFile))
122             {
123                 if (_aGTA.createDefaultReference())
124                 {
125                     GlobalLogWriter.get().println("Reference File doesn't exist, will create a default");
126                     bAbsoluteReferenceFile = false;
127                 }
128                 else
129                 {
130                     throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist.");
131                 }
132             }
133 
134             FileHelper.makeDirectories("", _sOutputPath);
135 
136             // runner.convwatch.compare();
137 
138             String sAbsoluteInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);
139 
140             String sInputFile = FileHelper.getBasename(_sAbsoluteInputFile);
141             // System.out.println("InputFile: " + sInputFile);
142 
143             String sInputFileNoSuffix = FileHelper.getNameNoSuffix(sInputFile);
144             // System.out.println("BasenameNoSuffix: " + sInputFileNoSuffix);
145 
146 
147             String sAbsoluteOutputFile = _sOutputPath + fs + sInputFile;
148             String sAbsoluteOutputFileURL = URLHelper.getFileURLFromSystemPath(sAbsoluteOutputFile);
149 
150             String sReferenceFile = FileHelper.getBasename(_sAbsoluteReferenceFile);
151             String sReferenceFileNoSuffix = FileHelper.getNameNoSuffix(sReferenceFile);
152             String sPostScriptFile = sReferenceFileNoSuffix + ".ps";
153             // System.out.println("PostscriptFile: " + sPostScriptFile);
154 
155             String sAbsolutePrintFile = _sOutputPath + fs + sPostScriptFile;
156             String sAbsolutePrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFile);
157 
158             // System.out.println("AbsoluteInputFileURL: " + sAbsoluteInputFileURL);
159             // System.out.println("AbsoluteOutputFileURL: " + sAbsoluteOutputFileURL);
160             // System.out.println("AbsolutePrintFileURL: " + sAbsolutePrintFileURL);
161 
162             // store and print the sAbsoluteInputFileURL file with StarOffice / OpenOffice.org
163             OfficePrint.printToFile(_aGTA, sAbsoluteInputFileURL, sAbsoluteOutputFileURL, sAbsolutePrintFileURL);
164 
165             // wait(2);
166 
167             if (! FileHelper.exists(sAbsolutePrintFile))
168             {
169                 throw new ConvWatchCancelException("createPostscriptStartCheck: Printed file " + sAbsolutePrintFile + " does not exist.");
170             }
171 
172             if (bAbsoluteReferenceFile == false)
173             {
174                 // copy AbsolutePrintFile to AbsoluteReferenceFile
175                 String sDestinationFile = sAbsolutePrintFile; // URLHelper.getSystemPathFromFileURL(...)
176                 String sSourceFile = _sAbsoluteReferenceFile;
177                 FileHelper.copy(sDestinationFile, sSourceFile);
178                 // now the fix reference of the AbsoluteReferenceFile should exist.
179                 if (! FileHelper.exists(_sAbsoluteReferenceFile))
180                 {
181                     throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist, after try to copy.");
182                 }
183             }
184 
185             PRNCompare a = new PRNCompare();
186             String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
187             String sReferencePath = FileHelper.getPath(_sAbsoluteReferenceFile);
188             // String sReferenceFile = FileHelper.getBasename(sAbsoluteReferenceFile);
189 
190             // System.out.println("InputPath: " + sInputPath);
191             // System.out.println("sReferencePath: " + sReferencePath);
192             // System.out.println("sReferenceFile: " + sReferenceFile);
193 
194             a.setInputPath(     sInputPath );
195             a.setReferencePath( sReferencePath );
196             a.setOutputPath(    _sOutputPath );
197             // a.setDocFile(       "1_Gov.ppt");
198             a.setReferenceFile( sReferenceFile );
199             a.setPostScriptFile(sPostScriptFile );
200             if (_aGTA.printAllPages() == true)
201             {
202                 a.setMaxPages(9999);
203             }
204             else
205             {
206                 if (_aGTA.getMaxPages() > 0)
207                 {
208                     a.setMaxPages(_aGTA.getMaxPages());
209                 }
210                 if (_aGTA.getOnlyPages().length() != 0)
211                 {
212                     // we can't interpret the string of getOnlyPages() right without much logic, so print all pages here!
213                     a.setMaxPages(9999);
214                 }
215             }
216 
217             a.setResolutionInDPI(_aGTA.getResolutionInDPI());
218             a.setBorderMove(_aGTA.getBorderMove());
219             a.setDocumentType(_aGTA.getDocumentType());
220 
221             StatusHelper[] aList = a.compare();
222 
223             _aGTA.setBorderMove(a.getBorderMove());
224             return aList;
225         }
226 
227     // -----------------------------------------------------------------------------
228     // This creates a status for exact on document
229     static boolean createINIStatus(StatusHelper[] aList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID, String _sRefBuildID)
230         {
231             // Status
232             String fs = System.getProperty("file.separator");
233             String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
234             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
235 //            String sHTMLFile = _sFilenamePrefix + sNameNoSuffix + ".html";
236 //            HTMLOutputter HTMLoutput = HTMLOutputter.create(_sOutputPath, sHTMLFile, "", "");
237 //            HTMLoutput.header(sNameNoSuffix);
238 //  TODO: version info was fine
239 //            HTMLoutput.checkSection(sBasename);
240             // Status end
241 
242             String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
243             INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, "", "");
244             INIoutput.createHeader();
245 //  TODO: version info was fine
246 
247             INIoutput.writeSection("global");
248             INIoutput.writeValue("pages", String.valueOf(aList.length));
249             INIoutput.writeValue("buildid", _sBuildID);
250             INIoutput.writeValue("refbuildid", _sRefBuildID);
251             INIoutput.writeValue("diffdiff", "no");
252             INIoutput.writeValue("basename", sBasename);
253 
254             boolean bResultIsOk = true;          // result over all pages
255             for (int i=0;i<aList.length; i++)
256             {
257                 INIoutput.writeSection("page" + String.valueOf(i + 1));   // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
258                 aList[i].printStatus();
259 
260                 boolean bCurrentResult = true;   // result over exact one page
261 
262                 int nCurrentDiffStatus = aList[i].nDiffStatus;
263 
264                 // check if the status is in a defined range
265                 if (nCurrentDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES)
266                 {
267                     // ok.
268                 }
269                 else if (nCurrentDiffStatus == StatusHelper.DIFF_DIFFERENCES_FOUND && aList[i].nPercent < 5)
270                 {
271                     // ok.
272                 }
273                 else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS)
274                 {
275                     // ok.
276                 }
277                 else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND && aList[i].nPercent2 < 5)
278                 {
279                     // ok.
280                 }
281                 else
282                 {
283                     // failed.
284                     bCurrentResult = false; // logic: nDiff==0 = true if there is no difference
285                 }
286 
287                 // Status
288 //                HTMLoutput.checkLine(aList[i], bCurrentResult);
289                 INIoutput.checkLine(aList[i], bCurrentResult);
290                 bResultIsOk &= bCurrentResult;
291             }
292             // Status
293 //            HTMLoutput.close();
294             INIoutput.close();
295             return bResultIsOk;
296         }
297 
298     // -----------------------------------------------------------------------------
299 
300     static void createINIStatus_DiffDiff(StatusHelper[] aDiffDiffList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID)
301         {
302             // Status
303             String fs = System.getProperty("file.separator");
304             String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
305             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
306             String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
307 
308 //            HTMLOutputter HTMLoutput = HTMLOutputter.create(_sOutputPath, sHTMLFile, _sFilenamePrefix, "");
309 //            HTMLoutput.header(sNameNoSuffix);
310 //            HTMLoutput.checkDiffDiffSection(sBasename);
311 
312             INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, _sFilenamePrefix, "");
313             INIoutput.createHeader();
314             // LLA? what if the are no values in the list? true or false;
315             INIoutput.writeSection("global");
316             INIoutput.writeValue("pages", String.valueOf(aDiffDiffList.length));
317             INIoutput.writeValue("buildid", _sBuildID);
318             INIoutput.writeValue("diffdiff", "yes");
319             INIoutput.writeValue("basename", sBasename);
320 
321             for (int i=0;i<aDiffDiffList.length; i++)
322             {
323                 INIoutput.writeSection("page" + String.valueOf(i + 1));   // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
324                 boolean bCurrentResult = (aDiffDiffList[i].nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference
325 
326 //                HTMLoutput.checkDiffDiffLine(aDiffDiffList[i], bCurrentResult);
327                 INIoutput.checkDiffDiffLine(aDiffDiffList[i], bCurrentResult);
328             }
329             // Status
330 //            HTMLoutput.close();
331             INIoutput.close();
332         }
333 
334 
335     // -----------------------------------------------------------------------------
336 
337     public static boolean check(GraphicalTestArguments _aGTA,
338                              String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
339         throws ConvWatchCancelException, ConvWatchException
340         {
341             ConvWatch a = new ConvWatch();
342             StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);
343             DB.writeNumberOfPages(aList.length);
344 
345             boolean bResultIsOk = createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());
346 
347             if (! bResultIsOk)
348             {
349                 // it could be that this will store in a DB, there are problems with '\'
350                 String sErrorMessage = "Graphical compare failed with file ";
351                 String sErrorFile = _sAbsoluteInputFile.replace('\\', '/');
352                 sErrorMessage = sErrorMessage + "'" + sErrorFile + "'";
353                 DB.writeErrorFile(sErrorFile);
354                 throw new ConvWatchException(sErrorMessage);
355             }
356             return bResultIsOk;
357         }
358 
359     // -----------------------------------------------------------------------------
360     public static boolean checkDiffDiff(GraphicalTestArguments _aGTA,
361                                      String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile,
362                                      String _sAbsoluteDiffPath)
363         throws ConvWatchCancelException, ConvWatchException
364         {
365             ConvWatch a = new ConvWatch();
366             _aGTA.setBorderMove(TriState.FALSE);
367             StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);
368 
369             // Status
370             boolean bResultIsOk = createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());
371 
372             StatusHelper[] aDiffDiffList = new StatusHelper[aList.length];
373 
374             String fs = System.getProperty("file.separator");
375 
376             boolean bDiffIsOk = true;
377             boolean bFoundAOldDiff = false;
378 
379             PRNCompare aCompare = new PRNCompare();
380             // LLA? what if the are no values in the list? true or false;
381             for (int i=0;i<aList.length; i++)
382             {
383                 String sOrigDiffName = aList[i].m_sDiffGfx;
384                 String sDiffBasename = FileHelper.getBasename(sOrigDiffName);
385 
386                 String sNewDiffName = _sAbsoluteDiffPath + fs + sDiffBasename;
387                 if (! FileHelper.exists(sNewDiffName))
388                 {
389                     GlobalLogWriter.get().println("checkDiffDiff: Old diff file: '" + sNewDiffName + "' does not exist." );
390                     continue;
391                 }
392                 // String sNewDiffName = _sAbsoluteDiffPath + fs + sDiffBasename;
393 
394                 // make a simple difference between these both diff files.
395                 String sSourcePath1 = FileHelper.getPath(sOrigDiffName);
396                 String sSourceFile1 = sDiffBasename;
397                 String sSourcePath2 = _sAbsoluteDiffPath;
398                 String sSourceFile2 = sDiffBasename;
399 
400                 StatusHelper aCurrentStatus = aCompare.checkDiffDiff(_sOutputPath, sSourcePath1, sSourceFile1, sSourcePath2, sSourceFile2);
401                 boolean bCurrentResult = (aCurrentStatus.nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference
402                 bDiffIsOk &= bCurrentResult;
403                 bFoundAOldDiff = true;
404 
405                 aDiffDiffList[i] = aCurrentStatus;
406             }
407 
408             createINIStatus_DiffDiff(aDiffDiffList, "DiffDiff_", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID());
409 
410             if (bFoundAOldDiff == false)
411             {
412                 throw new ConvWatchCancelException("No old difference file found." );
413             }
414             if (! bDiffIsOk)
415             {
416                 throw new ConvWatchException("Graphical difference compare failed with file '" + _sAbsoluteInputFile + "'");
417             }
418             return bDiffIsOk;
419         }
420 
421     // public static void main( String[] argv )
422     //     {
423     //         PRNCompare a = new PRNCompare();
424     //         a.setInputPath(     "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
425     //         a.setDocFile(       "1_Gov.ppt");
426     //         a.setReferencePath( "/cws/so-cwsserv06/qadev18/SRC680/src.m47/convwatch.keep/input/msoffice/xp/PowerPoint");
427     //         a.setReferenceFile( "1_Gov.prn" );
428     //
429     //         a.setOutputPath(    "/tmp/convwatch_java");
430     //         a.setPostScriptFile("1_Gov.ps" );
431     //     }
432 }
433