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