1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XCommandProcessor.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
33 import com
.sun
.star
.lang
.XMultiServiceFactory
;
34 import com
.sun
.star
.ucb
.Command
;
35 import com
.sun
.star
.ucb
.CommandAbortedException
;
36 import com
.sun
.star
.ucb
.CommandInfo
;
37 import com
.sun
.star
.ucb
.GlobalTransferCommandArgument
;
38 import com
.sun
.star
.ucb
.NameClash
;
39 import com
.sun
.star
.ucb
.TransferCommandOperation
;
40 import com
.sun
.star
.ucb
.UnsupportedCommandException
;
41 import com
.sun
.star
.ucb
.XCommandInfo
;
42 import com
.sun
.star
.ucb
.XCommandProcessor
;
43 import com
.sun
.star
.uno
.Exception
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
45 import lib
.MultiMethodTest
;
46 import lib
.StatusException
;
49 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
50 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
51 * <code>abort()</code> test.
53 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
56 * <li><code> createCommandIdentifier()</code></li>
57 * <li><code> execute()</code></li>
58 * <li><code> abort()</code></li>
60 * This test needs the following object relations :
62 * <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
63 * (of type <code>com.sun.star.ucb.Command</code>):
64 * specify command to abort in <code>abort()</code> test.
65 * If the relation is not specified the 'GlobalTransfer'
66 * command is used.</li>
68 * The following predefined files needed to complete the test:
70 * <li> <code>poliball.gif</code> : this file is required in case
71 * if the relation <code>'XCommandProcessor.AbortCommand'</code>
72 * is not specified. This file is used by 'GlobalTransfer'
73 * command as a source file for copying.</li>
75 * Test is <b> NOT </b> multithread compilant. <p>
76 * @see com.sun.star.ucb.XCommandProcessor
78 public class _XCommandProcessor
extends MultiMethodTest
{
81 * Conatins the tested object.
83 public XCommandProcessor oObj
;
86 * Contains the command id returned by <code>createCommandIdentifier()
87 * </code>. It is used in <code>abort()</code> test.
92 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
93 * and checks returned values. <p>
94 * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
96 public void _createCommandIdentifier() {
97 log
.println("creating a command line identifier");
99 int testCmdId
= oObj
.createCommandIdentifier();
100 cmdId
= oObj
.createCommandIdentifier();
102 if (cmdId
== 0 || testCmdId
== 0) {
103 log
.println("createCommandLineIdentifier() returned 0 - FAILED");
106 if (cmdId
== testCmdId
) {
107 log
.println("the command identifier is not unique");
110 tRes
.tested("createCommandIdentifier()",
111 testCmdId
!= 0 && cmdId
!= 0 && cmdId
!= testCmdId
);
115 * First executes 'geCommandInfo' command and examines returned
116 * command info information. Second tries to execute inproper
118 * Has <b> OK </b> status if in the first case returned information
119 * contains info about 'getCommandInfo' command and in the second
120 * case an exception is thrown. <p>
122 public void _execute() {
123 String commandName
= "getCommandInfo";
124 Command command
= new Command(commandName
, -1, null);
128 log
.println("executing command " + commandName
);
130 result
= oObj
.execute(command
, 0, null);
131 } catch (CommandAbortedException e
) {
132 log
.println("The command aborted " + e
.getMessage());
133 e
.printStackTrace(log
);
134 throw new StatusException("Unexpected exception", e
);
135 } catch (Exception e
) {
136 log
.println("Unexpected exception " + e
.getMessage());
137 e
.printStackTrace(log
);
138 throw new StatusException("Unexpected exception", e
);
141 XCommandInfo xCmdInfo
= (XCommandInfo
)UnoRuntime
.queryInterface(
142 XCommandInfo
.class, result
);
144 CommandInfo
[] cmdInfo
= xCmdInfo
.getCommands();
146 boolean found
= false;
148 for (int i
= 0; i
< cmdInfo
.length
; i
++) {
149 if (cmdInfo
[i
].Name
.equals(commandName
)) {
155 log
.println("testing execute with wrong command");
157 Command badCommand
= new Command("bad command", -1, null);
160 oObj
.execute(badCommand
, 0, null);
161 } catch (CommandAbortedException e
) {
162 log
.println("CommandAbortedException thrown - OK");
163 } catch (UnsupportedCommandException e
) {
164 log
.println("UnsupportedCommandException thrown - OK");
165 } catch (Exception e
) {
166 log
.println("Wrong exception thrown " + e
.getMessage());
167 e
.printStackTrace(log
);
168 throw new StatusException("Unexpected exception", e
);
171 tRes
.tested("execute()", found
);
175 * First a separate thread where <code>abort</code> method
176 * is called permanently. Then a "long" command (for example,
177 * "transfer") is started. I case if relation is not
178 * specified 'GlobalTransfer' command starts to
179 * copy a file to temporary directory (if the relation is present
180 * then the its command starts to work). <p>
181 * Has <b> OK </b> status if the command execution is aborted, i.e.
182 * <code>CommandAbortedException</code> is thrown. <p>
183 * The following method tests are to be completed successfully before :
185 * <li> <code> createCommandIdentifier() </code> : to have a unique
186 * identifier which is used to abourt started command. </li>
189 public void _abort() {
190 executeMethod("createCommandIdentifier()");
192 Command command
= (Command
)tEnv
.getObjRelation(
193 "XCommandProcessor.AbortCommand");
195 if (command
== null) {
196 String commandName
= "globalTransfer";
198 String srcURL
= util
.utils
.getFullTestURL("SwXTextEmbeddedObject.sdw") ;
199 String tmpURL
= util
.utils
.getOfficeTemp((XMultiServiceFactory
)tParam
.getMSF()) ;
200 log
.println("Copying '" + srcURL
+ "' to '" + tmpURL
) ;
202 GlobalTransferCommandArgument arg
= new
203 GlobalTransferCommandArgument(
204 TransferCommandOperation
.COPY
, srcURL
,
205 tmpURL
, "", NameClash
.OVERWRITE
);
207 command
= new Command(commandName
, -1, arg
);
210 Thread aborter
= new Thread() {
212 for (int i
= 0; i
< 10; i
++) {
213 log
.println("try to abort command");
217 } catch (InterruptedException e
) {
226 Thread
.currentThread().sleep(15);
227 } catch (InterruptedException e
) {
230 log
.println("executing command");
232 oObj
.execute(command
, cmdId
, null);
233 log
.println("Command execution completed");
234 log
.println("CommandAbortedException is not thrown");
235 log
.println("This is OK since there is no command implemented "+
236 "that can be aborted");
237 tRes
.tested("abort()", true);
238 } catch (CommandAbortedException e
) {
239 tRes
.tested("abort()", true);
240 } catch (Exception e
) {
241 log
.println("Unexpected exception " + e
.getMessage());
242 e
.printStackTrace(log
);
243 throw new StatusException("Unexpected exception", e
);
249 } catch(java
.lang
.InterruptedException e
) {