Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / ucb / _XCommandProcessor.java
blobd270850d78ca6197d6c740a54f68d6212b8e810b
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 ifc.ucb;
21 import com.sun.star.ucb.Command;
22 import com.sun.star.ucb.CommandAbortedException;
23 import com.sun.star.ucb.CommandInfo;
24 import com.sun.star.ucb.GlobalTransferCommandArgument;
25 import com.sun.star.ucb.NameClash;
26 import com.sun.star.ucb.TransferCommandOperation;
27 import com.sun.star.ucb.UnsupportedCommandException;
28 import com.sun.star.ucb.XCommandInfo;
29 import com.sun.star.ucb.XCommandProcessor;
30 import com.sun.star.uno.Exception;
31 import com.sun.star.uno.UnoRuntime;
32 import lib.MultiMethodTest;
33 import lib.StatusException;
35 /**
36 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
37 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
38 * <code>abort()</code> test.
40 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
41 * interface methods :
42 * <ul>
43 * <li><code> createCommandIdentifier()</code></li>
44 * <li><code> execute()</code></li>
45 * <li><code> abort()</code></li>
46 * </ul> <p>
47 * This test needs the following object relations :
48 * <ul>
49 * <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
50 * (of type <code>com.sun.star.ucb.Command</code>):
51 * specify command to abort in <code>abort()</code> test.
52 * If the relation is not specified the 'GlobalTransfer'
53 * command is used.</li>
54 * <ul> <p>
55 * The following predefined files needed to complete the test:
56 * <ul>
57 * <li> <code>poliball.gif</code> : this file is required in case
58 * if the relation <code>'XCommandProcessor.AbortCommand'</code>
59 * is not specified. This file is used by 'GlobalTransfer'
60 * command as a source file for copying.</li>
61 * <ul> <p>
62 * Test is <b> NOT </b> multithread compliant. <p>
63 * @see com.sun.star.ucb.XCommandProcessor
65 public class _XCommandProcessor extends MultiMethodTest {
67 /**
68 * Conatins the tested object.
70 public XCommandProcessor oObj;
72 /**
73 * Contains the command id returned by <code>createCommandIdentifier()
74 * </code>. It is used in <code>abort()</code> test.
76 int cmdId;
78 /**
79 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
80 * and checks returned values. <p>
81 * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
83 public void _createCommandIdentifier() {
84 log.println("creating a command line identifier");
86 int testCmdId = oObj.createCommandIdentifier();
87 cmdId = oObj.createCommandIdentifier();
89 if (cmdId == 0 || testCmdId == 0) {
90 log.println("createCommandLineIdentifier() returned 0 - FAILED");
93 if (cmdId == testCmdId) {
94 log.println("the command identifier is not unique");
97 tRes.tested("createCommandIdentifier()",
98 testCmdId != 0 && cmdId != 0 && cmdId != testCmdId);
102 * First executes 'geCommandInfo' command and examines returned
103 * command info information. Second tries to execute inproper
104 * command. <p>
105 * Has <b> OK </b> status if in the first case returned information
106 * contains info about 'getCommandInfo' command and in the second
107 * case an exception is thrown. <p>
109 public void _execute() {
110 String commandName = "getCommandInfo";
111 Command command = new Command(commandName, -1, null);
113 Object result;
115 log.println("executing command " + commandName);
116 try {
117 result = oObj.execute(command, 0, null);
118 } catch (CommandAbortedException e) {
119 log.println("The command aborted " + e.getMessage());
120 e.printStackTrace(log);
121 throw new StatusException("Unexpected exception", e);
122 } catch (Exception e) {
123 log.println("Unexpected exception " + e.getMessage());
124 e.printStackTrace(log);
125 throw new StatusException("Unexpected exception", e);
128 XCommandInfo xCmdInfo = UnoRuntime.queryInterface(
129 XCommandInfo.class, result);
131 CommandInfo[] cmdInfo = xCmdInfo.getCommands();
133 boolean found = false;
135 for (int i = 0; i < cmdInfo.length; i++) {
136 if (cmdInfo[i].Name.equals(commandName)) {
137 found = true;
138 break;
142 log.println("testing execute with wrong command");
144 Command badCommand = new Command("bad command", -1, null);
146 try {
147 oObj.execute(badCommand, 0, null);
148 } catch (CommandAbortedException e) {
149 log.println("CommandAbortedException thrown - OK");
150 } catch (UnsupportedCommandException e) {
151 log.println("UnsupportedCommandException thrown - OK");
152 } catch (Exception e) {
153 log.println("Wrong exception thrown " + e.getMessage());
154 e.printStackTrace(log);
155 throw new StatusException("Unexpected exception", e);
158 tRes.tested("execute()", found);
162 * First a separate thread where <code>abort</code> method
163 * is called permanently. Then a "long" command (for example,
164 * "transfer") is started. I case if relation is not
165 * specified 'GlobalTransfer' command starts to
166 * copy a file to temporary directory (if the relation is present
167 * then the its command starts to work). <p>
168 * Has <b> OK </b> status if the command execution is aborted, i.e.
169 * <code>CommandAbortedException</code> is thrown. <p>
170 * The following method tests are to be completed successfully before :
171 * <ul>
172 * <li> <code> createCommandIdentifier() </code> : to have a unique
173 * identifier which is used to abourt started command. </li>
174 * </ul>
176 public void _abort() {
177 executeMethod("createCommandIdentifier()");
179 Command command = (Command)tEnv.getObjRelation(
180 "XCommandProcessor.AbortCommand");
182 if (command == null) {
183 String commandName = "globalTransfer";
185 String srcURL = util.utils.getFullTestURL("SwXTextEmbeddedObject.sdw") ;
186 String tmpURL = util.utils.getOfficeTemp(tParam.getMSF()) ;
187 log.println("Copying '" + srcURL + "' to '" + tmpURL) ;
189 GlobalTransferCommandArgument arg = new
190 GlobalTransferCommandArgument(
191 TransferCommandOperation.COPY, srcURL,
192 tmpURL, "", NameClash.OVERWRITE);
194 command = new Command(commandName, -1, arg);
197 Thread aborter = new Thread() {
198 @Override
199 public void run() {
200 for (int i = 0; i < 10; i++) {
201 log.println("try to abort command");
202 oObj.abort(cmdId);
203 util.utils.pause(10);
208 aborter.start();
210 util.utils.pause(15);
212 log.println("executing command");
213 try {
214 oObj.execute(command, cmdId, null);
215 log.println("Command execution completed");
216 log.println("CommandAbortedException is not thrown");
217 log.println("This is OK since there is no command implemented "+
218 "that can be aborted");
219 tRes.tested("abort()", true);
220 } catch (CommandAbortedException e) {
221 tRes.tested("abort()", true);
222 } catch (Exception e) {
223 log.println("Unexpected exception " + e.getMessage());
224 e.printStackTrace(log);
225 throw new StatusException("Unexpected exception", e);
228 try {
229 aborter.join(5000);
230 aborter.interrupt();
231 } catch(InterruptedException e) {