1   /*$Id: JavaClassProviderTest.java,v 1.11 2006/03/17 17:50:58 clq2 Exp $
2    * Created on 08-Jun-2004
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
9    *
10  **/
11  package org.astrogrid.applications.javaclass;
12  
13  import org.astrogrid.applications.Application;
14  import org.astrogrid.applications.MockMonitor;
15  import org.astrogrid.applications.beans.v1.cea.castor.ResultListType;
16  import org.astrogrid.applications.beans.v1.parameters.ParameterValue;
17  import org.astrogrid.applications.manager.AppAuthorityIDResolver;
18  import org.astrogrid.applications.description.ApplicationDescription;
19  import org.astrogrid.applications.description.ApplicationDescriptionLibrary;
20  import org.astrogrid.applications.description.ApplicationInterface;
21  import org.astrogrid.applications.description.base.ApplicationDescriptionEnvironment;
22  import org.astrogrid.applications.description.base.TestAuthorityResolver;
23  import org.astrogrid.applications.javaclass.BaseJavaClassConfiguration;
24  import org.astrogrid.applications.manager.idgen.IdGen;
25  import org.astrogrid.applications.manager.idgen.InMemoryIdGen;
26  import org.astrogrid.applications.parameter.protocol.DefaultProtocolLibrary;
27  import org.astrogrid.applications.parameter.protocol.ProtocolLibrary;
28  import org.astrogrid.community.User;
29  import org.astrogrid.workflow.beans.v1.Input;
30  import org.astrogrid.workflow.beans.v1.Output;
31  import org.astrogrid.workflow.beans.v1.Tool;
32  
33  import junit.framework.TestCase;
34  
35  /*** Test of the JavaClass backend to CEa
36   * @author Noel Winstanley nw@jb.man.ac.uk 08-Jun-2004
37   *
38   */
39  public class JavaClassProviderTest extends TestCase {
40      /***
41       * Constructor for JavaClassCEATest.
42       * @param arg0
43       */
44      public JavaClassProviderTest(String arg0) {
45          super(arg0);
46      }
47      /*
48       * @see TestCase#setUp()
49       */
50      protected void setUp() throws Exception {
51          super.setUp();
52          IdGen idgen = new InMemoryIdGen();
53          ProtocolLibrary protocolLib = new DefaultProtocolLibrary();
54          monitor = new MockMonitor();
55          AppAuthorityIDResolver aresolver = new TestAuthorityResolver();
56          ApplicationDescriptionEnvironment env = new ApplicationDescriptionEnvironment(idgen,protocolLib,aresolver);
57          lib = new JavaClassApplicationDescriptionLibrary(new BaseJavaClassConfiguration(), env);
58          assertNotNull(lib);
59      }
60      protected ApplicationDescriptionLibrary lib;
61      protected MockMonitor monitor;
62      
63      protected final User user = new User();
64      public void testLibrary() throws Exception {
65          String[] names = lib.getApplicationNames();
66          assertNotNull(names);
67          assertEquals(4,names.length);        
68      }
69      
70      public void testHelloWorld() throws Exception {
71          ApplicationDescription hw = lib.getDescription("org.astrogrid.test/helloWorld");
72          assertNotNull(hw);
73          assertEquals("org.astrogrid.test/helloWorld",hw.getName());
74          ApplicationInterface iface = hw.getInterfaces()[0];
75          assertNotNull(iface);
76          Tool tool = new Tool();
77          Output output = new Output();
78          tool.setOutput(output);
79          ParameterValue result = new ParameterValue();
80          result.setName(iface.getArrayofOutputs()[0]);
81          output.addParameter(result);
82          Application app = hw.initializeApplication("testrun",user,tool);
83          assertNotNull(app);
84          app.addObserver(monitor);
85          app.createExecutionTask().run();
86          assertTrue(monitor.sawExit);
87          ResultListType results= app.getResult();
88          assertNotNull(results);
89          assertEquals(1,results.getResultCount());
90          String o = results.getResult(0).getValue();
91          assertNotNull(o);
92          System.out.println(o);
93      }
94      public void testSum() throws Exception {
95          ApplicationDescription hw = lib.getDescription("org.astrogrid.test/sum");
96          assertNotNull(hw);
97          ApplicationInterface iface = hw.getInterfaces()[0];
98          String[] inputParameterNames = iface.getArrayofInputs();
99          assertNotNull(iface);
100         Tool tool = new Tool();
101         Input input = new Input();
102         tool.setInput(input);
103         Output output = new Output();
104         tool.setOutput(output);
105         ParameterValue a = new ParameterValue();
106         ParameterValue b = new ParameterValue();        
107         a.setName(inputParameterNames[0]);
108         b.setName(inputParameterNames[1]);
109         a.setValue("1");
110         b.setValue("2");
111         input.addParameter(a);
112         input.addParameter(b);
113         ParameterValue result = new ParameterValue();
114         result.setName(iface.getArrayofOutputs()[0]);
115         output.addParameter(result);
116         Application app = hw.initializeApplication("testrun",user,tool);
117         assertNotNull(app);        
118         app.addObserver(monitor);
119 
120         app.createExecutionTask().run();
121         assertTrue(monitor.sawExit);
122         ResultListType results= app.getResult();
123         assertNotNull(results);
124         assertEquals(1,results.getResultCount());
125         String o = results.getResult(0).getValue();
126         assertNotNull(o);
127         System.out.println(o);
128         assertEquals("3",o);
129     }    
130     
131 
132     public void testEchoDifferentArgs() throws Exception {
133         ApplicationDescription hw = lib.getDescription("org.astrogrid.test/echoDifferentArgs");
134         assertNotNull(hw);
135         ApplicationInterface iface = hw.getInterfaces()[0];
136         String[] inputParameterNames = iface.getArrayofInputs();
137         assertNotNull(iface);
138         Tool tool = new Tool();
139         Input input = new Input();
140         tool.setInput(input);
141         Output output = new Output();
142         tool.setOutput(output);
143         ParameterValue a = new ParameterValue();
144         ParameterValue b = new ParameterValue();        
145         a.setName(inputParameterNames[0]);
146         b.setName(inputParameterNames[1]);
147         a.setValue("Hello");
148         b.setValue("2");
149         input.addParameter(a);
150         input.addParameter(b);
151         ParameterValue result = new ParameterValue();
152         result.setName(iface.getArrayofOutputs()[0]);
153         output.addParameter(result);
154         Application app = hw.initializeApplication("testrun",user,tool);
155         assertNotNull(app);        
156         app.addObserver(monitor);
157 
158         app.createExecutionTask().run();
159         assertTrue(monitor.sawExit);
160         ResultListType results= app.getResult();
161         assertNotNull(results);
162         assertEquals(1,results.getResultCount());
163         String o = results.getResult(0).getValue();
164         assertNotNull(o);
165         System.out.println(o);
166         assertEquals("Hello2",o);
167     }    
168 
169 }
170 
171 
172 /* 
173 $Log: JavaClassProviderTest.java,v $
174 Revision 1.11  2006/03/17 17:50:58  clq2
175 gtr_1489_cea correted version
176 
177 Revision 1.9  2006/03/07 21:45:26  clq2
178 gtr_1489_cea
179 
180 Revision 1.6.34.2  2006/02/01 12:09:54  gtr
181 Refactored and fixed to allow the tests to work with the new configuration.
182 
183 Revision 1.6.34.1  2005/12/18 14:48:25  gtr
184 Refactored to allow the component managers to pass their unit tests and the fingerprint JSP to work. See BZ1492.
185 
186 Revision 1.6  2005/07/15 14:44:32  jdt
187 merge from cea_jdt_1295
188 
189 Revision 1.5.84.1  2005/07/15 14:28:17  jdt
190 Fixed the parameter order in the application interface, and added a test.
191 
192 Revision 1.5  2004/11/27 13:20:02  pah
193 result of merge of pah_cea_bz561 branch
194 
195 Revision 1.4.16.1  2004/11/09 09:21:16  pah
196 initial attempt to rationalise authorityID use & self registering
197 
198 Revision 1.4  2004/09/17 01:22:12  nw
199 updated tests
200 
201 Revision 1.3.52.1  2004/09/14 13:44:59  nw
202 runs in same thread.
203 
204 Revision 1.3  2004/07/26 12:07:38  nw
205 renamed indirect package to protocol,
206 renamed classes and methods within protocol package
207 javadocs
208 
209 Revision 1.2  2004/07/01 11:16:22  nw
210 merged in branch
211 nww-itn06-componentization
212 
213 Revision 1.1.2.1  2004/07/01 01:42:46  nw
214 final version, before merge
215 
216 Revision 1.1.2.2  2004/06/17 09:21:23  nw
217 finished all major functionality additions to core
218 
219 Revision 1.1.2.1  2004/06/14 08:56:58  nw
220 factored applications into sub-projects,
221 got packaging of wars to work again
222  
223 */