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