1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-03 15:15:25 +02:00

[176577] wrong enablement of "Move up/down" in connection context menu

fix and unit tests
This commit is contained in:
David Dykstal 2007-09-20 12:56:18 +00:00
parent 8c7aef9f65
commit d12987030d
2 changed files with 243 additions and 59 deletions

View file

@ -22,9 +22,12 @@
package org.eclipse.rse.internal.core.model;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.eclipse.rse.core.IRSESystemType;
import org.eclipse.rse.core.IRSEUserIdConstants;
@ -406,68 +409,83 @@ public class SystemHostPool extends RSEModelObject implements ISystemHostPool
}
/**
* Move existing connections a given number of positions in the same pool.
* If the delta is negative, they are all moved up by the given amount. If
* positive, they are all moved down by the given amount.<p>
* <ul>
* <li>After the move, the pool containing the moved connection is saved to disk.
* <li>The connection's alias name must be unique in pool.
* </ul>
* <b>TODO PROBLEM: CAN'T RE-ORDER FOLDERS SO CAN WE SUPPORT THIS ACTION?</b>
* @param conns Array of SystemConnections to move.
* @param delta the amount by which to move the connections
*/
public void moveHosts(IHost conns[], int delta)
{
int[] oldPositions = new int[conns.length];
for (int idx=0; idx<conns.length; idx++)
oldPositions[idx] = getHostPosition(conns[idx]);
if (delta > 0) // moving down, process backwards
for (int idx=conns.length-1; idx>=0; idx--)
moveConnection(conns[idx], oldPositions[idx]+delta);
else
for (int idx=0; idx<conns.length; idx++)
moveConnection(conns[idx], oldPositions[idx]+delta);
commit();
}
* Move existing hosts a given number of positions in the same pool.
* If the delta is negative, they are all moved up (left) by the given amount. If
* positive, they are all moved down (right) by the given amount.<p>
* After the move, the pool containing the moved host is committed.
* @param hosts an Array of hosts to move, can be empty but must not be null.
* @param delta the amount by which to move the hosts
*/
public void moveHosts(IHost hosts[], int delta) {
/*
* Determine the indices of the supplied hosts in this pool.
* If the delta is positive this list should be in descending order,
* if negative, the list should be in ascending oder.
*/
final int m = (delta > 0) ? -1 : 1; // modifier that determines the ordering
SortedSet indices = new TreeSet(new Comparator() {
public int compare(Object o1, Object o2) {
return m * ((Integer)o1).compareTo((Integer)o2);
}
});
List hostList = getHostList();
for (int i = 0; i < hosts.length; i++) {
IHost host = hosts[i];
int index = hostList.indexOf(host);
if (index >= 0) {
indices.add(new Integer(index));
}
}
// Go through the sorted list of indices.
boolean moved = indices.size() > 0;
for (Iterator z = indices.iterator(); z.hasNext() && moved;) {
int index = ((Integer) z.next()).intValue();
moved &= moveHost(hostList, index, delta);
}
if (moved) {
invalidateCache();
commit();
}
}
/**
* Move one connection to a new location
* <b>TODO PROBLEM: CAN'T RE-ORDER FOLDERS SO CAN WE SUPPORT THIS ACTION?</b>
*/
private void moveConnection(IHost conn, int newPos)
{
/*
* DWD revisit, make sure that connections can be "moved", whatever that means.
* It appears that connections can be moved up and down in the list which
* probably provides for some rational for keeping this around.
*/
// java.util.List connList = getHostList();
//FIXME connList.move(newPos, conn);
invalidateCache();
}
* Move a host to a new location in the host pool.
* @param hostList the list of hosts to modify
* @param oldPos the index of the host to move. If outside the bounds of the list, the list is not altered.
* @param delta the amount by which to move the host. If the resulting
* position would be outside the bounds of the list, the list is not altered.
* If 0 then the list is not altered.
* @return true if the host was moved, false if not
*/
private boolean moveHost(List hostList, int oldPos, int delta) {
boolean moved = false; // assume the element will not be moved
if (0 <= oldPos && oldPos < hostList.size() && delta != 0) {
int newPos = oldPos + delta;
if (0 <= newPos && newPos < hostList.size()) {
IHost host = (IHost) hostList.remove(oldPos);
hostList.add(newPos, host);
moved = true;
}
}
return moved;
}
/**
* Order connections according to user preferences.
* Called after restore.
*/
public void orderHosts(String[] names)
{
java.util.List connList = getHostList();
IHost[] conns = new IHost[names.length];
for (int idx=0; idx<conns.length; idx++)
conns[idx] = getHost(names[idx]);
connList.clear();
//System.out.println("Ordering connections within pool " + getName() + "...");
for (int idx=0; idx<conns.length; idx++)
{
connList.add(conns[idx]);
//System.out.println(" '"+conns[idx].getAliasName()+"'");
}
//System.out.println();
invalidateCache();
}
/**
* Order connections according to user preferences.
* Called after restore.
*/
public void orderHosts(String[] names) {
List connList = getHostList();
IHost[] conns = new IHost[names.length];
for (int idx = 0; idx < conns.length; idx++) {
conns[idx] = getHost(names[idx]);
}
connList.clear();
for (int idx = 0; idx < conns.length; idx++) {
connList.add(conns[idx]);
}
invalidateCache();
}
/**
* Return the unqualified save file name with the extension .xmi

View file

@ -0,0 +1,166 @@
/********************************************************************************
* Copyright (c) 2007 IBM Corporation and others. All rights reserved.
* This program and the accompanying materials are made available under the terms
* of the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* David Dykstal (IBM) - initial API and implementation.
* Martin Oberhuber (Wind River) - [184095] Replace systemTypeName by IRSESystemType
* Martin Oberhuber (Wind River) - [177523] Unify singleton getter methods
* Martin Oberhuber (Wind River) - [186773] split ISystemRegistryUI from ISystemRegistry
********************************************************************************/
package org.eclipse.rse.tests.core;
import java.util.Properties;
import org.eclipse.rse.core.IRSESystemType;
import org.eclipse.rse.core.RSECorePlugin;
import org.eclipse.rse.core.model.IHost;
import org.eclipse.rse.core.model.ISystemRegistry;
import org.eclipse.rse.tests.core.connection.IRSEConnectionProperties;
import org.eclipse.rse.tests.core.connection.RSEBaseConnectionTestCase;
/**
* Tests for host move in the host pool.
* Each testcase method should leave the host pool as it was prior to running the method.
*/
public class HostMoveTest extends RSEBaseConnectionTestCase {
static final int NUMBER_OF_HOSTS = 6; // number of hosts
private IHost hostArray[] = null;
private ISystemRegistry registry = null;
/* (non-Javadoc)
* @see org.eclipse.rse.tests.core.RSECoreTestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
registry = RSECorePlugin.getTheSystemRegistry();
createHosts();
}
/* (non-Javadoc)
* @see org.eclipse.rse.tests.core.RSECoreTestCase#tearDown()
*/
protected void tearDown() throws Exception {
deleteHosts();
super.tearDown();
}
public void testMoveOneUp() {
checkPrecondition();
IHost host = hostArray[NUMBER_OF_HOSTS - 1];
IHost[] hosts = new IHost[] {host};
registry.moveHosts("TestProfile", hosts, -1);
assertEquals(NUMBER_OF_HOSTS - 2, registry.getHostPosition(host));
registry.moveHosts("TestProfile", hosts, 1);
assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(host));
}
public void testMoveManyUp() {
checkPrecondition();
IHost[] hosts = new IHost[] {hostArray[NUMBER_OF_HOSTS - 1], hostArray[NUMBER_OF_HOSTS - 2]};
registry.moveHosts("TestProfile", hosts, -2);
assertEquals(NUMBER_OF_HOSTS - 3, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 1]));
assertEquals(NUMBER_OF_HOSTS - 4, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 2]));
registry.moveHosts("TestProfile", hosts, 2);
assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 1]));
assertEquals(NUMBER_OF_HOSTS - 2, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 2]));
}
public void testMoveFirstUp() {
checkPrecondition();
IHost host = hostArray[0];
assertEquals(0, registry.getHostPosition(host));
IHost[] hosts = new IHost[] {host};
registry.moveHosts("TestProfile", hosts, -1); // should not actually move
assertEquals(0, registry.getHostPosition(host));
}
public void testMoveOneDown() {
checkPrecondition();
IHost host = hostArray[1]; // second in the list
assertEquals(1, registry.getHostPosition(host));
IHost[] hosts = new IHost[] {host};
registry.moveHosts("TestProfile", hosts, 1);
assertEquals(2, registry.getHostPosition(host));
registry.moveHosts("TestProfile", hosts, -1);
assertEquals(1, registry.getHostPosition(host));
}
public void testMoveManyDown() {
checkPrecondition();
IHost[] hosts = new IHost[] {hostArray[0], hostArray[2], hostArray[4]};
assertEquals(0, registry.getHostPosition(hostArray[0]));
assertEquals(2, registry.getHostPosition(hostArray[2]));
assertEquals(4, registry.getHostPosition(hostArray[4]));
registry.moveHosts("TestProfile", hosts, 1);
assertEquals(1, registry.getHostPosition(hostArray[0]));
assertEquals(3, registry.getHostPosition(hostArray[2]));
assertEquals(5, registry.getHostPosition(hostArray[4]));
registry.moveHosts("TestProfile", hosts, -1);
assertEquals(0, registry.getHostPosition(hostArray[0]));
assertEquals(2, registry.getHostPosition(hostArray[2]));
assertEquals(4, registry.getHostPosition(hostArray[4]));
}
public void testMoveLastDown() {
checkPrecondition();
IHost host = hostArray[NUMBER_OF_HOSTS - 1];
assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(host));
IHost[] hosts = new IHost[] {host};
registry.moveHosts("TestProfile", hosts, 1); // should not actually move
assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(host));
}
public void testNoHost() {
checkPrecondition();
IHost[] hosts = new IHost[] {};
registry.moveHosts("TestProfile", hosts, -1); // should not fail
}
/**
* Create the test hosts.
*/
private void createHosts() {
hostArray = new IHost[NUMBER_OF_HOSTS];
/* Common host properties */
Properties properties = new Properties();
properties.setProperty(IRSEConnectionProperties.ATTR_PROFILE_NAME, "TestProfile"); //$NON-NLS-1$
properties.setProperty(IRSEConnectionProperties.ATTR_ADDRESS, "localhost"); //$NON-NLS-1$
properties.setProperty(IRSEConnectionProperties.ATTR_SYSTEM_TYPE_ID, IRSESystemType.SYSTEMTYPE_UNIX_ID);
properties.setProperty(IRSEConnectionProperties.ATTR_USERID, "userid"); //$NON-NLS-1$
properties.setProperty(IRSEConnectionProperties.ATTR_PASSWORD, "password"); //$NON-NLS-1$
IRSEConnectionProperties props = getConnectionManager().loadConnectionProperties(properties, false);
for (int i = 0; i < hostArray.length; i++) {
String hostName = getHostName(i);
properties.setProperty(IRSEConnectionProperties.ATTR_NAME, hostName); //$NON-NLS-1$
hostArray[i] = getConnectionManager().findOrCreateConnection(props);
assertNotNull("Failed to create connection " + props.getProperty(IRSEConnectionProperties.ATTR_NAME), hostArray[i]); //$NON-NLS-1$
}
}
private void deleteHosts() {
for (int i = 1; i < hostArray.length; i++) {
registry.deleteHost(hostArray[i]);
}
}
private void checkPrecondition() {
for (int i = 0; i < hostArray.length; i++) {
assertEquals("Precondition check failed", i, registry.getHostPosition(hostArray[i]));
}
}
private String getHostName(int i) {
String hostName = "TestHost" + Integer.toString(i);
return hostName;
}
}