1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package convwatch;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir // imports
31*cdf0e10cSrcweir import java.io.File;
32*cdf0e10cSrcweir import java.io.FileFilter;
33*cdf0e10cSrcweir import java.util.ArrayList;
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import convwatch.DirectoryHelper;
38*cdf0e10cSrcweir import convwatch.ConvWatchException;
39*cdf0e10cSrcweir import convwatch.EnhancedComplexTestCase;
40*cdf0e10cSrcweir import convwatch.PropertyName;
41*cdf0e10cSrcweir import helper.OfficeProvider;
42*cdf0e10cSrcweir import convwatch.PerformanceContainer;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir /**
45*cdf0e10cSrcweir  * The following Complex Test will test
46*cdf0e10cSrcweir  * an already created document and it's postscript output (by an older office version)
47*cdf0e10cSrcweir  * with a new office version.
48*cdf0e10cSrcweir  * This test use Ghostscript for the jpeg export and graphically compare tools from ImageMagick.
49*cdf0e10cSrcweir  * Read the manual for more information.
50*cdf0e10cSrcweir  *
51*cdf0e10cSrcweir  * this is only the starter program
52*cdf0e10cSrcweir  * more is found in qadevOOo/runner/convwatch/*
53*cdf0e10cSrcweir  */
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir public class ReferenceBuilder extends EnhancedComplexTestCase
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     // The first of the mandatory functions:
58*cdf0e10cSrcweir     /**
59*cdf0e10cSrcweir      * Return the name of the test.
60*cdf0e10cSrcweir      * In this case it is the actual name of the service.
61*cdf0e10cSrcweir      * @return The tested service.
62*cdf0e10cSrcweir      */
63*cdf0e10cSrcweir     public String getTestObjectName() {
64*cdf0e10cSrcweir         return "ReferenceBuilder runner";
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     // The second of the mandatory functions: return all test methods as an
68*cdf0e10cSrcweir     // array. There is only one test function in this example.
69*cdf0e10cSrcweir     /**
70*cdf0e10cSrcweir      * Return all test methods.
71*cdf0e10cSrcweir      * @return The test methods.
72*cdf0e10cSrcweir      */
73*cdf0e10cSrcweir     public String[] getTestMethodNames() {
74*cdf0e10cSrcweir         return new String[]{"buildreference"};
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     // This test is fairly simple, so there is no need for before() or after()
78*cdf0e10cSrcweir     // methods.
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir     public void before()
81*cdf0e10cSrcweir         {
82*cdf0e10cSrcweir             // System.out.println("before()");
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir     public void after()
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir             // System.out.println("after()");
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir     // The test method itself.
91*cdf0e10cSrcweir     private String m_sInputPath = "";
92*cdf0e10cSrcweir     private String m_sReferencePath = "";
93*cdf0e10cSrcweir     private boolean m_bIncludeSubdirectories = true;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     void initMember()
96*cdf0e10cSrcweir         {
97*cdf0e10cSrcweir             // MUST PARAMETER
98*cdf0e10cSrcweir             // INPUT_PATH ----------
99*cdf0e10cSrcweir             String sINPATH = (String)param.get( PropertyName.DOC_COMPARATOR_INPUT_PATH );
100*cdf0e10cSrcweir             boolean bQuit = false;
101*cdf0e10cSrcweir             String sError = "";
102*cdf0e10cSrcweir             if (sINPATH == null || sINPATH.length() == 0)
103*cdf0e10cSrcweir             {
104*cdf0e10cSrcweir                 log.println("Please set input path (path to documents) " + PropertyName.DOC_COMPARATOR_INPUT_PATH + "=path.");
105*cdf0e10cSrcweir                 bQuit = true;
106*cdf0e10cSrcweir             }
107*cdf0e10cSrcweir             else
108*cdf0e10cSrcweir             {
109*cdf0e10cSrcweir                 log.println("found " + PropertyName.DOC_COMPARATOR_INPUT_PATH + " " + sINPATH);
110*cdf0e10cSrcweir                 m_sInputPath = sINPATH;
111*cdf0e10cSrcweir             }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             // REFERENCE_PATH ----------
114*cdf0e10cSrcweir             String sREF = (String)param.get( PropertyName.DOC_COMPARATOR_REFERENCE_PATH );
115*cdf0e10cSrcweir             if (sREF == null || sREF.length() == 0)
116*cdf0e10cSrcweir             {
117*cdf0e10cSrcweir                 log.println("Please set output path (path to a directory, where the references should stay) " + PropertyName.DOC_COMPARATOR_REFERENCE_PATH + "=path.");
118*cdf0e10cSrcweir                 bQuit = true;
119*cdf0e10cSrcweir             }
120*cdf0e10cSrcweir             else
121*cdf0e10cSrcweir             {
122*cdf0e10cSrcweir                 log.println("found " + PropertyName.DOC_COMPARATOR_REFERENCE_PATH + " " + sREF);
123*cdf0e10cSrcweir                 m_sReferencePath = sREF;
124*cdf0e10cSrcweir             }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir             if (bQuit == true)
127*cdf0e10cSrcweir             {
128*cdf0e10cSrcweir                 // log.println("must quit.");
129*cdf0e10cSrcweir                 assure("Must quit, Parameter problems.", false);
130*cdf0e10cSrcweir             }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir             if (m_sInputPath.startsWith("file:") ||
133*cdf0e10cSrcweir                 m_sReferencePath.startsWith("file:"))
134*cdf0e10cSrcweir             {
135*cdf0e10cSrcweir                 assure("We can't handle file: URL right, use system path instead.", false);
136*cdf0e10cSrcweir             }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir         }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     /**
141*cdf0e10cSrcweir      * Function returns a List of software which must accessable as an external executable
142*cdf0e10cSrcweir      */
143*cdf0e10cSrcweir     protected Object[] mustInstalledSoftware()
144*cdf0e10cSrcweir         {
145*cdf0e10cSrcweir             ArrayList aList = new ArrayList();
146*cdf0e10cSrcweir             aList.add("perl -version");
147*cdf0e10cSrcweir             return aList.toArray();
148*cdf0e10cSrcweir         }
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir     // the test ======================================================================
151*cdf0e10cSrcweir     public void buildreference()
152*cdf0e10cSrcweir         {
153*cdf0e10cSrcweir             GlobalLogWriter.set(log);
154*cdf0e10cSrcweir             String sDBConnection = (String)param.get( PropertyName.DB_CONNECTION_STRING );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir             // check if all need software is installed and accessable
157*cdf0e10cSrcweir             checkEnvironment(mustInstalledSoftware());
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir             // test_removeFirstDirectorysAndBasenameFrom();
160*cdf0e10cSrcweir             // Get the MultiServiceFactory.
161*cdf0e10cSrcweir             // XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
162*cdf0e10cSrcweir             GraphicalTestArguments aGTA = getGraphicalTestArguments();
163*cdf0e10cSrcweir             if (aGTA == null)
164*cdf0e10cSrcweir             {
165*cdf0e10cSrcweir                 assure("Must quit", false);
166*cdf0e10cSrcweir             }
167*cdf0e10cSrcweir             if (aGTA.cancelRequest())
168*cdf0e10cSrcweir             {
169*cdf0e10cSrcweir                 return;
170*cdf0e10cSrcweir             }
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             initMember();
173*cdf0e10cSrcweir             DB.init(aGTA.getDBInfoString() + "," + sDBConnection);
174*cdf0e10cSrcweir             File aInputPath = new File(m_sInputPath);
175*cdf0e10cSrcweir             if (aInputPath.isDirectory())
176*cdf0e10cSrcweir             {
177*cdf0e10cSrcweir                 String fs = System.getProperty("file.separator");
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir                 String sRemovePath = aInputPath.getAbsolutePath();
180*cdf0e10cSrcweir                 // a whole directory
181*cdf0e10cSrcweir                 FileFilter aFileFilter = FileHelper.getFileFilter();
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir                 Object[] aList = DirectoryHelper.traverse(m_sInputPath, aFileFilter, aGTA.includeSubDirectories());
184*cdf0e10cSrcweir                 // fill into DB
185*cdf0e10cSrcweir                 // DB.filesRemove(aGTA.getDBInfoString());
186*cdf0e10cSrcweir                 // for (int j=0;j<aList.length;j++)
187*cdf0e10cSrcweir                 // {
188*cdf0e10cSrcweir                 //     String sEntry = (String)aList[j];
189*cdf0e10cSrcweir                 //     DB.fileInsert(aGTA.getDBInfoString(), sEntry, sRemovePath);
190*cdf0e10cSrcweir                 // }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir                 // normal run.
193*cdf0e10cSrcweir                 for (int i=0;i<aList.length;i++)
194*cdf0e10cSrcweir                 {
195*cdf0e10cSrcweir                     String sEntry = (String)aList[i];
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir                     String sNewReferencePath = m_sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, m_sInputPath);
198*cdf0e10cSrcweir                     log.println("- next file is: ------------------------------");
199*cdf0e10cSrcweir                     log.println(sEntry);
200*cdf0e10cSrcweir                     log.println(sNewReferencePath);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir                     if (aGTA.checkIfUsableDocumentType(sEntry))
203*cdf0e10cSrcweir                     {
204*cdf0e10cSrcweir                         runGDC(sEntry, sNewReferencePath);
205*cdf0e10cSrcweir                     }
206*cdf0e10cSrcweir                     if (aGTA.cancelRequest())
207*cdf0e10cSrcweir                     {
208*cdf0e10cSrcweir                         break;
209*cdf0e10cSrcweir                     }
210*cdf0e10cSrcweir                 }
211*cdf0e10cSrcweir             }
212*cdf0e10cSrcweir             else
213*cdf0e10cSrcweir             {
214*cdf0e10cSrcweir                 // String sRemovePath = aInputPath.getAbsolutePath();
215*cdf0e10cSrcweir                 // DB.fileInsert(aGTA.getDBInfoString(), m_sInputPath, sRemovePath);
216*cdf0e10cSrcweir                 // DB.updatestate_status(aGTA.getDBInfoString(), "started: " + m_sInputPath);
217*cdf0e10cSrcweir                 if (aGTA.checkIfUsableDocumentType(m_sInputPath))
218*cdf0e10cSrcweir                 {
219*cdf0e10cSrcweir                     runGDC(m_sInputPath, m_sReferencePath);
220*cdf0e10cSrcweir                 }
221*cdf0e10cSrcweir             }
222*cdf0e10cSrcweir         }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     void runGDC(String _sInputPath, String _sReferencePath)
225*cdf0e10cSrcweir         {
226*cdf0e10cSrcweir             // first do a check if the reference not already exist, this is a big speedup, due to the fact,
227*cdf0e10cSrcweir             // we don't need to start a new office.
228*cdf0e10cSrcweir             GraphicalTestArguments aGTA = getGraphicalTestArguments();
229*cdf0e10cSrcweir             if (GraphicalDifferenceCheck.isReferenceExists(_sInputPath, _sReferencePath, aGTA) == false)
230*cdf0e10cSrcweir             {
231*cdf0e10cSrcweir             // start a fresh Office
232*cdf0e10cSrcweir                 OfficeProvider aProvider = null;
233*cdf0e10cSrcweir                 // SimpleFileSemaphore aSemaphore = new SimpleFileSemaphore();
234*cdf0e10cSrcweir                 if (aGTA.shouldOfficeStart())
235*cdf0e10cSrcweir                 {
236*cdf0e10cSrcweir                     // if (OSHelper.isWindows())
237*cdf0e10cSrcweir                     // {
238*cdf0e10cSrcweir                     //     aSemaphore.P(aSemaphore.getSemaphoreFile());
239*cdf0e10cSrcweir                     // }
240*cdf0e10cSrcweir                     aGTA.getPerformance().startTime(PerformanceContainer.OfficeStart);
241*cdf0e10cSrcweir                     aProvider = new OfficeProvider();
242*cdf0e10cSrcweir                     XMultiServiceFactory xMSF = (XMultiServiceFactory) aProvider.getManager(param);
243*cdf0e10cSrcweir                     param.put("ServiceFactory", xMSF);
244*cdf0e10cSrcweir                     aGTA.getPerformance().stopTime(PerformanceContainer.OfficeStart);
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir                     long nStartTime = aGTA.getPerformance().getTime(PerformanceContainer.OfficeStart);
247*cdf0e10cSrcweir                     aGTA = getGraphicalTestArguments();
248*cdf0e10cSrcweir                     aGTA.getPerformance().setTime(PerformanceContainer.OfficeStart, nStartTime);
249*cdf0e10cSrcweir                 }
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir                 // Watcher Object is need in log object to give a simple way to say if a running office is alive.
252*cdf0e10cSrcweir                 // As long as a log comes, it pings the Watcher and says the office is alive, if not an
253*cdf0e10cSrcweir                 // internal counter increase and at a given point (300 seconds) the office is killed.
254*cdf0e10cSrcweir                 GlobalLogWriter.get().println("Set office watcher");
255*cdf0e10cSrcweir                 Object aWatcher = param.get("Watcher");
256*cdf0e10cSrcweir                 GlobalLogWriter.get().setWatcher(aWatcher);
257*cdf0e10cSrcweir                 // initializeWatcher(param);
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir                 try
260*cdf0e10cSrcweir                 {
261*cdf0e10cSrcweir                     log.println("Reference type is " + aGTA.getReferenceType());
262*cdf0e10cSrcweir                     DB.source_start();
263*cdf0e10cSrcweir                     GraphicalDifferenceCheck.createOneReferenceFile(_sInputPath, _sReferencePath, aGTA);
264*cdf0e10cSrcweir                     DB.source_finished();
265*cdf0e10cSrcweir                 }
266*cdf0e10cSrcweir                 catch(ConvWatchCancelException e)
267*cdf0e10cSrcweir                 {
268*cdf0e10cSrcweir                     assure(e.getMessage(), false);
269*cdf0e10cSrcweir                     DB.source_failed(e.getMessage());
270*cdf0e10cSrcweir                 }
271*cdf0e10cSrcweir                 catch(ConvWatchException e)
272*cdf0e10cSrcweir                 {
273*cdf0e10cSrcweir                     assure(e.getMessage(), false);
274*cdf0e10cSrcweir                     DB.source_failed(e.getMessage());
275*cdf0e10cSrcweir                 }
276*cdf0e10cSrcweir                 catch(com.sun.star.lang.DisposedException e)
277*cdf0e10cSrcweir                 {
278*cdf0e10cSrcweir                     assure(e.getMessage(), false, true);
279*cdf0e10cSrcweir                     DB.source_failed(e.getMessage());
280*cdf0e10cSrcweir                 }
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir                 // Office shutdown
283*cdf0e10cSrcweir                 if (aProvider != null)
284*cdf0e10cSrcweir                 {
285*cdf0e10cSrcweir                     boolean bClosed = aProvider.closeExistingOffice(param, true);
286*cdf0e10cSrcweir                     // Hope I can check that the close of the office fails
287*cdf0e10cSrcweir                     assure("Office closed", bClosed, true);
288*cdf0e10cSrcweir                     // if (OSHelper.isWindows())
289*cdf0e10cSrcweir                     // {
290*cdf0e10cSrcweir                     //     aSemaphore.V(aSemaphore.getSemaphoreFile());
291*cdf0e10cSrcweir                     //     aSemaphore.sleep(2);
292*cdf0e10cSrcweir                     //     // wait some time maybe an other process will take the semaphore
293*cdf0e10cSrcweir                     //     // I know, this is absolutly dirty, but the whole convwatch is dirty and need a big cleanup.
294*cdf0e10cSrcweir                     // }
295*cdf0e10cSrcweir                 }
296*cdf0e10cSrcweir             }
297*cdf0e10cSrcweir             else
298*cdf0e10cSrcweir             {
299*cdf0e10cSrcweir                 // Reference already exist, do nothing, but DB change
300*cdf0e10cSrcweir                 DB.source_finished();
301*cdf0e10cSrcweir             }
302*cdf0e10cSrcweir         }
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305