Next version of JABA
[jabaws.git] / testsrc / compbio / engine / conf / RunnerConfigMarshallerTester.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.engine.conf;\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.ByteArrayOutputStream;\r
28 import java.io.File;\r
29 import java.io.FileInputStream;\r
30 import java.io.FileNotFoundException;\r
31 import java.io.FileOutputStream;\r
32 import java.io.IOException;\r
33 import java.net.MalformedURLException;\r
34 import java.net.URL;\r
35 import java.util.ArrayList;\r
36 import java.util.List;\r
37 \r
38 import javax.xml.bind.JAXBException;\r
39 import javax.xml.validation.Schema;\r
40 import javax.xml.validation.Validator;\r
41 \r
42 import org.testng.annotations.BeforeMethod;\r
43 import org.testng.annotations.Test;\r
44 import org.xml.sax.SAXException;\r
45 \r
46 import compbio.metadata.AllTestSuit;\r
47 import compbio.metadata.Option;\r
48 import compbio.metadata.Parameter;\r
49 import compbio.metadata.RunnerConfig;\r
50 import compbio.metadata.ValueConstrain;\r
51 import compbio.metadata.WrongParameterException;\r
52 import compbio.runner.msa.Mafft;\r
53 \r
54 public class RunnerConfigMarshallerTester {\r
55 \r
56     public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
57             + "MafftParameters.xml";\r
58     public static String test_schema_output = "RunnerConfigSchema.xml";\r
59     public static String test_output = AllTestSuit.OUTPUT_DIR_ABSOLUTE\r
60             + "MafftParameters.out.xml";\r
61 \r
62     public static String invalidDoc = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
63             + "InvalidMafftParameters.xml";\r
64 \r
65     RunnerConfig<Mafft> rconfig = null;\r
66     Parameter<Mafft> matrixParam = null;\r
67     RunnerConfigMarshaller<Mafft> pmarshaller = null;\r
68 \r
69     @BeforeMethod\r
70     public void setup() {\r
71         // write some parameters programmatically\r
72         try {\r
73             rconfig = new RunnerConfig<Mafft>();\r
74             rconfig.setRunnerClassName(Mafft.class.getName());\r
75             List<Option<Mafft>> prms = new ArrayList<Option<Mafft>>();\r
76 \r
77             Parameter<Mafft> p1 = new Parameter<Mafft>("Type",\r
78                     "Type of the sequence (PROTEIN or DNA)");\r
79             // TODO publish help on a compbio web site\r
80 \r
81             p1.setFurtherDetails(new URL("http",\r
82                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
83                     "Index.html"));\r
84             p1.addPossibleValues("PROTEIN", "DNA");\r
85             p1.setOptionName("-TYPE");\r
86             p1.setRequired(false);\r
87 \r
88             /*\r
89              * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or\r
90              * filename\r
91              */\r
92             Option<Mafft> p2 = new Option<Mafft>("MATRIX",\r
93                     "Protein weight matrix");\r
94             // TODO publish help on a compbio web site\r
95 \r
96             p2.setFurtherDetails(new URL("http",\r
97                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
98                     "Index.html"));\r
99 \r
100             p2.addOptionNames("-jtree"); // "-retree"\r
101             p2.setRequired(false);\r
102 \r
103             Parameter<Mafft> p3 = new Parameter<Mafft>("Matrix1",\r
104                     "Protein weight matrix");\r
105             // TODO publish help on a compbio web site\r
106             p3.setFurtherDetails(new URL("http",\r
107                     "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",\r
108                     "Index.html"));\r
109 \r
110             p3.addPossibleValues("BLOSUM", "PAM", "GONNET", "ID");\r
111             // This attribute is required by strict schema\r
112             p3.setOptionName("--AAMATRIX");\r
113             p3.setRequired(true);\r
114             p3.setDefaultValue("pam");\r
115             ValueConstrain vc = new ValueConstrain();\r
116             vc.setType(ValueConstrain.Type.Float);\r
117             vc.setMin("-10.12");\r
118             vc.setMax("0");\r
119             p3.setValidValue(vc);\r
120 \r
121             prms.add(p1);\r
122             prms.add(p2);\r
123             prms.add(p3);\r
124             matrixParam = p3;\r
125             rconfig.setOptions(prms);\r
126 \r
127             pmarshaller = new RunnerConfigMarshaller<Mafft>(RunnerConfig.class,\r
128                     Parameter.class, Option.class, ValueConstrain.class);\r
129         } catch (MalformedURLException e) {\r
130             e.printStackTrace();\r
131             fail(e.getLocalizedMessage());\r
132         } catch (JAXBException e) {\r
133             e.printStackTrace();\r
134             fail(e.getLocalizedMessage());\r
135         } catch (WrongParameterException e) {\r
136             e.printStackTrace();\r
137             fail(e.getLocalizedMessage());\r
138         }\r
139 \r
140     }\r
141 \r
142     @Test()\r
143     public void testMarshalling() {\r
144 \r
145         File outfile = new File(this.test_output);\r
146         try {\r
147             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
148         } catch (FileNotFoundException e) {\r
149             e.printStackTrace();\r
150             fail(e.getLocalizedMessage());\r
151         } catch (JAXBException e) {\r
152             e.printStackTrace();\r
153             fail(e.getLocalizedMessage());\r
154         } catch (IOException e) {\r
155             e.printStackTrace();\r
156             fail(e.getLocalizedMessage());\r
157         }\r
158         assertTrue("Output file expected, but nothing found!", outfile.exists());\r
159         outfile.delete();\r
160     }\r
161 \r
162     @Test()\r
163     public void testUnMarshalling() {\r
164 \r
165         File outfile = new File(this.test_output);\r
166         try {\r
167             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
168 \r
169             RunnerConfig<?> rconfig = pmarshaller.read(new FileInputStream(\r
170                     outfile), RunnerConfig.class, Parameter.class,\r
171                     Option.class, ValueConstrain.class);\r
172             assertNotNull(rconfig);\r
173             assertEquals(rconfig.getParameters().size(), this.rconfig\r
174                     .getParameters().size());\r
175             assertEquals(rconfig.getRunnerClassName(), this.rconfig\r
176                     .getRunnerClassName());\r
177             assertTrue(matrixParam.equals(rconfig.getArgument("MATRIX1")));\r
178             assertFalse(matrixParam.equals(rconfig.getArgument("Type")));\r
179         } catch (FileNotFoundException e) {\r
180             e.printStackTrace();\r
181             fail(e.getLocalizedMessage());\r
182         } catch (JAXBException e) {\r
183             e.printStackTrace();\r
184             fail(e.getLocalizedMessage());\r
185         } catch (IOException e) {\r
186             e.printStackTrace();\r
187             fail(e.getLocalizedMessage());\r
188         }\r
189         // outfile.delete();\r
190     }\r
191 \r
192     @Test()\r
193     public void testValidation() {\r
194         try {\r
195             System.out.println("CCCC " + rconfig);\r
196             // write schema\r
197             pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
198                     test_schema_output);\r
199 \r
200             File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
201                     test_schema_output);\r
202             assertTrue(schemafile.exists());\r
203             // document is NOT valid even against a loose schema as elements in\r
204             // java are annotated as required\r
205             Validator looseValidator = RunnerConfigMarshaller\r
206                     .getValidator(schemafile.getAbsolutePath());\r
207 \r
208             // write output xml file\r
209             File outfile = new File(this.test_output);\r
210             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
211 \r
212             assertTrue("Invalid output is NOT expected", RunnerConfigMarshaller\r
213                     .validate(looseValidator, test_output));\r
214 \r
215             Schema strictSchema = RunnerConfigMarshaller\r
216                     .getSchema(AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
217                             + File.separator + "RunnerConfigSchema.xsd");\r
218 \r
219             Validator strictVal = RunnerConfigMarshaller\r
220                     .getValidator(strictSchema);\r
221 \r
222             // document is invalid against strict schema\r
223             assertFalse("Invalid output is expected", RunnerConfigMarshaller\r
224                     .validate(strictVal, invalidDoc));\r
225 \r
226             // schemafile.delete();\r
227             // outfile.delete();\r
228 \r
229         } catch (MalformedURLException e) {\r
230             e.printStackTrace();\r
231             fail(e.getLocalizedMessage());\r
232         } catch (JAXBException e) {\r
233             e.printStackTrace();\r
234             fail(e.getLocalizedMessage());\r
235         } catch (IOException e) {\r
236             e.printStackTrace();\r
237             fail(e.getLocalizedMessage());\r
238         } catch (SAXException e) {\r
239             e.printStackTrace();\r
240             fail(e.getMessage());\r
241         }\r
242 \r
243     }\r
244 \r
245     @Test(expectedExceptions = JAXBException.class)\r
246     public void testValidationOnMarshalling() throws SAXException,\r
247             JAXBException {\r
248         // This is not valid parameter\r
249         Parameter<Mafft> p = new Parameter<Mafft>("MATRIXXX",\r
250                 "Protein weight matrix");\r
251         // This attribute is required by strict schema\r
252         // p.setOptionName("-M");\r
253         p.setRequired(true);\r
254         rconfig.addParameter(p);\r
255         try {\r
256 \r
257             // strict schema invalidate this document and throw an exception\r
258             // just discard the output\r
259             pmarshaller.writeAndValidate(rconfig,\r
260                     AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator\r
261                             + "RunnerConfigSchema.xsd",\r
262                     new ByteArrayOutputStream());\r
263 \r
264             fail("Exception has been thrown before this place in unreachable");\r
265 \r
266         } catch (MalformedURLException e) {\r
267             e.printStackTrace();\r
268             fail(e.getLocalizedMessage());\r
269         } catch (IOException e) {\r
270             e.printStackTrace();\r
271             fail(e.getLocalizedMessage());\r
272         }\r
273     }\r
274 \r
275     @Test()\r
276     public void testSchemaFromCodeGeneration() {\r
277         try {\r
278 \r
279             pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
280                     test_schema_output);\r
281         } catch (JAXBException e) {\r
282             e.printStackTrace();\r
283             fail(e.getLocalizedMessage());\r
284         } catch (IOException e) {\r
285             e.printStackTrace();\r
286             fail(e.getLocalizedMessage());\r
287         }\r
288         File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
289                 test_schema_output);\r
290         assertTrue("Schema file expected but not found", schemafile.exists());\r
291         assertTrue("Schema file seems to be empty", schemafile.length() > 50);\r
292         // schemafile.delete();\r
293     }\r
294 \r
295 }\r