1ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ef39d40dSAndrew Rist  * distributed with this work for additional information
6ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10ef39d40dSAndrew Rist  *
11ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12ef39d40dSAndrew Rist  *
13ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18ef39d40dSAndrew Rist  * under the License.
19ef39d40dSAndrew Rist  *
20ef39d40dSAndrew Rist  *************************************************************/
21ef39d40dSAndrew Rist 
22ef39d40dSAndrew 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.OfficePrint;
35cdf0e10cSrcweir import convwatch.ConvWatchException;
36cdf0e10cSrcweir import convwatch.EnhancedComplexTestCase;
37cdf0e10cSrcweir import convwatch.PropertyName;
38cdf0e10cSrcweir import helper.OfficeProvider;
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 DocumentConverter 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 "DocumentConverter 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[]{"convert"};
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     /**
137*bb6af6bcSPedro Giffuni      * Function returns a List of software which must accessible 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 ======================================================================
convert()147cdf0e10cSrcweir     public void convert()
148cdf0e10cSrcweir         {
149cdf0e10cSrcweir             GlobalLogWriter.set(log);
150*bb6af6bcSPedro Giffuni             // check if all need software is installed and accessible
151cdf0e10cSrcweir             checkEnvironment(mustInstalledSoftware());
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // test_removeFirstDirectorysAndBasenameFrom();
154cdf0e10cSrcweir             // Get the MultiServiceFactory.
155cdf0e10cSrcweir             // XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
156cdf0e10cSrcweir             GraphicalTestArguments aGTA = getGraphicalTestArguments();
157cdf0e10cSrcweir             if (aGTA == null)
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 assure("Must quit", false);
160cdf0e10cSrcweir             }
161cdf0e10cSrcweir 
162cdf0e10cSrcweir             initMember();
163cdf0e10cSrcweir 
164cdf0e10cSrcweir             File aInputPath = new File(m_sInputPath);
165cdf0e10cSrcweir             if (aInputPath.isDirectory())
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 String fs = System.getProperty("file.separator");
168cdf0e10cSrcweir 
169cdf0e10cSrcweir                 String sRemovePath = aInputPath.getAbsolutePath();
170cdf0e10cSrcweir                 // a whole directory
171cdf0e10cSrcweir                 FileFilter aFileFilter = FileHelper.getFileFilter();
172cdf0e10cSrcweir 
173cdf0e10cSrcweir                 Object[] aList = DirectoryHelper.traverse(m_sInputPath, aFileFilter, aGTA.includeSubDirectories());
174cdf0e10cSrcweir                 for (int i=0;i<aList.length;i++)
175cdf0e10cSrcweir                 {
176cdf0e10cSrcweir                     String sEntry = (String)aList[i];
177cdf0e10cSrcweir 
178cdf0e10cSrcweir                     String sNewReferencePath = m_sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, m_sInputPath);
179cdf0e10cSrcweir                     log.println("- next file is: ------------------------------");
180cdf0e10cSrcweir                     log.println(sEntry);
181cdf0e10cSrcweir 
182cdf0e10cSrcweir                     if (aGTA.checkIfUsableDocumentType(sEntry))
183cdf0e10cSrcweir                     {
184cdf0e10cSrcweir                         runGDC(sEntry, sNewReferencePath);
185cdf0e10cSrcweir                     }
186cdf0e10cSrcweir                     if (aGTA.cancelRequest())
187cdf0e10cSrcweir                     {
188cdf0e10cSrcweir                         break;
189cdf0e10cSrcweir                     }
190cdf0e10cSrcweir                 }
191cdf0e10cSrcweir             }
192cdf0e10cSrcweir             else
193cdf0e10cSrcweir             {
194cdf0e10cSrcweir                 if (aGTA.checkIfUsableDocumentType(m_sInputPath))
195cdf0e10cSrcweir                 {
196cdf0e10cSrcweir                     runGDC(m_sInputPath, m_sReferencePath);
197cdf0e10cSrcweir                 }
198cdf0e10cSrcweir             }
199cdf0e10cSrcweir         }
200cdf0e10cSrcweir 
runGDC(String _sInputFile, String _sReferencePath)201cdf0e10cSrcweir     void runGDC(String _sInputFile, String _sReferencePath)
202cdf0e10cSrcweir         {
203cdf0e10cSrcweir             // first do a check if the reference not already exist, this is a big speedup, due to the fact,
204cdf0e10cSrcweir             // we don't need to start a new office.
205cdf0e10cSrcweir             GraphicalTestArguments aGTA_local = getGraphicalTestArguments();
206cdf0e10cSrcweir             // if (GraphicalDifferenceCheck.isReferenceExists(_sInputFile, _sReferencePath, aGTA_local) == false)
207cdf0e10cSrcweir             // {
208cdf0e10cSrcweir             // start a fresh Office
209cdf0e10cSrcweir             OfficeProvider aProvider = null;
210cdf0e10cSrcweir             if (aGTA_local.restartOffice())
211cdf0e10cSrcweir             {
212cdf0e10cSrcweir                 aProvider = new OfficeProvider();
213cdf0e10cSrcweir                 XMultiServiceFactory xMSF = (XMultiServiceFactory) aProvider.getManager(param);
214cdf0e10cSrcweir                 param.put("ServiceFactory", xMSF);
215cdf0e10cSrcweir             }
216cdf0e10cSrcweir             GraphicalTestArguments aGTA = getGraphicalTestArguments();
217cdf0e10cSrcweir 
218cdf0e10cSrcweir             if (aGTA.getOfficeProgram().toLowerCase().equals("msoffice"))
219cdf0e10cSrcweir             {
220cdf0e10cSrcweir                 // ReferenceType is MSOffice
221cdf0e10cSrcweir                 GlobalLogWriter.get().println("USE MSOFFICE AS EXPORT FORMAT.");
222cdf0e10cSrcweir                 MSOfficePrint a = new MSOfficePrint();
223cdf0e10cSrcweir                 try
224cdf0e10cSrcweir                 {
225cdf0e10cSrcweir                     String sInputFileBasename = FileHelper.getBasename(_sInputFile);
226cdf0e10cSrcweir                     String fs = System.getProperty("file.separator");
227cdf0e10cSrcweir                     FileHelper.makeDirectories("", _sReferencePath);
228cdf0e10cSrcweir                     String sOutputFile = _sReferencePath;
229cdf0e10cSrcweir                     if (sOutputFile.endsWith(fs))
230cdf0e10cSrcweir                     {
231cdf0e10cSrcweir                         sOutputFile += sInputFileBasename;
232cdf0e10cSrcweir                     }
233cdf0e10cSrcweir                     else
234cdf0e10cSrcweir                     {
235cdf0e10cSrcweir                         sOutputFile += fs + sInputFileBasename;
236cdf0e10cSrcweir                     }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir                     a.storeToFileWithMSOffice(aGTA, _sInputFile, sOutputFile);
239cdf0e10cSrcweir                 }
240cdf0e10cSrcweir                 catch(ConvWatchCancelException e)
241cdf0e10cSrcweir                 {
242cdf0e10cSrcweir                     GlobalLogWriter.get().println(e.getMessage());
243cdf0e10cSrcweir                 }
244cdf0e10cSrcweir                 catch(java.io.IOException e)
245cdf0e10cSrcweir                 {
246cdf0e10cSrcweir                     GlobalLogWriter.get().println(e.getMessage());
247cdf0e10cSrcweir                 }
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir             else
250cdf0e10cSrcweir             {
251cdf0e10cSrcweir                 try
252cdf0e10cSrcweir                 {
253cdf0e10cSrcweir                     OfficePrint.convertDocument(_sInputFile, _sReferencePath, aGTA);
254cdf0e10cSrcweir                 }
255cdf0e10cSrcweir                 catch(ConvWatchCancelException e)
256cdf0e10cSrcweir                 {
257cdf0e10cSrcweir                     assure(e.getMessage(), false);
258cdf0e10cSrcweir                 }
259cdf0e10cSrcweir                 catch(ConvWatchException e)
260cdf0e10cSrcweir                 {
261cdf0e10cSrcweir                     assure(e.getMessage(), false);
262cdf0e10cSrcweir                 }
263cdf0e10cSrcweir             }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir             if (aGTA.restartOffice())
266cdf0e10cSrcweir             {
267cdf0e10cSrcweir                 // Office shutdown
268cdf0e10cSrcweir                 aProvider.closeExistingOffice(param, true);
269cdf0e10cSrcweir             }
270cdf0e10cSrcweir             // }
271cdf0e10cSrcweir         }
272cdf0e10cSrcweir }
273cdf0e10cSrcweir 
274