--- /dev/null
+/* Copyright (c) 2009 Peter Troshin\r
+ * \r
+ * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ * Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ * License for more details.\r
+ * \r
+ * A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.engine.conf;\r
+\r
+import static org.testng.AssertJUnit.assertEquals;\r
+import static org.testng.AssertJUnit.assertFalse;\r
+import static org.testng.AssertJUnit.assertNotNull;\r
+import static org.testng.AssertJUnit.assertTrue;\r
+import static org.testng.AssertJUnit.fail;\r
+\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.net.MalformedURLException;\r
+import java.net.URL;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import javax.xml.bind.JAXBException;\r
+import javax.xml.validation.Schema;\r
+import javax.xml.validation.Validator;\r
+\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+import org.xml.sax.SAXException;\r
+\r
+import compbio.metadata.AllTestSuit;\r
+import compbio.metadata.Option;\r
+import compbio.metadata.Parameter;\r
+import compbio.metadata.RunnerConfig;\r
+import compbio.metadata.ValueConstrain;\r
+import compbio.metadata.WrongParameterException;\r
+import compbio.runner.msa.Mafft;\r
+\r
+public class RunnerConfigMarshallerTester {\r
+\r
+ public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "MafftParameters.xml";\r
+ public static String test_schema_output = "RunnerConfigSchema.xml";\r
+ public static String test_output = AllTestSuit.OUTPUT_DIR_ABSOLUTE + "MafftParameters.out.xml";\r
+\r
+ public static String invalidDoc = AllTestSuit.TEST_DATA_PATH_ABSOLUTE + "InvalidMafftParameters.xml";\r
+\r
+ RunnerConfig<Mafft> rconfig = null;\r
+ Parameter<Mafft> matrixParam = null;\r
+ RunnerConfigMarshaller<Mafft> pmarshaller = null;\r
+\r
+ @BeforeMethod\r
+ public void setup() {\r
+ // write some parameters programmatically\r
+ try {\r
+ rconfig = new RunnerConfig<Mafft>();\r
+ rconfig.setRunnerClassName(Mafft.class.getName());\r
+ List<Option<Mafft>> prms = new ArrayList<Option<Mafft>>();\r
+\r
+ Parameter<Mafft> p1 = new Parameter<Mafft>("Type", "Type of the sequence (PROTEIN or DNA)");\r
+ // TODO publish help on a compbio web site\r
+\r
+ p1.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
+ p1.addPossibleValues("PROTEIN", "DNA");\r
+ p1.setOptionName("-TYPE");\r
+ p1.setRequired(false);\r
+\r
+ /*\r
+ * -MATRIX= :Protein weight matrix=BLOSUM, PAM, GONNET, ID or\r
+ * filename\r
+ */\r
+ Option<Mafft> p2 = new Option<Mafft>("MATRIX", "Protein weight matrix");\r
+ // TODO publish help on a compbio web site\r
+\r
+ p2.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
+\r
+ p2.addOptionNames("-jtree"); // "-retree"\r
+ p2.setRequired(false);\r
+\r
+ Parameter<Mafft> p3 = new Parameter<Mafft>("Matrix1", "Protein weight matrix");\r
+ // TODO publish help on a compbio web site\r
+ p3.setFurtherDetails("http://www.compbio.dundee.ac.uk/users/pvtroshin/ws/Index.html");\r
+\r
+ p3.addPossibleValues("BLOSUM", "PAM", "GONNET", "ID");\r
+ // This attribute is required by strict schema\r
+ p3.setOptionName("--AAMATRIX");\r
+ p3.setRequired(true);\r
+ p3.setDefaultValue("pam");\r
+ ValueConstrain vc = new ValueConstrain();\r
+ vc.setType(ValueConstrain.Type.Float);\r
+ vc.setMin("-10.12");\r
+ vc.setMax("0");\r
+ p3.setValidValue(vc);\r
+\r
+ prms.add(p1);\r
+ prms.add(p2);\r
+ prms.add(p3);\r
+ matrixParam = p3;\r
+ rconfig.setOptions(prms);\r
+\r
+ pmarshaller = new RunnerConfigMarshaller<Mafft>(RunnerConfig.class, Parameter.class, Option.class, ValueConstrain.class);\r
+ } catch (JAXBException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (WrongParameterException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+\r
+ }\r
+\r
+ @Test()\r
+ public void testMarshalling() {\r
+\r
+ File outfile = new File(this.test_output);\r
+ try {\r
+ pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (JAXBException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ assertTrue("Output file expected, but nothing found!", outfile.exists());\r
+ outfile.delete();\r
+ }\r
+\r
+ @Test()\r
+ public void testUnMarshalling() {\r
+\r
+ File outfile = new File(this.test_output);\r
+ try {\r
+ pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
+\r
+ RunnerConfig<?> rconfig = pmarshaller.read(new FileInputStream(outfile), RunnerConfig.class, Parameter.class, Option.class,\r
+ ValueConstrain.class);\r
+ assertNotNull(rconfig);\r
+ assertEquals(rconfig.getParameters().size(), this.rconfig.getParameters().size());\r
+ assertEquals(rconfig.getRunnerClassName(), this.rconfig.getRunnerClassName());\r
+ assertTrue(matrixParam.equals(rconfig.getArgument("MATRIX1")));\r
+ assertFalse(matrixParam.equals(rconfig.getArgument("Type")));\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (JAXBException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ // outfile.delete();\r
+ }\r
+\r
+ @Test()\r
+ public void testValidation() {\r
+ try {\r
+ System.out.println("CCCC " + rconfig);\r
+ // write schema\r
+ pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_schema_output);\r
+\r
+ File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_schema_output);\r
+ assertTrue(schemafile.exists());\r
+ // document is NOT valid even against a loose schema as elements in\r
+ // java are annotated as required\r
+ Validator looseValidator = RunnerConfigMarshaller.getValidator(schemafile.getAbsolutePath());\r
+\r
+ // write output xml file\r
+ File outfile = new File(this.test_output);\r
+ pmarshaller.write(rconfig, new FileOutputStream(outfile));\r
+\r
+ assertTrue("Invalid output is NOT expected", RunnerConfigMarshaller.validate(looseValidator, test_output));\r
+\r
+ Schema strictSchema = RunnerConfigMarshaller.getSchema(AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator\r
+ + "RunnerConfigSchema.xsd");\r
+\r
+ Validator strictVal = RunnerConfigMarshaller.getValidator(strictSchema);\r
+\r
+ // document is invalid against strict schema\r
+ assertFalse("Invalid output is expected", RunnerConfigMarshaller.validate(strictVal, invalidDoc));\r
+\r
+ // schemafile.delete();\r
+ // outfile.delete();\r
+\r
+ } catch (MalformedURLException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (JAXBException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (SAXException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ }\r
+\r
+ }\r
+\r
+ @Test(expectedExceptions = JAXBException.class)\r
+ public void testValidationOnMarshalling() throws SAXException, JAXBException, MalformedURLException {\r
+ // This is not valid parameter\r
+ Parameter<Mafft> p = new Parameter<Mafft>("MATRIXXX", "Protein weight matrix");\r
+ // This attribute is required by strict schema\r
+ // p.setOptionName("-M");\r
+ p.setRequired(true);\r
+ rconfig.addParameter(p);\r
+ try {\r
+\r
+ // strict schema invalidate this document and throw an exception\r
+ // just discard the output\r
+ pmarshaller.writeAndValidate(rconfig, AllTestSuit.TEST_DATA_PATH_ABSOLUTE + File.separator + "RunnerConfigSchema.xsd",\r
+ new ByteArrayOutputStream());\r
+\r
+ fail("Exception has been thrown before this place in unreachable");\r
+\r
+ } catch (MalformedURLException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void testSchemaFromCodeGeneration() {\r
+ try {\r
+\r
+ pmarshaller.generateSchema(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_schema_output);\r
+ } catch (JAXBException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ File schemafile = new File(AllTestSuit.OUTPUT_DIR_ABSOLUTE, test_schema_output);\r
+ assertTrue("Schema file expected but not found", schemafile.exists());\r
+ assertTrue("Schema file seems to be empty", schemafile.length() > 50);\r
+ // schemafile.delete();\r
+ }\r
+\r
+}\r