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