summaryrefslogtreecommitdiff
path: root/qadevOOo/runner/convwatch/ConvWatch.java
blob: b1591d40ac09b2ac7bcb701101217912b5d24d48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package convwatch;

import helper.URLHelper;
import java.io.File;

public class ConvWatch
{

    private String getBuildID_FromFile(String _sInfoFile)
    {
        String sBuildID = "";
        IniFile aIniFile = new IniFile(_sInfoFile);
        if (aIniFile.is())
        {
            sBuildID = aIniFile.getValue("", "buildid");
        }
        return sBuildID;
    }
    /**
     * Check if given document (_sAbsoluteInputFile) and it's postscript representation (_sAbsoluteReferenceFile) produce
     * the same output like the StarOffice / OpenOffice.org which is accessible with XMultiServiceFactory.
     * Here a simple graphically difference check is run through.
     *
     * Hint: In the OutputPath all needed files will create, there must
     * be very much space. It's not possible to say how much.
     * One page need up to 800kb as jpeg.
     * Sample: If a document contains 2 pages, we need 2*800kb for prn
     * output and 2*800kb for ps output and 2*800kb for it's difference
     * output. So up to 4800kb or 4.8mb.
     *
     * RAM is need least 300mb. Will say, it's tested on a pc with 128mb RAM and 256mb swap.
     *
     * It's also absolutlly impossible to say, how much time this functions consume.
     */

