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