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