1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\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
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
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
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.conf;
\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
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
38 import javax.xml.bind.JAXBException;
\r
39 import javax.xml.validation.Schema;
\r
40 import javax.xml.validation.Validator;
\r
42 import org.testng.annotations.BeforeMethod;
\r
43 import org.testng.annotations.Test;
\r
44 import org.xml.sax.SAXException;
\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
54 public class RunnerConfigMarshallerTester {
\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
62 public static String invalidDoc = AllTestSuit.TEST_DATA_PATH_ABSOLUTE
\r
63 + "InvalidMafftParameters.xml";
\r
65 RunnerConfig<Mafft> rconfig = null;
\r
66 Parameter<Mafft> matrixParam = null;
\r
67 RunnerConfigMarshaller<Mafft> pmarshaller = null;
\r
70 public void setup() {
\r
71 // write some parameters programmatically
\r
73 rconfig = new RunnerConfig<Mafft>();
\r
74 rconfig.setRunnerClassName(Mafft.class.getName());
\r
75 List<Option<Mafft>> prms = new ArrayList<Option<Mafft>>();
\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
81 p1.setFurtherDetails(new URL("http",
\r
82 "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",
\r
84 p1.addPossibleValues("PROTEIN", "DNA");
\r
85 p1.setOptionName("-TYPE");
\r
86 p1.setRequired(false);
\r
89 * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or
\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
96 p2.setFurtherDetails(new URL("http",
\r
97 "www.compbio.dundee.ac.uk/users/pvtroshin/ws/",
\r
100 p2.addOptionNames("-jtree"); // "-retree"
\r
101 p2.setRequired(false);
\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
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
119 p3.setValidValue(vc);
\r
125 rconfig.setOptions(prms);
\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
143 public void testMarshalling() {
\r
145 File outfile = new File(this.test_output);
\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
158 assertTrue("Output file expected, but nothing found!", outfile.exists());
\r
163 public void testUnMarshalling() {
\r
165 File outfile = new File(this.test_output);
\r
167 pmarshaller.write(rconfig, new FileOutputStream(outfile));
\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
189 // outfile.delete();
\r
193 public void testValidation() {
\r
195 System.out.println("CCCC " + rconfig);
\r
197 pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,
\r
198 test_schema_output);
\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
208 // write output xml file
\r
209 File outfile = new File(this.test_output);
\r
210 pmarshaller.write(rconfig, new FileOutputStream(outfile));
\r
212 assertTrue("Invalid output is NOT expected", RunnerConfigMarshaller
\r
213 .validate(looseValidator, test_output));
\r
215 Schema strictSchema = RunnerConfigMarshaller
\r
216 .getSchema(AllTestSuit.TEST_DATA_PATH_ABSOLUTE
\r
217 + File.separator + "RunnerConfigSchema.xsd");
\r
219 Validator strictVal = RunnerConfigMarshaller
\r
220 .getValidator(strictSchema);
\r
222 // document is invalid against strict schema
\r
223 assertFalse("Invalid output is expected", RunnerConfigMarshaller
\r
224 .validate(strictVal, invalidDoc));
\r
226 // schemafile.delete();
\r
227 // outfile.delete();
\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
245 @Test(expectedExceptions = JAXBException.class)
\r
246 public void testValidationOnMarshalling() throws SAXException,
\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
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
264 fail("Exception has been thrown before this place in unreachable");
\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
276 public void testSchemaFromCodeGeneration() {
\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
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