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