Java tutorial
/** * Copyright (C) 2012 BonitaSoft S.A. * BonitaSoft, 31 rue Gustave Eiffel - 38000 Grenoble * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2.0 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.bonitasoft.studio.connectors.test.swtbot; import org.bonitasoft.studio.common.jface.FileActionDialog; import org.bonitasoft.studio.common.repository.RepositoryManager; import org.bonitasoft.studio.connector.model.definition.Category; import org.bonitasoft.studio.connector.model.definition.ConnectorDefinition; import org.bonitasoft.studio.connector.model.definition.wizard.ConnectorDefinitionTreeLabelProvider; import org.bonitasoft.studio.connector.model.i18n.DefinitionResourceProvider; import org.bonitasoft.studio.connector.model.i18n.Messages; import org.bonitasoft.studio.connectors.ConnectorPlugin; import org.bonitasoft.studio.connectors.repository.ConnectorDefRepositoryStore; import org.eclipse.jface.dialogs.IDialogConstants; import org.eclipse.swtbot.eclipse.gef.finder.SWTBotGefTestCase; import org.eclipse.swtbot.swt.finder.SWTBot; import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner; import org.eclipse.swtbot.swt.finder.waits.Conditions; import org.eclipse.swtbot.swt.finder.waits.ICondition; import org.junit.Test; import org.junit.runner.RunWith; /** * @author Aurlie Zara * */ @RunWith(SWTBotJunit4ClassRunner.class) public class ConnectorEditionTest extends SWTBotGefTestCase { public void removeConnectorDefinition(final String name, final String version) { SWTBotConnectorTestUtil.activateConnectorDefEditionShell(bot); bot.tree().setFocus(); bot.waitUntil(new ICondition() { public boolean test() throws Exception { bot.tree().select("Uncategorized").expandNode("Uncategorized").select(name + " (" + version + ")"); return bot.tree().selectionCount() > 0; } public void init(SWTBot bot) { } public String getFailureMessage() { return "Cannot select tree item"; } }, 10000, 1000); assertNotNull("could not find" + name + " (" + version + ")", bot.tree().select("Uncategorized") .expandNode("Uncategorized").getNode(name + " (" + version + ")")); bot.tree().select("Uncategorized").expandNode("Uncategorized").select(name + " (" + version + ")"); bot.button("Delete").click(); if (!FileActionDialog.getDisablePopup()) { bot.waitUntil(Conditions.shellIsActive("Delete?")); bot.button(IDialogConstants.YES_LABEL).click(); } bot.button(IDialogConstants.CANCEL_LABEL).click(); } @Test public void testIdRenameEdit() throws Exception { final String id = "testEdit"; final String version = "1.0.0"; final String id2 = "testEdit2"; SWTBotConnectorTestUtil.activateConnectorDefinitionShell(bot); SWTBotConnectorTestUtil.createConnectorDefinition(bot, id, version); bot.button(IDialogConstants.FINISH_LABEL).click(); SWTBotConnectorTestUtil.activateConnectorDefEditionShell(bot); bot.tree().setFocus(); bot.waitUntil(Conditions.widgetIsEnabled( bot.tree().select("Uncategorized").expandNode("Uncategorized").select(id + " (" + version + ")")), 10000); bot.tree().expandNode("Uncategorized").select(id + " (" + version + ")"); bot.button(Messages.Edit).click(); assertEquals(bot.textWithLabel("Definition id *").getText(), id); assertEquals(bot.textWithLabel("Version *").getText(), version); bot.textWithLabel("Definition id *").setText(id2); bot.button(IDialogConstants.FINISH_LABEL).click(); ConnectorDefRepositoryStore store = (ConnectorDefRepositoryStore) RepositoryManager.getInstance() .getRepositoryStore(ConnectorDefRepositoryStore.class); ConnectorDefinition connectorDef = store.getDefinition(id, version); ConnectorDefinition connectorDef2 = store.getDefinition(id2, version); assertNull("the connectorDef with previous id shouldn't exist anymore", connectorDef); assertNotNull("the connectorDef with new id does not exist", connectorDef2); removeConnectorDefinition(id2, version); } @Test public void testVersionEdit() throws Exception { final String id = "testEdit3"; final String version = "1.0.0"; final String version2 = "1.0.1"; SWTBotConnectorTestUtil.activateConnectorDefinitionShell(bot); SWTBotConnectorTestUtil.createConnectorDefinition(bot, id, version); bot.button(IDialogConstants.FINISH_LABEL).click(); SWTBotConnectorTestUtil.activateConnectorDefEditionShell(bot); bot.tree().setFocus(); bot.waitUntil(Conditions.widgetIsEnabled( bot.tree().select("Uncategorized").expandNode("Uncategorized").select(id + " (" + version + ")")), 10000); bot.tree().select("Uncategorized").expandNode("Uncategorized").select(id + " (" + version + ")"); bot.button(Messages.Edit).click(); bot.textWithLabel("Version *").setText(version2); bot.button(IDialogConstants.FINISH_LABEL).click(); ConnectorDefRepositoryStore store = (ConnectorDefRepositoryStore) RepositoryManager.getInstance() .getRepositoryStore(ConnectorDefRepositoryStore.class); ConnectorDefinition connectorDef = store.getDefinition(id, version); ConnectorDefinition connectorDef2 = store.getDefinition(id, version2); assertNull("the connectorDef with previous version shouldn't exist anymore", connectorDef); assertNotNull("the connectorDef with new version does not exist", connectorDef2); removeConnectorDefinition(id, version2); } @Test public void testAddCategory() throws Exception { final String id = "testEdit4"; final String version = "1.0.0"; SWTBotConnectorTestUtil.activateConnectorDefinitionShell(bot); SWTBotConnectorTestUtil.createConnectorDefinition(bot, id, version); bot.button(IDialogConstants.FINISH_LABEL).click(); SWTBotConnectorTestUtil.activateConnectorDefEditionShell(bot); bot.tree().setFocus(); bot.waitUntil(Conditions .widgetIsEnabled(bot.tree().expandNode("Uncategorized").select(id + " (" + version + ")")), 10000); bot.tree().select("Uncategorized").expandNode("Uncategorized").select(id + " (" + version + ")"); bot.button(Messages.Edit).click(); bot.treeWithLabel(Messages.categoryLabel).select(0); bot.button(IDialogConstants.FINISH_LABEL).click(); ConnectorDefRepositoryStore store = (ConnectorDefRepositoryStore) RepositoryManager.getInstance() .getRepositoryStore(ConnectorDefRepositoryStore.class); ConnectorDefinition connectorDef = store.getDefinition(id, version); assertEquals("category size should be equal to 1", 1, connectorDef.getCategory().size()); DefinitionResourceProvider messageProvider = DefinitionResourceProvider.getInstance(store, ConnectorPlugin.getDefault().getBundle()); SWTBotConnectorTestUtil.activateConnectorDefEditionShell(bot); final Category category = connectorDef.getCategory().get(0); String categoryLabel = messageProvider.getCategoryLabel(category); if (categoryLabel == null) { categoryLabel = category.getId(); } final String connectorLabel = new ConnectorDefinitionTreeLabelProvider(messageProvider) .getText(connectorDef); assertNotNull("could not find " + connectorLabel, bot.tree().getTreeItem(categoryLabel).expand().getNode(connectorLabel)); bot.button(IDialogConstants.CANCEL_LABEL).click(); } }