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