1*c7cc89feSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*c7cc89feSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*c7cc89feSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*c7cc89feSAndrew Rist  * distributed with this work for additional information
6*c7cc89feSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*c7cc89feSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*c7cc89feSAndrew Rist  * "License"); you may not use this file except in compliance
9*c7cc89feSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*c7cc89feSAndrew Rist  *
11*c7cc89feSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*c7cc89feSAndrew Rist  *
13*c7cc89feSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*c7cc89feSAndrew Rist  * software distributed under the License is distributed on an
15*c7cc89feSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*c7cc89feSAndrew Rist  * KIND, either express or implied.  See the License for the
17*c7cc89feSAndrew Rist  * specific language governing permissions and limitations
18*c7cc89feSAndrew Rist  * under the License.
19*c7cc89feSAndrew Rist  *
20*c7cc89feSAndrew Rist  *************************************************************/
21*c7cc89feSAndrew Rist 
22*c7cc89feSAndrew Rist 
23cdf0e10cSrcweir package complex.memCheck;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
26cdf0e10cSrcweir import com.sun.star.frame.XStorable;
27cdf0e10cSrcweir import com.sun.star.lang.XComponent;
28cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
30cdf0e10cSrcweir import com.sun.star.util.XCloseable;
31cdf0e10cSrcweir // import complexlib.ComplexTestCase;
32cdf0e10cSrcweir import helper.ProcessHandler;
33cdf0e10cSrcweir import java.io.File;
34cdf0e10cSrcweir // import java.io.FilePermission;
35cdf0e10cSrcweir import java.io.FileWriter;
36cdf0e10cSrcweir import java.io.FilenameFilter;
37cdf0e10cSrcweir import java.io.PrintWriter;
38cdf0e10cSrcweir import java.util.Enumeration;
39cdf0e10cSrcweir import java.util.StringTokenizer;
40cdf0e10cSrcweir import java.util.Vector;
41cdf0e10cSrcweir import lib.*;
42cdf0e10cSrcweir import util.DesktopTools;
43cdf0e10cSrcweir // import util.WriterTools;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir import org.junit.After;
46cdf0e10cSrcweir import org.junit.AfterClass;
47cdf0e10cSrcweir import org.junit.Before;
48cdf0e10cSrcweir import org.junit.BeforeClass;
49cdf0e10cSrcweir import org.junit.Test;
50cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
51cdf0e10cSrcweir import static org.junit.Assert.*;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir /**
54cdf0e10cSrcweir  * Documents are opened and exported with StarOffice. The memory usage of
55cdf0e10cSrcweir  * StarOffice is monitored and if the usage exceeds the allowed kilobytes,
56cdf0e10cSrcweir  * the test is failed. Used for monitoring the StarOffice process is the
57cdf0e10cSrcweir  * command line tool 'pmap', available on Solaris or Linux. This test will not
58cdf0e10cSrcweir  * run on Windows.<br>Test procedure: every given document type is searched in
59cdf0e10cSrcweir  * the source directory
60cdf0e10cSrcweir  * Needed paramters:
61cdf0e10cSrcweir  * <ul>
62cdf0e10cSrcweir  *   <li>"TestDocumentPath" - the path where test documents are located.</li>
63cdf0e10cSrcweir  *   <li>"AllowMemoryIncrease" (optional) - the allowed memory increase measured in kByte per exported document. The default is 10 kByte.</li>
64cdf0e10cSrcweir  *   <li>"ExportDocCount" (optional) - the amount of exports for each document that is loaded. Is defaulted to 25.
65cdf0e10cSrcweir  *   <li>"FileExportFilter" (optional) - a relation between loaded document type and used export filter. Is defaulted to
66cdf0e10cSrcweir  *       writer, calc and impress. This parameter can be set with a number to give more than one relation. Example:<br>
67cdf0e10cSrcweir  *       "FileExportFilter1=sxw,writer_pdf_Export"<br>
68cdf0e10cSrcweir  *       "FileExportFilter2=sxc,calc_pdf_Export"<br>
69cdf0e10cSrcweir  *       "FileExportFilter3=sxi,impress_pdf_Export"<br></li>
70cdf0e10cSrcweir  *       All parameters are used for iteration over the test document path.
71cdf0e10cSrcweir  * </ul>
72cdf0e10cSrcweir  */
73cdf0e10cSrcweir class TempDir
74cdf0e10cSrcweir {
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     private String m_sTempDir;
77cdf0e10cSrcweir 
TempDir(String _sTempDir)78cdf0e10cSrcweir     public TempDir(String _sTempDir)
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         m_sTempDir = _sTempDir;
81cdf0e10cSrcweir     }
82cdf0e10cSrcweir 
getOfficeTempDir()83cdf0e10cSrcweir     public String getOfficeTempDir()
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir         return m_sTempDir;
86cdf0e10cSrcweir     }
87cdf0e10cSrcweir 
getTempDir()88cdf0e10cSrcweir     public String getTempDir()
89cdf0e10cSrcweir     {
90cdf0e10cSrcweir         final String sTempDir = FileHelper.getJavaCompatibleFilename(m_sTempDir);
91cdf0e10cSrcweir         return sTempDir;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir public class CheckMemoryUsage /* extends ComplexTestCase */
96cdf0e10cSrcweir 
97cdf0e10cSrcweir {
98cdf0e10cSrcweir 
99cdf0e10cSrcweir     private final String sWriterDoc = "sxw,writer_pdf_Export";
100cdf0e10cSrcweir     private final String sCalcDoc = "sxc,calc_pdf_Export";
101cdf0e10cSrcweir     private final String sImpressDoc = "sxi,impress_pdf_Export";
102cdf0e10cSrcweir     // private String sProcessIdCommand = null;
103cdf0e10cSrcweir     TempDir m_aTempDir;
104cdf0e10cSrcweir     // private String sFS = null;
105cdf0e10cSrcweir     // private String sMemoryMap1 = null;
106cdf0e10cSrcweir     // private String sMemoryMap2 = null;
107cdf0e10cSrcweir     // private String sDocumentPath = "";
108cdf0e10cSrcweir     private String[][] sDocTypeExportFilter;
109cdf0e10cSrcweir     private String[][] sDocuments;
110cdf0e10cSrcweir     private int iAllowMemoryIncrease = 10;
111cdf0e10cSrcweir     private int iExportDocCount = 25;
112cdf0e10cSrcweir     /**
113cdf0e10cSrcweir      * The test parameters
114cdf0e10cSrcweir      */
115cdf0e10cSrcweir     private static TestParameters param = null;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /**
118cdf0e10cSrcweir      * Get all test methods
119cdf0e10cSrcweir      * @return The test methods.
120cdf0e10cSrcweir     //     */
121cdf0e10cSrcweir //    public String[] getTestMethodNames() {
122cdf0e10cSrcweir //        return new String[] {"loadAndSaveDocuments"};
123cdf0e10cSrcweir //    }
124cdf0e10cSrcweir     /**
125cdf0e10cSrcweir      * Collect all documnets to load and all filters used for export.
126cdf0e10cSrcweir      */
127cdf0e10cSrcweir     @Before
before()128cdf0e10cSrcweir     public void before()
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         final XMultiServiceFactory xMsf = getMSF();
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // some Tests need the qadevOOo TestParameters, it is like a Hashmap for Properties.
134cdf0e10cSrcweir         param = new TestParameters();
135cdf0e10cSrcweir         param.put("ServiceFactory", xMsf); // some qadevOOo functions need the ServiceFactory
136cdf0e10cSrcweir 
137cdf0e10cSrcweir         // test does definitely not run on Windows.
138cdf0e10cSrcweir         if (param.get("OperatingSystem").equals("wntmsci"))
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             System.out.println("Test can only reasonably be executed with a tool that "
141cdf0e10cSrcweir                     + "displays the memory usage of StarOffice.");
142cdf0e10cSrcweir             System.out.println("Test does not run on Windows, only on Solaris or Linux.");
143cdf0e10cSrcweir             // in an automatic environment it is better to say, there is no error here.
144cdf0e10cSrcweir             // it is a limitation, but no error.
145cdf0e10cSrcweir             System.exit(0);
146cdf0e10cSrcweir         }
147cdf0e10cSrcweir 
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         // how many times is every document exported.
150cdf0e10cSrcweir         int count = param.getInt("ExportDocCount");
151cdf0e10cSrcweir         if (count != 0)
152cdf0e10cSrcweir         {
153cdf0e10cSrcweir             iExportDocCount = count;
154cdf0e10cSrcweir         }
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         // get the temp dir for creating the command scripts.
157cdf0e10cSrcweir         // sTempDir = System.getProperty("java.io.tmpdir");
158cdf0e10cSrcweir         m_aTempDir = new TempDir(util.utils.getOfficeTemp/*Dir*/(xMsf));
159cdf0e10cSrcweir 
160cdf0e10cSrcweir         // get the file extension, export filter connection
161cdf0e10cSrcweir         Enumeration keys = param.keys();
162cdf0e10cSrcweir         Vector<String> v = new Vector<String>();
163cdf0e10cSrcweir         while (keys.hasMoreElements())
164cdf0e10cSrcweir         {
165cdf0e10cSrcweir             String key = (String) keys.nextElement();
166cdf0e10cSrcweir             if (key.startsWith("FileExportFilter"))
167cdf0e10cSrcweir             {
168cdf0e10cSrcweir                 v.add((String) param.get(key));
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir         }
171cdf0e10cSrcweir         // if no param given, set defaults.
172cdf0e10cSrcweir         if (v.size() == 0)
173cdf0e10cSrcweir         {
174cdf0e10cSrcweir             v.add(sWriterDoc);
175cdf0e10cSrcweir             v.add(sCalcDoc);
176cdf0e10cSrcweir             v.add(sImpressDoc);
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         // store a file extension
179cdf0e10cSrcweir         sDocTypeExportFilter = new String[v.size()][2];
180cdf0e10cSrcweir         for (int i = 0; i < v.size(); i++)
181cdf0e10cSrcweir         {
182cdf0e10cSrcweir             // 2do: error routine for wrong given params
183cdf0e10cSrcweir             final String sVContent = v.get(i);
184cdf0e10cSrcweir             StringTokenizer t = new StringTokenizer(sVContent, ",");
185cdf0e10cSrcweir             final String sExt = t.nextToken();
186cdf0e10cSrcweir             final String sName = t.nextToken();
187cdf0e10cSrcweir             sDocTypeExportFilter[i][0] = sExt;
188cdf0e10cSrcweir             sDocTypeExportFilter[i][1] = sName;
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir         // get files to load and export
192cdf0e10cSrcweir //        sDocumentPath = (String) param.get("TestDocumentPath");
193cdf0e10cSrcweir         String sDocumentPath = TestDocument.getUrl();
194cdf0e10cSrcweir         File f = new File(FileHelper.getJavaCompatibleFilename(sDocumentPath));
195cdf0e10cSrcweir         // sDocumentPath = f.getAbsolutePath();
196cdf0e10cSrcweir         // String sFS = System.getProperty("file.separator");
197cdf0e10cSrcweir         sDocuments = new String[sDocTypeExportFilter.length][];
198cdf0e10cSrcweir         for (int j = 0; j < sDocTypeExportFilter.length; j++)
199cdf0e10cSrcweir         {
200cdf0e10cSrcweir             FileFilter filter = new FileFilter(sDocTypeExportFilter[j][0]);
201cdf0e10cSrcweir             String[] doc = f.list(filter);
202cdf0e10cSrcweir             sDocuments[j] = new String[doc.length];
203cdf0e10cSrcweir             for (int i = 0; i < doc.length; i++)
204cdf0e10cSrcweir             {
205cdf0e10cSrcweir                 // final String sDocument = FileHelper.appendPath(sDocumentPath, doc[i]);
206cdf0e10cSrcweir                 // sDocuments[j][i] = utils.getFullURL(sDocuments[j][i]);
207cdf0e10cSrcweir                 sDocuments[j][i] = TestDocument.getUrl(doc[i]);
208cdf0e10cSrcweir             }
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir     }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir     /**
213cdf0e10cSrcweir      * delete all created files on disk
214cdf0e10cSrcweir      */
215cdf0e10cSrcweir     @After
after()216cdf0e10cSrcweir     public void after()
217cdf0e10cSrcweir     {
218cdf0e10cSrcweir         // delete the constructed files.
219cdf0e10cSrcweir // we don't need to delete anything, all is stored in $USER_TREE
220cdf0e10cSrcweir //        for (int i = 0; i < iExportDocCount; i++)
221cdf0e10cSrcweir //        {
222cdf0e10cSrcweir //            final String sDocumentName = "DocExport" + i + ".pdf";
223cdf0e10cSrcweir //            final String sFilename = FileHelper.appendPath(m_sTempDir, sDocumentName);
224cdf0e10cSrcweir //            File f = new File(FileHelper.getJavaCompatibleFilename(sFilename));
225cdf0e10cSrcweir //            f.delete();
226cdf0e10cSrcweir //        }
227cdf0e10cSrcweir         // File f = new File(sProcessIdCommand);
228cdf0e10cSrcweir         // f.delete();
229cdf0e10cSrcweir         // f = new File(sOfficeMemoryCommand);
230cdf0e10cSrcweir         // f.delete();
231cdf0e10cSrcweir     }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir     /**
234cdf0e10cSrcweir      * The test function: load documents and save them using the given filters
235cdf0e10cSrcweir      * for each given document type.
236cdf0e10cSrcweir      */
237cdf0e10cSrcweir     @Test
loadAndSaveDocuments()238cdf0e10cSrcweir     public void loadAndSaveDocuments()
239cdf0e10cSrcweir     {
240cdf0e10cSrcweir         int nOk = 0;
241cdf0e10cSrcweir         int nRunThrough = 0;
242cdf0e10cSrcweir 
243cdf0e10cSrcweir         // At first:
244cdf0e10cSrcweir         // we load the document, there will be some post work in office like late initialisations
245cdf0e10cSrcweir         // we store exact one time the document
246cdf0e10cSrcweir         // so the memory footprint should be right
247cdf0e10cSrcweir 
248cdf0e10cSrcweir         // iterate over all document types
249cdf0e10cSrcweir         for (int k = 0; k < sDocTypeExportFilter.length; k++)
250cdf0e10cSrcweir         {
251cdf0e10cSrcweir             // iterate over all documents of this type
252cdf0e10cSrcweir             for (int i = 0; i < sDocuments[k].length; i++)
253cdf0e10cSrcweir             {
254cdf0e10cSrcweir 
255cdf0e10cSrcweir                 final String sDocument = sDocuments[k][i];
256cdf0e10cSrcweir                 final String sExtension = sDocTypeExportFilter[k][1];
257cdf0e10cSrcweir 
258cdf0e10cSrcweir //                OfficeMemchecker aChecker = new OfficeMemchecker();
259cdf0e10cSrcweir //                aChecker.setDocumentName(FileHelper.getBasename(sDocument));
260cdf0e10cSrcweir //                aChecker.setExtension(sExtension);
261cdf0e10cSrcweir //                aChecker.start();
262cdf0e10cSrcweir 
263cdf0e10cSrcweir                 loadAndSaveNTimesDocument(sDocument, 1, sExtension);
264cdf0e10cSrcweir 
265cdf0e10cSrcweir //                nOk += checkMemory(aChecker);
266cdf0e10cSrcweir //                nRunThrough ++;
267cdf0e10cSrcweir             }
268cdf0e10cSrcweir             System.out.println();
269cdf0e10cSrcweir             System.out.println();
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir 
272cdf0e10cSrcweir         shortWait(10000);
273cdf0e10cSrcweir 
274cdf0e10cSrcweir         // Now the real test, load document and store 25 times
275cdf0e10cSrcweir 
276cdf0e10cSrcweir         // iterate over all document types
277cdf0e10cSrcweir         for (int k = 0; k < sDocTypeExportFilter.length; k++)
278cdf0e10cSrcweir         {
279cdf0e10cSrcweir             // iterate over all documents of this type
280cdf0e10cSrcweir             for (int i = 0; i < sDocuments[k].length; i++)
281cdf0e10cSrcweir             {
282cdf0e10cSrcweir 
283cdf0e10cSrcweir                 final String sDocument = sDocuments[k][i];
284cdf0e10cSrcweir                 final String sExtension = sDocTypeExportFilter[k][1];
285cdf0e10cSrcweir 
286cdf0e10cSrcweir                 OfficeMemchecker aChecker = new OfficeMemchecker();
287cdf0e10cSrcweir                 aChecker.setDocumentName(FileHelper.getBasename(sDocument));
288cdf0e10cSrcweir                 aChecker.setExtension(sExtension);
289cdf0e10cSrcweir                 aChecker.start();
290cdf0e10cSrcweir 
291cdf0e10cSrcweir                 loadAndSaveNTimesDocument(sDocument, iExportDocCount, sExtension);
292cdf0e10cSrcweir 
293cdf0e10cSrcweir                 aChecker.stop();
294cdf0e10cSrcweir                 final int nConsumMore = aChecker.getConsumMore();
295cdf0e10cSrcweir 
296cdf0e10cSrcweir                 nOk += checkMemory(nConsumMore);
297cdf0e10cSrcweir                 nRunThrough++;
298cdf0e10cSrcweir             }
299cdf0e10cSrcweir             System.out.println();
300cdf0e10cSrcweir             System.out.println();
301cdf0e10cSrcweir         }
302cdf0e10cSrcweir         System.out.println("Find the output of used 'pmap' here: " + m_aTempDir.getTempDir() + " if test failed.");
303cdf0e10cSrcweir         assertTrue("Office consumes too many memory.", nOk == nRunThrough);
304cdf0e10cSrcweir     }
305cdf0e10cSrcweir 
306cdf0e10cSrcweir     /**
307cdf0e10cSrcweir      * Checks how much memory should consum
308cdf0e10cSrcweir      * @param storageBefore
309cdf0e10cSrcweir      * @return 1 if consum is ok, else 0
310cdf0e10cSrcweir      */
checkMemory(int nConsumMore)311cdf0e10cSrcweir     private int checkMemory(int nConsumMore)
312cdf0e10cSrcweir     {
313cdf0e10cSrcweir         int nAllowed = iAllowMemoryIncrease * iExportDocCount;
314cdf0e10cSrcweir         System.out.println("The Office consumes now " + nConsumMore
315cdf0e10cSrcweir                 + "K more memory than at the start of the test; allowed were "
316cdf0e10cSrcweir                 + nAllowed + "K.");
317cdf0e10cSrcweir         if (nConsumMore > nAllowed)
318cdf0e10cSrcweir         {
319cdf0e10cSrcweir             System.out.println("ERROR: This is not allowed.");
320cdf0e10cSrcweir             return 0;
321cdf0e10cSrcweir         }
322cdf0e10cSrcweir         System.out.println("OK.");
323cdf0e10cSrcweir         return 1;
324cdf0e10cSrcweir     }
325cdf0e10cSrcweir 
326cdf0e10cSrcweir     /**
327cdf0e10cSrcweir      * load and save exact one document
328cdf0e10cSrcweir      */
loadAndSaveNTimesDocument(String _sDocument, int _nCount, String _sStoreExtension)329cdf0e10cSrcweir     private void loadAndSaveNTimesDocument(String _sDocument, int _nCount, String _sStoreExtension)
330cdf0e10cSrcweir     {
331cdf0e10cSrcweir         System.out.println("Document: " + _sDocument);
332cdf0e10cSrcweir         XComponent xComponent = DesktopTools.loadDoc(getMSF(), _sDocument, null);
333cdf0e10cSrcweir         XStorable xStorable = UnoRuntime.queryInterface(XStorable.class, xComponent);
334cdf0e10cSrcweir         if (xStorable != null)
335cdf0e10cSrcweir         {
336cdf0e10cSrcweir             // export each document iExportDocCount times
337cdf0e10cSrcweir             for (int j = 0; j < _nCount; j++)
338cdf0e10cSrcweir             {
339cdf0e10cSrcweir                 final String sDocumentName = FileHelper.getBasename(_sDocument) + "_" + j + ".pdf";
340cdf0e10cSrcweir                 final String sFilename = FileHelper.appendPath(m_aTempDir.getOfficeTempDir(), sDocumentName);
341cdf0e10cSrcweir                 // String url = utils.getFullURL(sFilename);
342cdf0e10cSrcweir                 String url = sFilename; // graphical.FileHelper.getFileURLFromSystemPath(sFilename);
343cdf0e10cSrcweir                 try
344cdf0e10cSrcweir                 {
345cdf0e10cSrcweir                     PropertyValue[] props = new PropertyValue[1];
346cdf0e10cSrcweir                     props[0] = new PropertyValue();
347cdf0e10cSrcweir                     props[0].Name = "FilterName";
348cdf0e10cSrcweir                     // use export filter for this doc type
349cdf0e10cSrcweir                     props[0].Value = _sStoreExtension;
350cdf0e10cSrcweir                     xStorable.storeToURL(url, props);
351cdf0e10cSrcweir                 }
352cdf0e10cSrcweir                 catch (com.sun.star.io.IOException e)
353cdf0e10cSrcweir                 {
354cdf0e10cSrcweir                     fail("Could not store to '" + url + "'");
355cdf0e10cSrcweir                 }
356cdf0e10cSrcweir             }
357cdf0e10cSrcweir             // close the doc
358cdf0e10cSrcweir             XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xStorable);
359cdf0e10cSrcweir             try
360cdf0e10cSrcweir             {
361cdf0e10cSrcweir                 xCloseable.close(true);
362cdf0e10cSrcweir             }
363cdf0e10cSrcweir             catch (com.sun.star.util.CloseVetoException e)
364cdf0e10cSrcweir             {
365cdf0e10cSrcweir                 e.printStackTrace();
366cdf0e10cSrcweir                 fail("Cannot close document: test is futile, Office will surely use more space.");
367cdf0e10cSrcweir             }
368cdf0e10cSrcweir         }
369cdf0e10cSrcweir         else
370cdf0e10cSrcweir         {
371cdf0e10cSrcweir             System.out.println("Cannot query for XStorable interface on document '" + _sDocument + "'");
372cdf0e10cSrcweir             System.out.println(" -> Skipping storage.");
373cdf0e10cSrcweir         }
374cdf0e10cSrcweir 
375cdf0e10cSrcweir     }
376cdf0e10cSrcweir 
377cdf0e10cSrcweir // -----------------------------------------------------------------------------
378cdf0e10cSrcweir     private class OfficeMemchecker
379cdf0e10cSrcweir     {
380cdf0e10cSrcweir 
381cdf0e10cSrcweir         /**
382cdf0e10cSrcweir          * After called start() it contains the memory need at startup
383cdf0e10cSrcweir          */
384cdf0e10cSrcweir         private int m_nMemoryStart;
385cdf0e10cSrcweir         /**
386cdf0e10cSrcweir          * After called stop() it contains the memory usage
387cdf0e10cSrcweir          */
388cdf0e10cSrcweir         private int m_nMemoryUsage;
389cdf0e10cSrcweir         private String m_sDocumentName;
390cdf0e10cSrcweir         private String m_sExtension;
391cdf0e10cSrcweir 
OfficeMemchecker()392cdf0e10cSrcweir         public OfficeMemchecker()
393cdf0e10cSrcweir         {
394cdf0e10cSrcweir             m_nMemoryStart = 0;
395cdf0e10cSrcweir         }
396cdf0e10cSrcweir 
setDocumentName(String _sDocName)397cdf0e10cSrcweir         public void setDocumentName(String _sDocName)
398cdf0e10cSrcweir         {
399cdf0e10cSrcweir             m_sDocumentName = _sDocName;
400cdf0e10cSrcweir         }
401cdf0e10cSrcweir 
setExtension(String _sExt)402cdf0e10cSrcweir         public void setExtension(String _sExt)
403cdf0e10cSrcweir         {
404cdf0e10cSrcweir             m_sExtension = _sExt;
405cdf0e10cSrcweir         }
406cdf0e10cSrcweir 
start()407cdf0e10cSrcweir         public void start()
408cdf0e10cSrcweir         {
409cdf0e10cSrcweir             m_nMemoryStart = getOfficeMemoryUsage(createModeName("start", 0));
410cdf0e10cSrcweir         }
411cdf0e10cSrcweir 
createModeName(String _sSub, int _nCount)412cdf0e10cSrcweir         private String createModeName(String _sSub, int _nCount)
413cdf0e10cSrcweir         {
414cdf0e10cSrcweir             StringBuffer aBuf = new StringBuffer();
415cdf0e10cSrcweir             aBuf.append(_sSub);
416cdf0e10cSrcweir             aBuf.append('_').append(m_sDocumentName).append('_').append(m_sExtension);
417cdf0e10cSrcweir             aBuf.append('_').append(_nCount);
418cdf0e10cSrcweir             return aBuf.toString();
419cdf0e10cSrcweir         }
420cdf0e10cSrcweir 
stop()421cdf0e10cSrcweir         public void stop()
422cdf0e10cSrcweir         {
423cdf0e10cSrcweir             // short wait for the office to 'calm down' and free some memory
424cdf0e10cSrcweir             shortWait(20000);
425cdf0e10cSrcweir             // wait util memory is not freed anymore.
426cdf0e10cSrcweir             int storageAfter = getOfficeMemoryUsage(createModeName("stop", 0));
427cdf0e10cSrcweir             int mem = 0;
428cdf0e10cSrcweir             int count = 0;
429cdf0e10cSrcweir             while (storageAfter != mem && count < 10)
430cdf0e10cSrcweir             {
431cdf0e10cSrcweir                 count++;
432cdf0e10cSrcweir                 mem = storageAfter;
433cdf0e10cSrcweir                 storageAfter = getOfficeMemoryUsage(createModeName("stop", count));
434cdf0e10cSrcweir                 shortWait(1000);
435cdf0e10cSrcweir             }
436cdf0e10cSrcweir             m_nMemoryUsage = (storageAfter - m_nMemoryStart);
437cdf0e10cSrcweir         }
438cdf0e10cSrcweir 
getConsumMore()439cdf0e10cSrcweir         public int getConsumMore()
440cdf0e10cSrcweir         {
441cdf0e10cSrcweir             return m_nMemoryUsage;
442cdf0e10cSrcweir         }
443cdf0e10cSrcweir 
444cdf0e10cSrcweir         /**
445cdf0e10cSrcweir          * Get the process ID from the Office
446cdf0e10cSrcweir          * @return the Id as String
447cdf0e10cSrcweir          */
getOfficeProcessID()448cdf0e10cSrcweir         private String getOfficeProcessID()
449cdf0e10cSrcweir         {
450cdf0e10cSrcweir             String sProcessIdCommand = FileHelper.appendPath(m_aTempDir.getTempDir(), "getPS");
451cdf0e10cSrcweir             final String sofficeArg = org.openoffice.test.Argument.get("soffice");
452cdf0e10cSrcweir             final String sPSGrep = "ps -ef | grep $USER | grep <soffice>.bin | grep -v grep";
453cdf0e10cSrcweir             final String sProcessId = sPSGrep.replaceAll("<soffice>", FileHelper.getJavaCompatibleFilename(sofficeArg));
454cdf0e10cSrcweir 
455cdf0e10cSrcweir             createExecutableFile(sProcessIdCommand, sProcessId);
456cdf0e10cSrcweir             ProcessHandler processID = new ProcessHandler(sProcessIdCommand);
457cdf0e10cSrcweir             processID.noOutput();
458cdf0e10cSrcweir             processID.executeSynchronously();
459cdf0e10cSrcweir             String text = processID.getOutputText();
460cdf0e10cSrcweir             if (text == null || text.equals("") || text.indexOf(' ') == -1)
461cdf0e10cSrcweir             {
462cdf0e10cSrcweir                 fail("Could not determine Office process ID. Check " + sProcessIdCommand);
463cdf0e10cSrcweir             }
464cdf0e10cSrcweir             StringTokenizer aToken = new StringTokenizer(text);
465cdf0e10cSrcweir             // this is not nice, but ps gives the same output on every machine
466cdf0e10cSrcweir             aToken.nextToken();
467cdf0e10cSrcweir             String id = aToken.nextToken();
468cdf0e10cSrcweir             return id;
469cdf0e10cSrcweir         }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir         /**
472cdf0e10cSrcweir          * Get the memory usage of the Office in KByte.
473cdf0e10cSrcweir          * @return The memory used by the Office.
474cdf0e10cSrcweir          */
getOfficeMemoryUsage(String _sMode)475cdf0e10cSrcweir         private int getOfficeMemoryUsage(String _sMode)
476cdf0e10cSrcweir         {
477cdf0e10cSrcweir             final String sMemoryMonitor = "pmap <processID> |tee <pmapoutputfile> | grep total";
478cdf0e10cSrcweir             String sOfficeMemoryCommand = null;
479cdf0e10cSrcweir             sOfficeMemoryCommand = FileHelper.appendPath(m_aTempDir.getTempDir(), "getPmap");
480cdf0e10cSrcweir             // sOfficeMemoryCommand = FileHelper.getJavaCompatibleFilename(sOfficeMemoryCommand);
481cdf0e10cSrcweir             String command = sMemoryMonitor.replaceAll("<processID>", getOfficeProcessID());
482cdf0e10cSrcweir             String sPmapOutputFile = FileHelper.appendPath(m_aTempDir.getTempDir(), "pmap_" + _sMode + ".txt");
483cdf0e10cSrcweir             command = command.replaceAll("<pmapoutputfile>", sPmapOutputFile);
484cdf0e10cSrcweir             createExecutableFile(sOfficeMemoryCommand, command);
485cdf0e10cSrcweir 
486cdf0e10cSrcweir             ProcessHandler processID = new ProcessHandler(sOfficeMemoryCommand);
487cdf0e10cSrcweir             processID.noOutput();
488cdf0e10cSrcweir             processID.executeSynchronously();
489cdf0e10cSrcweir             int nError = processID.getExitCode();
490cdf0e10cSrcweir             assertTrue("Execute of " + sOfficeMemoryCommand + " failed", nError == 0);
491cdf0e10cSrcweir             String text = processID.getOutputText();
492cdf0e10cSrcweir             if (text == null || text.equals("") || text.indexOf(' ') == -1)
493cdf0e10cSrcweir             {
494cdf0e10cSrcweir                 fail("Could not determine Office memory usage. Check " + sOfficeMemoryCommand);
495cdf0e10cSrcweir             }
496cdf0e10cSrcweir             StringTokenizer aToken = new StringTokenizer(text);
497cdf0e10cSrcweir             // this works, because the output of pmap is quite standardized.
498cdf0e10cSrcweir             aToken.nextToken();
499cdf0e10cSrcweir             String mem = aToken.nextToken();
500cdf0e10cSrcweir             mem = mem.substring(0, mem.indexOf('K'));
501cdf0e10cSrcweir             Integer memory = new Integer(mem);
502cdf0e10cSrcweir             return memory.intValue();
503cdf0e10cSrcweir         }
504cdf0e10cSrcweir 
505cdf0e10cSrcweir         /**
506cdf0e10cSrcweir          * Write a script file and set its rights to rwxrwxrwx.
507cdf0e10cSrcweir          * @param fileName The name of the created file
508cdf0e10cSrcweir          * @param line The commandline that has to be written inside of the file.
509cdf0e10cSrcweir          */
createExecutableFile(String fileName, String line)510cdf0e10cSrcweir         private void createExecutableFile(String fileName, String line)
511cdf0e10cSrcweir         {
512cdf0e10cSrcweir             final String sChmod = "chmod a+x ";
513cdf0e10cSrcweir             final String bash = "#!/bin/bash";
514cdf0e10cSrcweir 
515cdf0e10cSrcweir             try
516cdf0e10cSrcweir             {
517cdf0e10cSrcweir                 String sFilename = FileHelper.getJavaCompatibleFilename(fileName);
518cdf0e10cSrcweir                 PrintWriter fWriter = new PrintWriter(new FileWriter(sFilename));
519cdf0e10cSrcweir                 fWriter.println(bash);
520cdf0e10cSrcweir                 fWriter.println(line);
521cdf0e10cSrcweir                 fWriter.close();
522cdf0e10cSrcweir                 // change rights to rwxrwxrwx
523cdf0e10cSrcweir                 ProcessHandler processID = new ProcessHandler(sChmod + sFilename);
524cdf0e10cSrcweir                 processID.noOutput();
525cdf0e10cSrcweir                 processID.executeSynchronously();
526cdf0e10cSrcweir                 int nError = processID.getExitCode();
527cdf0e10cSrcweir                 assertTrue("chmod failed. ", nError == 0);
528cdf0e10cSrcweir             }
529cdf0e10cSrcweir             catch (java.io.IOException e)
530cdf0e10cSrcweir             {
531cdf0e10cSrcweir             }
532cdf0e10cSrcweir         }
533cdf0e10cSrcweir     }
534cdf0e10cSrcweir 
535cdf0e10cSrcweir     /**
536cdf0e10cSrcweir      * Let this thread sleep for some time
537cdf0e10cSrcweir      * @param milliSeconds time to wait in milliseconds.
538cdf0e10cSrcweir      */
shortWait(int milliSeconds)539cdf0e10cSrcweir     public static void shortWait(int milliSeconds)
540cdf0e10cSrcweir     {
541cdf0e10cSrcweir         System.out.println("Wait for: " + milliSeconds + "ms");
542cdf0e10cSrcweir         try
543cdf0e10cSrcweir         {
544cdf0e10cSrcweir             Thread.sleep(milliSeconds);
545cdf0e10cSrcweir         }
546cdf0e10cSrcweir         catch (java.lang.InterruptedException e)
547cdf0e10cSrcweir         { // ignore
548cdf0e10cSrcweir         }
549cdf0e10cSrcweir     }
550cdf0e10cSrcweir 
551cdf0e10cSrcweir     /**
552cdf0e10cSrcweir      * Own file filter, will just return ok for all files that end with a given
553cdf0e10cSrcweir      * suffix
554cdf0e10cSrcweir      */
555cdf0e10cSrcweir     private class FileFilter implements FilenameFilter
556cdf0e10cSrcweir     {
557cdf0e10cSrcweir 
558cdf0e10cSrcweir         private String suffix = null;
559cdf0e10cSrcweir 
560cdf0e10cSrcweir         /**
561cdf0e10cSrcweir          * C'tor.
562cdf0e10cSrcweir          * @param suffix The suffix each filename should end with.
563cdf0e10cSrcweir          */
FileFilter(String suffix)564cdf0e10cSrcweir         public FileFilter(String suffix)
565cdf0e10cSrcweir         {
566cdf0e10cSrcweir             this.suffix = suffix;
567cdf0e10cSrcweir         }
568cdf0e10cSrcweir 
569cdf0e10cSrcweir         /**
570cdf0e10cSrcweir          * Returns true, if the name of the file has the suffix given to the
571cdf0e10cSrcweir          * c'tor.
572cdf0e10cSrcweir          * @param name The filename that is tested.
573cdf0e10cSrcweir          * @param file Not used.
574cdf0e10cSrcweir          * @return True, if name ends with suffix.
575cdf0e10cSrcweir          */
accept(File file, String name)576cdf0e10cSrcweir         public boolean accept(File file, String name)
577cdf0e10cSrcweir         {
578cdf0e10cSrcweir             return name.endsWith(suffix);
579cdf0e10cSrcweir         }
580cdf0e10cSrcweir     }
581cdf0e10cSrcweir 
getMSF()582cdf0e10cSrcweir     private XMultiServiceFactory getMSF()
583cdf0e10cSrcweir     {
584cdf0e10cSrcweir         final XMultiServiceFactory xMSF1 = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
585cdf0e10cSrcweir         return xMSF1;
586cdf0e10cSrcweir     }
587cdf0e10cSrcweir 
588cdf0e10cSrcweir     // setup and close connections
589cdf0e10cSrcweir     @BeforeClass
setUpConnection()590cdf0e10cSrcweir     public static void setUpConnection() throws Exception
591cdf0e10cSrcweir     {
592cdf0e10cSrcweir         System.out.println("setUpConnection()");
593cdf0e10cSrcweir         connection.setUp();
594cdf0e10cSrcweir     }
595cdf0e10cSrcweir 
596cdf0e10cSrcweir     @AfterClass
tearDownConnection()597cdf0e10cSrcweir     public static void tearDownConnection()
598cdf0e10cSrcweir             throws InterruptedException, com.sun.star.uno.Exception
599cdf0e10cSrcweir     {
600cdf0e10cSrcweir         System.out.println("tearDownConnection()");
601cdf0e10cSrcweir         connection.tearDown();
602cdf0e10cSrcweir     }
603cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
604cdf0e10cSrcweir }
605