1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.system;
29 
30 import lib.MultiMethodTest;
31 import util.utils;
32 
33 import com.sun.star.lang.XMultiServiceFactory;
34 import com.sun.star.system.XSystemShellExecute;
35 import com.sun.star.ucb.XSimpleFileAccess;
36 import com.sun.star.uno.UnoRuntime;
37 
38 
39 /**
40 * Testing <code>com.sun.star.system.XSystemShellExecute</code>
41 * interface methods :
42 * <ul>
43 *  <li><code> execute()</code></li>
44 * </ul> <p>
45 * Test is <b> NOT </b> multithread compilant. <p>
46 * @see com.sun.star.system.XSystemShellExecute
47 */
48 public class _XSystemShellExecute extends MultiMethodTest {
49 
50     public XSystemShellExecute oObj = null;
51 
52     /**
53     * Excecutes 'java SystemShellExecute SystemShellExecute.txt' command line.
54     * <p>Has <b> OK </b> status if the method successfully returns
55     * and file 'SystemShellExecute.txt' was created. <p>
56     */
57     public void _execute() {
58         String cClassPath = System.getProperty("DOCPTH");
59         String cResFile = utils.getOfficeTempDirSys((XMultiServiceFactory)tParam.getMSF())+"SystemShellExecute.txt";
60         String cResURL = utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF())+"SystemShellExecute.txt";
61         String cArgs = "-classpath " + cClassPath +
62                        " SystemShellExecute " + cResFile;
63 
64         String jh = System.getProperty("java.home");
65         String fs = System.getProperty("file.separator");
66         String cmd = jh+fs+"bin"+fs+"java";
67 
68         log.println("Executing : '"+cmd+" " + cArgs + "'");
69         try {
70             oObj.execute(cmd, cArgs, 1);
71         } catch (com.sun.star.system.SystemShellExecuteException e) {
72             log.println("Exception during execute: " + e);
73             log.println("This has been implemented due to security reasons");
74             tRes.tested("execute()", true);
75             return;
76         } catch (com.sun.star.lang.IllegalArgumentException e) {
77             log.println("Exception during execute: " + e);
78             tRes.tested("execute()", false);
79             return;
80         }
81 
82         XSimpleFileAccess xFileAccess = null;
83         try {
84             XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
85             Object fa = xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
86             xFileAccess = (XSimpleFileAccess)
87                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
88         } catch (com.sun.star.uno.Exception e) {
89             log.println("Couldn't create SimpleFileAccess:" + e);
90             tRes.tested("execute()", false);
91         }
92 
93         log.println("Waiting while the file will be created or timeout "+
94             "reached ...");
95         boolean bExist = false;
96         int i = 0;
97         while (i < 20 && !bExist) {
98             try {
99                 bExist = xFileAccess.exists(cResURL);
100             } catch(com.sun.star.uno.Exception e) {
101                 log.println("Exception:" + e);
102             }
103             shortWait();
104             i++;
105         }
106 
107         if (bExist) {
108             log.println("The command was executed and file created in " +
109                  i + " sec.");
110         } else {
111             log.println("File was not created");
112         }
113 
114         tRes.tested("execute()", bExist);
115     }
116 
117     /**
118     * Sleeps to allow StarOffice to react on <code>
119     * reset</code> call.
120     */
121     private void shortWait() {
122         try {
123             Thread.sleep(1000) ;
124         } catch (InterruptedException e) {
125             log.println("While waiting :" + e) ;
126         }
127     }
128 }  // finish class _XSystemShellExecute
129 
130 
131