net.sourceforge.seqware.pipeline.tutorial.DeveloperPhase1.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.seqware.pipeline.tutorial.DeveloperPhase1.java

Source

/*
 * Copyright (C) 2013 SeqWare
 *
 * 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 3 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 net.sourceforge.seqware.pipeline.tutorial;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import junit.framework.Assert;
import net.sourceforge.seqware.common.util.Log;
import net.sourceforge.seqware.pipeline.plugins.ITUtility;
import net.sourceforge.seqware.pipeline.plugins.PluginRunnerIT;
import net.sourceforge.seqware.pipeline.runner.PluginRunner;
import org.apache.commons.io.FileUtils;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Build and install a bundle, used by both the User tutorial and the Developer tutorial
 * @author dyuen
 */
public class DeveloperPhase1 {

    public static final String WORKFLOW = "Workflow";
    public static File BundleDir = null;
    public static File JavaClient = null;

    @BeforeClass
    public static void testListAvailableWorkflowsAndTheirParameters() throws IOException {
        PluginRunnerIT pit = new PluginRunnerIT();
        PluginRunnerIT.clearStaticVariables();
        PluginRunner it = new PluginRunner();
        String SEQWARE_VERSION = it.getClass().getPackage().getImplementationVersion();
        Assert.assertTrue("unable to detect seqware version", SEQWARE_VERSION != null);
        Log.info("SeqWare version detected as: " + SEQWARE_VERSION);

        // for all tests, we're going to need to create and install our basic archetypes
        String[] archetypes = { "java-workflow" };
        PluginRunnerIT.buildAndInstallArchetypes(archetypes, SEQWARE_VERSION);

        //list workflows and ensure that the workflow is installed
        List<Integer> accessions = new ArrayList<Integer>();
        accessions.addAll(PluginRunnerIT.getInstalledWorkflows().values());
        Assert.assertTrue("one accession expected", accessions.size() == 1);
        AccessionMap.accessionMap.put(WORKFLOW, accessions.get(0).toString());

        // ensure that a couple of the files we talk about in the tutorial exist
        File bundleDir = PluginRunnerIT.getBundleLocations().get("seqware-archetype-java-workflow").getParentFile()
                .getParentFile();
        Log.info("Looking for files in the bundle dir at " + bundleDir.getAbsolutePath());
        File pomXML = new File(bundleDir, "pom.xml");
        Assert.assertTrue("pom.xml does not exist", pomXML.exists());
        File properties = new File(bundleDir, "workflow.properties");
        Assert.assertTrue("workflow.properties does not exist", properties.exists());
        File metadata = new File(bundleDir.getAbsolutePath() + File.separatorChar + "workflow", "metadata.xml");
        Assert.assertTrue("metadata.xml does not exist at " + metadata.getAbsolutePath(), metadata.exists());
        File workflowClientJava = new File(bundleDir.getAbsolutePath() + File.separatorChar + "src"
                + File.separatorChar + "main" + File.separatorChar + "java" + File.separatorChar + "com"
                + File.separatorChar + "seqware" + File.separatorChar + "github" + File.separatorChar,
                "WorkflowClient.java");
        Assert.assertTrue("java client does not exist at " + workflowClientJava.getAbsolutePath(),
                workflowClientJava.exists());

        // allocate needed items for future tests
        BundleDir = bundleDir;
        JavaClient = workflowClientJava;

    }

    @Test
    public void testModifyingTheWorkflow() throws IOException {
        Log.info("Editing java client at " + JavaClient.getAbsolutePath());
        List<String> readLines = FileUtils.readLines(JavaClient);
        // edit lines to match tutorial changes
        boolean linesAdded = false;
        for (int i = 0; i < readLines.size(); i++) {
            if (readLines.get(i).contains("job11.addParent(job00)")) {
                readLines.add(i + 1,
                        "\nJob job12 = this.getWorkflow().createBashJob(\"bash_date\");\njob12.setCommand(\"date > dir1/time\");\njob12.addParent(job11);");
                linesAdded = true;
            }
        }
        Assert.assertTrue("lines were not added", linesAdded);
        // write back modified lines
        FileUtils.writeLines(JavaClient, readLines, false);
        //build and install modified bundle
        File buildDir = BundleDir;
        Log.info("build dir detected as " + buildDir.getAbsolutePath());
        String command = "mvn install";
        String genOutput = ITUtility.runArbitraryCommand(command, 0, buildDir);
    }
}