05bb4afff3f2c5332b0ca80bb9bd1393b1c0149a
[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 java.util.Locale;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertFalse;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import jalview.bin.Cache;
29 import jalview.gui.JvOptionPane;
30 import jalview.ws.api.ServiceWithParameters;
31 import jalview.ws.api.UIinfo;
32 import jalview.ws.jabaws.JalviewJabawsTestUtils;
33 import jalview.ws.jws2.jabaws2.Jws2Instance;
34
35 import java.util.ArrayList;
36 import java.util.Iterator;
37 import java.util.List;
38
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 import compbio.metadata.Option;
43 import compbio.metadata.Parameter;
44 import compbio.metadata.Preset;
45 import compbio.metadata.PresetManager;
46 import compbio.metadata.WrongParameterException;
47
48 /*
49  * All methods in this class are set to the Network group because setUpBeforeClass will fail
50  * if there is no network.
51  */
52 @Test(singleThreaded = true)
53 public class ParameterUtilsTest
54 {
55
56   @BeforeClass(alwaysRun = true)
57   public void setUpJvOptionPane()
58   {
59     JvOptionPane.setInteractiveMode(false);
60     JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
61   }
62
63   /*
64    * To limit tests to specify services, add them to this list; leave list empty
65    * to test all
66    */
67   private static List<String> serviceTests = new ArrayList<>();
68
69   private static Jws2Discoverer disc = null;
70
71   @BeforeClass(alwaysRun = true)
72   public static void setUpBeforeClass() throws Exception
73   {
74     serviceTests.add("AAConWS".toLowerCase(Locale.ROOT));
75     Cache.loadProperties("test/jalview/io/testProps.jvprops");
76     Cache.initLogger();
77     disc = JalviewJabawsTestUtils.getJabawsDiscoverer();
78   }
79
80   @Test(groups = { "Network" })
81   public void testWriteParameterSet() throws WrongParameterException
82   {
83     for (ServiceWithParameters _service : disc.getServices())
84     {
85       if (isForTesting(_service))
86       {
87         Jws2Instance service = (Jws2Instance) _service;
88
89         List<Preset> prl = null;
90         PresetManager prman = service.getPresets();
91         if (prman == null)
92         {
93           continue;
94         }
95         prl = prman.getPresets();
96         if (prl == null)
97         {
98           continue;
99         }
100         for (Preset pr : prl)
101         {
102           List<String> writeparam = null;
103           List<String> readparam = null;
104           writeparam = ParameterUtils.writeParameterSet(
105                   pr.getArguments(service.getRunnerConfig()), " ");
106           List<Option> pset = ParameterUtils.processParameters(writeparam,
107                   service.getRunnerConfig(), " ");
108           readparam = ParameterUtils.writeParameterSet(pset, " ");
109           Iterator<String> o = pr.getOptions().iterator();
110           Iterator<String> s = writeparam.iterator();
111           Iterator<String> t = readparam.iterator();
112           boolean failed = false;
113           while (s.hasNext() && t.hasNext())
114           {
115             String on = o.next();
116             String sn = s.next();
117             String st = t.next();
118             final String errorMsg = "Original was " + on
119                     + " Phase 1 wrote " + sn + "\tPhase 2 wrote " + st;
120             assertEquals(errorMsg, sn, st);
121             assertEquals(errorMsg, sn, on);
122           }
123         }
124       }
125     }
126   }
127
128   /**
129    * Returns true if the service is in the list of the ones we chose to test,
130    * _or_ the list is empty (test all)
131    * 
132    * @param service
133    * @return
134    */
135   public boolean isForTesting(UIinfo service)
136   {
137     return serviceTests.size() == 0
138             || serviceTests.contains(service.serviceType.toLowerCase(Locale.ROOT));
139   }
140
141   @Test(groups = { "Network" })
142   public void testCopyOption()
143   {
144     for (ServiceWithParameters _service : disc.getServices())
145     {
146       if (isForTesting(_service))
147       {
148         Jws2Instance service = (Jws2Instance) _service;
149         List<Option<?>> options = service.getRunnerConfig().getOptions();
150         for (Option<?> o : options)
151         {
152           System.out.println("Testing copyOption for option " + o.getName()
153                   + " of " + service.getActionText());
154           Option<?> cpy = ParameterUtils.copyOption(o);
155           assertTrue(cpy.equals(o));
156           assertEquals(cpy.getName(), o.getName());
157           assertFalse(cpy == o);
158           // todo more assertions?
159         }
160       }
161     }
162   }
163
164   /**
165    */
166   @Test(groups = { "Network" })
167   public void testCopyParameter()
168   {
169     for (ServiceWithParameters _service : disc.getServices())
170     {
171       if (isForTesting(_service))
172       {
173         Jws2Instance service = (Jws2Instance) _service;
174         List<Parameter> parameters = service.getRunnerConfig()
175                 .getParameters();
176         for (Parameter o : parameters)
177         {
178           System.out.println("Testing copyParameter for parameter "
179                   + o.getName() + " of " + service.getActionText());
180           Parameter cpy = ParameterUtils.copyParameter(o);
181           assertTrue(cpy.equals(o));
182           assertFalse(cpy == o);
183           assertEquals(cpy.getName(), o.getName());
184           // todo more assertions?
185         }
186       }
187     }
188   }
189 }