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