cid#1607171 Data race condition
[LibreOffice.git] / ucb / qa / complex / tdoc / _XCommandProcessor.java
blob222a34e2f7005d57051818be32d8c75f1718b0ec
1 /*
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 .
19 package complex.tdoc;
21 import com.sun.star.beans.Property;
22 import com.sun.star.beans.XPropertySetInfo;
23 import com.sun.star.lang.XMultiServiceFactory;
24 import com.sun.star.ucb.Command;
25 import com.sun.star.ucb.CommandAbortedException;
26 import com.sun.star.ucb.CommandInfo;
27 import com.sun.star.ucb.GlobalTransferCommandArgument;
28 import com.sun.star.ucb.NameClash;
29 import com.sun.star.ucb.TransferCommandOperation;
30 import com.sun.star.ucb.XCommandInfo;
31 import com.sun.star.ucb.XCommandProcessor;
32 import com.sun.star.uno.Exception;
33 import com.sun.star.uno.UnoRuntime;
34 import share.LogWriter;
36 /**
37 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
38 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
39 * <code>abort()</code> test.
41 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
42 * interface methods :
43 * <ul>
44 * <li><code> createCommandIdentifier()</code></li>
45 * <li><code> execute()</code></li>
46 * <li><code> abort()</code></li>
47 * </ul> <p>
48 * This test needs the following object relations :
49 * <ul>
50 * <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
51 * (of type <code>com.sun.star.ucb.Command</code>):
52 * specify command to abort in <code>abort()</code> test.
53 * If the relation is not specified the 'GlobalTransfer'
54 * command is used.</li>
55 * <ul> <p>
56 * The following predefined files needed to complete the test:
57 * <ul>
58 * <li> <code>poliball.gif</code> : this file is required in case
59 * if the relation <code>'XCommandProcessor.AbortCommand'</code>
60 * is not specified. This file is used by 'GlobalTransfer'
61 * command as a source file for copying.</li>
62 * <ul> <p>
63 * Test is <b> NOT </b> multithread compliant. <p>
64 * @see com.sun.star.ucb.XCommandProcessor
66 public class _XCommandProcessor {
68 /**
69 * Contains the tested object.
71 public XCommandProcessor oObj;
72 private final LogWriter log = null;
73 private XMultiServiceFactory xMSF = null;
75 /**
76 * Contains the command id returned by <code>createCommandIdentifier()
77 * </code>. It is used in <code>abort()</code> test.
79 private int cmdId;
81 public void before(XMultiServiceFactory _xMSF) {
82 xMSF = _xMSF;
85 /**
86 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
87 * and checks returned values. <p>
88 * Has <b>OK</b> status if values are unique correct identifiers: not 0.
90 public boolean _createCommandIdentifier() {
91 log.println("creating a command line identifier");
93 int testCmdId = oObj.createCommandIdentifier();
94 cmdId = oObj.createCommandIdentifier();
96 if (cmdId == 0 || testCmdId == 0) {
97 log.println("createCommandLineIdentifier() returned 0 - FAILED");
100 if (cmdId == testCmdId) {
101 log.println("the command identifier is not unique");
104 return testCmdId != 0 && cmdId != 0 && cmdId != testCmdId;
108 * First executes 'geCommandInfo' command and examines returned
109 * command info information. Second tries to execute in proper
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>
115 public boolean _execute() throws Exception {
116 String[]commands = new String[] {"getCommandInfo", "getPropertySetInfo"};
117 boolean returnVal = true;
118 for (int j=0; j<commands.length; j++) {
119 String commandName = commands[j];
120 Command command = new Command(commandName, -1, null);
122 Object result;
124 log.println("executing command " + commandName);
125 result = oObj.execute(command, 0, null);
127 boolean found = false;
129 XCommandInfo xCmdInfo = UnoRuntime.queryInterface(
130 XCommandInfo.class, result);
131 if (xCmdInfo != null) {
132 CommandInfo[] cmdInfo = xCmdInfo.getCommands();
133 for (int i = 0; i < cmdInfo.length; i++) {
134 log.println("\t##### " + cmdInfo[i].Name + " - " + cmdInfo[i].Handle + " - " + cmdInfo[i].ArgType.getTypeName());
135 if (cmdInfo[i].Name.equals(commandName)) {
136 found = true;
137 // break;
142 XPropertySetInfo xPropInfo = UnoRuntime.queryInterface(
143 XPropertySetInfo.class, result);
144 if (xPropInfo != null) {
145 Property[] props = xPropInfo.getProperties();
146 String[] defProps = new String[] {"ContentType", "IsDocument", "IsFolder", "Title"};
147 int propCount = defProps.length;
148 for (int i = 0; i < props.length; i++) {
149 for (int k=0; k<defProps.length; k++) {
150 if (props[i].Name.equals(defProps[k])) {
151 propCount--;
152 log.println("Property '" + defProps[k] + "' has been found.");
156 found = propCount == 0;
159 returnVal &= found;
161 if (!found) {
162 log.println("Command '" + commandName + "' was not executed correctly.");
165 return returnVal;
169 * First a separate thread where <code>abort</code> method
170 * is called permanently. Then a "long" command (for example,
171 * "transfer") is started. I case if relation is not
172 * specified 'GlobalTransfer' command starts to
173 * copy a file to temporary directory (if the relation is present
174 * then the its command starts to work). <p>
175 * Has <b> OK </b> status if the command execution is aborted, i.e.
176 * <code>CommandAbortedException</code> is thrown. <p>
177 * The following method tests are to be completed successfully before :
178 * <ul>
179 * <li> <code> createCommandIdentifier() </code> : to have a unique
180 * identifier which is used to abort started command. </li>
181 * </ul>
183 public boolean _abort() {
185 Command command = new Command("getCommandInfo", -1, null);
187 String commandName = "globalTransfer";
189 String srcURL = util.utils.getFullTestURL("solibrary.jar") ;
190 String tmpURL = util.utils.getOfficeTemp(xMSF) ;
191 log.println("Copying '" + srcURL + "' to '" + tmpURL) ;
193 GlobalTransferCommandArgument arg = new
194 GlobalTransferCommandArgument(
195 TransferCommandOperation.COPY, srcURL,
196 tmpURL, "", NameClash.OVERWRITE);
198 command = new Command(commandName, -1, arg);
200 Thread aborter = new Thread() {
201 @Override
202 public void run() {
203 for (int i = 0; i < 10; i++) {
204 log.println("try to abort command");
205 oObj.abort(cmdId);
206 util.utils.pause(10);
211 aborter.start();
213 util.utils.pause(15);
215 log.println("executing command");
216 try {
217 oObj.execute(command, cmdId, null);
218 log.println("Command execution completed");
219 log.println("CommandAbortedException is not thrown");
220 log.println("This is OK since there is no command implemented "+
221 "that can be aborted");
222 } catch (CommandAbortedException e) {
223 return true;
224 } catch (Exception e) {
225 log.println("Unexpected exception " + e.getMessage());
226 e.printStackTrace((java.io.PrintWriter)log);
227 return false;
230 try {
231 aborter.join(5000);
232 aborter.interrupt();
233 } catch(InterruptedException e) {
235 return true;