b681729820a4dec2f517a9c1df2d6818ba1f8838
[proteocache.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 + "MafftParameters.xml";\r
53         public static String test_schema_output = "NextGenMafftOptionsSchema.xml";\r
54         public static String test_output = "MafftOptions.xml.out";\r
55         public static String reWrittenInput = AllTestSuit.OUTPUT_DIR_ABSOLUTE + "rewrittenMafftParams.xml";\r
56 \r
57         RunnerConfig<Mafft> rconfig = null;\r
58         Option<Mafft> matrixParam = null;\r
59 \r
60         @BeforeMethod()\r
61         public void setup() {\r
62                 // write some parameters programmatically\r
63                 try {\r
64                         rconfig = new RunnerConfig<Mafft>();\r
65                         rconfig.setRunnerClassName(Mafft.class.getName());\r
66                         List<Option<Mafft>> prms = new ArrayList<Option<Mafft>>();\r
67 \r
68                         Parameter<Mafft> p1 = new Parameter<Mafft>("Type", "Type of the sequence (PROTEIN or DNA)");\r
69                         // TODO publish help on a compbio web site\r
70 \r
71                         p1.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
72                         p1.addPossibleValues("PROTEIN", "DNA");\r
73                         p1.setOptionName("-TYPE");\r
74                         p1.setRequired(false);\r
75 \r
76                         /*\r
77                          * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or\r
78                          * filename\r
79                          */\r
80                         Option<Mafft> p2 = new Option<Mafft>("MATRIX", "Protein weight matrix");\r
81                         // TODO publish help on a compbio web site\r
82 \r
83                         p2.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
84 \r
85                         p2.addOptionNames("-jtree");\r
86                         p2.addOptionNames("-jfasta");\r
87                         p2.setRequired(false);\r
88 \r
89                         Parameter<Mafft> p3 = new Parameter<Mafft>("MATRIX2", "Protein weight matrix");\r
90                         // TODO publish help on a compbio web site\r
91                         p3.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
92 \r
93                         p3.addPossibleValues("BLOSUM", "PAM", "GONNET", "ID");\r
94                         // This attribute is required by strict schema\r
95                         p3.setOptionName("-MATRIX");\r
96                         p3.setRequired(true);\r
97                         p3.setDefaultValue("id");\r
98                         ValueConstrain vc = new ValueConstrain();\r
99                         vc.setType(ValueConstrain.Type.Float);\r
100                         vc.setMin("-10.12");\r
101                         vc.setMax("0");\r
102                         p3.setValidValue(vc);\r
103 \r
104                         prms.add(p1);\r
105                         prms.add(p2);\r
106                         prms.add(p3);\r
107                         matrixParam = p2;\r
108                         rconfig.setOptions(prms);\r
109 \r
110                 } catch (WrongParameterException e) {\r
111                         e.printStackTrace();\r
112                         fail(e.getLocalizedMessage());\r
113                 }\r
114         }\r
115 \r
116         @Test(expectedExceptions = { javax.xml.bind.MarshalException.class })\r
117         public void testMarshalling() throws JAXBException {\r
118 \r
119                 File outfile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_output);\r
120                 try {\r
121                         RunnerConfigMarshaller<Mafft> rmarsh = new RunnerConfigMarshaller<Mafft>(RunnerConfig.class);\r
122 \r
123                         // This throws an exception\r
124                         // I am not sure why\r
125                         rmarsh.writeAndValidate(rconfig, AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator + "RunnerConfigSchema.xsd",\r
126                                         new FileOutputStream(outfile));\r
127 \r
128                 } catch (FileNotFoundException e) {\r
129                         e.printStackTrace();\r
130                         fail(e.getLocalizedMessage());\r
131                 } catch (IOException e) {\r
132                         e.printStackTrace();\r
133                         fail(e.getLocalizedMessage());\r
134                 } catch (SAXException e) {\r
135                         e.printStackTrace();\r
136                         fail(e.getLocalizedMessage());\r
137                 }\r
138                 assertTrue("Output file expected, but nothing found!", outfile.exists());\r
139                 // outfile.delete();\r
140         }\r
141 \r
142         @Test()\r
143         public void testUnMarshalling() {\r
144                 try {\r
145                         File input = new File(this.test_input);\r
146                         assertTrue(input.exists());\r
147                         JAXBContext ctx = JAXBContext.newInstance(RunnerConfig.class);\r
148                         Unmarshaller um = ctx.createUnmarshaller();\r
149                         JAXBElement<RunnerConfig> rconfig = um.unmarshal(new StreamSource(input), RunnerConfig.class);\r
150                         RunnerConfig<Mafft> runner = rconfig.getValue();\r
151                         assertNotNull(runner);\r
152                         System.out.println(runner);\r
153                         assertFalse(runner.options.isEmpty());\r
154                         assertFalse(runner.parameters.isEmpty());\r
155                         assertEquals(7, runner.options.size());\r
156                         assertEquals(8, runner.parameters.size());\r
157                         Option<Mafft> stypeOption = runner.getArgument("Sequence type");\r
158                         System.out.println(stypeOption);\r
159                         assertNotNull(stypeOption);\r
160                         assertFalse(stypeOption.isRequired);\r
161                         assertEquals("--auto", stypeOption.defaultValue);\r
162                         assertEquals(2, stypeOption.optionNames.size());\r
163 \r
164                         assertEquals(" ", runner.getPrmSeparator());\r
165                         Option<Mafft> guidetrOption = runner.getArgument("Guide tree rebuild");\r
166                         Parameter<Mafft> guidetr = (Parameter<Mafft>) guidetrOption;\r
167                         ValueConstrain constraint = guidetr.getValidValue();\r
168                         assertEquals("Integer", constraint.type.toString());\r
169                         assertEquals(1, constraint.getMin());\r
170                         assertEquals(100, constraint.getMax());\r
171 \r
172                         RunnerConfigMarshaller<Mafft> rmarsh = new RunnerConfigMarshaller<Mafft>(RunnerConfig.class);\r
173                         // Now see if we can write a valid document back discard the actual\r
174                         // output only validation is important here\r
175                         rmarsh.write(rconfig, new FileOutputStream(new File(reWrittenInput)));\r
176                         RunnerConfig<Mafft> rc = rmarsh.readAndValidate(new FileInputStream(new File(reWrittenInput)), RunnerConfig.class);\r
177                         assertEquals(runner, rc);\r
178 \r
179                 } catch (JAXBException e) {\r
180                         e.printStackTrace();\r
181                         fail(e.getLocalizedMessage());\r
182                 } catch (IOException e) {\r
183                         e.printStackTrace();\r
184                         fail(e.getLocalizedMessage());\r
185                 } catch (SAXException e) {\r
186                         e.printStackTrace();\r
187                         fail(e.getLocalizedMessage());\r
188                 }\r
189         }\r
190 }\r