8daacff569f149ba32afb92fe21ce28ccc431905
[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("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
82             p1.addPossibleValues("PROTEIN", "DNA");\r
83             p1.setOptionName("-TYPE");\r
84             p1.setRequired(false);\r
85 \r
86             /*\r
87              * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or\r
88              * filename\r
89              */\r
90             Option<Mafft> p2 = new Option<Mafft>("MATRIX",\r
91                     "Protein weight matrix");\r
92             // TODO publish help on a compbio web site\r
93 \r
94             p2.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
95 \r
96             p2.addOptionNames("-jtree"); // "-retree"\r
97             p2.setRequired(false);\r
98 \r
99             Parameter<Mafft> p3 = new Parameter<Mafft>("Matrix1",\r
100                     "Protein weight matrix");\r
101             // TODO publish help on a compbio web site\r
102             p3.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/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("--AAMATRIX");\r
107             p3.setRequired(true);\r
108             p3.setDefaultValue("pam");\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 = p3;\r
119             rconfig.setOptions(prms);\r
120 \r
121             pmarshaller = new RunnerConfigMarshaller<Mafft>(RunnerConfig.class,\r
122                     Parameter.class, Option.class, ValueConstrain.class);\r
123         } catch (MalformedURLException e) {\r
124             e.printStackTrace();\r
125             fail(e.getLocalizedMessage());\r
126         } catch (JAXBException e) {\r
127             e.printStackTrace();\r
128             fail(e.getLocalizedMessage());\r
129         } catch (WrongParameterException e) {\r
130             e.printStackTrace();\r
131             fail(e.getLocalizedMessage());\r
132         }\r
133 \r
134     }\r
135 \r
136     @Test()\r
137     public void testMarshalling() {\r
138 \r
139         File outfile = new File(this.test_output);\r
140         try {\r
141             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
142         } catch (FileNotFoundException e) {\r
143             e.printStackTrace();\r
144             fail(e.getLocalizedMessage());\r
145         } catch (JAXBException 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         }\r
152         assertTrue("Output file expected, but nothing found!", outfile.exists());\r
153         outfile.delete();\r
154     }\r
155 \r
156     @Test()\r
157     public void testUnMarshalling() {\r
158 \r
159         File outfile = new File(this.test_output);\r
160         try {\r
161             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
162 \r
163             RunnerConfig<?> rconfig = pmarshaller.read(new FileInputStream(\r
164                     outfile), RunnerConfig.class, Parameter.class,\r
165                     Option.class, ValueConstrain.class);\r
166             assertNotNull(rconfig);\r
167             assertEquals(rconfig.getParameters().size(), this.rconfig\r
168                     .getParameters().size());\r
169             assertEquals(rconfig.getRunnerClassName(), this.rconfig\r
170                     .getRunnerClassName());\r
171             assertTrue(matrixParam.equals(rconfig.getArgument("MATRIX1")));\r
172             assertFalse(matrixParam.equals(rconfig.getArgument("Type")));\r
173         } catch (FileNotFoundException e) {\r
174             e.printStackTrace();\r
175             fail(e.getLocalizedMessage());\r
176         } catch (JAXBException e) {\r
177             e.printStackTrace();\r
178             fail(e.getLocalizedMessage());\r
179         } catch (IOException e) {\r
180             e.printStackTrace();\r
181             fail(e.getLocalizedMessage());\r
182         }\r
183         // outfile.delete();\r
184     }\r
185 \r
186     @Test()\r
187     public void testValidation() {\r
188         try {\r
189             System.out.println("CCCC " + rconfig);\r
190             // write schema\r
191             pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
192                     test_schema_output);\r
193 \r
194             File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
195                     test_schema_output);\r
196             assertTrue(schemafile.exists());\r
197             // document is NOT valid even against a loose schema as elements in\r
198             // java are annotated as required\r
199             Validator looseValidator = RunnerConfigMarshaller\r
200                     .getValidator(schemafile.getAbsolutePath());\r
201 \r
202             // write output xml file\r
203             File outfile = new File(this.test_output);\r
204             pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
205 \r
206             assertTrue("Invalid output is NOT expected", RunnerConfigMarshaller\r
207                     .validate(looseValidator, test_output));\r
208 \r
209             Schema strictSchema = RunnerConfigMarshaller\r
210                     .getSchema(AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
211                             + File.separator + "RunnerConfigSchema.xsd");\r
212 \r
213             Validator strictVal = RunnerConfigMarshaller\r
214                     .getValidator(strictSchema);\r
215 \r
216             // document is invalid against strict schema\r
217             assertFalse("Invalid output is expected", RunnerConfigMarshaller\r
218                     .validate(strictVal, invalidDoc));\r
219 \r
220             // schemafile.delete();\r
221             // outfile.delete();\r
222 \r
223         } catch (MalformedURLException e) {\r
224             e.printStackTrace();\r
225             fail(e.getLocalizedMessage());\r
226         } catch (JAXBException e) {\r
227             e.printStackTrace();\r
228             fail(e.getLocalizedMessage());\r
229         } catch (IOException e) {\r
230             e.printStackTrace();\r
231             fail(e.getLocalizedMessage());\r
232         } catch (SAXException e) {\r
233             e.printStackTrace();\r
234             fail(e.getMessage());\r
235         }\r
236 \r
237     }\r
238 \r
239     @Test(expectedExceptions = JAXBException.class)\r
240     public void testValidationOnMarshalling() throws SAXException,\r
241             JAXBException, MalformedURLException {\r
242         // This is not valid parameter\r
243         Parameter<Mafft> p = new Parameter<Mafft>("MATRIXXX", "Protein weight matrix");\r
244         // This attribute is required by strict schema\r
245         // p.setOptionName("-M");\r
246         p.setRequired(true);\r
247         rconfig.addParameter(p);\r
248         try {\r
249 \r
250             // strict schema invalidate this document and throw an exception\r
251             // just discard the output\r
252             pmarshaller.writeAndValidate(rconfig,\r
253                     AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator\r
254                             + "RunnerConfigSchema.xsd",\r
255                     new ByteArrayOutputStream());\r
256 \r
257             fail("Exception has been thrown before this place in unreachable");\r
258 \r
259         } catch (MalformedURLException e) {\r
260             e.printStackTrace();\r
261             fail(e.getLocalizedMessage());\r
262         } catch (IOException e) {\r
263             e.printStackTrace();\r
264             fail(e.getLocalizedMessage());\r
265         }\r
266     }\r
267 \r
268     @Test()\r
269     public void testSchemaFromCodeGeneration() {\r
270         try {\r
271 \r
272             pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
273                     test_schema_output);\r
274         } catch (JAXBException e) {\r
275             e.printStackTrace();\r
276             fail(e.getLocalizedMessage());\r
277         } catch (IOException e) {\r
278             e.printStackTrace();\r
279             fail(e.getLocalizedMessage());\r
280         }\r
281         File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE,\r
282                 test_schema_output);\r
283         assertTrue("Schema file expected but not found", schemafile.exists());\r
284         assertTrue("Schema file seems to be empty", schemafile.length() > 50);\r
285         // schemafile.delete();\r
286     }\r
287 \r
288 }\r