1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package ifc.ucb;
29 
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.ucb.Command;
32 import com.sun.star.ucb.CommandAbortedException;
33 import com.sun.star.ucb.CommandInfo;
34 import com.sun.star.ucb.GlobalTransferCommandArgument;
35 import com.sun.star.ucb.NameClash;
36 import com.sun.star.ucb.TransferCommandOperation;
37 import com.sun.star.ucb.UnsupportedCommandException;
38 import com.sun.star.ucb.XCommandInfo;
39 import com.sun.star.ucb.XCommandProcessor;
40 import com.sun.star.uno.Exception;
41 import com.sun.star.uno.UnoRuntime;
42 import lib.MultiMethodTest;
43 import lib.StatusException;
44 
45 /**
46 * Tests <code>XCommandProcessor</code>. The TestCase can pass (but doesn't have
47 * to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
48 * <code>abort()</code> test.
49 *
50 * Testing <code>com.sun.star.ucb.XCommandProcessor</code>
51 * interface methods :
52 * <ul>
53 *  <li><code> createCommandIdentifier()</code></li>
54 *  <li><code> execute()</code></li>
55 *  <li><code> abort()</code></li>
56 * </ul> <p>
57 * This test needs the following object relations :
58 * <ul>
59 *  <li> <code>'XCommandProcessor.AbortCommand'</code> <b>optional</b>
60 *   (of type <code>com.sun.star.ucb.Command</code>):
61 *   specify command to abort in <code>abort()</code> test.
62 *   If the relation is not specified the 'GlobalTransfer'
63 *   command is used.</li>
64 * <ul> <p>
65 * The following predefined files needed to complete the test:
66 * <ul>
67 *  <li> <code>poliball.gif</code> : this file is required in case
68 *   if the relation <code>'XCommandProcessor.AbortCommand'</code>
69 *   is not specified. This file is used by 'GlobalTransfer'
70 *   command as a source file for copying.</li>
71 * <ul> <p>
72 * Test is <b> NOT </b> multithread compilant. <p>
73 * @see com.sun.star.ucb.XCommandProcessor
74 */
75 public class _XCommandProcessor extends MultiMethodTest {
76 
77     /**
78      * Conatins the tested object.
79      */
80     public XCommandProcessor oObj;
81 
82     /**
83      * Contains the command id returned by <code>createCommandIdentifier()
84      * </code>. It is used in <code>abort()</code> test.
85      */
86     int cmdId;
87 
88     /**
89      * Tests <code>createCommandIdentifier()</code>. Calls it for two times
90      * and checks returned values. <p>
91      * Has <b>OK</b> status if values are unique correct idenifiers: not 0.
92      */
93     public void _createCommandIdentifier() {
94         log.println("creating a command line identifier");
95 
96         int testCmdId = oObj.createCommandIdentifier();
97         cmdId = oObj.createCommandIdentifier();
98 
99         if (cmdId == 0 || testCmdId == 0) {
100             log.println("createCommandLineIdentifier() returned 0 - FAILED");
101         }
102 
103         if (cmdId == testCmdId) {
104             log.println("the command identifier is not unique");
105         }
106 
107         tRes.tested("createCommandIdentifier()",
108                 testCmdId != 0 && cmdId != 0 && cmdId != testCmdId);
109     }
110 
111     /**
112      * First executes 'geCommandInfo' command and examines returned
113      * command info information. Second tries to execute inproper
114      * command. <p>
115      * Has <b> OK </b> status if in the first case returned information
116      * contains info about 'getCommandInfo' command and in the second
117      * case an exception is thrown. <p>
118      */
119     public void _execute() {
120         String commandName = "getCommandInfo";
121         Command command = new Command(commandName, -1, null);
122 
123         Object result;
124 
125         log.println("executing command " + commandName);
126         try {
127             result = oObj.execute(command, 0, null);
128         } catch (CommandAbortedException e) {
129             log.println("The command aborted " + e.getMessage());
130             e.printStackTrace(log);
131             throw new StatusException("Unexpected exception", e);
132         } catch (Exception e) {
133             log.println("Unexpected exception " + e.getMessage());
134             e.printStackTrace(log);
135             throw new StatusException("Unexpected exception", e);
136         }
137 
138         XCommandInfo xCmdInfo = (XCommandInfo)UnoRuntime.queryInterface(
139                 XCommandInfo.class, result);
140 
141         CommandInfo[] cmdInfo = xCmdInfo.getCommands();
142 
143         boolean found = false;
144 
145         for (int i = 0; i < cmdInfo.length; i++) {
146             if (cmdInfo[i].Name.equals(commandName)) {
147                 found = true;
148                 break;
149             }
150         }
151 
152         log.println("testing execute with wrong command");
153 
154         Command badCommand = new Command("bad command", -1, null);
155 
156         try {
157             oObj.execute(badCommand, 0, null);
158         } catch (CommandAbortedException e) {
159             log.println("CommandAbortedException thrown - OK");
160         } catch (UnsupportedCommandException e) {
161             log.println("UnsupportedCommandException thrown - OK");
162         } catch (Exception e) {
163             log.println("Wrong exception thrown " + e.getMessage());
164             e.printStackTrace(log);
165             throw new StatusException("Unexpected exception", e);
166         }
167 
168         tRes.tested("execute()", found);
169     }
170 
171     /**
172      * First a separate thread where <code>abort</code> method
173      * is called permanently. Then a "long" command (for example,
174      * "transfer") is started. I case if relation is not
175      * specified 'GlobalTransfer' command starts to
176      * copy a file to temporary directory (if the relation is present
177      * then the its command starts to work). <p>
178      * Has <b> OK </b> status if the command execution is aborted, i.e.
179      * <code>CommandAbortedException</code> is thrown. <p>
180      * The following method tests are to be completed successfully before :
181      * <ul>
182      *  <li> <code> createCommandIdentifier() </code> : to have a unique
183      *  identifier which is used to abourt started command. </li>
184      * </ul>
185      */
186     public void _abort() {
187         executeMethod("createCommandIdentifier()");
188 
189         Command command = (Command)tEnv.getObjRelation(
190                 "XCommandProcessor.AbortCommand");
191 
192         if (command == null) {
193             String commandName = "globalTransfer";
194 
195             String srcURL = util.utils.getFullTestURL("SwXTextEmbeddedObject.sdw") ;
196             String tmpURL = util.utils.getOfficeTemp((XMultiServiceFactory)tParam.getMSF()) ;
197             log.println("Copying '" + srcURL + "' to '" + tmpURL) ;
198 
199             GlobalTransferCommandArgument arg = new
200                 GlobalTransferCommandArgument(
201                     TransferCommandOperation.COPY, srcURL,
202                         tmpURL, "", NameClash.OVERWRITE);
203 
204             command = new Command(commandName, -1, arg);
205         }
206 
207         Thread aborter = new Thread() {
208             public void run() {
209                 for (int i = 0; i < 10; i++) {
210                     log.println("try to abort command");
211                     oObj.abort(cmdId);
212                     try {
213                         Thread.sleep(10);
214                     } catch (InterruptedException e) {
215                     }
216                 }
217             }
218         };
219 
220         aborter.start();
221 
222         try {
223             Thread.currentThread().sleep(15);
224         } catch (InterruptedException e) {
225         }
226 
227         log.println("executing command");
228         try {
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             tRes.tested("abort()", true);
235         } catch (CommandAbortedException e) {
236             tRes.tested("abort()", true);
237         } catch (Exception e) {
238             log.println("Unexpected exception " + e.getMessage());
239             e.printStackTrace(log);
240             throw new StatusException("Unexpected exception", e);
241         }
242 
243         try {
244             aborter.join(5000);
245             aborter.interrupt();
246         } catch(java.lang.InterruptedException e) {
247         }
248     }
249 }
250