1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package ifc.system;
25 
26 import lib.MultiMethodTest;
27 import util.utils;
28 
29 import com.sun.star.lang.XMultiServiceFactory;
30 import com.sun.star.system.XSystemShellExecute;
31 import com.sun.star.ucb.XSimpleFileAccess;
32 import com.sun.star.uno.UnoRuntime;
33 
34 
35 /**
36 * Testing <code>com.sun.star.system.XSystemShellExecute</code>
37 * interface methods :
38 * <ul>
39 *  <li><code> execute()</code></li>
40 * </ul> <p>
41 * Test is <b> NOT </b> multithread compilant. <p>
42 * @see com.sun.star.system.XSystemShellExecute
43 */
44 public class _XSystemShellExecute extends MultiMethodTest {
45 
46     public XSystemShellExecute oObj = null;
47 
48     /**
49     * Executes 'java SystemShellExecute SystemShellExecute.txt' command line.
50     * <p>Has <b> OK </b> status if the method successfully returns
51     * and file 'SystemShellExecute.txt' was created. <p>
52     */
_execute()53     public void _execute() {
54         String cClassPath = System.getProperty("DOCPTH");
55         String cResFile = utils.getOfficeTempDirSys((XMultiServiceFactory)tParam.getMSF())+"SystemShellExecute.txt";
56         String cResURL = utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF())+"SystemShellExecute.txt";
57         String cArgs = "-classpath " + cClassPath +
58                        " SystemShellExecute " + cResFile;
59 
60         String jh = System.getProperty("java.home");
61         String fs = System.getProperty("file.separator");
62         String cmd = jh+fs+"bin"+fs+"java";
63 
64         log.println("Executing : '"+cmd+" " + cArgs + "'");
65         try {
66             oObj.execute(cmd, cArgs, 1);
67         } catch (com.sun.star.system.SystemShellExecuteException e) {
68             log.println("Exception during execute: " + e);
69             log.println("This has been implemented due to security reasons");
70             tRes.tested("execute()", true);
71             return;
72         } catch (com.sun.star.lang.IllegalArgumentException e) {
73             log.println("Exception during execute: " + e);
74             tRes.tested("execute()", false);
75             return;
76         }
77 
78         XSimpleFileAccess xFileAccess = null;
79         try {
80             XMultiServiceFactory xMSF = (XMultiServiceFactory)tParam.getMSF();
81             Object fa = xMSF.createInstance("com.sun.star.ucb.SimpleFileAccess");
82             xFileAccess = (XSimpleFileAccess)
83                 UnoRuntime.queryInterface(XSimpleFileAccess.class, fa);
84         } catch (com.sun.star.uno.Exception e) {
85             log.println("Couldn't create SimpleFileAccess:" + e);
86             tRes.tested("execute()", false);
87         }
88 
89         log.println("Waiting while the file will be created or timeout "+
90             "reached ...");
91         boolean bExist = false;
92         int i = 0;
93         while (i < 20 && !bExist) {
94             try {
95                 bExist = xFileAccess.exists(cResURL);
96             } catch(com.sun.star.uno.Exception e) {
97                 log.println("Exception:" + e);
98             }
99             shortWait();
100             i++;
101         }
102 
103         if (bExist) {
104             log.println("The command was executed and file created in " +
105                  i + " sec.");
106         } else {
107             log.println("File was not created");
108         }
109 
110         tRes.tested("execute()", bExist);
111     }
112 
113     /**
114     * Sleeps to allow StarOffice to react on <code>
115     * reset</code> call.
116     */
shortWait()117     private void shortWait() {
118         try {
119             Thread.sleep(1000) ;
120         } catch (InterruptedException e) {
121             log.println("While waiting :" + e) ;
122         }
123     }
124 }  // finish class _XSystemShellExecute
125 
126 
127