2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import com
.sun
.star
.beans
.Property
;
22 import com
.sun
.star
.beans
.XPropertySetInfo
;
23 import lib
.StatusException
;
25 import com
.sun
.star
.lang
.XMultiServiceFactory
;
26 import com
.sun
.star
.ucb
.Command
;
27 import com
.sun
.star
.ucb
.CommandAbortedException
;
28 import com
.sun
.star
.ucb
.CommandInfo
;
29 import com
.sun
.star
.ucb
.GlobalTransferCommandArgument
;
30 import com
.sun
.star
.ucb
.NameClash
;
31 import com
.sun
.star
.ucb
.TransferCommandOperation
;
32 import com
.sun
.star
.ucb
.XCommandInfo
;
33 import com
.sun
.star
.ucb
.XCommandProcessor
;
34 import com
.sun
.star
.uno
.Exception
;
35 import com
.sun
.star
.uno
.UnoRuntime
;
36 import share
.LogWriter
;
39 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
40 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
41 * <code>abort()</code> test.
43 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
46 * <li><code> createCommandIdentifier()</code></li>
47 * <li><code> execute()</code></li>
48 * <li><code> abort()</code></li>
50 * This test needs the following object relations :
52 * <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
53 * (of type <code>com.sun.star.ucb.Command</code>):
54 * specify command to abort in <code>abort()</code> test.
55 * If the relation is not specified the 'GlobalTransfer'
56 * command is used.</li>
58 * The following predefined files needed to complete the test:
60 * <li> <code>poliball.gif</code> : this file is required in case
61 * if the relation <code>'XCommandProcessor.AbortCommand'</code>
62 * is not specified. This file is used by 'GlobalTransfer'
63 * command as a source file for copying.</li>
65 * Test is <b> NOT </b> multithread compliant. <p>
66 * @see com.sun.star.ucb.XCommandProcessor
68 public class _XCommandProcessor
{
71 * Conatins the tested object.
73 public XCommandProcessor oObj
;
74 private final LogWriter log
= null;
75 private XMultiServiceFactory xMSF
= null;
78 * Contains the command id returned by <code>createCommandIdentifier()
79 * </code>. It is used in <code>abort()</code> test.
83 public void before(XMultiServiceFactory _xMSF
) {
88 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
89 * and checks returned values. <p>
90 * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
92 public boolean _createCommandIdentifier() {
93 log
.println("creating a command line identifier");
95 int testCmdId
= oObj
.createCommandIdentifier();
96 cmdId
= oObj
.createCommandIdentifier();
98 if (cmdId
== 0 || testCmdId
== 0) {
99 log
.println("createCommandLineIdentifier() returned 0 - FAILED");
102 if (cmdId
== testCmdId
) {
103 log
.println("the command identifier is not unique");
106 return testCmdId
!= 0 && cmdId
!= 0 && cmdId
!= testCmdId
;
110 * First executes 'geCommandInfo' command and examines returned
111 * command info information. Second tries to execute inproper
113 * Has <b> OK </b> status if in the first case returned information
114 * contains info about 'getCommandInfo' command and in the second
115 * case an exception is thrown. <p>
117 public boolean _execute() {
118 String
[]commands
= new String
[] {"getCommandInfo", "getPropertySetInfo"};
119 boolean returnVal
= true;
120 for (int j
=0; j
<commands
.length
; j
++) {
121 String commandName
= commands
[j
];
122 Command command
= new Command(commandName
, -1, null);
126 log
.println("executing command " + commandName
);
128 result
= oObj
.execute(command
, 0, null);
129 } catch (CommandAbortedException e
) {
130 log
.println("The command aborted " + e
.getMessage());
131 e
.printStackTrace((java
.io
.PrintWriter
)log
);
132 throw new StatusException("Unexpected exception", e
);
133 } catch (Exception e
) {
134 log
.println("Unexpected exception " + e
.getMessage());
135 e
.printStackTrace((java
.io
.PrintWriter
)log
);
136 throw new StatusException("Unexpected exception", e
);
139 boolean found
= false;
141 XCommandInfo xCmdInfo
= UnoRuntime
.queryInterface(
142 XCommandInfo
.class, result
);
143 if (xCmdInfo
!= null) {
144 CommandInfo
[] cmdInfo
= xCmdInfo
.getCommands();
145 for (int i
= 0; i
< cmdInfo
.length
; i
++) {
146 log
.println("\t##### " + cmdInfo
[i
].Name
+ " - " + cmdInfo
[i
].Handle
+ " - " + cmdInfo
[i
].ArgType
.getTypeName());
147 if (cmdInfo
[i
].Name
.equals(commandName
)) {
154 XPropertySetInfo xPropInfo
= UnoRuntime
.queryInterface(
155 XPropertySetInfo
.class, result
);
156 if (xPropInfo
!= null) {
157 Property
[] props
= xPropInfo
.getProperties();
158 String
[] defProps
= new String
[] {"ContentType", "IsDocument", "IsFolder", "Title"};
159 int propCount
= defProps
.length
;
160 for (int i
= 0; i
< props
.length
; i
++) {
161 for (int k
=0; k
<defProps
.length
; k
++) {
162 if (props
[i
].Name
.equals(defProps
[k
])) {
164 log
.println("Property '" + defProps
[k
] + "' has been found.");
168 found
= propCount
== 0;
174 log
.println("Command '" + commandName
+ "' was not executed correctly.");
181 * First a separate thread where <code>abort</code> method
182 * is called permanently. Then a "long" command (for example,
183 * "transfer") is started. I case if relation is not
184 * specified 'GlobalTransfer' command starts to
185 * copy a file to temporary directory (if the relation is present
186 * then the its command starts to work). <p>
187 * Has <b> OK </b> status if the command execution is aborted, i.e.
188 * <code>CommandAbortedException</code> is thrown. <p>
189 * The following method tests are to be completed successfully before :
191 * <li> <code> createCommandIdentifier() </code> : to have a unique
192 * identifier which is used to abourt started command. </li>
195 public boolean _abort() {
197 Command command
= new Command("getCommandInfo", -1, null);
199 String commandName
= "globalTransfer";
201 String srcURL
= util
.utils
.getFullTestURL("solibrary.jar") ;
202 String tmpURL
= util
.utils
.getOfficeTemp(xMSF
) ;
203 log
.println("Copying '" + srcURL
+ "' to '" + tmpURL
) ;
205 GlobalTransferCommandArgument arg
= new
206 GlobalTransferCommandArgument(
207 TransferCommandOperation
.COPY
, srcURL
,
208 tmpURL
, "", NameClash
.OVERWRITE
);
210 command
= new Command(commandName
, -1, arg
);
212 Thread aborter
= new Thread() {
215 for (int i
= 0; i
< 10; i
++) {
216 log
.println("try to abort command");
218 util
.utils
.pause(10);
225 util
.utils
.pause(15);
227 log
.println("executing command");
229 oObj
.execute(command
, cmdId
, null);
230 log
.println("Command execution completed");
231 log
.println("CommandAbortedException is not thrown");
232 log
.println("This is OK since there is no command implemented "+
233 "that can be aborted");
234 } catch (CommandAbortedException e
) {
236 } catch (Exception e
) {
237 log
.println("Unexpected exception " + e
.getMessage());
238 e
.printStackTrace((java
.io
.PrintWriter
)log
);
245 } catch(InterruptedException e
) {