org.bonitasoft.studio.engine.test.bar.BarExporterTest.java Source code

Java tutorial

Introduction

Here is the source code for org.bonitasoft.studio.engine.test.bar.BarExporterTest.java

Source

/**
 * 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.engine.test.bar;

import java.io.File;
import java.util.Vector;

import junit.framework.Assert;

import org.bonitasoft.studio.common.ProjectUtil;
import org.bonitasoft.studio.common.jface.FileActionDialog;
import org.bonitasoft.studio.engine.i18n.Messages;
import org.bonitasoft.studio.test.swtbot.util.SWTBotTestUtil;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotEditor;
import org.eclipse.swtbot.eclipse.gef.finder.SWTBotGefTestCase;
import org.eclipse.swtbot.eclipse.gef.finder.widgets.SWTBotGefEditor;
import org.eclipse.swtbot.swt.finder.junit.SWTBotJunit4ClassRunner;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * @author Florine Boudin
 *
 */
@RunWith(SWTBotJunit4ClassRunner.class)
public class BarExporterTest extends SWTBotGefTestCase {

    private static boolean disablePopup;
    final public static String EditorTitleRegex = "(.*)\\s\\((.*)\\)";

    @BeforeClass
    public static void setUpBeforeClass() {
        disablePopup = FileActionDialog.getDisablePopup();
        FileActionDialog.setDisablePopup(true);
    }

    @AfterClass
    public static void tearDownAfterClass() {
        FileActionDialog.setDisablePopup(disablePopup);
    }

    @Test
    public void testServerBuild() {

        SWTBotTestUtil.createNewDiagram(bot);
        SWTBotEditor activeEditor = bot.activeEditor();
        String editorTitle = activeEditor.getTitle();
        //System.out.println("editorTitle1 = "+editorTitle1);
        Assert.assertFalse("Error: first diagram name is empty.", editorTitle.isEmpty());

        // get the GEF editor to activate tools
        final SWTBotGefEditor gmfEditor = bot.gefEditor(editorTitle);

        // Create 2 Pools
        gmfEditor.activateTool("Pool");
        gmfEditor.click(200, 500);
        gmfEditor.activateTool("Pool");
        gmfEditor.click(200, 800);

        // Save Diagram
        bot.menu("Diagram").menu("Save").click();

        //System.out.println(editorTitle);
        bot.waitUntil(Conditions.widgetIsEnabled(bot.menu("Server")));

        // Menu Server > Build...
        bot.menu("Server").menu("Build...").click();

        // shell 'Build'
        final SWTBotShell shell = bot.shell(Messages.buildTitle);
        bot.waitUntil(Conditions.shellIsActive(Messages.buildTitle));

        // select and check created Diagram in the Tree
        SWTBotTree tree = bot.treeWithLabel("Select processes to export");
        SWTBotTreeItem diagramTreeItem = tree.getTreeItem(editorTitle);

        // check the diagram to export
        diagramTreeItem.select().check();
        diagramTreeItem.expand();

        // Number of pool in the diagram
        int poolListSize = diagramTreeItem.getItems().length;
        //System.out.println("Diagram contains "+ poolListSize + " items");
        Assert.assertEquals("Error: Diagram must contain 3 Pools.", 3, poolListSize);

        // unselect the first pool of the list

        diagramTreeItem.getNode(0).select();
        diagramTreeItem.getNode(0).uncheck();

        Vector<String> poolTitleList = new Vector<String>(3);
        int poolTitleListSize = poolTitleList.size();

        // check the selected pools
        for (int i = 1; i < poolTitleListSize; i++) {
            SWTBotTreeItem poolTreeItem = diagramTreeItem.getNode(i);
            poolTitleList.set(i, poolTreeItem.getText());
            String poolName = getItemName(poolTitleList.get(i));

            // test the good pool is checked
            Assert.assertFalse("Error: Pool " + i + " should be checked.", !poolTreeItem.isChecked());
            Assert.assertFalse("Error: Pool selected is not the good one.", !poolName.equals("Pool" + i));
        }
        // create tmp directory to write diagrams
        File tmpBarFolder = new File(ProjectUtil.getBonitaStudioWorkFolder(), "testExportBar");
        tmpBarFolder.mkdirs();
        //set the path where files are saved
        final String tmpBarFolderPath = tmpBarFolder.getAbsolutePath();
        bot.comboBoxWithLabel(Messages.destinationPath + " *").setText(tmpBarFolderPath);
        //      String tmpBarFolderPath = bot.comboBoxWithLabel(Messages.destinationPath+" *").getText();
        //      System.out.println("tmpBarFolder = "+tmpBarFolderPath);

        // click 'Finish' button to close 'Build' shell
        bot.waitUntil(Conditions.widgetIsEnabled(bot.button(IDialogConstants.FINISH_LABEL)));
        bot.button(IDialogConstants.FINISH_LABEL).click();

        //  click 'OK' button to close 'Export' shell
        bot.waitUntil(Conditions.shellIsActive(Messages.exportSuccessTitle));
        bot.button(IDialogConstants.OK_LABEL).click();

        // wait the shell to close before checking creation of files
        bot.waitUntil(Conditions.shellCloses(shell));

        // check directory exists
        String diagramDir1 = getItemName(editorTitle) + "--" + getItemVersion(editorTitle);
        //System.out.println("diagramDir1 = "+diagramDir1);

        // check pools files exist
        for (int i = 1; i < poolTitleListSize; i++) {
            String tmpPoolTitle = poolTitleList.get(i);
            String poolFileName = getItemName(tmpPoolTitle) + "--" + getItemVersion(tmpPoolTitle) + ".bar";
            final File poolFile = new File(tmpBarFolderPath, poolFileName);

            assertTrue("Error: The Pool export must exist", poolFile.exists());
            assertTrue("Error: The Pool export must be a file", poolFile.isFile());
        }
    }

    /**Return the name of a Diagram from the Title of the Editor
     * 
     * @param s title of the editor
     * @return the name of the diagram
     * <p>
     * <b>Example:</b>
     * <p>
     * entry  : "MyDiagram (1.0)"<br>
     * return : "MyDiagram"
     */
    public String getItemName(String s) {
        return s.replaceFirst(EditorTitleRegex, "$1");
    }

    /**Return the version of a Diagram from the Title of the Editor
     * 
     * @param s title of the editor
     * @return the name of the diagram
     * <p>
     * <b>Example:</b>
     * <p>
     * entry  : "MyDiagram (1.0)"<br>
     * return : "1.0"
     */
    public String getItemVersion(String s) {
        return s.replaceFirst(EditorTitleRegex, "$2");
    }

}