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 complex.tdoc;
25 
26 import com.sun.star.beans.Property;
27 import com.sun.star.beans.XPropertySetInfo;
28 import lib.MultiMethodTest;
29 import lib.StatusException;
30 
31 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.ucb.Command;
33 import com.sun.star.ucb.CommandAbortedException;
34 import com.sun.star.ucb.CommandInfo;
35 import com.sun.star.ucb.GlobalTransferCommandArgument;
36 import com.sun.star.ucb.NameClash;
37 import com.sun.star.ucb.TransferCommandOperation;
38 import com.sun.star.ucb.UnsupportedCommandException;
39 import com.sun.star.ucb.XCommandInfo;
40 import com.sun.star.ucb.XCommandProcessor;
41 import com.sun.star.uno.Exception;
42 import com.sun.star.uno.UnoRuntime;
43 import share.LogWriter;
44 
45 /**
46 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
47 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
48 * <code>abort()</code> test.
49 *
50 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
51 * interface methods :
52 * <ul>
53 *  <li><code> createCommandIdentifier()</code></li>
54 *  <li><code> execute()</code></li>
55 *  <li><code> abort()</code></li>
56 * </ul> <p>
57 * This test needs the following object relations :
58 * <ul>
59 *  <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
60 *   (of type <code>com.sun.star.ucb.Command</code>):
61 *   specify command to abort in <code>abort()</code> test.
62 *   If the relation is not specified the 'GlobalTransfer'
63 *   command is used.</li>
64 * <ul> <p>
65 * The following predefined files needed to complete the test:
66 * <ul>
67 *  <li> <code>poliball.gif</code> : this file is required in case
68 *   if the relation <code>'XCommandProcessor.AbortCommand'</code>
69 *   is not specified. This file is used by 'GlobalTransfer'
70 *   command as a source file for copying.</li>
71 * <ul> <p>
72 * Test is <b> NOT </b> multithread compilant. <p>
73 * @see com.sun.star.ucb.XCommandProcessor
74 */
75 public class _XCommandProcessor {
76 
77     /**
78      * Conatins the tested object.
79      */
80     public XCommandProcessor oObj;
81     public LogWriter log = null;
82     private XMultiServiceFactory xMSF = null;
83 
84     /**
85      * Contains the command id returned by <code>createCommandIdentifier()
86      * </code>. It is used in <code>abort()</code> test.
87      */
88     int cmdId;
89 
before(XMultiServiceFactory _xMSF)90     public void before(XMultiServiceFactory _xMSF) {
91         xMSF = _xMSF;
92     }
93 
94     /**
95      * Tests <code>createCommandIdentifier()</code>. Calls it for two times
96      * and checks returned values. <p>
97      * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
98      */
_createCommandIdentifier()99     public boolean _createCommandIdentifier() {
100         log.println("creating a command line identifier");
101 
102         int testCmdId = oObj.createCommandIdentifier();
103         cmdId = oObj.createCommandIdentifier();
104 
105         if (cmdId == 0 || testCmdId == 0) {
106             log.println("createCommandLineIdentifier() returned 0 - FAILED");
107         }
108 
109         if (cmdId == testCmdId) {
110             log.println("the command identifier is not unique");
111         }
112 
113         return testCmdId != 0 && cmdId != 0 && cmdId != testCmdId;
114     }
115 
116     /**
117      * First executes 'geCommandInfo' command and examines returned
118      * command info information. Second tries to execute inproper
119      * command. <p>
120      * Has <b> OK </b> status if in the first case returned information
121      * contains info about 'getCommandInfo' command and in the second
122      * case an exception is thrown. <p>
123      */
_execute()124     public boolean _execute() {
125         String[]commands = new String[] {"getCommandInfo", "getPropertySetInfo"};
126         boolean returnVal = true;
127         for (int j=0; j<commands.length; j++) {
128             String commandName = commands[j];
129             Command command = new Command(commandName, -1, null);
130 
131             Object result;
132 
133             log.println("executing command " + commandName);
134             try {
135                 result = oObj.execute(command, 0, null);
136             } catch (CommandAbortedException e) {
137                 log.println("The command aborted " + e.getMessage());
138                 e.printStackTrace((java.io.PrintWriter)log);
139                 throw new StatusException("Unexpected exception", e);
140             } catch (Exception e) {
141                 log.println("Unexpected exception " + e.getMessage());
142                 e.printStackTrace((java.io.PrintWriter)log);
143                 throw new StatusException("Unexpected exception", e);
144             }
145 
146             boolean found = false;
147 
148             XCommandInfo xCmdInfo = (XCommandInfo)UnoRuntime.queryInterface(
149                     XCommandInfo.class, result);
150             if (xCmdInfo != null) {
151                 CommandInfo[] cmdInfo = xCmdInfo.getCommands();
152     	        for (int i = 0; i < cmdInfo.length; i++) {
153                     log.println("\t##### " + cmdInfo[i].Name + " - " + cmdInfo[i].Handle + " - " + cmdInfo[i].ArgType.getTypeName());
154         	        if (cmdInfo[i].Name.equals(commandName)) {
155             	        found = true;
156 //                	    break;
157 	                }
158     	        }
159             }
160 
161             XPropertySetInfo xPropInfo = (XPropertySetInfo)UnoRuntime.queryInterface(
162                     XPropertySetInfo.class, result);
163             if (xPropInfo != null) {
164                 Property[] props = xPropInfo.getProperties();
165                 String[] defProps = new String[] {"ContentType", "IsDocument", "IsFolder", "Title"};
166                 int propCount = defProps.length;
167     	        for (int i = 0; i < props.length; i++) {
168                     for (int k=0; k<defProps.length; k++) {
169                         if (props[i].Name.equals(defProps[k])) {
170                             propCount--;
171                             log.println("Property '" + defProps[k] + "' has been found.");
172                         }
173                     }
174     	        }
175                 found = propCount == 0;
176             }
177 
178             returnVal &= found;
179 
180             if (!found) {
181                 log.println("Command '" + commandName + "' was not executed correctly.");
182             }
183         }
184 /*        log.println("testing execute with wrong command");
185 
186         Command badCommand = new Command("bad command", -1, null);
187 
188         try {
189             oObj.execute(badCommand, 0, null);
190         } catch (CommandAbortedException e) {
191             log.println("CommandAbortedException thrown - OK");
192         } catch (UnsupportedCommandException e) {
193             log.println("UnsupportedCommandException thrown - OK");
194         } catch (Exception e) {
195             log.println("Wrong exception thrown " + e.getMessage());
196             e.printStackTrace((java.io.PrintWriter)log);
197             throw new StatusException("Unexpected exception", e);
198         }
199 */
200         return returnVal;
201     }
202 
203     /**
204      * First a separate thread where <code>abort</code> method
205      * is called permanently. Then a "long" command (for example,
206      * "transfer") is started. I case if relation is not
207      * specified 'GlobalTransfer' command starts to
208      * copy a file to temporary directory (if the relation is present
209      * then the its command starts to work). <p>
210      * Has <b> OK </b> status if the command execution is aborted, i.e.
211      * <code>CommandAbortedException</code> is thrown. <p>
212      * The following method tests are to be completed successfully before :
213      * <ul>
214      *  <li> <code> createCommandIdentifier() </code> : to have a unique
215      *  identifier which is used to abourt started command. </li>
216      * </ul>
217      */
_abort()218     public boolean _abort() {
219         //executeMethod("createCommandIdentifier()");
220 
221 //        Command command = null;//(Command)tEnv.getObjRelation(
222                 //"XCommandProcessor.AbortCommand");
223         Command command = new Command("getCommandInfo", -1, null);
224 
225         if (command == null) {
226             String commandName = "globalTransfer";
227 
228             String srcURL = util.utils.getFullTestURL("solibrary.jar") ;
229             String tmpURL = util.utils.getOfficeTemp(xMSF) ;
230             log.println("Copying '" + srcURL + "' to '" + tmpURL) ;
231 
232             GlobalTransferCommandArgument arg = new
233                 GlobalTransferCommandArgument(
234                     TransferCommandOperation.COPY, srcURL,
235                         tmpURL, "", NameClash.OVERWRITE);
236 
237             command = new Command(commandName, -1, arg);
238         }
239 
240         Thread aborter = new Thread() {
241             public void run() {
242                 for (int i = 0; i < 10; i++) {
243                     log.println("try to abort command");
244                     oObj.abort(cmdId);
245                     try {
246                         Thread.sleep(10);
247                     } catch (InterruptedException e) {
248                     }
249                 }
250             }
251         };
252 
253         aborter.start();
254 
255         try {
256             Thread.sleep(15);
257         } catch (InterruptedException e) {
258         }
259 
260         log.println("executing command");
261         try {
262             oObj.execute(command, cmdId, null);
263             log.println("Command execution completed");
264             log.println("CommandAbortedException is not thrown");
265             log.println("This is OK since there is no command implemented "+
266                 "that can be aborted");
267         } catch (CommandAbortedException e) {
268             return true;
269         } catch (Exception e) {
270             log.println("Unexpected exception " + e.getMessage());
271             e.printStackTrace((java.io.PrintWriter)log);
272             return false;
273         }
274 
275         try {
276             aborter.join(5000);
277             aborter.interrupt();
278         } catch(java.lang.InterruptedException e) {
279         }
280         return true;
281     }
282 }
283