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.OfficePrint;
39 import convwatch.ConvWatchException;
40 import convwatch.EnhancedComplexTestCase;
41 import convwatch.PropertyName;
42 import helper.OfficeProvider;
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 DocumentConverter 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 "DocumentConverter 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[]{"convert"};
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 convert()
152         {
153             GlobalLogWriter.set(log);
154             // check if all need software is installed and accessable
155             checkEnvironment(mustInstalledSoftware());
156 
157             // test_removeFirstDirectorysAndBasenameFrom();
158             // Get the MultiServiceFactory.
159             // XMultiServiceFactory xMSF = (XMultiServiceFactory)param.getMSF();
160             GraphicalTestArguments aGTA = getGraphicalTestArguments();
161             if (aGTA == null)
162             {
163                 assure("Must quit", false);
164             }
165 
166             initMember();
167 
168             File aInputPath = new File(m_sInputPath);
169             if (aInputPath.isDirectory())
170             {
171                 String fs = System.getProperty("file.separator");
172 
173                 String sRemovePath = aInputPath.getAbsolutePath();
174                 // a whole directory
175                 FileFilter aFileFilter = FileHelper.getFileFilter();
176 
177                 Object[] aList = DirectoryHelper.traverse(m_sInputPath, aFileFilter, aGTA.includeSubDirectories());
178                 for (int i=0;i<aList.length;i++)
179                 {
180                     String sEntry = (String)aList[i];
181 
182                     String sNewReferencePath = m_sReferencePath + fs + FileHelper.removeFirstDirectorysAndBasenameFrom(sEntry, m_sInputPath);
183                     log.println("- next file is: ------------------------------");
184                     log.println(sEntry);
185 
186                     if (aGTA.checkIfUsableDocumentType(sEntry))
187                     {
188                         runGDC(sEntry, sNewReferencePath);
189                     }
190                     if (aGTA.cancelRequest())
191                     {
192                         break;
193                     }
194                 }
195             }
196             else
197             {
198                 if (aGTA.checkIfUsableDocumentType(m_sInputPath))
199                 {
200                     runGDC(m_sInputPath, m_sReferencePath);
201                 }
202             }
203         }
204 
205     void runGDC(String _sInputFile, String _sReferencePath)
206         {
207             // first do a check if the reference not already exist, this is a big speedup, due to the fact,
208             // we don't need to start a new office.
209             GraphicalTestArguments aGTA_local = getGraphicalTestArguments();
210             // if (GraphicalDifferenceCheck.isReferenceExists(_sInputFile, _sReferencePath, aGTA_local) == false)
211             // {
212             // start a fresh Office
213             OfficeProvider aProvider = null;
214             if (aGTA_local.restartOffice())
215             {
216                 aProvider = new OfficeProvider();
217                 XMultiServiceFactory xMSF = (XMultiServiceFactory) aProvider.getManager(param);
218                 param.put("ServiceFactory", xMSF);
219             }
220             GraphicalTestArguments aGTA = getGraphicalTestArguments();
221 
222             if (aGTA.getOfficeProgram().toLowerCase().equals("msoffice"))
223             {
224                 // ReferenceType is MSOffice
225                 GlobalLogWriter.get().println("USE MSOFFICE AS EXPORT FORMAT.");
226                 MSOfficePrint a = new MSOfficePrint();
227                 try
228                 {
229                     String sInputFileBasename = FileHelper.getBasename(_sInputFile);
230                     String fs = System.getProperty("file.separator");
231                     FileHelper.makeDirectories("", _sReferencePath);
232                     String sOutputFile = _sReferencePath;
233                     if (sOutputFile.endsWith(fs))
234                     {
235                         sOutputFile += sInputFileBasename;
236                     }
237                     else
238                     {
239                         sOutputFile += fs + sInputFileBasename;
240                     }
241 
242                     a.storeToFileWithMSOffice(aGTA, _sInputFile, sOutputFile);
243                 }
244                 catch(ConvWatchCancelException e)
245                 {
246                     GlobalLogWriter.get().println(e.getMessage());
247                 }
248                 catch(java.io.IOException e)
249                 {
250                     GlobalLogWriter.get().println(e.getMessage());
251                 }
252             }
253             else
254             {
255                 try
256                 {
257                     OfficePrint.convertDocument(_sInputFile, _sReferencePath, aGTA);
258                 }
259                 catch(ConvWatchCancelException e)
260                 {
261                     assure(e.getMessage(), false);
262                 }
263                 catch(ConvWatchException e)
264                 {
265                     assure(e.getMessage(), false);
266                 }
267             }
268 
269             if (aGTA.restartOffice())
270             {
271                 // Office shutdown
272                 aProvider.closeExistingOffice(param, true);
273             }
274             // }
275         }
276 }
277 
278