xref: /AOO41X/main/qadevOOo/runner/util/compare/GraphicalComparator.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 package util.compare;
28 
29 import convwatch.GraphicalDifferenceCheck;
30 import convwatch.GraphicalTestArguments;
31 import convwatch.DirectoryHelper;
32 import convwatch.FileHelper;
33 
34 import lib.TestParameters;
35 import java.io.File;
36 import java.io.FileFilter;
37 import java.io.IOException;
38 
39 import util.compare.DocComparator;
40 import convwatch.ConvWatchException;
41 
42 // -----------------------------------------------------------------------------
43 class GraphicalComparator implements DocComparator
44 {
45     GraphicalTestArguments m_aArguments;
46 
47     protected GraphicalComparator(TestParameters aParams)
48         {
49             m_aArguments = new GraphicalTestArguments(aParams);
50         }
51 
52     /**
53      * @return an instance of this object, but only it's interface
54      */
55     static DocComparator getInstance(TestParameters aParams)
56         {
57             // setting the default test parameter
58             // TEST aParams
59             GraphicalComparator a = new GraphicalComparator(aParams);
60             return a;
61         }
62 
63     /**
64      * return a (FileFilter) function, which returns true, if the filename is a '*.prn' file
65      */
66     FileFilter getTrueIfPRNFile_FileFilter()
67         {
68             FileFilter aFileFilter = new FileFilter()
69                 {
70                     public boolean accept( File pathname )
71                         {
72                             if (pathname.getName().endsWith(".prn"))
73                             {
74                                 return true;
75                             }
76                             return false;
77                         }
78                 };
79             return aFileFilter;
80         }
81 
82     /**
83      * build a new file from _sEntry by
84      * replacing the path equals to _sInputPath with _sReferencePath and replace it's suffix by _sNewSuffix.
85      *  If _sInputPath is empty, replace the whole path by _sReferencePath.
86      */
87     protected String createSpecialFile(String _sEntry, String _sInputPath, String _sReferencePath, String _sNewSuffix)
88         {
89             String fs = System.getProperty("file.separator");
90             String sNewSubDir = "";
91             if (_sInputPath.length() > 0)
92             {
93                 sNewSubDir = FileHelper.removeFirstDirectorysAndBasenameFrom(_sEntry, _sInputPath);
94             }
95             String sNameNoSuffix = FileHelper.getNameNoSuffix(FileHelper.getBasename(_sEntry));
96 
97             // add the sub path to the difference path
98             String sNewReferencePath;
99             if (sNewSubDir.length() > 0)
100             {
101                 sNewReferencePath = _sReferencePath + fs + sNewSubDir;
102             }
103             else
104             {
105                 sNewReferencePath = _sReferencePath;
106             }
107             // add the difference name
108             sNewReferencePath += fs + sNameNoSuffix + _sNewSuffix;
109             return sNewReferencePath;
110         }
111 
112     boolean isReferenceOrDiffExistent(String _sNewSuffix)
113         {
114             boolean isExistent = false;
115 
116             // LLA? What if sReferencePath is a directory, but directory is empty? is the result then true or false;
117 
118             // wir muessen durch den InputPath durch und dann fuer jedes Dokument prufen, ob im angegebenen ReferencePath eine Reference existiert.
119             String sInputPath = m_aArguments.getInputPath();
120             if (FileHelper.isDir(sInputPath))
121             {
122                 Object[] aList = DirectoryHelper.traverse(sInputPath, FileHelper.getFileFilter(), m_aArguments.includeSubDirectories());
123                 for (int i=0;i<aList.length;i++)
124                 {
125                     // get document + path
126                     String sEntry = (String)aList[i];
127                     String sNewReferencePath = createSpecialFile(sEntry, sInputPath, m_aArguments.getReferencePath(), _sNewSuffix);
128                     // split path from document path which only is equal to sInputPath (sub path)
129                     if (FileHelper.exists(sNewReferencePath))
130                     {
131                         isExistent = true;
132                     }
133                 }
134             }
135             else
136             {
137                 // sInputPath is a file
138                 String sNewReferencePath = createSpecialFile(sInputPath, "", m_aArguments.getReferencePath(), _sNewSuffix);
139                 if (FileHelper.exists(sNewReferencePath))
140                 {
141                     isExistent = true;
142                 }
143             }
144             return isExistent;
145         }
146 
147     /**
148      *  REFERENCE_PATH must set to directory/file, where the reference (*.prn files) (should) exist
149      */
150     public boolean isReferenceExistent()
151         {
152             return isReferenceOrDiffExistent(".prn");
153         }
154 
155     /**
156      *  INPUT_PATH must set, to directory/file, where the documents exist.
157      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) will create.
158      */
159     public void createReference() throws IOException
160         {
161             // woher kommt das TestDocument
162             // INPUT_PATH
163             // wohin
164             // REFERENCE_PATH
165             // mit was (Reference Application)
166             // AppExecutionCmd
167             try
168             {
169                 String referenceInputPath = null;
170                 if(m_aArguments.getReferenceInputPath() == null)
171                 {
172                     GraphicalDifferenceCheck.createReferences(m_aArguments.getInputPath(), m_aArguments.getReferencePath(), m_aArguments);
173                 }
174                 else
175                 {
176                     referenceInputPath = m_aArguments.getReferenceInputPath();
177                     GraphicalDifferenceCheck.createReferences(referenceInputPath, m_aArguments.getReferencePath(), m_aArguments);
178                 }
179             }
180             catch (ConvWatchException e)
181             {
182                 // wrap it to IOException
183                 throw new java.io.IOException(e.getMessage());
184             }
185         }
186 
187     /**
188      *  INPUT_PATH must set, to directory/file, where the documents exist.
189      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) will create.
190      *  OUTPUT_PATH must set to a directory, there the whole ouptut will create
191      */
192     public boolean compare() throws IOException
193         {
194             try
195             {
196                 if (FileHelper.isDebugEnabled())
197                 {
198                     System.err.println("    Inputpath: '" + m_aArguments.getInputPath() + "'");
199                     System.err.println("   Outputpath: '" + m_aArguments.getOutputPath() + "'");
200                     System.err.println("Referencepath: '" + m_aArguments.getReferencePath() + "'");
201                 }
202                 return GraphicalDifferenceCheck.check(m_aArguments.getInputPath(), m_aArguments.getOutputPath(), m_aArguments.getReferencePath(), m_aArguments);
203             }
204             catch(ConvWatchException e)
205             {
206                 // wrap it to IOException
207                 if (FileHelper.isDebugEnabled())
208                 {
209                     System.err.println("Exception caught");
210                     System.err.println("    Inputpath: '" + m_aArguments.getInputPath() + "'");
211                     System.err.println("   Outputpath: '" + m_aArguments.getOutputPath() + "'");
212                     System.err.println("Referencepath: '" + m_aArguments.getReferencePath() + "'");
213                 }
214                 throw new java.io.IOException(e.getMessage());
215             }
216         }
217 
218     /**
219      *
220      * INPUT_PATH must set to the original documents the directory structure is taken to see if the references exist in the DIFF_PATH
221      * DIFF_PATH must set to the diff references
222      */
223     public boolean isDiffReferenceExistent() throws IOException
224         {
225             return isReferenceOrDiffExistent(".prn.diff0001.jpg");
226         }
227 
228     /**
229      *  INPUT_PATH must set, to directory/file, where the documents exist.
230      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) exists.
231      *  OUTPUT_PATH must set to a directory, where the whole ouptut will create. Here the diffReference will create.
232      *              At the momemt it's not possible to say only where the diffreferences will create.
233      */
234     public void createDiffReference() throws IOException
235         {
236             // this is the same like compareDiff(), but trash the result.
237             compareDiff();
238         }
239 
240     /**
241      *  INPUT_PATH must set, to directory/file, where the documents exist.
242      *  REFERENCE_PATH must set to directory/file, where the created references (*.prn files) exists.
243      *  OUTPUT_PATH must set to a directory, where the whole ouptut will create.
244      *  DIFF_PATH must set to a directory, where the older difference references exist, it's possible to set this to the same as REFERENCE_PATH
245      *  but this is not the default and will not automatically set.
246      */
247     public boolean compareDiff() throws IOException
248         {
249             try
250             {
251                 return GraphicalDifferenceCheck.check(m_aArguments.getInputPath(), m_aArguments.getOutputPath(), m_aArguments.getReferencePath(), m_aArguments.getDiffPath(), m_aArguments);
252             }
253             catch(ConvWatchException e)
254             {
255                 // wrap it to IOException
256                 throw new java.io.IOException(e.getMessage());
257             }
258         }
259 
260 }
261