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