Basic Testing Setup, no getResults method yet
[jabaws.git] / testsrc / compbio / runner / structure / RNAalifoldParametersTester.java
diff --git a/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java b/testsrc/compbio/runner/structure/RNAalifoldParametersTester.java
new file mode 100644 (file)
index 0000000..9c15537
--- /dev/null
@@ -0,0 +1,202 @@
+package compbio.runner.structure;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.ValidationException;
+
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import compbio.engine.Configurator;
+import compbio.engine.client.ConfiguredExecutable;
+import compbio.engine.client.Executable.ExecProvider;
+import compbio.engine.conf.RunnerConfigMarshaller;
+import compbio.engine.local.ExecutableWrapper;
+import compbio.engine.local.LocalRunner;
+import compbio.metadata.AllTestSuit;
+import compbio.metadata.JobExecutionException;
+import compbio.metadata.JobSubmissionException;
+import compbio.metadata.Option;
+import compbio.metadata.Parameter;
+import compbio.metadata.Preset;
+import compbio.metadata.PresetManager;
+import compbio.metadata.ResultNotAvailableException;
+import compbio.metadata.RunnerConfig;
+import compbio.runner.OptionCombinator;
+import compbio.runner.structure.RNAalifold;
+import compbio.util.FileUtil;
+
+
+public class RNAalifoldParametersTester {
+
+       
+       // should be: AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml"
+       static final String rnaalifoldConfigFile = 
+                       AllTestSuit.TEST_DATA_PATH + "RNAalifoldParameters.xml";
+       public static String test_outfile = "rnaalifold.out.txt";
+
+       private static Logger log = Logger
+                       .getLogger(AllTestSuit.RUNNER_TEST_LOGGER);
+       
+       static {
+               log.setLevel(Level.INFO);
+       }
+       
+       RunnerConfig<RNAalifold> rnaalifoldConfig = null;
+       OptionCombinator rnaalifoldOpc = null;
+       
+       @BeforeMethod(groups = { AllTestSuit.test_group_runner })
+       @SuppressWarnings("unchecked")
+       public void setup() {
+               try {
+                       RunnerConfigMarshaller<RNAalifold> rnaalifoldmarsh = 
+                                       new RunnerConfigMarshaller<RNAalifold>(RunnerConfig.class);
+                       rnaalifoldConfig = rnaalifoldmarsh.read(new FileInputStream(new File(
+                                       rnaalifoldConfigFile)), RunnerConfig.class);
+                       rnaalifoldOpc = new OptionCombinator(rnaalifoldConfig);
+                       
+                       log.info("Num options: " + rnaalifoldOpc.getAllOptions().size());
+                       log.info("Num params: " + rnaalifoldOpc.getAllParameters().size());
+                       
+                       
+               } catch (JAXBException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (FileNotFoundException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               }
+       }
+       
+       @Test
+       public void testConfiguration() {
+               try {
+                       this.rnaalifoldConfig.validate();
+               } catch (ValidationException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (IllegalStateException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               }
+       }
+
+       @Test(groups = { AllTestSuit.test_group_runner })
+       public void testDefaultParameters() {
+               RNAalifold rnaalifold = new RNAalifold();
+               rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
+
+               try {
+                       // For local execution use relavive
+                       ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
+                                       .configureExecutable(rnaalifold);
+                       LocalRunner lr = new LocalRunner(confRNAalifold);
+                       lr.executeJob();
+                       confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
+                       assertNotNull(confRNAalifold.getResults());
+               } catch (JobSubmissionException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (JobExecutionException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (ResultNotAvailableException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               }
+       }
+       
+       @Test(groups = { AllTestSuit.test_group_runner })
+       public void testOptions() {
+               test(rnaalifoldOpc.getAllOptions());
+       }
+
+       @Test(groups = { AllTestSuit.test_group_runner })
+       public void testParameters() {
+               List<Parameter<?>> params = rnaalifoldOpc.getAllParameters();
+               Collections.shuffle(params);
+               test(params);
+       }
+
+       @Test(groups = { AllTestSuit.test_group_runner })
+       public void testArguments() {
+               List<Option<?>> options = rnaalifoldOpc.getAllOptions();
+               options.addAll(rnaalifoldOpc.getAllParameters());
+               Collections.shuffle(options);
+               test(options);
+       }
+       
+       void test(Map<Parameter<?>, String> paramValue) {
+               List<Parameter<?>> paramList = new ArrayList<Parameter<?>>(paramValue
+                               .keySet());
+               for (int i = 0; i < paramValue.size(); i++) {
+                       List<String> args = rnaalifoldOpc.parametersToCommandString(paramList, 
+                                       paramValue);
+                       singleTest(args);
+                       Collections.shuffle(paramList);
+               }
+               log.info("NUMBER OF COMBINATION TESTED: " + paramValue.size());
+       }
+       
+       void test(List<? extends Option<?>> params) {
+               for (int i = 0; i < params.size(); i++) {
+                       List<String> args = rnaalifoldOpc.argumentsToCommandString(params);
+                       singleTest(args);
+                       Collections.shuffle(params);
+               }
+               log.info("NUMBER OF COMBINATION TESTED: " + params.size());
+       }
+       
+       void singleTest(List<String> params) {
+               try {
+                       log.info("Using arguments: " + params);
+                       RNAalifold rnaalifold = new RNAalifold();
+                       rnaalifold.setInput(AllTestSuit.test_input_aln).setOutput(test_outfile);
+                       
+                       ConfiguredExecutable<RNAalifold> confRNAalifold = Configurator
+                                       .configureExecutable(rnaalifold, ExecProvider.Local);
+                       // Add options to the executable
+                       confRNAalifold.addParameters(params);
+                       LocalRunner lr = new LocalRunner(confRNAalifold);
+                       lr.executeJob();
+                       confRNAalifold = (ConfiguredExecutable<RNAalifold>) lr.waitForResult();
+                       assertNotNull(confRNAalifold.getResults());
+                       File errors = new File(confRNAalifold.getWorkDirectory(),
+                                       ExecutableWrapper.PROC_ERR_FILE);
+                       if (errors.length() != 0) {
+                               log.error("PROBLEMS:\n " + FileUtil.readFileToString(errors));
+                       }
+                       assertTrue(errors.length() == 0, "Run with arguments : " + params
+                                       + " FAILED!");
+                       Collections.shuffle(params);
+               } catch (JobSubmissionException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (JobExecutionException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (IOException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               } catch (ResultNotAvailableException e) {
+                       e.printStackTrace();
+                       fail(e.getLocalizedMessage());
+               }
+       }
+       
+}