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