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("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
87 * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or
\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
94 p2.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");
\r
96 p2.addOptionNames("-jtree"); // "-retree"
\r
97 p2.setRequired(false);
\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
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
113 p3.setValidValue(vc);
\r
119 rconfig.setOptions(prms);
\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
137 public void testMarshalling() {
\r
139 File outfile = new File(this.test_output);
\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
152 assertTrue("Output file expected, but nothing found!", outfile.exists());
\r
157 public void testUnMarshalling() {
\r
159 File outfile = new File(this.test_output);
\r
161 pmarshaller.write(rconfig, new FileOutputStream(outfile));
\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
183 // outfile.delete();
\r
187 public void testValidation() {
\r
189 System.out.println("CCCC " + rconfig);
\r
191 pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE,
\r
192 test_schema_output);
\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
202 // write output xml file
\r
203 File outfile = new File(this.test_output);
\r
204 pmarshaller.write(rconfig, new FileOutputStream(outfile));
\r
206 assertTrue("Invalid output is NOT expected", RunnerConfigMarshaller
\r
207 .validate(looseValidator, test_output));
\r
209 Schema strictSchema = RunnerConfigMarshaller
\r
210 .getSchema(AllTestSuit.TEST_DATA_PATH_ABSOLUTE
\r
211 + File.separator + "RunnerConfigSchema.xsd");
\r
213 Validator strictVal = RunnerConfigMarshaller
\r
214 .getValidator(strictSchema);
\r
216 // document is invalid against strict schema
\r
217 assertFalse("Invalid output is expected", RunnerConfigMarshaller
\r
218 .validate(strictVal, invalidDoc));
\r
220 // schemafile.delete();
\r
221 // outfile.delete();
\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
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
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
257 fail("Exception has been thrown before this place in unreachable");
\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
269 public void testSchemaFromCodeGeneration() {
\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
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