/*
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

package org.broad.igv.dev.plugin;

import org.broad.igv.AbstractHeadlessTest;
import org.broad.igv.Globals;
import org.broad.igv.track.FeatureTrack;
import org.broad.igv.track.TrackLoader;
import org.broad.igv.util.ResourceLocator;
import org.broad.igv.util.TestUtils;
import org.broad.tribble.Feature;
import org.junit.Assume;
import org.junit.Test;
import org.w3c.dom.Element;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * User: jacob
 * Date: 2012-Aug-10
 */
public class PluginFeatureSourceTest extends AbstractHeadlessTest {


    @Test
    public void testGetFeaturesCat() throws Exception {

        Assume.assumeTrue(!Globals.IS_WINDOWS);

        PluginSpecReader reader = AbstractPluginTest.getCatReader();

        Element tool = reader.getTools().get(0);
        Element command = reader.getCommands(tool).get(0);
        List<Argument> argumentList = reader.getArguments(tool, command);


        LinkedHashMap<Argument, Object> arguments = new LinkedHashMap<Argument, Object>(argumentList.size());

        int argnum = 0;
        arguments.put(argumentList.get(argnum++), "");

        TrackLoader loader = new TrackLoader();
        String[] paths = new String[]{TestUtils.DATA_DIR + "bed/test.bed", TestUtils.DATA_DIR + "bed/testAlternateColor.bed"};
        for (String path : paths) {
            TestUtils.createIndex(path);
            FeatureTrack track = (FeatureTrack) loader.load(new ResourceLocator(path), genome).get(0);
            arguments.put(argumentList.get(argnum++), track);
        }


        List<String> cmd = Arrays.asList(reader.getToolPath(tool), command.getAttribute("cmd_arg"));
        PluginFeatureSource pluginSource = new PluginFeatureSource(cmd, arguments, reader.getParsingAttributes(tool, command), reader.getSpecPath());

        Iterator<Feature> featuresExp = ((FeatureTrack) arguments.get(argumentList.get(1))).getFeatures("chr2", 1, 30).iterator();
        Iterator<Feature> featuresAct = pluginSource.getFeatures("chr2", 1, 30);

        TestUtils.assertFeatureListsEqual(featuresExp, featuresAct);

        int start = 178707289 - 1;
        int end = 179714478;
        featuresExp = ((FeatureTrack) arguments.get(argumentList.get(2))).getFeatures("chr2", start, end).iterator();
        featuresAct = pluginSource.getFeatures("chr2", start, end);

        TestUtils.assertFeatureListsEqual(featuresExp, featuresAct);

    }
}

@@ -770,24 +770,64 @@
 rceLocator;%0A
+import org.broad.igv.util.RuntimeUtils;%0A
 import org.b
@@ -962,16 +962,109 @@
 ement;%0A%0A
+import java.io.BufferedReader;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader;%0A
 import j
@@ -1163,16 +1163,105 @@
 .List;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0A
 /**%0A * U
@@ -1366,16 +1366,1125 @@
 est %7B%0A%0A%0A
+    @Test%0A    public void testSimpleRuntimeIO() throws Exception%7B%0A        String%5B%5D fullCmd = null;%0A        String expLine = null;%0A        int expLines = -1;%0A        if(Globals.IS_MAC %7C%7C Globals.IS_LINUX)%7B%0A            fullCmd = new String%5B%5D%7B%22/bin/bash%22, %22-c%22, %22whereis bash%22%7D;%0A            expLine = %22/bin/bash%22;%0A            expLines = 1;%0A        %7Delse if(Globals.IS_WINDOWS)%7B%0A            fullCmd = new String%5B%5D%7B%22cmd%22,%22/C%22,%22ver%22%7D;%0A            expLine = %22Microsoft Windows%22;%0A            expLines = 2;%0A        %7D%0A%0A        Assume.assumeNotNull(fullCmd);%0A        Process process = RuntimeUtils.startExternalProcess(fullCmd, null, null);%0A        InputStream is = process.getInputStream();%0A        BufferedReader bis = new BufferedReader(new InputStreamReader(is));%0A%0A        String line;%0A        boolean foundLine = false;%0A        int count = 0;%0A%0A        process.waitFor();%0A        while((line = bis.readLine()) != null)%7B%0A            System.out.println(line);%0A            count++;%0A            foundLine %7C= line.contains(expLine);%0A        %7D%0A        assertTrue(foundLine);%0A        assertEquals(expLines, count);%0A    %7D%0A%0A%0A
     @Tes

Add test for reading/writing to external processes, should work for windows as well