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