Add testing dirs
[proteocache.git] / testsrc / compbio / metadata / PresetTester.java
diff --git a/testsrc/compbio/metadata/PresetTester.java b/testsrc/compbio/metadata/PresetTester.java
new file mode 100644 (file)
index 0000000..0b5a0a3
--- /dev/null
@@ -0,0 +1,194 @@
+/* Copyright (c) 2009 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.metadata;\r
+\r
+import static org.testng.Assert.assertEquals;\r
+import static org.testng.Assert.assertFalse;\r
+import static org.testng.Assert.assertNotNull;\r
+import static org.testng.Assert.assertTrue;\r
+import static org.testng.Assert.fail;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.JAXBContext;\r
+import javax.xml.bind.JAXBElement;\r
+import javax.xml.bind.JAXBException;\r
+import javax.xml.bind.Unmarshaller;\r
+import javax.xml.transform.stream.StreamSource;\r
+\r
+import org.testng.annotations.BeforeTest;\r
+import org.testng.annotations.Test;\r
+import org.xml.sax.SAXException;\r
+\r
+import compbio.engine.conf.RunnerConfigMarshaller;\r
+import compbio.runner.msa.Mafft;\r
+\r
+public class PresetTester {\r
+\r
+    public static final String input = AllTestSuit.TEST_DATA_PATH\r
+           + "MafftPresets.xml";\r
+    public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
+           + "MafftParameters.xml";\r
+\r
+    PresetManager<Mafft> presets = null;\r
+    RunnerConfig<Mafft> rconfParams = null;\r
+\r
+    @BeforeTest(enabled = true)\r
+    public void loadPresets() {\r
+       try {\r
+           // Load Preset definitions\r
+           RunnerConfigMarshaller<Mafft> rconfigPresets = new RunnerConfigMarshaller<Mafft>(\r
+                   PresetManager.class);\r
+           File infile = new File(input);\r
+           assertTrue(infile.exists());\r
+           presets = rconfigPresets.read(new FileInputStream(infile),\r
+                   PresetManager.class);\r
+           assertNotNull(presets);\r
+           assertFalse(presets.preset.isEmpty());\r
+\r
+           // Load Parameters definitions\r
+           File input = new File(this.test_input);\r
+           assertTrue(input.exists());\r
+           JAXBContext ctx = JAXBContext.newInstance(RunnerConfig.class);\r
+           Unmarshaller um = ctx.createUnmarshaller();\r
+           JAXBElement<RunnerConfig> rconfigParams = um.unmarshal(\r
+                   new StreamSource(input), RunnerConfig.class);\r
+           rconfParams = rconfigParams.getValue();\r
+           assertNotNull(rconfParams);\r
+\r
+       } catch (JAXBException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       } catch (FileNotFoundException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       }\r
+    }\r
+\r
+    @Test\r
+    public void marshallPreset() {\r
+       try {\r
+           RunnerConfigMarshaller<Mafft> rconfig = new RunnerConfigMarshaller<Mafft>(\r
+                   PresetManager.class);\r
+           PresetManager<Mafft> pman = getPresets();\r
+           assertNotNull(pman);\r
+           rconfig.readAndValidate(new FileInputStream(new File(input)),\r
+                   PresetManager.class);\r
+       } catch (JAXBException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       } catch (IOException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       } catch (SAXException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       }\r
+    }\r
+\r
+    @Test\r
+    public void validatePresets() {\r
+       assertNotNull(presets);\r
+       assertEquals(presets.getPresets().size(), 6);\r
+       try {\r
+           for (Preset<Mafft> pr : presets.getPresets()) {\r
+               List<Option<Mafft>> options;\r
+               options = pr.getArguments(rconfParams);\r
+               assertFalse(options.isEmpty());\r
+               if (pr.name.equals("L-INS-i (Accuracy-oriented)")) {\r
+                   assertEquals(options.size(), 2);\r
+                   Option<Mafft> o = options.get(0);\r
+                   if (o.name.equals("Pairwise alignment computation method")) {\r
+                       List<String> onames = o.getOptionNames();\r
+                       boolean match = false;\r
+                       for (String oname : onames) {\r
+                           if (oname.equals("--localpair")) {\r
+                               match = true;\r
+                               break;\r
+                           }\r
+                       }\r
+                       assertTrue(match);\r
+                   }\r
+               }\r
+               if (pr.name.equals("NW-NS-PartTree-1 (Speed oriented)")) {\r
+                   assertEquals(options.size(), 4);\r
+               }\r
+           }\r
+       } catch (WrongParameterException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       }\r
+    }\r
+\r
+    @Test\r
+    public void testPresetWithMuptiOptions() {\r
+       assertNotNull(presets);\r
+       assertEquals(presets.getPresets().size(), 6);\r
+       try {\r
+           for (Preset<Mafft> pr : presets.getPresets()) {\r
+               List<Option<Mafft>> options;\r
+               options = pr.getArguments(rconfParams);\r
+               assertFalse(options.isEmpty());\r
+               if (pr.name.equals("E-INS-i (Accuracy-oriented)")) {\r
+                   assertEquals(options.size(), 3);\r
+                   Option<Mafft> o = options.get(0);\r
+                   if (o.name.equals("Pairwise alignment computation method")) {\r
+                       List<String> onames = o.getOptionNames();\r
+                       boolean match = false;\r
+                       for (String oname : onames) {\r
+                           if (oname.equals("--genafpair")) {\r
+                               match = true;\r
+                               break;\r
+                           }\r
+                       }\r
+                       assertTrue(match);\r
+                   }\r
+               }\r
+               if (pr.name.equals("NW-NS-PartTree-1 (Speed oriented)")) {\r
+                   assertEquals(options.size(), 4);\r
+               }\r
+           }\r
+       } catch (WrongParameterException e) {\r
+           e.printStackTrace();\r
+           fail(e.getMessage());\r
+       }\r
+    }\r
+\r
+    private static PresetManager<Mafft> getPresets() {\r
+       Preset<Mafft> preset1 = new Preset<Mafft>();\r
+       preset1.name = "L-INS-i (Accuracy-oriented)";\r
+       preset1.description = "dsfjkg fdjksghkjsgdfh jksdfg sdfgkjhsdfgk kjsdfg ";\r
+\r
+       List<String> optionNames = new ArrayList<String>();\r
+       optionNames.add("--localpair");\r
+       optionNames.add("--maxiterate 1000");\r
+       preset1.option = optionNames;\r
+       PresetManager<Mafft> prman = new PresetManager<Mafft>();\r
+       prman.preset = Collections.singletonList(preset1);\r
+       prman.runnerClassName = Mafft.class.getCanonicalName();\r
+\r
+       return prman;\r
+    }\r
+}\r