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