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