Modified: directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SWTBotUtils.java URL: http://svn.apache.org/viewvc/directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SWTBotUtils.java?rev=741561&r1=741560&r2=741561&view=diff ============================================================================== --- directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SWTBotUtils.java (original) +++ directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SWTBotUtils.java Fri Feb 6 13:21:59 2009 @@ -20,31 +20,14 @@ package org.apache.directory.studio.test.integration.ui; - -import static net.sf.swtbot.finder.UIThreadRunnable.syncExec; -import static net.sf.swtbot.matcher.WidgetMatcherFactory.widgetOfType; +import static org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable.syncExec; +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType; import static org.hamcrest.Matchers.anything; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import net.sf.swtbot.eclipse.finder.SWTEclipseBot; -import net.sf.swtbot.eclipse.finder.widgets.SWTBotView; -import net.sf.swtbot.eclipse.finder.widgets.SWTBotWorkbenchPart; -import net.sf.swtbot.finder.UIThreadRunnable; -import net.sf.swtbot.finder.results.VoidResult; -import net.sf.swtbot.finder.results.WidgetResult; -import net.sf.swtbot.wait.DefaultCondition; -import net.sf.swtbot.wait.ICondition; -import net.sf.swtbot.widgets.SWTBotButton; -import net.sf.swtbot.widgets.SWTBotMenu; -import net.sf.swtbot.widgets.SWTBotTable; -import net.sf.swtbot.widgets.SWTBotTree; -import net.sf.swtbot.widgets.SWTBotTreeItem; -import net.sf.swtbot.widgets.TimeoutException; -import net.sf.swtbot.widgets.WidgetNotFoundException; - import org.apache.directory.studio.connection.core.Connection; import org.apache.directory.studio.connection.core.ConnectionCorePlugin; import org.apache.directory.studio.connection.core.ConnectionFolder; @@ -56,459 +39,454 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.Widget; +import org.eclipse.swtbot.eclipse.finder.SWTEclipseBot; +import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView; +import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotWorkbenchPart; +import org.eclipse.swtbot.swt.finder.exceptions.WidgetNotFoundException; +import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; +import org.eclipse.swtbot.swt.finder.results.VoidResult; +import org.eclipse.swtbot.swt.finder.results.WidgetResult; +import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; +import org.eclipse.swtbot.swt.finder.waits.ICondition; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotButton; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotMenu; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTable; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; +import org.eclipse.swtbot.swt.finder.widgets.TimeoutException; import org.eclipse.ui.IEditorReference; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; - /** * Helpers for using SWTBot. - * - * @author Apache Directory Project + * + * @author Apache Directory + * Project * @version $Rev$, $Date$ */ -public class SWTBotUtils -{ +public class SWTBotUtils { - /** - * Opens the LDAP perspective. - * - * @param bot the bot - * - * @throws Exception the exception - */ - public static void openLdapPerspective( final SWTEclipseBot eBot ) throws Exception - { - // optimization: only open LDAP perspective if not yet opened - try - { - if ( eBot.view( "Connections" ) != null ) - { - // LDAP perspective already opened - return; - } - } - catch ( Exception e ) - { - // no 'Connections' view, continue to open the LDAP perspective - } - - // open "Open Perspective" dialog - SWTBotMenu windowMenu = eBot.menu( "Window" ); - windowMenu.click(); - SWTBotMenu perspectiveMenu = windowMenu.menu( "Open Perspective" ); - perspectiveMenu.click(); - SWTBotMenu otherMenu = windowMenu.menu( "Other..." ); - otherMenu.click(); - - // select "LDAP" perspective - SWTBotTable table = eBot.table(); - table.select( "LDAP" ); - - // press "OK" - SWTBotButton okButton = eBot.button( "OK" ); - okButton.click(); - - // wait till Connections view become visible - eBot.waitUntil( new DefaultCondition() - { - public boolean test() throws Exception - { - return eBot.view( "Connections" ) != null; - } - - - public String getFailureMessage() - { - return "Could not find widget"; - } - } ); - - // close welcome view, if it is there - try - { - eBot.view( "Welcome" ).close(); - } - catch ( Exception e ) - { - } - } - - - /** - * Creates the test connection. - * - * @param bot the bot - * @param name the name of the connection - * @param port the port to use - * - * @return the connection - * - * @throws Exception the exception - */ - public static Connection createTestConnection( SWTEclipseBot bot, String name, int port ) throws Exception - { - SWTBotTree connectionsTree = getConnectionsTree( bot ); - - ConnectionManager connectionManager = ConnectionCorePlugin.getDefault().getConnectionManager(); - ConnectionParameter connectionParameter = new ConnectionParameter(); - connectionParameter.setName( name ); - connectionParameter.setHost( "localhost" ); - connectionParameter.setPort( port ); - connectionParameter.setEncryptionMethod( EncryptionMethod.NONE ); - connectionParameter.setAuthMethod( AuthenticationMethod.SIMPLE ); - connectionParameter.setBindPrincipal( "uid=admin,ou=system" ); - connectionParameter.setBindPassword( "secret" ); - Connection connection = new Connection( connectionParameter ); - connectionManager.addConnection( connection ); - - ConnectionFolderManager connectionFolderManager = ConnectionCorePlugin.getDefault() - .getConnectionFolderManager(); - ConnectionFolder rootConnectionFolder = connectionFolderManager.getRootConnectionFolder(); - rootConnectionFolder.addConnectionId( connection.getId() ); - - connectionsTree.select( name ); - //new OpenConnectionsJob( connection ).execute(); - - Thread.sleep( 1000 ); - return connection; - } - - - /** - * Deletes the test connection. - */ - public static void deleteTestConnections() - { - ConnectionManager connectionManager = ConnectionCorePlugin.getDefault().getConnectionManager(); - for ( Connection connection : connectionManager.getConnections() ) - { - connectionManager.removeConnection( connection ); - } - } - - - /** - * Gets the connections tree. - * - * @param bot the bot - * - * @return the connections tree - * - * @throws Exception the exception - */ - public static SWTBotTree getConnectionsTree( SWTEclipseBot bot ) throws Exception - { - SWTBotView view = bot.view( "Connections" ); - view.show(); - - Tree tree = ( Tree ) bot.widget( widgetOfType( Tree.class ), view.widget ); - return new SWTBotTree( tree ); - } - - - /** - * Gets the ldap browser tree. - * - * @param bot the bot - * - * @return the ldap browser tree - * - * @throws Exception the exception - */ - public static SWTBotTree getLdapBrowserTree( SWTEclipseBot bot ) throws Exception - { - SWTBotView view = bot.view( "LDAP Browser" ); - view.show(); - - Tree tree = ( Tree ) bot.widget( widgetOfType( Tree.class ), view.widget ); - return new SWTBotTree( tree ); - } - - - /** - * Gets the entry editor tree. - * - * @param bot the bot - * - * @return the entry editor tree - * - * @throws Exception the exception - */ - public static SWTBotTree getEntryEditorTree( final SWTEclipseBot bot ) throws Exception - { - Tree tree = UIThreadRunnable.syncExec( new WidgetResult() - { - public Tree run() - { - IWorkbench workbench = PlatformUI.getWorkbench(); - IWorkbenchWindow activeWorkbenchWindow = workbench.getActiveWorkbenchWindow(); - IWorkbenchPage[] pages = activeWorkbenchWindow.getPages(); - for ( int i = 0; i < pages.length; i++ ) - { - IWorkbenchPage page = pages[i]; - IEditorReference[] editorReferences = page.getEditorReferences(); - for ( int j = 0; j < editorReferences.length; j++ ) - { - IEditorReference editorReference = editorReferences[j]; - if ( editorReference.getName().equals( "Entry Editor" ) ) - { - DummyEditor editor = new DummyEditor( editorReference, bot ); - Tree tree = ( Tree ) bot.widget( widgetOfType( Tree.class ), editor.widget ); - return tree; - - } - } - } - throw new WidgetNotFoundException( "Could not find Entry Editor tree" ); - } - } ); - return new SWTBotTree( tree ); - } - - static class DummyEditor extends SWTBotWorkbenchPart - { - public Widget widget; - - - public DummyEditor( IEditorReference editorReference, SWTEclipseBot bot ) - { - super( editorReference, bot ); - widget = findWidget( anything() ); - } - - - public void setFocus() - { - syncExec( new VoidResult() - { - public void run() - { - ( ( Control ) widget ).setFocus(); - } - } ); - } - } - - - /** - * Clicks a button asynchronously and waits till the given condition - * is fulfilled. - * - * @param bot the SWT bot - * @param button the button to click - * @param waitCondition the condition to wait for, may be null - * - * @throws TimeoutException - */ - public static void asyncClick( final SWTEclipseBot bot, final SWTBotButton button, final ICondition waitCondition ) - throws TimeoutException - { - bot.waitUntil( new DefaultCondition() - { - public boolean test() throws Exception - { - return button.isEnabled(); - } - - - public String getFailureMessage() - { - return "Button isn't enabled."; - } - } ); - - UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult() - { - public void run() - { - button.click(); - } - } ); - - if ( waitCondition != null ) - { - bot.waitUntil( waitCondition ); - } - } - - - /** - * Clicks a menu item asynchronously and waits till the given condition - * is fulfilled. - * - * @param bot the SWT bot - * @param button the button to click - * @param waitCondition the condition to wait for, may be null - * - * @throws TimeoutException - */ - public static void asyncClick( final SWTEclipseBot bot, final SWTBotMenu menu, final ICondition waitCondition ) - throws TimeoutException - { - UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult() - { - public void run() - { - menu.click(); - } - } ); - - if ( waitCondition != null ) - { - bot.waitUntil( waitCondition ); - } - } - - - /** - * Clicks a tree item asynchronously and waits till the given condition - * is fulfilled. - * - * @param bot the SWT bot - * @param item the tree item to click - * @param waitCondition the condition to wait for, may be null - * - * @throws TimeoutException the timeout exception - */ - public static void asyncClick( final SWTEclipseBot bot, final SWTBotTreeItem item, final ICondition waitCondition ) - throws TimeoutException - { - UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult() - { - public void run() - { - item.click(); - } - } ); - - if ( waitCondition != null ) - { - bot.waitUntil( waitCondition ); - } - } - - - /** - * Selects an entry in the browser tree and optionally expands the selected entry. - * Takes care that all attributes and child entries are initialized so - * that there are no pending background actions and event notifications. - * This is necessary to avoid race conditions. - * - * @param bot the SWT bot - * @param tree the browser tree - * @param expandChild true to expand the child entry - * @param path the path to the entry - * - * @return the selected entry as SWTBotTreeItem - * - * @throws Exception the exception - */ - public static SWTBotTreeItem selectEntry( final SWTEclipseBot bot, final SWTBotTree tree, - final boolean expandChild, final String... path ) throws Exception - { - List pathList = new ArrayList( Arrays.asList( path ) ); - SWTBotTreeItem entry = null; - - while ( !pathList.isEmpty() ) - { - String currentPath = pathList.remove( 0 ); - - if ( entry == null ) - { - entry = tree.getTreeItem( currentPath ); - } - else - { - // adjust current path, because the label is decorated with the number of children - currentPath = adjustNodeName( entry, currentPath ); - entry = entry.getNode( currentPath ); - } - - final SWTBotTreeItem tempEntry = entry; - UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult() - { - public void run() - { - tempEntry.select(); - } - } ); - - if ( !pathList.isEmpty() || expandChild ) - { - // expand entry and wait till - // - children are displayed - // - next child is visible - final String nextName = !pathList.isEmpty() ? pathList.get( 0 ) : null; - expandEntry( bot, entry, nextName ); - } - - } - return entry; - } - - - /** - * Expands the entry. - * Takes care that all attributes and child entries are initialized so - * that there are no pending background actions and event notifications. - * This is necessary to avoid race conditions. - * - * @param bot the bot - * @param entry the entry to expand - * @param nextName the name of the entry that must become visible, may be null - * - * @throws Exception the exception - */ - public static void expandEntry( final SWTEclipseBot bot, final SWTBotTreeItem entry, final String nextName ) - throws Exception - { - UIThreadRunnable.asyncExec( bot.getDisplay(), new VoidResult() - { - public void run() - { - entry.expand(); - } - } ); - - bot.waitUntil( new DefaultCondition() - { - public boolean test() throws Exception - { - if ( nextName != null ) - { - String adjustedNodeName = nextName != null ? adjustNodeName( entry, nextName ) : null; - SWTBotTreeItem node = entry.getNode( adjustedNodeName ); - if ( node == null ) - { - return false; - } - } - return !entry.getNodes().contains( "Fetching Entries..." ); - } - - - public String getFailureMessage() - { - return "Could not find entry " + entry.getText() + " -> " + nextName; - } - } ); - } - - - private static String adjustNodeName( SWTBotTreeItem child, String nodeName ) - { - List nodes = child.getNodes(); - for ( String node : nodes ) - { - if ( node.toUpperCase().startsWith( nodeName.toUpperCase() ) ) - { - return node; - } - } - return null; - } + /** + * Opens the LDAP perspective. + * + * @param bot + * the bot + * + * @throws Exception + * the exception + */ + public static void openLdapPerspective(final SWTEclipseBot eBot) + throws Exception { + // optimization: only open LDAP perspective if not yet opened + try { + if (eBot.view("Connections") != null) { + // LDAP perspective already opened + return; + } + } catch (Exception e) { + // no 'Connections' view, continue to open the LDAP perspective + } + + // open "Open Perspective" dialog + SWTBotMenu windowMenu = eBot.menu("Window"); + windowMenu.click(); + SWTBotMenu perspectiveMenu = windowMenu.menu("Open Perspective"); + perspectiveMenu.click(); + SWTBotMenu otherMenu = windowMenu.menu("Other..."); + otherMenu.click(); + + // select "LDAP" perspective + SWTBotTable table = eBot.table(); + table.select("LDAP"); + + // press "OK" + SWTBotButton okButton = eBot.button("OK"); + okButton.click(); + + // wait till Connections view become visible + eBot.waitUntil(new DefaultCondition() { + public boolean test() throws Exception { + return eBot.view("Connections") != null; + } + + public String getFailureMessage() { + return "Could not find widget"; + } + }); + + // close welcome view, if it is there + try { + eBot.view("Welcome").close(); + } catch (Exception e) { + } + } + + /** + * Creates the test connection. + * + * @param bot + * the bot + * @param name + * the name of the connection + * @param port + * the port to use + * + * @return the connection + * + * @throws Exception + * the exception + */ + public static Connection createTestConnection(SWTEclipseBot bot, + String name, int port) throws Exception { + SWTBotTree connectionsTree = getConnectionsTree(bot); + + ConnectionManager connectionManager = ConnectionCorePlugin.getDefault() + .getConnectionManager(); + ConnectionParameter connectionParameter = new ConnectionParameter(); + connectionParameter.setName(name); + connectionParameter.setHost("localhost"); + connectionParameter.setPort(port); + connectionParameter.setEncryptionMethod(EncryptionMethod.NONE); + connectionParameter.setAuthMethod(AuthenticationMethod.SIMPLE); + connectionParameter.setBindPrincipal("uid=admin,ou=system"); + connectionParameter.setBindPassword("secret"); + Connection connection = new Connection(connectionParameter); + connectionManager.addConnection(connection); + + ConnectionFolderManager connectionFolderManager = ConnectionCorePlugin + .getDefault().getConnectionFolderManager(); + ConnectionFolder rootConnectionFolder = connectionFolderManager + .getRootConnectionFolder(); + rootConnectionFolder.addConnectionId(connection.getId()); + + connectionsTree.select(name); + // new OpenConnectionsJob( connection ).execute(); + + Thread.sleep(1000); + return connection; + } + + /** + * Deletes the test connection. + */ + public static void deleteTestConnections() { + ConnectionManager connectionManager = ConnectionCorePlugin.getDefault() + .getConnectionManager(); + for (Connection connection : connectionManager.getConnections()) { + connectionManager.removeConnection(connection); + } + } + + /** + * Gets the connections tree. + * + * @param bot + * the bot + * + * @return the connections tree + * + * @throws Exception + * the exception + */ + public static SWTBotTree getConnectionsTree(SWTEclipseBot bot) + throws Exception { + SWTBotView view = bot.view("Connections"); + view.show(); + + Tree tree = (Tree) bot.widget(widgetOfType(Tree.class), view + .getWidget()); + return new SWTBotTree(tree); + } + + /** + * Gets the ldap browser tree. + * + * @param bot + * the bot + * + * @return the ldap browser tree + * + * @throws Exception + * the exception + */ + public static SWTBotTree getLdapBrowserTree(SWTEclipseBot bot) + throws Exception { + SWTBotView view = bot.view("LDAP Browser"); + view.show(); + + Tree tree = (Tree) bot.widget(widgetOfType(Tree.class), view + .getWidget()); + return new SWTBotTree(tree); + } + + /** + * Gets the entry editor tree. + * + * @param bot + * the bot + * + * @return the entry editor tree + * + * @throws Exception + * the exception + */ + public static SWTBotTree getEntryEditorTree(final SWTEclipseBot bot) + throws Exception { + Tree tree = UIThreadRunnable.syncExec(new WidgetResult() { + public Tree run() { + IWorkbench workbench = PlatformUI.getWorkbench(); + IWorkbenchWindow activeWorkbenchWindow = workbench + .getActiveWorkbenchWindow(); + IWorkbenchPage[] pages = activeWorkbenchWindow.getPages(); + for (int i = 0; i < pages.length; i++) { + IWorkbenchPage page = pages[i]; + IEditorReference[] editorReferences = page + .getEditorReferences(); + for (int j = 0; j < editorReferences.length; j++) { + IEditorReference editorReference = editorReferences[j]; + if (editorReference.getName().equals("Entry Editor")) { + DummyEditor editor = new DummyEditor( + editorReference, bot); + Tree tree = (Tree) bot.widget( + widgetOfType(Tree.class), editor.widget); + return tree; + + } + } + } + throw new WidgetNotFoundException( + "Could not find Entry Editor tree"); + } + }); + return new SWTBotTree(tree); + } + + static class DummyEditor extends SWTBotWorkbenchPart { + public Widget widget; + + public DummyEditor(IEditorReference editorReference, SWTEclipseBot bot) { + super(editorReference, bot); + widget = findWidget(anything()); + } + + public void setFocus() { + syncExec(new VoidResult() { + public void run() { + ((Control) widget).setFocus(); + } + }); + } + + public boolean isActive() { + return true; + } + } + + /** + * Clicks a button asynchronously and waits till the given condition is + * fulfilled. + * + * @param bot + * the SWT bot + * @param button + * the button to click + * @param waitCondition + * the condition to wait for, may be null + * + * @throws TimeoutException + */ + public static void asyncClick(final SWTEclipseBot bot, + final SWTBotButton button, final ICondition waitCondition) + throws TimeoutException { + bot.waitUntil(new DefaultCondition() { + public boolean test() throws Exception { + return button.isEnabled(); + } + + public String getFailureMessage() { + return "Button isn't enabled."; + } + }); + + UIThreadRunnable.asyncExec(bot.getDisplay(), new VoidResult() { + public void run() { + button.click(); + } + }); + + if (waitCondition != null) { + bot.waitUntil(waitCondition); + } + } + + /** + * Clicks a menu item asynchronously and waits till the given condition is + * fulfilled. + * + * @param bot + * the SWT bot + * @param button + * the button to click + * @param waitCondition + * the condition to wait for, may be null + * + * @throws TimeoutException + */ + public static void asyncClick(final SWTEclipseBot bot, + final SWTBotMenu menu, final ICondition waitCondition) + throws TimeoutException { + UIThreadRunnable.asyncExec(bot.getDisplay(), new VoidResult() { + public void run() { + menu.click(); + } + }); + + if (waitCondition != null) { + bot.waitUntil(waitCondition); + } + } + + /** + * Clicks a tree item asynchronously and waits till the given condition is + * fulfilled. + * + * @param bot + * the SWT bot + * @param item + * the tree item to click + * @param waitCondition + * the condition to wait for, may be null + * + * @throws TimeoutException + * the timeout exception + */ + public static void asyncClick(final SWTEclipseBot bot, + final SWTBotTreeItem item, final ICondition waitCondition) + throws TimeoutException { + UIThreadRunnable.asyncExec(bot.getDisplay(), new VoidResult() { + public void run() { + item.click(); + } + }); + + if (waitCondition != null) { + bot.waitUntil(waitCondition); + } + } + + /** + * Selects an entry in the browser tree and optionally expands the selected + * entry. Takes care that all attributes and child entries are initialized + * so that there are no pending background actions and event notifications. + * This is necessary to avoid race conditions. + * + * @param bot + * the SWT bot + * @param tree + * the browser tree + * @param expandChild + * true to expand the child entry + * @param path + * the path to the entry + * + * @return the selected entry as SWTBotTreeItem + * + * @throws Exception + * the exception + */ + public static SWTBotTreeItem selectEntry(final SWTEclipseBot bot, + final SWTBotTree tree, final boolean expandChild, + final String... path) throws Exception { + List pathList = new ArrayList(Arrays.asList(path)); + SWTBotTreeItem entry = null; + + while (!pathList.isEmpty()) { + String currentPath = pathList.remove(0); + + if (entry == null) { + entry = tree.getTreeItem(currentPath); + } else { + // adjust current path, because the label is decorated with the + // number of children + currentPath = adjustNodeName(entry, currentPath); + entry = entry.getNode(currentPath); + } + + final SWTBotTreeItem tempEntry = entry; + UIThreadRunnable.asyncExec(bot.getDisplay(), new VoidResult() { + public void run() { + tempEntry.select(); + } + }); + + if (!pathList.isEmpty() || expandChild) { + // expand entry and wait till + // - children are displayed + // - next child is visible + final String nextName = !pathList.isEmpty() ? pathList.get(0) + : null; + expandEntry(bot, entry, nextName); + } + + } + return entry; + } + + /** + * Expands the entry. Takes care that all attributes and child entries are + * initialized so that there are no pending background actions and event + * notifications. This is necessary to avoid race conditions. + * + * @param bot + * the bot + * @param entry + * the entry to expand + * @param nextName + * the name of the entry that must become visible, may be null + * + * @throws Exception + * the exception + */ + public static void expandEntry(final SWTEclipseBot bot, + final SWTBotTreeItem entry, final String nextName) throws Exception { + UIThreadRunnable.asyncExec(bot.getDisplay(), new VoidResult() { + public void run() { + entry.expand(); + } + }); + + bot.waitUntil(new DefaultCondition() { + public boolean test() throws Exception { + if (nextName != null) { + String adjustedNodeName = nextName != null ? adjustNodeName( + entry, nextName) + : null; + SWTBotTreeItem node = entry.getNode(adjustedNodeName); + if (node == null) { + return false; + } + } + return !entry.getNodes().contains("Fetching Entries..."); + } + + public String getFailureMessage() { + return "Could not find entry " + entry.getText() + " -> " + + nextName; + } + }); + } + + private static String adjustNodeName(SWTBotTreeItem child, String nodeName) { + List nodes = child.getNodes(); + for (String node : nodes) { + if (node.toUpperCase().startsWith(nodeName.toUpperCase())) { + return node; + } + } + return null; + } } Modified: directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SwtResourcesTest.java URL: http://svn.apache.org/viewvc/directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SwtResourcesTest.java?rev=741561&r1=741560&r2=741561&view=diff ============================================================================== --- directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SwtResourcesTest.java (original) +++ directory/studio/trunk/test-integration-ui/src/main/java/org/apache/directory/studio/test/integration/ui/SwtResourcesTest.java Fri Feb 6 13:21:59 2009 @@ -20,197 +20,173 @@ package org.apache.directory.studio.test.integration.ui; - -import net.sf.swtbot.eclipse.finder.SWTEclipseBot; -import net.sf.swtbot.finder.UIThreadRunnable; -import net.sf.swtbot.finder.results.IntResult; -import net.sf.swtbot.wait.DefaultCondition; -import net.sf.swtbot.widgets.SWTBotCombo; -import net.sf.swtbot.widgets.SWTBotText; -import net.sf.swtbot.widgets.SWTBotTree; - import org.apache.directory.server.unit.AbstractServerTest; import org.eclipse.swt.graphics.DeviceData; - +import org.eclipse.swtbot.eclipse.finder.SWTEclipseBot; +import org.eclipse.swtbot.swt.finder.finders.UIThreadRunnable; +import org.eclipse.swtbot.swt.finder.results.IntResult; +import org.eclipse.swtbot.swt.finder.waits.DefaultCondition; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotCombo; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotText; +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; /** * Tests allocation of SWT Resources. - * - * @author Apache Directory Project + * + * @author Apache Directory + * Project * @version $Rev$, $Date$ */ -public class SwtResourcesTest extends AbstractServerTest -{ - private SWTEclipseBot bot; - - - protected void setUp() throws Exception - { - super.setUp(); - bot = new SWTEclipseBot(); - SWTBotUtils.openLdapPerspective( bot ); - SWTBotUtils.createTestConnection( bot, "SwtResourcesTest", ldapService.getPort() ); - } - - - protected void tearDown() throws Exception - { - SWTBotUtils.deleteTestConnections(); - bot = null; - super.tearDown(); - } - - - /** - * Test for DIRSTUDIO-319. - * - * Creates multiple entries using the New Entry wizard. Checks - * that we don't allocate too much SWT resources during the run. - * - * @throws Exception the exception - */ - public void testSwtResourcesDelta() throws Exception - { - SWTBotTree browserTree = SWTBotUtils.getLdapBrowserTree( bot ); - - // run the new entry wizard once to ensure all SWT resources are created - createAndDeleteEntry( browserTree, "testSwtResourcesDelta" + 0 ); - - // remember the SWT objects before the run - int beforeObjectCount = getSwtObjectCount(); - - // now lets run the new entry wizard it several times - for ( int i = 1; i < 25; i++ ) - { - createAndDeleteEntry( browserTree, "testSwtResourcesDelta" + i ); - } - - // get the SWT objects after the run - int afterObjectCount = getSwtObjectCount(); - - // we expect not more than 10 additional SWT objects - assertTrue( "Too many SWT resources were allocated in testSwtResourcesDelta: before=" + beforeObjectCount - + ", after=" + afterObjectCount, afterObjectCount - beforeObjectCount < 10 ); - } - - - /** - * Ensure that we have not allocated more the 1000 SWT resources - * during the complete test suite. - * - * 1000 is not a fix number but it is a good starting point. - * - * @throws Exception the exception - */ - public void testSwtResourcesCount() throws Exception - { - int swtObjectCount = getSwtObjectCount(); - assertTrue( "Too many SWT resources were allocated: " + swtObjectCount, swtObjectCount < 1000 ); - } - - - private int getSwtObjectCount() - { - return UIThreadRunnable.syncExec( bot.getDisplay(), new IntResult() - { - public Integer run() - { - DeviceData info = bot.getDisplay().getDeviceData(); - if ( !info.tracking ) - { - fail( "To run this test options 'org.eclipse.ui/debug' and 'org.eclipse.ui/trace/graphics' must be true." ); - } - return info.objects.length; - } - } ); - } - - - private void createAndDeleteEntry( final SWTBotTree browserTree, final String name ) throws Exception - { - SWTBotUtils.selectEntry( bot, browserTree, true, "DIT", "Root DSE", "ou=system" ); - SWTBotUtils.asyncClick( bot, browserTree.contextMenu( "New Entry..." ), new DefaultCondition() - { - public boolean test() throws Exception - { - return bot.shell( "New Entry" ) != null; - } - - - public String getFailureMessage() - { - return "Could not find dialog 'New Entry'"; - } - } ); - - bot.radio( "Create entry from scratch" ).click(); - bot.button( "Next >" ).click(); - - bot.table( 0 ).select( "organization" ); - bot.button( "Add" ).click(); - bot.button( "Next >" ).click(); - - SWTBotCombo typeCombo = bot.comboBox( "" ); - typeCombo.setText( "o" ); - SWTBotText valueText = bot.text( "" ); - valueText.setText( name ); - SWTBotUtils.asyncClick( bot, bot.button( "Next >" ), new DefaultCondition() - { - public boolean test() throws Exception - { - return bot.button( "Finish" ).isEnabled(); - } - - - public String getFailureMessage() - { - return "Finish button is not enabled"; - } - } ); - - // click finish to create the entry - SWTBotUtils.asyncClick( bot, bot.button( "Finish" ), new DefaultCondition() - { - public boolean test() throws Exception - { - return browserTree.selection().get( 0 ).get( 0 ).startsWith( "o=" + name ); - } - - - public String getFailureMessage() - { - return "Could not select 'o=" + name + "'"; - } - } ); - - // delete the entry - SWTBotUtils.selectEntry( bot, browserTree, false, "DIT", "Root DSE", "ou=system", "o=" + name ); - SWTBotUtils.asyncClick( bot, browserTree.contextMenu( "Delete Entry" ), new DefaultCondition() - { - public boolean test() throws Exception - { - return bot.shell( "Delete Entry" ) != null; - } - - - public String getFailureMessage() - { - return "Could not find dialog 'New Entry'"; - } - } ); - SWTBotUtils.asyncClick( bot, bot.button( "OK" ), new DefaultCondition() - { - public boolean test() throws Exception - { - return browserTree.selection().get( 0 ).get( 0 ).startsWith( "ou=system" ); - } - - - public String getFailureMessage() - { - return "Could not select 'ou=system'"; - } - } ); - } +public class SwtResourcesTest extends AbstractServerTest { + private SWTEclipseBot bot; + + protected void setUp() throws Exception { + super.setUp(); + bot = new SWTEclipseBot(); + SWTBotUtils.openLdapPerspective(bot); + SWTBotUtils.createTestConnection(bot, "SwtResourcesTest", ldapService + .getPort()); + } + + protected void tearDown() throws Exception { + SWTBotUtils.deleteTestConnections(); + bot = null; + super.tearDown(); + } + + /** + * Test for DIRSTUDIO-319. + * + * Creates multiple entries using the New Entry wizard. Checks that we don't + * allocate too much SWT resources during the run. + * + * @throws Exception + * the exception + */ + public void testSwtResourcesDelta() throws Exception { + SWTBotTree browserTree = SWTBotUtils.getLdapBrowserTree(bot); + + // run the new entry wizard once to ensure all SWT resources are created + createAndDeleteEntry(browserTree, "testSwtResourcesDelta" + 0); + + // remember the SWT objects before the run + int beforeObjectCount = getSwtObjectCount(); + + // now lets run the new entry wizard it several times + for (int i = 1; i < 25; i++) { + createAndDeleteEntry(browserTree, "testSwtResourcesDelta" + i); + } + + // get the SWT objects after the run + int afterObjectCount = getSwtObjectCount(); + + // we expect not more than 10 additional SWT objects + assertTrue( + "Too many SWT resources were allocated in testSwtResourcesDelta: before=" + + beforeObjectCount + ", after=" + afterObjectCount, + afterObjectCount - beforeObjectCount < 10); + } + + /** + * Ensure that we have not allocated more the 1000 SWT resources during the + * complete test suite. + * + * 1000 is not a fix number but it is a good starting point. + * + * @throws Exception + * the exception + */ + public void testSwtResourcesCount() throws Exception { + int swtObjectCount = getSwtObjectCount(); + assertTrue("Too many SWT resources were allocated: " + swtObjectCount, + swtObjectCount < 1000); + } + + private int getSwtObjectCount() { + return UIThreadRunnable.syncExec(bot.getDisplay(), new IntResult() { + public Integer run() { + DeviceData info = bot.getDisplay().getDeviceData(); + if (!info.tracking) { + fail("To run this test options 'org.eclipse.ui/debug' and 'org.eclipse.ui/trace/graphics' must be true."); + } + return info.objects.length; + } + }); + } + + private void createAndDeleteEntry(final SWTBotTree browserTree, + final String name) throws Exception { + SWTBotUtils.selectEntry(bot, browserTree, true, "DIT", "Root DSE", + "ou=system"); + SWTBotUtils.asyncClick(bot, browserTree.contextMenu("New Entry..."), + new DefaultCondition() { + public boolean test() throws Exception { + return bot.shell("New Entry") != null; + } + + public String getFailureMessage() { + return "Could not find dialog 'New Entry'"; + } + }); + + bot.radio("Create entry from scratch").click(); + bot.button("Next >").click(); + + bot.table(0).select("organization"); + bot.button("Add").click(); + bot.button("Next >").click(); + + SWTBotCombo typeCombo = bot.comboBox(""); + typeCombo.setText("o"); + SWTBotText valueText = bot.text(""); + valueText.setText(name); + SWTBotUtils.asyncClick(bot, bot.button("Next >"), + new DefaultCondition() { + public boolean test() throws Exception { + return bot.button("Finish").isEnabled(); + } + + public String getFailureMessage() { + return "Finish button is not enabled"; + } + }); + + // click finish to create the entry + SWTBotUtils.asyncClick(bot, bot.button("Finish"), + new DefaultCondition() { + public boolean test() throws Exception { + return browserTree.selection().get(0).get(0) + .startsWith("o=" + name); + } + + public String getFailureMessage() { + return "Could not select 'o=" + name + "'"; + } + }); + + // delete the entry + SWTBotUtils.selectEntry(bot, browserTree, false, "DIT", "Root DSE", + "ou=system", "o=" + name); + SWTBotUtils.asyncClick(bot, browserTree.contextMenu("Delete Entry"), + new DefaultCondition() { + public boolean test() throws Exception { + return bot.shell("Delete Entry") != null; + } + + public String getFailureMessage() { + return "Could not find dialog 'New Entry'"; + } + }); + SWTBotUtils.asyncClick(bot, bot.button("OK"), new DefaultCondition() { + public boolean test() throws Exception { + return browserTree.selection().get(0).get(0).startsWith( + "ou=system"); + } + + public String getFailureMessage() { + return "Could not select 'ou=system'"; + } + }); + } }