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