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 ************************************************************************/
31 package complex
.tdoc
.interfaces
;
33 import com
.sun
.star
.beans
.Property
;
34 import com
.sun
.star
.beans
.XPropertySetInfo
;
35 import lib
.MultiMethodTest
;
36 import lib
.StatusException
;
38 import com
.sun
.star
.lang
.XMultiServiceFactory
;
39 import com
.sun
.star
.ucb
.Command
;
40 import com
.sun
.star
.ucb
.CommandAbortedException
;
41 import com
.sun
.star
.ucb
.CommandInfo
;
42 import com
.sun
.star
.ucb
.GlobalTransferCommandArgument
;
43 import com
.sun
.star
.ucb
.NameClash
;
44 import com
.sun
.star
.ucb
.TransferCommandOperation
;
45 import com
.sun
.star
.ucb
.UnsupportedCommandException
;
46 import com
.sun
.star
.ucb
.XCommandInfo
;
47 import com
.sun
.star
.ucb
.XCommandProcessor
;
48 import com
.sun
.star
.uno
.Exception
;
49 import com
.sun
.star
.uno
.UnoRuntime
;
50 import share
.LogWriter
;
53 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
54 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
55 * <code>abort()</code> test.
57 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
60 * <li><code> createCommandIdentifier()</code></li>
61 * <li><code> execute()</code></li>
62 * <li><code> abort()</code></li>
64 * This test needs the following object relations :
66 * <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
67 * (of type <code>com.sun.star.ucb.Command</code>):
68 * specify command to abort in <code>abort()</code> test.
69 * If the relation is not specified the 'GlobalTransfer'
70 * command is used.</li>
72 * The following predefined files needed to complete the test:
74 * <li> <code>poliball.gif</code> : this file is required in case
75 * if the relation <code>'XCommandProcessor.AbortCommand'</code>
76 * is not specified. This file is used by 'GlobalTransfer'
77 * command as a source file for copying.</li>
79 * Test is <b> NOT </b> multithread compilant. <p>
80 * @see com.sun.star.ucb.XCommandProcessor
82 public class _XCommandProcessor
{
85 * Conatins the tested object.
87 public XCommandProcessor oObj
;
88 public LogWriter log
= null;
89 private XMultiServiceFactory xMSF
= null;
92 * Contains the command id returned by <code>createCommandIdentifier()
93 * </code>. It is used in <code>abort()</code> test.
97 public void before(XMultiServiceFactory _xMSF
) {
102 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
103 * and checks returned values. <p>
104 * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
106 public boolean _createCommandIdentifier() {
107 log
.println("creating a command line identifier");
109 int testCmdId
= oObj
.createCommandIdentifier();
110 cmdId
= oObj
.createCommandIdentifier();
112 if (cmdId
== 0 || testCmdId
== 0) {
113 log
.println("createCommandLineIdentifier() returned 0 - FAILED");
116 if (cmdId
== testCmdId
) {
117 log
.println("the command identifier is not unique");
120 return testCmdId
!= 0 && cmdId
!= 0 && cmdId
!= testCmdId
;
124 * First executes 'geCommandInfo' command and examines returned
125 * command info information. Second tries to execute inproper
127 * Has <b> OK </b> status if in the first case returned information
128 * contains info about 'getCommandInfo' command and in the second
129 * case an exception is thrown. <p>
131 public boolean _execute() {
132 String
[]commands
= new String
[] {"getCommandInfo", "getPropertySetInfo"};
133 boolean returnVal
= true;
134 for (int j
=0; j
<commands
.length
; j
++) {
135 String commandName
= commands
[j
];
136 Command command
= new Command(commandName
, -1, null);
140 log
.println("executing command " + commandName
);
142 result
= oObj
.execute(command
, 0, null);
143 } catch (CommandAbortedException e
) {
144 log
.println("The command aborted " + e
.getMessage());
145 e
.printStackTrace((java
.io
.PrintWriter
)log
);
146 throw new StatusException("Unexpected exception", e
);
147 } catch (Exception e
) {
148 log
.println("Unexpected exception " + e
.getMessage());
149 e
.printStackTrace((java
.io
.PrintWriter
)log
);
150 throw new StatusException("Unexpected exception", e
);
153 boolean found
= false;
155 XCommandInfo xCmdInfo
= (XCommandInfo
)UnoRuntime
.queryInterface(
156 XCommandInfo
.class, result
);
157 if (xCmdInfo
!= null) {
158 CommandInfo
[] cmdInfo
= xCmdInfo
.getCommands();
159 for (int i
= 0; i
< cmdInfo
.length
; i
++) {
160 log
.println("\t##### " + cmdInfo
[i
].Name
+ " - " + cmdInfo
[i
].Handle
+ " - " + cmdInfo
[i
].ArgType
.getTypeName());
161 if (cmdInfo
[i
].Name
.equals(commandName
)) {
168 XPropertySetInfo xPropInfo
= (XPropertySetInfo
)UnoRuntime
.queryInterface(
169 XPropertySetInfo
.class, result
);
170 if (xPropInfo
!= null) {
171 Property
[] props
= xPropInfo
.getProperties();
172 String
[] defProps
= new String
[] {"ContentType", "IsDocument", "IsFolder", "Title"};
173 int propCount
= defProps
.length
;
174 for (int i
= 0; i
< props
.length
; i
++) {
175 for (int k
=0; k
<defProps
.length
; k
++) {
176 if (props
[i
].Name
.equals(defProps
[k
])) {
178 log
.println("Property '" + defProps
[k
] + "' has been found.");
182 found
= propCount
== 0;
188 log
.println("Command '" + commandName
+ "' was not executed correctly.");
191 /* log.println("testing execute with wrong command");
193 Command badCommand = new Command("bad command", -1, null);
196 oObj.execute(badCommand, 0, null);
197 } catch (CommandAbortedException e) {
198 log.println("CommandAbortedException thrown - OK");
199 } catch (UnsupportedCommandException e) {
200 log.println("UnsupportedCommandException thrown - OK");
201 } catch (Exception e) {
202 log.println("Wrong exception thrown " + e.getMessage());
203 e.printStackTrace((java.io.PrintWriter)log);
204 throw new StatusException("Unexpected exception", e);
211 * First a separate thread where <code>abort</code> method
212 * is called permanently. Then a "long" command (for example,
213 * "transfer") is started. I case if relation is not
214 * specified 'GlobalTransfer' command starts to
215 * copy a file to temporary directory (if the relation is present
216 * then the its command starts to work). <p>
217 * Has <b> OK </b> status if the command execution is aborted, i.e.
218 * <code>CommandAbortedException</code> is thrown. <p>
219 * The following method tests are to be completed successfully before :
221 * <li> <code> createCommandIdentifier() </code> : to have a unique
222 * identifier which is used to abourt started command. </li>
225 public boolean _abort() {
226 //executeMethod("createCommandIdentifier()");
228 // Command command = null;//(Command)tEnv.getObjRelation(
229 //"XCommandProcessor.AbortCommand");
230 Command command
= new Command("getCommandInfo", -1, null);
232 if (command
== null) {
233 String commandName
= "globalTransfer";
235 String srcURL
= util
.utils
.getFullTestURL("solibrary.jar") ;
236 String tmpURL
= util
.utils
.getOfficeTemp(xMSF
) ;
237 log
.println("Copying '" + srcURL
+ "' to '" + tmpURL
) ;
239 GlobalTransferCommandArgument arg
= new
240 GlobalTransferCommandArgument(
241 TransferCommandOperation
.COPY
, srcURL
,
242 tmpURL
, "", NameClash
.OVERWRITE
);
244 command
= new Command(commandName
, -1, arg
);
247 Thread aborter
= new Thread() {
249 for (int i
= 0; i
< 10; i
++) {
250 log
.println("try to abort command");
254 } catch (InterruptedException e
) {
264 } catch (InterruptedException e
) {
267 log
.println("executing command");
269 oObj
.execute(command
, cmdId
, null);
270 log
.println("Command execution completed");
271 log
.println("CommandAbortedException is not thrown");
272 log
.println("This is OK since there is no command implemented "+
273 "that can be aborted");
274 } catch (CommandAbortedException e
) {
276 } catch (Exception e
) {
277 log
.println("Unexpected exception " + e
.getMessage());
278 e
.printStackTrace((java
.io
.PrintWriter
)log
);
285 } catch(java
.lang
.InterruptedException e
) {