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