0b5a0a37ad7a3465cfee5ab073cb988fa6502293
[proteocache.git] / testsrc / compbio / metadata / PresetTester.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.metadata;\r
20 \r
21 import static org.testng.Assert.assertEquals;\r
22 import static org.testng.Assert.assertFalse;\r
23 import static org.testng.Assert.assertNotNull;\r
24 import static org.testng.Assert.assertTrue;\r
25 import static org.testng.Assert.fail;\r
26 \r
27 import java.io.File;\r
28 import java.io.FileInputStream;\r
29 import java.io.FileNotFoundException;\r
30 import java.io.IOException;\r
31 import java.util.ArrayList;\r
32 import java.util.Collections;\r
33 import java.util.List;\r
34 \r
35 import javax.xml.bind.JAXBContext;\r
36 import javax.xml.bind.JAXBElement;\r
37 import javax.xml.bind.JAXBException;\r
38 import javax.xml.bind.Unmarshaller;\r
39 import javax.xml.transform.stream.StreamSource;\r
40 \r
41 import org.testng.annotations.BeforeTest;\r
42 import org.testng.annotations.Test;\r
43 import org.xml.sax.SAXException;\r
44 \r
45 import compbio.engine.conf.RunnerConfigMarshaller;\r
46 import compbio.runner.msa.Mafft;\r
47 \r
48 public class PresetTester {\r
49 \r
50     public static final String input = AllTestSuit.TEST_DATA_PATH\r
51             + "MafftPresets.xml";\r
52     public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
53             + "MafftParameters.xml";\r
54 \r
55     PresetManager<Mafft> presets = null;\r
56     RunnerConfig<Mafft> rconfParams = null;\r
57 \r
58     @BeforeTest(enabled = true)\r
59     public void loadPresets() {\r
60         try {\r
61             // Load Preset definitions\r
62             RunnerConfigMarshaller<Mafft> rconfigPresets = new RunnerConfigMarshaller<Mafft>(\r
63                     PresetManager.class);\r
64             File infile = new File(input);\r
65             assertTrue(infile.exists());\r
66             presets = rconfigPresets.read(new FileInputStream(infile),\r
67                     PresetManager.class);\r
68             assertNotNull(presets);\r
69             assertFalse(presets.preset.isEmpty());\r
70 \r
71             // Load Parameters definitions\r
72             File input = new File(this.test_input);\r
73             assertTrue(input.exists());\r
74             JAXBContext ctx = JAXBContext.newInstance(RunnerConfig.class);\r
75             Unmarshaller um = ctx.createUnmarshaller();\r
76             JAXBElement<RunnerConfig> rconfigParams = um.unmarshal(\r
77                     new StreamSource(input), RunnerConfig.class);\r
78             rconfParams = rconfigParams.getValue();\r
79             assertNotNull(rconfParams);\r
80 \r
81         } catch (JAXBException e) {\r
82             e.printStackTrace();\r
83             fail(e.getMessage());\r
84         } catch (FileNotFoundException e) {\r
85             e.printStackTrace();\r
86             fail(e.getMessage());\r
87         }\r
88     }\r
89 \r
90     @Test\r
91     public void marshallPreset() {\r
92         try {\r
93             RunnerConfigMarshaller<Mafft> rconfig = new RunnerConfigMarshaller<Mafft>(\r
94                     PresetManager.class);\r
95             PresetManager<Mafft> pman = getPresets();\r
96             assertNotNull(pman);\r
97             rconfig.readAndValidate(new FileInputStream(new File(input)),\r
98                     PresetManager.class);\r
99         } catch (JAXBException e) {\r
100             e.printStackTrace();\r
101             fail(e.getMessage());\r
102         } catch (IOException e) {\r
103             e.printStackTrace();\r
104             fail(e.getMessage());\r
105         } catch (SAXException e) {\r
106             e.printStackTrace();\r
107             fail(e.getMessage());\r
108         }\r
109     }\r
110 \r
111     @Test\r
112     public void validatePresets() {\r
113         assertNotNull(presets);\r
114         assertEquals(presets.getPresets().size(), 6);\r
115         try {\r
116             for (Preset<Mafft> pr : presets.getPresets()) {\r
117                 List<Option<Mafft>> options;\r
118                 options = pr.getArguments(rconfParams);\r
119                 assertFalse(options.isEmpty());\r
120                 if (pr.name.equals("L-INS-i (Accuracy-oriented)")) {\r
121                     assertEquals(options.size(), 2);\r
122                     Option<Mafft> o = options.get(0);\r
123                     if (o.name.equals("Pairwise alignment computation method")) {\r
124                         List<String> onames = o.getOptionNames();\r
125                         boolean match = false;\r
126                         for (String oname : onames) {\r
127                             if (oname.equals("--localpair")) {\r
128                                 match = true;\r
129                                 break;\r
130                             }\r
131                         }\r
132                         assertTrue(match);\r
133                     }\r
134                 }\r
135                 if (pr.name.equals("NW-NS-PartTree-1 (Speed oriented)")) {\r
136                     assertEquals(options.size(), 4);\r
137                 }\r
138             }\r
139         } catch (WrongParameterException e) {\r
140             e.printStackTrace();\r
141             fail(e.getMessage());\r
142         }\r
143     }\r
144 \r
145     @Test\r
146     public void testPresetWithMuptiOptions() {\r
147         assertNotNull(presets);\r
148         assertEquals(presets.getPresets().size(), 6);\r
149         try {\r
150             for (Preset<Mafft> pr : presets.getPresets()) {\r
151                 List<Option<Mafft>> options;\r
152                 options = pr.getArguments(rconfParams);\r
153                 assertFalse(options.isEmpty());\r
154                 if (pr.name.equals("E-INS-i (Accuracy-oriented)")) {\r
155                     assertEquals(options.size(), 3);\r
156                     Option<Mafft> o = options.get(0);\r
157                     if (o.name.equals("Pairwise alignment computation method")) {\r
158                         List<String> onames = o.getOptionNames();\r
159                         boolean match = false;\r
160                         for (String oname : onames) {\r
161                             if (oname.equals("--genafpair")) {\r
162                                 match = true;\r
163                                 break;\r
164                             }\r
165                         }\r
166                         assertTrue(match);\r
167                     }\r
168                 }\r
169                 if (pr.name.equals("NW-NS-PartTree-1 (Speed oriented)")) {\r
170                     assertEquals(options.size(), 4);\r
171                 }\r
172             }\r
173         } catch (WrongParameterException e) {\r
174             e.printStackTrace();\r
175             fail(e.getMessage());\r
176         }\r
177     }\r
178 \r
179     private static PresetManager<Mafft> getPresets() {\r
180         Preset<Mafft> preset1 = new Preset<Mafft>();\r
181         preset1.name = "L-INS-i (Accuracy-oriented)";\r
182         preset1.description = "dsfjkg fdjksghkjsgdfh jksdfg sdfgkjhsdfgk kjsdfg ";\r
183 \r
184         List<String> optionNames = new ArrayList<String>();\r
185         optionNames.add("--localpair");\r
186         optionNames.add("--maxiterate 1000");\r
187         preset1.option = optionNames;\r
188         PresetManager<Mafft> prman = new PresetManager<Mafft>();\r
189         prman.preset = Collections.singletonList(preset1);\r
190         prman.runnerClassName = Mafft.class.getCanonicalName();\r
191 \r
192         return prman;\r
193     }\r
194 }\r