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