bump product version to 4.2.0.1
[LibreOffice.git] / qadevOOo / tests / java / ifc / ucb / _XCommandProcessor.java
bloba5437cda31046250d9888e8e30712ee43ca4da3d
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.lang.XMultiServiceFactory;
22 import com.sun.star.ucb.Command;
23 import com.sun.star.ucb.CommandAbortedException;
24 import com.sun.star.ucb.CommandInfo;
25 import com.sun.star.ucb.GlobalTransferCommandArgument;
26 import com.sun.star.ucb.NameClash;
27 import com.sun.star.ucb.TransferCommandOperation;
28 import com.sun.star.ucb.UnsupportedCommandException;
29 import com.sun.star.ucb.XCommandInfo;
30 import com.sun.star.ucb.XCommandProcessor;
31 import com.sun.star.uno.Exception;
32 import com.sun.star.uno.UnoRuntime;
33 import lib.MultiMethodTest;
34 import lib.StatusException;
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 compilant. <p>
64 * @see com.sun.star.ucb.XCommandProcessor
66 public class _XCommandProcessor extends MultiMethodTest {
68 /**
69 * Conatins the tested object.
71 public XCommandProcessor oObj;
73 /**
74 * Contains the command id returned by <code>createCommandIdentifier()
75 * </code>. It is used in <code>abort()</code> test.
77 int cmdId;
79 /**
80 * Tests <code>createCommandIdentifier()</code>. Calls it for two times
81 * and checks returned values. <p>
82 * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
84 public void _createCommandIdentifier() {
85 log.println("creating a command line identifier");
87 int testCmdId = oObj.createCommandIdentifier();
88 cmdId = oObj.createCommandIdentifier();
90 if (cmdId == 0 || testCmdId == 0) {
91 log.println("createCommandLineIdentifier() returned 0 - FAILED");
94 if (cmdId == testCmdId) {
95 log.println("the command identifier is not unique");
98 tRes.tested("createCommandIdentifier()",
99 testCmdId != 0 && cmdId != 0 && cmdId != testCmdId);
103 * First executes 'geCommandInfo' command and examines returned
104 * command info information. Second tries to execute inproper
105 * command. <p>
106 * Has <b> OK </b> status if in the first case returned information
107 * contains info about 'getCommandInfo' command and in the second
108 * case an exception is thrown. <p>
110 public void _execute() {
111 String commandName = "getCommandInfo";
112 Command command = new Command(commandName, -1, null);
114 Object result;
116 log.println("executing command " + commandName);
117 try {
118 result = oObj.execute(command, 0, null);
119 } catch (CommandAbortedException e) {
120 log.println("The command aborted " + e.getMessage());
121 e.printStackTrace(log);
122 throw new StatusException("Unexpected exception", e);
123 } catch (Exception e) {
124 log.println("Unexpected exception " + e.getMessage());
125 e.printStackTrace(log);
126 throw new StatusException("Unexpected exception", e);
129 XCommandInfo xCmdInfo = UnoRuntime.queryInterface(
130 XCommandInfo.class, result);
132 CommandInfo[] cmdInfo = xCmdInfo.getCommands();
134 boolean found = false;
136 for (int i = 0; i < cmdInfo.length; i++) {
137 if (cmdInfo[i].Name.equals(commandName)) {
138 found = true;
139 break;
143 log.println("testing execute with wrong command");
145 Command badCommand = new Command("bad command", -1, null);
147 try {
148 oObj.execute(badCommand, 0, null);
149 } catch (CommandAbortedException e) {
150 log.println("CommandAbortedException thrown - OK");
151 } catch (UnsupportedCommandException e) {
152 log.println("UnsupportedCommandException thrown - OK");
153 } catch (Exception e) {
154 log.println("Wrong exception thrown " + e.getMessage());
155 e.printStackTrace(log);
156 throw new StatusException("Unexpected exception", e);
159 tRes.tested("execute()", found);
163 * First a separate thread where <code>abort</code> method
164 * is called permanently. Then a "long" command (for example,
165 * "transfer") is started. I case if relation is not
166 * specified 'GlobalTransfer' command starts to
167 * copy a file to temporary directory (if the relation is present
168 * then the its command starts to work). <p>
169 * Has <b> OK </b> status if the command execution is aborted, i.e.
170 * <code>CommandAbortedException</code> is thrown. <p>
171 * The following method tests are to be completed successfully before :
172 * <ul>
173 * <li> <code> createCommandIdentifier() </code> : to have a unique
174 * identifier which is used to abourt started command. </li>
175 * </ul>
177 public void _abort() {
178 executeMethod("createCommandIdentifier()");
180 Command command = (Command)tEnv.getObjRelation(
181 "XCommandProcessor.AbortCommand");
183 if (command == null) {
184 String commandName = "globalTransfer";
186 String srcURL = util.utils.getFullTestURL("SwXTextEmbeddedObject.sdw") ;
187 String tmpURL = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
188 log.println("Copying '" + srcURL + "' to '" + tmpURL) ;
190 GlobalTransferCommandArgument arg = new
191 GlobalTransferCommandArgument(
192 TransferCommandOperation.COPY, srcURL,
193 tmpURL, "", NameClash.OVERWRITE);
195 command = new Command(commandName, -1, arg);
198 Thread aborter = new Thread() {
199 public void run() {
200 for (int i = 0; i < 10; i++) {
201 log.println("try to abort command");
202 oObj.abort(cmdId);
203 try {
204 Thread.sleep(10);
205 } catch (InterruptedException e) {
211 aborter.start();
213 try {
214 Thread.sleep(15);
215 } catch (InterruptedException e) {
218 log.println("executing command");
219 try {
220 oObj.execute(command, cmdId, null);
221 log.println("Command execution completed");
222 log.println("CommandAbortedException is not thrown");
223 log.println("This is OK since there is no command implemented "+
224 "that can be aborted");
225 tRes.tested("abort()", true);
226 } catch (CommandAbortedException e) {
227 tRes.tested("abort()", true);
228 } catch (Exception e) {
229 log.println("Unexpected exception " + e.getMessage());
230 e.printStackTrace(log);
231 throw new StatusException("Unexpected exception", e);
234 try {
235 aborter.join(5000);
236 aborter.interrupt();
237 } catch(java.lang.InterruptedException e) {