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 java.util.ArrayList;
27 import convwatch.EnhancedComplexTestCase;
28 import convwatch.PRNCompare;
29 import convwatch.GraphicalTestArguments;
30 import helper.URLHelper;
31 import convwatch.OfficePrint;
32 import java.io.File;
33 
34 public class GfxCompare extends EnhancedComplexTestCase
35 {
36     // The first of the mandatory functions:
37     /**
38      * Return the name of the test.
39      * In this case it is the actual name of the service.
40      * @return The tested service.
41      */
42     // public String getTestObjectName() {
43     //     return "ConvWatch runner";
44     // }
45 
46     // The second of the mandatory functions: return all test methods as an
47     // array. There is only one test function in this example.
48     /**
49      * Return all test methods.
50      * @return The test methods.
51      */
52 
getTestMethodNames()53     public String[] getTestMethodNames() {
54         return new String[]{"gfxcompare"};
55     }
56 
57     /**
58      *
59      * @return a List of software which must accessible as an external executable
60      */
mustInstalledSoftware()61     protected Object[] mustInstalledSoftware()
62         {
63             ArrayList aList = new ArrayList();
64             // Tools from ImageMagick
65             aList.add( "composite -version" );
66             aList.add( "identify -version" );
67 
68             // Ghostscript
69             aList.add( "gs -version" );
70             return aList.toArray();
71         }
72 
73 
74     GraphicalTestArguments m_aArguments = null;
75     /**
76      * The test method itself.
77      * Don't try to call it from outside, it is started only from qadevOOo runner
78      */
79 
80     /* protected */
gfxcompare()81     public void gfxcompare()
82         {
83             GlobalLogWriter.set(log);
84 
85             // check if all need software is installed and accessible
86             checkEnvironment(mustInstalledSoftware());
87 
88             m_aArguments = getGraphicalTestArguments();
89 
90             String sFile1 = (String)param.get("FILE1");
91             String sFile2 = (String)param.get("FILE2");
92             compare(sFile1, sFile2);
93         }
94 
95     // -----------------------------------------------------------------------------
96 
createJPEG(String _sFile, String _sAdditional)97     String createJPEG(String _sFile, String _sAdditional)
98         {
99             String sJPEGFile = "";
100             if (_sFile.startsWith("file:///"))
101             {
102                 _sFile = FileHelper.getSystemPathFromFileURL(_sFile);
103             }
104             File aFile = new File(_sFile);
105             if (aFile.exists())
106             {
107                 String sAbsFile = aFile.getAbsolutePath();
108                 if (!sAbsFile.equals(_sFile))
109                 {
110                     _sFile = sAbsFile;
111                 }
112             }
113             else
114             {
115                 GlobalLogWriter.get().println("File: '" + _sFile + "' doesn't exist.");
116                 return "";
117             }
118             String sFileDir = FileHelper.getPath(_sFile);
119             String sBasename = FileHelper.getBasename(_sFile);
120             String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
121 
122             String fs = System.getProperty("file.separator");
123             String sTmpDir = util.utils.getUsersTempDir();
124             if (m_aArguments.getOutputPath() != null)
125             {
126                 sTmpDir = m_aArguments.getOutputPath();
127             }
128 
129             if (_sFile.toLowerCase().endsWith("ps") ||
130                 _sFile.toLowerCase().endsWith("prn") ||
131                 _sFile.toLowerCase().endsWith("pdf"))
132             {
133                 // seems to be a Postscript of PDF file
134 
135                 String[] aList = PRNCompare.createJPEGFromPostscript(sTmpDir, sFileDir, sBasename, m_aArguments.getResolutionInDPI());
136                 sJPEGFile = aList[0];
137             }
138             else if (_sFile.toLowerCase().endsWith("jpg") ||
139                      _sFile.toLowerCase().endsWith("jpeg"))
140             {
141                 // do nothing, it's already a picture.
142                 return _sFile;
143             }
144             else
145             {
146                 // we assume it's an office document.
147                 String sInputURL;
148                 String sOutputURL;
149                 String sPrintFileURL;
150 
151                 String sInputFile = sFileDir + fs + sBasename;
152                 sInputURL = URLHelper.getFileURLFromSystemPath(sInputFile);
153 
154                 String sOutputFile = sTmpDir + fs + sBasename;
155                 sOutputURL = URLHelper.getFileURLFromSystemPath(sOutputFile);
156 
157                 String sPrintFile = sTmpDir + fs + sNameNoSuffix + _sAdditional + ".ps";
158                 sPrintFileURL = URLHelper.getFileURLFromSystemPath(sPrintFile);
159 
160                 try
161                 {
162                     OfficePrint.printToFile(m_aArguments, sInputURL, sOutputURL, sPrintFileURL);
163                     sJPEGFile = createJPEG(sPrintFile, _sAdditional);
164                 }
165                 catch (ConvWatchCancelException e)
166                 {
167                     GlobalLogWriter.get().println("Exception caught, can't create:" + sPrintFileURL);
168                 }
169             }
170             return sJPEGFile;
171         }
172 
173 
compare(String _sFile1, String _sFile2)174     public String compare(String _sFile1, String _sFile2)
175         {
176             String sJPEGFile1 = createJPEG(_sFile1, "-1");
177             String sJPEGFile2 = createJPEG(_sFile2, "-2");
178 
179             if (sJPEGFile1.length() > 0 && sJPEGFile2.length() > 0)
180             {
181                 String sDiffFile = PRNCompare.compareJPEGs(sJPEGFile1, sJPEGFile2);
182 
183                 if (sDiffFile.length() > 0)
184                 {
185                     GlobalLogWriter.get().println("Difference created: " + sDiffFile);
186                 }
187                 return sDiffFile;
188             }
189             return "";
190         }
191 
192 }
193