    private StatusHelper[] createPostscriptStartCheck(GraphicalTestArguments _aGTA,
                                              String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
        throws ConvWatchCancelException
        {
//  TODO: some more checks

            if (! FileHelper.exists(_sAbsoluteInputFile))
            {
                throw new ConvWatchCancelException("createPostscriptStartCheck: Input file: " + _sAbsoluteInputFile + " does not exist.");
            }

            if (_sAbsoluteReferenceFile == null)
            {
                // we assume, that the prn file is near the document
                String sAbsoluteInputFileNoSuffix = FileHelper.getNameNoSuffix(_sAbsoluteInputFile);
                _sAbsoluteReferenceFile = sAbsoluteInputFileNoSuffix + ".prn";
            }

            String fs = System.getProperty("file.separator");
            File aAbsoluteReferenceFile = new File(_sAbsoluteReferenceFile);

            if (aAbsoluteReferenceFile.isDirectory())
            {
                String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
                String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
                String sAbsoluteReferenceFileInfo = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".info";
                _sAbsoluteReferenceFile = _sAbsoluteReferenceFile + fs + sNameNoSuffix + ".prn";

                // Read the reference from the info file
                String sRefBuildID = "";
                if (FileHelper.exists(sAbsoluteReferenceFileInfo))
                {
                    sRefBuildID = getBuildID_FromFile(sAbsoluteReferenceFileInfo);
                }
                _aGTA.setRefBuildID(sRefBuildID);

            }
            else
            {
                // java file has problems to check for directories, if the given directory doesn't exist.
                String sName = FileHelper.getBasename(_sAbsoluteReferenceFile);
                // thanks to Mircosoft, every document has a suffix, so if a name doesn't have a suffix, it must be a directory name
                int nIdx = sName.indexOf('.');
                if (nIdx == -1)
                {
                    // must be a directory
                    throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference directory: '" + _sAbsoluteReferenceFile + "' does not exist.");
                }
            }


            boolean bAbsoluteReferenceFile = true;
            if (! FileHelper.exists(_sAbsoluteReferenceFile))
            {
                if (_aGTA.createDefaultReference())
                {
                    GlobalLogWriter.get().println("Reference File doesn't exist, will create a default");
                    bAbsoluteReferenceFile = false;
                }
                else
                {
                    throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist.");
                }
            }

            FileHelper.makeDirectories("", _sOutputPath);

            String sAbsoluteInputFileURL = URLHelper.getFileURLFromSystemPath(_sAbsoluteInputFile);

            String sInputFile = FileHelper.getBasename(_sAbsoluteInputFile);

            FileHelper.getNameNoSuffix(sInputFile);


            String sAbsoluteOutputFile = _sOutputPath + fs + sInputFile;
            String sAbsoluteOutputFileURL = URLHelper.getFileURLFromSystemPath(sAbsoluteOutputFile);

            String sReferenceFile = FileHelper.getBasename(_sAbsoluteReferenceFile);
            String sReferenceFileNoSuffix = FileHelper.getNameNoSuffix(sReferenceFile);
            String sPostScriptFile = sReferenceFileNoSuffix + ".ps";

            String sAbsolutePrintFile = _sOutputPath + fs + sPostScriptFile;
            String sAbsolutePrintFileURL = URLHelper.getFileURLFromSystemPath(sAbsolutePrintFile);

            // store and print the sAbsoluteInputFileURL file with StarOffice / OpenOffice.org
            OfficePrint.printToFile(_aGTA, sAbsoluteInputFileURL, sAbsoluteOutputFileURL, sAbsolutePrintFileURL);

            if (! FileHelper.exists(sAbsolutePrintFile))
            {
                throw new ConvWatchCancelException("createPostscriptStartCheck: Printed file " + sAbsolutePrintFile + " does not exist.");
            }

            if (!bAbsoluteReferenceFile)
            {
                // copy AbsolutePrintFile to AbsoluteReferenceFile
                String sDestinationFile = sAbsolutePrintFile; // URLHelper.getSystemPathFromFileURL(...)
                String sSourceFile = _sAbsoluteReferenceFile;
                FileHelper.copy(sDestinationFile, sSourceFile);
                // now the fix reference of the AbsoluteReferenceFile should exist.
                if (! FileHelper.exists(_sAbsoluteReferenceFile))
                {
                    throw new ConvWatchCancelException("createPostscriptStartCheck: Given reference file: " + _sAbsoluteReferenceFile + " does not exist, after try to copy.");
                }
            }

            PRNCompare a = new PRNCompare();
            String sInputPath = FileHelper.getPath(_sAbsoluteInputFile);
            String sReferencePath = FileHelper.getPath(_sAbsoluteReferenceFile);

            a.setReferencePath( sReferencePath );
            a.setOutputPath(    _sOutputPath );
            a.setReferenceFile( sReferenceFile );
            a.setPostScriptFile(sPostScriptFile );
            if (_aGTA.printAllPages())
            {
                a.setMaxPages(9999);
            }
            else
            {
                if (_aGTA.getMaxPages() > 0)
                {
                    a.setMaxPages(_aGTA.getMaxPages());
                }
                if (_aGTA.getOnlyPages().length() != 0)
                {
                    // we can't interpret the string of getOnlyPages() right without much logic, so print all pages here!
                    a.setMaxPages(9999);
                }
            }

            a.setResolutionInDPI(_aGTA.getResolutionInDPI());
            a.setBorderMove(_aGTA.getBorderMove());
            a.setDocumentType(_aGTA.getDocumentType());

            StatusHelper[] aList = a.compare();

            _aGTA.setBorderMove(a.getBorderMove());
            return aList;
        }


    // This creates a status for exact on document
    private static boolean createINIStatus(StatusHelper[] aList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID, String _sRefBuildID)
        {
            System.getProperty("file.separator");
            String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
            String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
//  TODO: version info was fine
//            HTMLoutput.checkSection(sBasename);
            // Status end

            String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";
            INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, "");
            INIoutput.createHeader();
//  TODO: version info was fine

            INIoutput.writeSection("global");
            INIoutput.writeValue("pages", String.valueOf(aList.length));
            INIoutput.writeValue("buildid", _sBuildID);
            INIoutput.writeValue("refbuildid", _sRefBuildID);
            INIoutput.writeValue("diffdiff", "no");
            INIoutput.writeValue("basename", sBasename);

            boolean bResultIsOk = true;          // result over all pages
            for (int i=0;i<aList.length; i++)
            {
                INIoutput.writeSection("page" + (i + 1));   // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
                aList[i].printStatus();

                boolean bCurrentResult = true;   // result over exact one page

                int nCurrentDiffStatus = aList[i].nDiffStatus;

                // check if the status is in a defined range
                if (nCurrentDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES)
                {
                    // ok.
                }
                else if (nCurrentDiffStatus == StatusHelper.DIFF_DIFFERENCES_FOUND && aList[i].nPercent < 5)
                {
                    // ok.
                }
                else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_NO_PROBLEMS)
                {
                    // ok.
                }
                else if (nCurrentDiffStatus == StatusHelper.DIFF_AFTER_MOVE_DONE_DIFFERENCES_FOUND && aList[i].nPercent2 < 5)
                {
                    // ok.
                }
                else
                {
                    // failed.
                    bCurrentResult = false; // logic: nDiff==0 = true if there is no difference
                }

                // Status
                INIoutput.checkLine(aList[i], bCurrentResult);
                bResultIsOk &= bCurrentResult;
            }
            // Status
            INIoutput.close();
            return bResultIsOk;
        }



    private static void createINIStatus_DiffDiff(StatusHelper[] aDiffDiffList, String _sFilenamePrefix, String _sOutputPath, String _sAbsoluteInputFile, String _sBuildID)
        {
            System.getProperty("file.separator");
            String sBasename = FileHelper.getBasename(_sAbsoluteInputFile);
            String sNameNoSuffix = FileHelper.getNameNoSuffix(sBasename);
            String sINIFile = _sFilenamePrefix + sNameNoSuffix + ".ini";

            INIOutputter INIoutput = INIOutputter.create(_sOutputPath, sINIFile, _sFilenamePrefix);
            INIoutput.createHeader();
            // LLA? what if the are no values in the list? true or false;
            INIoutput.writeSection("global");
            INIoutput.writeValue("pages", String.valueOf(aDiffDiffList.length));
            INIoutput.writeValue("buildid", _sBuildID);
            INIoutput.writeValue("diffdiff", "yes");
            INIoutput.writeValue("basename", sBasename);

            for (int i=0;i<aDiffDiffList.length; i++)
            {
                INIoutput.writeSection("page" + (i + 1));   // list start at point 0, but this is page 1 and so on... current_page = (i + 1)
                boolean bCurrentResult = (aDiffDiffList[i].nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference

                INIoutput.checkDiffDiffLine(aDiffDiffList[i], bCurrentResult);
            }
            // Status
            INIoutput.close();
        }




    public static boolean check(GraphicalTestArguments _aGTA,
                             String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile)
        throws ConvWatchCancelException, ConvWatchException
        {
            ConvWatch a = new ConvWatch();
            StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);
            DB.writeNumberOfPages(aList.length);

            boolean bResultIsOk = createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());

            if (! bResultIsOk)
            {
                // it could be that this will store in a DB, there are problems with '\'
                String sErrorMessage = "Graphical compare failed with file ";
                String sErrorFile = _sAbsoluteInputFile.replace('\\', '/');
                sErrorMessage = sErrorMessage + "'" + sErrorFile + "'";
                DB.writeErrorFile(sErrorFile);
                throw new ConvWatchException(sErrorMessage);
            }
            return bResultIsOk;
        }


    public static boolean checkDiffDiff(GraphicalTestArguments _aGTA,
                                     String _sOutputPath, String _sAbsoluteInputFile, String _sAbsoluteReferenceFile,
                                     String _sAbsoluteDiffPath)
        throws ConvWatchCancelException, ConvWatchException
        {
            ConvWatch a = new ConvWatch();
            _aGTA.setBorderMove(TriState.FALSE);
            StatusHelper[] aList = a.createPostscriptStartCheck(_aGTA, _sOutputPath, _sAbsoluteInputFile, _sAbsoluteReferenceFile);

            createINIStatus(aList, "", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID(), _aGTA.getRefBuildID());

            StatusHelper[] aDiffDiffList = new StatusHelper[aList.length];

            String fs = System.getProperty("file.separator");

            boolean bDiffIsOk = true;
            boolean bFoundAOldDiff = false;

            PRNCompare aCompare = new PRNCompare();
            // LLA? what if the are no values in the list? true or false;
            for (int i=0;i<aList.length; i++)
            {
                String sOrigDiffName = aList[i].m_sDiffGfx;
                String sDiffBasename = FileHelper.getBasename(sOrigDiffName);

                String sNewDiffName = _sAbsoluteDiffPath + fs + sDiffBasename;
                if (! FileHelper.exists(sNewDiffName))
                {
                    GlobalLogWriter.get().println("checkDiffDiff: Old diff file: '" + sNewDiffName + "' does not exist." );
                    continue;
                }

                // make a simple difference between these both diff files.
                String sSourcePath1 = FileHelper.getPath(sOrigDiffName);
                String sSourceFile1 = sDiffBasename;
                String sSourcePath2 = _sAbsoluteDiffPath;
                String sSourceFile2 = sDiffBasename;

                StatusHelper aCurrentStatus = aCompare.checkDiffDiff(_sOutputPath, sSourcePath1, sSourceFile1, sSourcePath2, sSourceFile2);
                boolean bCurrentResult = (aCurrentStatus.nDiffStatus == StatusHelper.DIFF_NO_DIFFERENCES); // logic: nDiff==0 = true if there is no difference
                bDiffIsOk &= bCurrentResult;
                bFoundAOldDiff = true;

                aDiffDiffList[i] = aCurrentStatus;
            }

            createINIStatus_DiffDiff(aDiffDiffList, "DiffDiff_", _sOutputPath, _sAbsoluteInputFile, _aGTA.getBuildID());

            if (!bFoundAOldDiff)
            {
                throw new ConvWatchCancelException("No old difference file found." );
            }
            if (! bDiffIsOk)
            {
                throw new ConvWatchException("Graphical difference compare failed with file '" + _sAbsoluteInputFile + "'");
            }
            return bDiffIsOk;
        }

}