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