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