/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
package ifc.ucb;
import com.sun.star.ucb.Command;
import com.sun.star.ucb.CommandAbortedException;
import com.sun.star.ucb.CommandInfo;
import com.sun.star.ucb.GlobalTransferCommandArgument;
import com.sun.star.ucb.NameClash;
import com.sun.star.ucb.TransferCommandOperation;
import com.sun.star.ucb.UnsupportedCommandException;
import com.sun.star.ucb.XCommandInfo;
import com.sun.star.ucb.XCommandProcessor;
import com.sun.star.uno.Exception;
import com.sun.star.uno.UnoRuntime;
import lib.MultiMethodTest;
import lib.StatusException;
/**
* Tests XCommandProcessor
. The TestCase can pass (but doesn't have
* to) "XCommandProcessor.AbortCommand" relation, to specify command to abort in
* abort()
test.
*
* Testing com.sun.star.ucb.XCommandProcessor
* interface methods :
*
createCommandIdentifier()
execute()
abort()
* This test needs the following object relations : *
'XCommandProcessor.AbortCommand'
optional
* (of type com.sun.star.ucb.Command
):
* specify command to abort in abort()
test.
* If the relation is not specified the 'GlobalTransfer'
* command is used.* The following predefined files needed to complete the test: *
poliball.gif
: this file is required in case
* if the relation 'XCommandProcessor.AbortCommand'
* is not specified. This file is used by 'GlobalTransfer'
* command as a source file for copying.* Test is NOT multithread compliant.
* @see com.sun.star.ucb.XCommandProcessor
*/
public class _XCommandProcessor extends MultiMethodTest {
/**
* Contains the tested object.
*/
public XCommandProcessor oObj;
/**
* Contains the command id returned by createCommandIdentifier()
*
. It is used in abort()
test.
*/
int cmdId;
/**
* Tests createCommandIdentifier()
. Calls it for two times
* and checks returned values.
* Has OK status if values are unique correct idenifiers: not 0. */ public void _createCommandIdentifier() { log.println("creating a command line identifier"); int testCmdId = oObj.createCommandIdentifier(); cmdId = oObj.createCommandIdentifier(); if (cmdId == 0 || testCmdId == 0) { log.println("createCommandLineIdentifier() returned 0 - FAILED"); } if (cmdId == testCmdId) { log.println("the command identifier is not unique"); } tRes.tested("createCommandIdentifier()", testCmdId != 0 && cmdId != 0 && cmdId != testCmdId); } /** * First executes 'geCommandInfo' command and examines returned * command info information. Second tries to execute improper * command.
* Has OK status if in the first case returned information * contains info about 'getCommandInfo' command and in the second * case an exception is thrown.
*/
public void _execute() {
String commandName = "getCommandInfo";
Command command = new Command(commandName, -1, null);
Object result;
log.println("executing command " + commandName);
try {
result = oObj.execute(command, 0, null);
} catch (CommandAbortedException e) {
log.println("The command aborted " + e.getMessage());
e.printStackTrace(log);
throw new StatusException("Unexpected exception", e);
} catch (Exception e) {
log.println("Unexpected exception " + e.getMessage());
e.printStackTrace(log);
throw new StatusException("Unexpected exception", e);
}
XCommandInfo xCmdInfo = UnoRuntime.queryInterface(
XCommandInfo.class, result);
CommandInfo[] cmdInfo = xCmdInfo.getCommands();
boolean found = false;
for (int i = 0; i < cmdInfo.length; i++) {
if (cmdInfo[i].Name.equals(commandName)) {
found = true;
break;
}
}
log.println("testing execute with wrong command");
Command badCommand = new Command("bad command", -1, null);
try {
oObj.execute(badCommand, 0, null);
} catch (CommandAbortedException e) {
log.println("CommandAbortedException thrown - OK");
} catch (UnsupportedCommandException e) {
log.println("UnsupportedCommandException thrown - OK");
} catch (Exception e) {
log.println("Wrong exception thrown " + e.getMessage());
e.printStackTrace(log);
throw new StatusException("Unexpected exception", e);
}
tRes.tested("execute()", found);
}
/**
* First a separate thread where abort
method
* is called permanently. Then a "long" command (for example,
* "transfer") is started. I case if relation is not
* specified 'GlobalTransfer' command starts to
* copy a file to temporary directory (if the relation is present
* then the its command starts to work).
* Has OK status if the command execution is aborted, i.e.
* CommandAbortedException
is thrown.
* The following method tests are to be completed successfully before : *
createCommandIdentifier()
: to have a unique
* identifier which is used to abort started command.