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