1 /*
2  * ************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org.  If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  * ***********************************************************************
28  */
29 
30 package graphical;
31 
32 import complexlib.ComplexTestCase;
33 import java.io.File;
34 import java.io.FileFilter;
35 import java.util.ArrayList;
36 
37 /**
38  *
39  * @author ll93751
40  */
41 abstract public class EnhancedComplexTestCase extends ComplexTestCase implements IDocument
42 {
43 
44 
45 private void callEntry(String _sEntry, ParameterHelper _aParam)
46 {
47     // log.println("- next file is: ------------------------------");
48     log.println("      File: " + _sEntry);
49     // TODO: check if 'sEntry' is a guilty document.
50     File aFile = new File(_aParam.getInputPath());
51     String sPath = _aParam.getInputPath();
52     // problem here, isFile() checks also if the file exists, but a not existing file is not really a directory
53     // therefore we check if the given file a path (isDirectory()) if not it must be a file
54     if (aFile.isDirectory())
55     {
56     }
57     else
58     {
59         // special case, if a file is given in inputpath
60         sPath = FileHelper.getPath(_aParam.getInputPath());
61     }
62     String sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(_sEntry, sPath);
63 
64 //    String sNewReferencePath = _aParam.getReferencePath();
65     String sNewOutputPath = _aParam.getOutputPath();
66     // String sNewDiffPath = m_sDiffPath;
67 
68     // if there exist a subdirectory, add it to all result path
69     if (sNewSubDir.length() > 0)
70     {
71 //        if (sNewReferencePath != null)
72 //        {
73 //            sNewReferencePath = FileHelper.appendPath(sNewReferencePath, sNewSubDir);
74 //        }
75 
76         sNewOutputPath = FileHelper.appendPath(sNewOutputPath, sNewSubDir);
77     //                        if (sNewDiffPath != null)
78     //                        {
79     //                            sNewDiffPath = FileHelper.appendPath(sNewDiffPath, sNewSubDir);
80     //                        }
81     }
82     // log.println("sEntry: " + _sEntry + " " /* + sNewReferencePath + " " */ + sNewOutputPath);
83     log.println("Outputpath: " + sNewOutputPath);
84 
85 
86     // call interface with parameters
87     try
88     {
89         checkOneFile(_sEntry, sNewOutputPath, _aParam);
90     }
91     catch (OfficeException e)
92     {
93         // TODO: unhandled yet.
94         GlobalLogWriter.println("Warning: caught OfficeException " + e.getMessage());
95         assure("Exception caught: " + e.getMessage(), false);
96     }
97 
98 }
99 
100 /**
101  * Run through all documents found in Inputpath.
102  * Call the IDocument interface function call(...);
103  * @param _aParam
104  */
105     public void foreachDocumentinInputPath(ParameterHelper _aParam)
106     {
107                 // TODO: auslagern in eine function, die ein Interface annimmt.
108         File aInputPath = new File(_aParam.getInputPath());
109         if (aInputPath.isDirectory())
110         {
111             // check a whole directory
112             // a whole directory
113             FileFilter aFileFilter = FileHelper.getFileFilter();
114             traverseDirectory(aFileFilter, _aParam);
115         }
116         else
117         {
118             callEntry(_aParam.getInputPath(), _aParam);
119         }
120     }
121 
122     private void traverseDirectory(FileFilter _aFileFilter, ParameterHelper _aParam)
123     {
124         Object[] aList = DirectoryHelper.traverse(_aParam.getInputPath(), _aFileFilter, _aParam.isIncludeSubDirectories());
125         if (aList.length == 0)
126         {
127             log.println("Nothing to do, there are no document files found.");
128         }
129         else
130         {
131             for (int i=0;i<aList.length;i++)
132             {
133                 String sEntry = (String)aList[i];
134                 callEntry(sEntry, _aParam);
135             }
136         }
137     }
138 /**
139  * Run through a given index.ini or run through a given directory,
140  * find all postscript or pdf files.
141  * Call the IDocument interface function call(...);
142  * @param _aParam
143  */
144     public void foreachPSorPDFinInputPath(ParameterHelper _aParam)
145     {
146         // TODO: auslagern in eine function, die ein Interface annimmt.
147         String sInputPath = _aParam.getInputPath();
148         File aInputPath = new File(sInputPath);
149 //        if (!aInputPath.exists())
150 //        {
151 //            GlobalLogWriter.println("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'");
152 //            assure("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'", false);
153 //        }
154         if (aInputPath.isDirectory())
155         {
156             // check a whole directory
157             // a whole directory
158             FileFilter aFileFilter = FileHelper.getFileFilterPSorPDF();
159             traverseDirectory(aFileFilter, _aParam);
160         }
161         else
162         {
163             // the inputpath contains a file
164             if (sInputPath.toLowerCase().endsWith(".ini"))
165             {
166                 IniFile aIniFile = new IniFile(_aParam.getInputPath());
167                 while (aIniFile.hasMoreElements())
168                 {
169                     String sKey = (String)aIniFile.nextElement();
170                     String sPath = FileHelper.getPath(_aParam.getInputPath());
171                     String sEntry = FileHelper.appendPath(sPath, sKey);
172                     File aFile = new File(sEntry);
173                     assure("File '" + sEntry + "' doesn't exists.", aFile.exists(), true);
174                     if (aFile.exists())
175                     {
176                         callEntry(sEntry, _aParam);
177                     }
178                 }
179             }
180             else
181             {
182                 // call for a single pdf/ps file
183                 if (sInputPath.toLowerCase().endsWith(".ps") ||
184                     sInputPath.toLowerCase().endsWith(".pdf") ||
185                     sInputPath.toLowerCase().endsWith(".prn"))
186                 {
187                     callEntry(sInputPath, _aParam);
188                 }
189                 else
190                 {
191                     String sInputPathWithPDF = sInputPath + ".pdf";
192                     File aInputPathWithPDF = new File(sInputPathWithPDF);
193 
194                     if (aInputPathWithPDF.exists() &&
195                         _aParam.getReferenceType().toLowerCase().equals("pdf"))
196                     {
197                         // create PDF only if a pdf file exists and creatortype is set to PDF
198                         callEntry(sInputPathWithPDF, _aParam);
199                     }
200                     else
201                     {
202                         String sInputPathWithPS = sInputPath + ".ps";
203 
204                         File aInputPathWithPS = new File(sInputPathWithPS);
205                         if (aInputPathWithPS.exists())
206                         {
207                             callEntry(sInputPathWithPS, _aParam);
208                         }
209                         else
210                         {
211                             String sPath = FileHelper.getPath(sInputPath);
212                             String sBasename = FileHelper.getBasename(sInputPath);
213 
214                             // there exist an index file, therefore we assume the given
215                             // file is already converted to postscript or pdf
216                             runThroughEveryReportInIndex(sPath, sBasename, _aParam);
217                         }
218                     }
219                 }
220             }
221         }
222     }
223 
224     private void runThroughEveryReportInIndex(String _sPath, String _sBasename, ParameterHelper _aParam)
225     {
226         String sIndexFile = FileHelper.appendPath(_sPath, "index.ini");
227         File aIndexFile = new File(sIndexFile);
228         if (aIndexFile.exists())
229         {
230             IniFile aIniFile = new IniFile(sIndexFile);
231 
232             if (aIniFile.hasSection(_sBasename))
233             {
234                 // special case for odb files
235                 int nFileCount = aIniFile.getIntValue(_sBasename, "reportcount", 0);
236                 ArrayList<String> aList = new ArrayList<String>();
237                 for (int i=0;i<nFileCount;i++)
238                 {
239                     String sValue = aIniFile.getValue(_sBasename, "report" + i);
240 
241                     String sPSorPDFName = getPSorPDFNameFromIniFile(aIniFile, sValue);
242                     if (sPSorPDFName.length() > 0)
243                     {
244                         String sEntry = FileHelper.appendPath(_sPath, sPSorPDFName);
245                         aList.add(sEntry);
246                     }
247                 }
248                 aIniFile.close();
249 
250                 int nOkStatus = 0;
251                 String sStatusRunThrough = "";
252                 String sStatusInfo = "";
253                 // get the bad status and store it into the
254                 for (int i=0;i<aList.size();i++)
255                 {
256                     String sEntry = aList.get(i);
257                     try
258                     {
259                         callEntry(sEntry, _aParam);
260                     }
261                     catch (AssureException e)
262                     {
263                         // we only need to catch the assure()
264                         // nOkStatus += 2;
265                     }
266                     // we want to know the current status of the run through
267                     // if the status is greater (more bad) then the current,
268                     // we will remember this. Only the very bad status will
269                     // seen.
270                     int nCurrentOkStatus = _aParam.getTestParameters().getInt("current_ok_status");
271                     if (nCurrentOkStatus > nOkStatus)
272                     {
273                         sStatusRunThrough = (String)_aParam.getTestParameters().get("current_state");
274                         sStatusInfo = (String)_aParam.getTestParameters().get("current_info");
275                         nOkStatus = nCurrentOkStatus;
276                     }
277                 }
278                 if (nOkStatus > 0)
279                 {
280                     _aParam.getTestParameters().put("last_state", sStatusRunThrough);
281                     _aParam.getTestParameters().put("last_info", sStatusInfo);
282                 }
283             }
284             else
285             {
286                 // runThroughOneFileInIndex();
287                 String sPSorPDFName = getPSorPDFNameFromIniFile(aIniFile, _sBasename);
288 
289                 aIniFile.close();
290 
291                 if (sPSorPDFName.length() > 0)
292                 {
293                     String sEntry = FileHelper.appendPath(_sPath, sPSorPDFName);
294                     callEntry(sEntry, _aParam);
295                 }
296             }
297         }
298         else
299         {
300              assure("File '" + sIndexFile + "' doesn't exists.", aIndexFile.exists(), true);
301         }
302     }
303 
304     private String getPSorPDFNameFromIniFile(IniFile _aIniFile, String _sName)
305     {
306         boolean bHasPostscriptOrPDF = false;
307         String sPSBasename = _sName + ".ps";
308         if (_aIniFile.hasSection(sPSBasename))       // checks for Postscript
309         {
310             bHasPostscriptOrPDF = true;
311         }
312         else
313         {
314             sPSBasename = _sName + ".pdf";       // checks for PDF
315             if (_aIniFile.hasSection(sPSBasename))
316             {
317                 bHasPostscriptOrPDF = true;
318             }
319         }
320         if (bHasPostscriptOrPDF)
321         {
322             return sPSBasename;
323         }
324         return "";
325     }
326 
327     public void runThroughOneFileInIndex(String _sPath, String _sBasename, ParameterHelper _aParam)
328     {
329 
330     }
331 /**
332  * Run through a given index.ini or run through a given directory,
333  * find all postscript or pdf files.
334  * Call the IDocument interface function call(...);
335  * @param _aParam
336  */
337     public void foreachJPEGcompareWithJPEG(ParameterHelper _aParam)
338     {
339         // TODO: auslagern in eine function, die ein Interface annimmt.
340         String sInputPath = _aParam.getInputPath();
341         File aInputPath = new File(sInputPath);
342 //        if (!aInputPath.exists())
343 //        {
344 //            GlobalLogWriter.println("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'");
345 //            assure("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'", false);
346 //        }
347         if (aInputPath.isDirectory())
348         {
349             // check a whole directory
350             // a whole directory
351             FileFilter aFileFilter = FileHelper.getFileFilterJPEG();
352             traverseDirectory(aFileFilter, _aParam);
353         }
354         else
355         {
356             // the inputpath contains a file
357             if (sInputPath.toLowerCase().endsWith(".ini"))
358             {
359                 IniFile aIniFile = new IniFile(_aParam.getInputPath());
360                 while (aIniFile.hasMoreElements())
361                 {
362                     String sSection = (String)aIniFile.nextElement();
363 // TODO: not supported yet.
364                     // callEveryPictureInIniFile(aIniFile, sSection, _aParam);
365                 }
366             }
367             else
368             {
369                 // call for a single jpeg file
370                 try
371                 {
372                     String sOutputFilename = _aParam.getOutputPath();
373                     if (sInputPath.toLowerCase().endsWith(".jpg") ||
374                         sInputPath.toLowerCase().endsWith(".jpeg")    )
375                     {
376                         checkOneFile(sInputPath, sOutputFilename, _aParam);
377                     }
378                     else
379                     {
380                         // check if there exists a ini file
381                         String sPath = FileHelper.getPath(sInputPath);
382                         String sBasename = FileHelper.getBasename(sInputPath);
383 
384                         runThroughEveryReportInIndex(sPath, sBasename, _aParam);
385 
386                         String sStatusRunThrough = (String)_aParam.getTestParameters().get("last_state");
387                         String sStatusInfo = (String)_aParam.getTestParameters().get("last_info");
388 
389                         if (sStatusRunThrough != null &&
390                             sStatusInfo != null )
391                         {
392                             // store the bad status in the <Name>.odb.ps.ini file
393                             String sOutputPath = _aParam.getOutputPath();
394                             String sBasenameIni = FileHelper.appendPath(sOutputPath, sBasename + ".ps.ini");
395                             IniFile aBasenameIni = new IniFile(sBasenameIni);
396                             aBasenameIni.insertValue("global", "state", sStatusRunThrough);
397                             aBasenameIni.insertValue("global", "info", sStatusInfo);
398                             aBasenameIni.close();
399                         }
400 
401                     }
402                 }
403                 catch (OfficeException e)
404                 {
405                     // TODO: unhandled yet.
406                     GlobalLogWriter.println("Warning: caught OfficeException " + e.getMessage());
407                 }
408             // callEntry(sInputPath, _aParam);
409             }
410         }
411     }
412 
413 
414 /**
415  * Run through a given index.ini or run through a given directory,
416  * find all ini files.
417  * Call the IDocument interface function call(...);
418  * @param _aParam
419  */
420     public void foreachResultCreateHTML(ParameterHelper _aParam)
421     {
422         // TODO: auslagern in eine function, die ein Interface annimmt.
423         String sInputPath = _aParam.getInputPath();
424         File aInputPath = new File(sInputPath);
425 //        if (!aInputPath.exists())
426 //        {
427 //            GlobalLogWriter.println("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'");
428 //            assure("Error, InputPath or File in InputPath doesn't exists. Please check: '" + sInputPath + "'", false);
429 //        }
430 
431         // call for a single ini file
432         if (sInputPath.toLowerCase().endsWith(".ini") )
433         {
434             callEntry(sInputPath, _aParam);
435         }
436         else
437         {
438             // check if there exists an ini file
439             String sPath = FileHelper.getPath(sInputPath);
440             String sBasename = FileHelper.getBasename(sInputPath);
441 
442             runThroughEveryReportInIndex(sPath, sBasename, _aParam);
443 
444             // Create a HTML page which shows locally to all files in .odb
445             if (sInputPath.toLowerCase().endsWith(".odb"))
446             {
447                 String sIndexFile = FileHelper.appendPath(sPath, "index.ini");
448                 File aIndexFile = new File(sIndexFile);
449                 if (aIndexFile.exists())
450                 {
451                     IniFile aIniFile = new IniFile(sIndexFile);
452 
453                     if (aIniFile.hasSection(sBasename))
454                     {
455                         // special case for odb files
456                         int nFileCount = aIniFile.getIntValue(sBasename, "reportcount", 0);
457                         ArrayList<String> aList = new ArrayList<String>();
458                         for (int i=0;i<nFileCount;i++)
459                         {
460                             String sValue = aIniFile.getValue(sBasename, "report" + i);
461 
462                             String sPSorPDFName = getPSorPDFNameFromIniFile(aIniFile, sValue);
463                             if (sPSorPDFName.length() > 0)
464                             {
465                                 aList.add(sPSorPDFName);
466                             }
467                         }
468                         if (aList.size() > 0)
469                         {
470                             // HTML output for the odb file, shows only all other documents.
471                             HTMLResult aOutputter = new HTMLResult(sPath, sBasename + ".ps.html" );
472                             aOutputter.header("content of DB file: " + sBasename);
473                             aOutputter.indexSection(sBasename);
474 
475                             for (int i=0;i<aList.size();i++)
476                             {
477                                 String sPSFile = aList.get(i);
478 
479                                 // Read information out of the ini files
480                                 String sIndexFile2 = FileHelper.appendPath(sPath, sPSFile + ".ini");
481                                 IniFile aIniFile2 = new IniFile(sIndexFile2);
482                                 String sStatusRunThrough = aIniFile2.getValue("global", "state");
483                                 String sStatusMessage = ""; // aIniFile2.getValue("global", "info");
484                                 aIniFile2.close();
485 
486 
487                                 String sHTMLFile = sPSFile + ".html";
488                                 aOutputter.indexLine(sHTMLFile, sPSFile, sStatusRunThrough, sStatusMessage);
489                             }
490                             aOutputter.close();
491 
492 //                            String sHTMLFile = FileHelper.appendPath(sPath, sBasename + ".ps.html");
493 //                            try
494 //                            {
495 //
496 //                                FileOutputStream out2 = new FileOutputStream(sHTMLFile);
497 //                                PrintStream out = new PrintStream(out2);
498 //
499 //                                out.println("<HTML>");
500 //                                out.println("<BODY>");
501 //                                for (int i=0;i<aList.size();i++)
502 //                                {
503 //                                    // <A href="link">blah</A>
504 //                                    String sPSFile = (String)aList.get(i);
505 //                                    out.print("<A href=\"");
506 //                                    out.print(sPSFile + ".html");
507 //                                    out.print("\">");
508 //                                    out.print(sPSFile);
509 //                                    out.println("</A>");
510 //                                    out.println("<BR>");
511 //                                }
512 //                                out.println("</BODY></HTML>");
513 //                                out.close();
514 //                                out2.close();
515 //                            }
516 //                            catch (java.io.IOException e)
517 //                            {
518 //
519 //                            }
520                         }
521                     }
522                     aIniFile.close();
523                 }
524 
525             }
526         }
527     }
528 
529 
530 }
531