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