JAL-2089 patch broken merge to master for Release 2.10.0b1
[jalview.git] / test / jalview / ws / jws2 / ParameterUtilsTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ws.jws2;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertTrue;
26
27 import jalview.bin.Cache;
28 import jalview.ws.jabaws.JalviewJabawsTestUtils;
29 import jalview.ws.jws2.jabaws2.Jws2Instance;
30
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34
35 import org.testng.annotations.BeforeClass;
36 import org.testng.annotations.Test;
37
38 import compbio.metadata.Option;
39 import compbio.metadata.Parameter;
40 import compbio.metadata.Preset;
41 import compbio.metadata.PresetManager;
42 import compbio.metadata.WrongParameterException;
43
44 public class ParameterUtilsTest
45 {
46   /*
47    * To limit tests to specify services, add them to this list; leave list empty
48    * to test all
49    */
50   private static List<String> serviceTests = new ArrayList<String>();
51
52   private static Jws2Discoverer disc = null;
53
54   @BeforeClass(alwaysRun = true)
55   public static void setUpBeforeClass() throws Exception
56   {
57     serviceTests.add("AAConWS".toLowerCase());
58     Cache.loadProperties("test/jalview/io/testProps.jvprops");
59     Cache.initLogger();
60     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
61   }
62
63   @Test(groups = { "Functional" })
64   public void testWriteParameterSet() throws WrongParameterException
65   {
66     for (Jws2Instance service : disc.getServices())
67     {
68       if (isForTesting(service))
69       {
70
71         List<Preset> prl = null;
72         PresetManager prman = service.getPresets();
73         if (prman == null)
74         {
75           continue;
76         }
77         prl = prman.getPresets();
78         if (prl == null)
79         {
80           continue;
81         }
82         for (Preset pr : prl)
83         {
84           List<String> writeparam = null;
85           List<String> readparam = null;
86           writeparam = ParameterUtils.writeParameterSet(
87                   pr.getArguments(service.getRunnerConfig()), " ");
88           List<Option> pset = ParameterUtils.processParameters(writeparam,
89                   service.getRunnerConfig(), " ");
90           readparam = ParameterUtils.writeParameterSet(pset, " ");
91           Iterator<String> o = pr.getOptions().iterator();
92           Iterator<String> s = writeparam.iterator();
93           Iterator<String> t = readparam.iterator();
94           boolean failed = false;
95           while (s.hasNext() && t.hasNext())
96           {
97             String on = o.next();
98             String sn = s.next();
99             String st = t.next();
100             final String errorMsg = "Original was " + on
101                     + " Phase 1 wrote " + sn + "\tPhase 2 wrote " + st;
102             assertEquals(errorMsg, sn, st);
103             assertEquals(errorMsg, sn, on);
104           }
105         }
106       }
107     }
108   }
109
110   /**
111    * Returns true if the service is in the list of the ones we chose to test,
112    * _or_ the list is empty (test all)
113    * 
114    * @param service
115    * @return
116    */
117   public boolean isForTesting(Jws2Instance service)
118   {
119     return serviceTests.size() == 0
120             || serviceTests.contains(service.serviceType.toLowerCase());
121   }
122
123   @Test(groups = { "Functional" })
124   public void testCopyOption()
125   {
126     for (Jws2Instance service : disc.getServices())
127     {
128       if (isForTesting(service))
129       {
130         List<Option<?>> options = service.getRunnerConfig().getOptions();
131         for (Option<?> o : options)
132         {
133           System.out.println("Testing copyOption for option " + o.getName()
134                   + " of " + service.getActionText());
135           Option<?> cpy = ParameterUtils.copyOption(o);
136           assertTrue(cpy.equals(o));
137           assertEquals(cpy.getName(), o.getName());
138           assertFalse(cpy == o);
139           // todo more assertions?
140         }
141       }
142     }
143   }
144
145   /**
146    */
147   @Test(groups = { "Functional" })
148   public void testCopyParameter()
149   {
150     for (Jws2Instance service : disc.getServices())
151     {
152       if (isForTesting(service))
153       {
154         List<Parameter> parameters = service.getRunnerConfig()
155                 .getParameters();
156         for (Parameter o : parameters)
157         {
158           System.out.println("Testing copyParameter for parameter "
159                   + o.getName() + " of " + service.getActionText());
160           Parameter cpy = ParameterUtils.copyParameter(o);
161           assertTrue(cpy.equals(o));
162           assertFalse(cpy == o);
163           assertEquals(cpy.getName(), o.getName());
164           // todo more assertions?
165         }
166       }
167     }
168   }
169 }