900d6d773e8c39ce876072164bb7813af1ca4d24
[jabaws.git] / testsrc / compbio / metadata / OptionMarshallerTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.metadata;\r
20 \r
21 import static org.testng.AssertJUnit.assertEquals;\r
22 import static org.testng.AssertJUnit.assertFalse;\r
23 import static org.testng.AssertJUnit.assertNotNull;\r
24 import static org.testng.AssertJUnit.assertTrue;\r
25 import static org.testng.AssertJUnit.fail;\r
26 \r
27 import java.io.File;\r
28 import java.io.FileInputStream;\r
29 import java.io.FileNotFoundException;\r
30 import java.io.FileOutputStream;\r
31 import java.io.IOException;\r
32 import java.net.MalformedURLException;\r
33 import java.net.URL;\r
34 import java.util.ArrayList;\r
35 import java.util.List;\r
36 \r
37 import javax.xml.bind.JAXBContext;\r
38 import javax.xml.bind.JAXBElement;\r
39 import javax.xml.bind.JAXBException;\r
40 import javax.xml.bind.Unmarshaller;\r
41 import javax.xml.transform.stream.StreamSource;\r
42 \r
43 import org.testng.annotations.BeforeMethod;\r
44 import org.testng.annotations.Test;\r
45 import org.xml.sax.SAXException;\r
46 \r
47 import compbio.engine.conf.RunnerConfigMarshaller;\r
48 import compbio.runner.msa.Mafft;\r
49 \r
50 public class OptionMarshallerTester {\r
51 \r
52     public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
53             + "MafftParameters.xml";\r
54     public static String test_schema_output = "NextGenMafftOptionsSchema.xml";\r
55     public static String test_output = "MafftOptions.xml.out";\r
56     public static String reWrittenInput = AllTestSuit.OUTPUT_DIR_ABSOLUTE\r
57             + "rewrittenMafftParams.xml";\r
58 \r
59     RunnerConfig<Mafft> rconfig = null;\r
60     Option<Mafft> matrixParam = null;\r
61 \r
62     @BeforeMethod()\r
63     public void setup() {\r
64         // write some parameters programmatically\r
65         try {\r
66             rconfig = new RunnerConfig<Mafft>();\r
67             rconfig.setRunnerClassName(Mafft.class.getName());\r
68             List<Option<Mafft>> prms = new ArrayList<Option<Mafft>>();\r
69 \r
70             Parameter<Mafft> p1 = new Parameter<Mafft>("Type",\r
71                     "Type of the sequence (PROTEIN or DNA)");\r
72             // TODO publish help on a compbio web site\r
73 \r
74             p1.setFurtherDetails(new URL("http",\r
75                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
76                     "Index.html"));\r
77             p1.addPossibleValues("PROTEIN", "DNA");\r
78             p1.setOptionName("-TYPE");\r
79             p1.setRequired(false);\r
80 \r
81             /*\r
82              * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or\r
83              * filename\r
84              */\r
85             Option<Mafft> p2 = new Option<Mafft>("MATRIX",\r
86                     "Protein weight matrix");\r
87             // TODO publish help on a compbio web site\r
88 \r
89             p2.setFurtherDetails(new URL("http",\r
90                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
91                     "Index.html"));\r
92 \r
93             p2.addOptionNames("-jtree");\r
94             p2.addOptionNames("-jfasta");\r
95             p2.setRequired(false);\r
96 \r
97             Parameter<Mafft> p3 = new Parameter<Mafft>("MATRIX2",\r
98                     "Protein weight matrix");\r
99             // TODO publish help on a compbio web site\r
100             p3.setFurtherDetails(new URL("http",\r
101                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
102                     "Index.html"));\r
103 \r
104             p3.addPossibleValues("BLOSUM", "PAM", "GONNET", "ID");\r
105             // This attribute is required by strict schema\r
106             p3.setOptionName("-MATRIX");\r
107             p3.setRequired(true);\r
108             p3.setDefaultValue("id");\r
109             ValueConstrain vc = new ValueConstrain();\r
110             vc.setType(ValueConstrain.Type.Float);\r
111             vc.setMin("-10.12");\r
112             vc.setMax("0");\r
113             p3.setValidValue(vc);\r
114 \r
115             prms.add(p1);\r
116             prms.add(p2);\r
117             prms.add(p3);\r
118             matrixParam = p2;\r
119             rconfig.setOptions(prms);\r
120 \r
121         } catch (MalformedURLException e) {\r
122             e.printStackTrace();\r
123             fail(e.getLocalizedMessage());\r
124         } catch (WrongParameterException e) {\r
125             e.printStackTrace();\r
126             fail(e.getLocalizedMessage());\r
127         }\r
128     }\r
129 \r
130     @Test(expectedExceptions = { javax.xml.bind.MarshalException.class })\r
131     public void testMarshalling() throws JAXBException {\r
132 \r
133         File outfile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_output);\r
134         try {\r
135             RunnerConfigMarshaller<Mafft> rmarsh = new RunnerConfigMarshaller<Mafft>(\r
136                     RunnerConfig.class);\r
137 \r
138             // This throws an exception\r
139             // I am not sure why\r
140             rmarsh.writeAndValidate(rconfig,\r
141                     AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator\r
142                             + "RunnerConfigSchema.xsd", new FileOutputStream(\r
143                             outfile));\r
144 \r
145         } catch (FileNotFoundException e) {\r
146             e.printStackTrace();\r
147             fail(e.getLocalizedMessage());\r
148         } catch (IOException e) {\r
149             e.printStackTrace();\r
150             fail(e.getLocalizedMessage());\r
151         } catch (SAXException e) {\r
152             e.printStackTrace();\r
153             fail(e.getLocalizedMessage());\r
154         }\r
155         assertTrue("Output file expected, but nothing found!", outfile.exists());\r
156         // outfile.delete();\r
157     }\r
158 \r
159     @Test()\r
160     public void testUnMarshalling() {\r
161         try {\r
162             File input = new File(this.test_input);\r
163             assertTrue(input.exists());\r
164             JAXBContext ctx = JAXBContext.newInstance(RunnerConfig.class);\r
165             Unmarshaller um = ctx.createUnmarshaller();\r
166             JAXBElement<RunnerConfig> rconfig = um.unmarshal(new StreamSource(\r
167                     input), RunnerConfig.class);\r
168             RunnerConfig<Mafft> runner = rconfig.getValue();\r
169             assertNotNull(runner);\r
170             System.out.println(runner);\r
171             assertFalse(runner.options.isEmpty());\r
172             assertFalse(runner.parameters.isEmpty());\r
173             assertEquals(7, runner.options.size());\r
174             assertEquals(8, runner.parameters.size());\r
175             Option<Mafft> stypeOption = runner.getArgument("Sequence type");\r
176             System.out.println(stypeOption);\r
177             assertNotNull(stypeOption);\r
178             assertFalse(stypeOption.isRequired);\r
179             assertEquals("--auto", stypeOption.defaultValue);\r
180             assertEquals(2, stypeOption.optionNames.size());\r
181 \r
182             assertEquals(" ", runner.getPrmSeparator());\r
183             Option<Mafft> guidetrOption = runner\r
184                     .getArgument("Guide tree rebuild");\r
185             Parameter<Mafft> guidetr = (Parameter<Mafft>) guidetrOption;\r
186             ValueConstrain constraint = guidetr.getValidValue();\r
187             assertEquals("Integer", constraint.type.toString());\r
188             assertEquals(1, constraint.getMin());\r
189             assertEquals(100, constraint.getMax());\r
190 \r
191             RunnerConfigMarshaller<Mafft> rmarsh = new RunnerConfigMarshaller<Mafft>(\r
192                     RunnerConfig.class);\r
193             // Now see if we can write a valid document back discard the actual\r
194             // output only validation is important here\r
195             rmarsh.write(rconfig,\r
196                     new FileOutputStream(new File(reWrittenInput)));\r
197 \r
198             RunnerConfig<Mafft> rc = rmarsh.readAndValidate(\r
199                     new FileInputStream(new File(reWrittenInput)),\r
200                     RunnerConfig.class);\r
201             assertEquals(runner, rc);\r
202 \r
203         } catch (JAXBException e) {\r
204             e.printStackTrace();\r
205             fail(e.getLocalizedMessage());\r
206         } catch (IOException e) {\r
207             e.printStackTrace();\r
208             fail(e.getLocalizedMessage());\r
209         } catch (SAXException e) {\r
210             e.printStackTrace();\r
211             fail(e.getLocalizedMessage());\r
212         }\r
213     }\r
214 }\r