1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.remoting.xmlrpc;
16  
17  import org.figure8.join.services.remoting.ArtifactService;
18  import org.figure8.join.services.remoting.ProcessControlService;
19  import org.figure8.join.services.remoting.beans.RemoteRelease;
20  
21  import junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  import org.apache.xmlrpc.WebServer;
25  import org.apache.xmlrpc.XmlRpcClient;
26  import org.easymock.MockControl;
27  
28  import java.util.Map;
29  import java.util.Date;
30  import java.util.Vector;
31  /**
32   * JUnit test case for testing DefaultXmlRpcHandler and its interaction with services.
33   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
34   * @version $Revision: 1.1 $
35   */
36  public class DefaultXmlRpcHandlerTest extends TestCase{
37  
38     // Static -------------------------------------------------------------------
39  
40     /** Constant representing security token used for tests */
41     protected static final String TOKEN = "securitytoken@jdoe";
42     /** Constant representing serverl url used for tests */
43     protected static final String SERVER_URL = "http://localhost:6666";
44  
45  
46     // Attributes ---------------------------------------------------------------
47  
48     /** Instance of class under test */
49     private DefaultXmlRpcHandler handler = null;
50     /** Embedded instance of Xml-Rpc server */
51     private WebServer xmlrpc = null;
52  
53     /** Control for mock artifact service */
54     private MockControl artifactControl = null;
55     /** ArtifactService mock implementation */
56     private ArtifactService artifactService = null;
57  
58     /** Control for mock process control service */
59     private MockControl controlControl = null;
60     /** ProcessControlService mockImplementation */
61     private ProcessControlService controlService = null;
62  
63  
64     // Constructors -------------------------------------------------------------
65  
66     /** Default constructor. Build a test case using a name. */
67     public DefaultXmlRpcHandlerTest(String testName){
68        super(testName);
69     }
70  
71  
72     // Override of TestCase -----------------------------------------------------
73  
74     /**
75      * Make this class a TestSuite.
76      * @return A TestSuite containing this TestCase.
77      */
78     public static Test suite(){
79        TestSuite suite = new TestSuite(DefaultXmlRpcHandlerTest.class);
80        return suite;
81     }
82     
83     
84     // Public -------------------------------------------------------------------
85  
86     /** Creates mock objects and starts XmlRpc server */
87     public void setUp(){
88        // Mock an artifactService.
89        artifactControl = MockControl.createControl(ArtifactService.class);
90        artifactService = (ArtifactService)artifactControl.getMock();
91        // Mock a processControlService.
92        controlControl = MockControl.createControl(ProcessControlService.class);
93        controlService = (ProcessControlService)controlControl.getMock();
94  
95        // Set common expectations.
96        try{
97           controlService.login("jdoe", "password");
98           controlControl.setReturnValue(TOKEN);
99           controlService.logout(TOKEN);
100       }
101       catch (Exception e){
102          fail("Setting up Mock services fails !");
103       }
104       // Create a default handler and assign services.
105       handler = new DefaultXmlRpcHandler();
106       handler.setArtifactService(artifactService);
107       handler.setProcessControlService(controlService);
108       // Start Xml-Rpc server and register handler.
109       xmlrpc = new WebServer(6666);
110       xmlrpc.addHandler("join", handler);
111       xmlrpc.start();
112    }
113 
114    /** Test login() and logout() methods */
115    public void testLoginLogout(){
116       // No more expectations.
117       controlControl.replay();
118       artifactControl.replay();
119 
120       try{
121          // Call login, logout.
122          XmlRpcClient client = new XmlRpcClient(SERVER_URL);
123          String token = (String)client.execute("join.login", makeParams("jdoe", "password"));
124          client.execute("join.logout", makeParam(token));
125       }
126       catch (Exception e){
127          e.printStackTrace();
128          fail("Exception thrown during Xml-Rpc invocation.");
129       }
130       // Verify mock controls.
131       controlControl.verify();
132       artifactControl.verify();
133    }
134 
135    /** Test the retrieval of releases */
136    public void testGetReleases(){
137       Vector releases = null;
138       // Add return expectations.
139       try{
140          artifactService.getReleases(TOKEN);
141          artifactControl.setReturnValue(new RemoteRelease[]{
142             new RemoteRelease("1.0", new Date()), new RemoteRelease("2.0", new Date())
143          });
144       }
145       catch (Exception e){
146          fail("Setting up Mock services fails !");
147       }
148       controlControl.replay();
149       artifactControl.replay();
150 
151       try{
152          // Call login, getReleases and logout.
153          XmlRpcClient client = new XmlRpcClient(SERVER_URL);
154          String token = (String)client.execute("join.login", makeParams("jdoe", "password"));
155          releases = (Vector)client.execute("join.getReleases", makeParam(token));
156          client.execute("join.logout", makeParam(token));
157       }
158       catch (Exception e){
159          e.printStackTrace();
160          fail("Exception thrown during Xml-Rpc invocation.");
161       }
162       // Verify mock controls.
163       controlControl.verify();
164       artifactControl.verify();
165       // Assert on releases content.
166       assertNotNull("Found some releases", releases);
167       assertEquals("Found the correct number of releases", 2, releases.size());
168       assertEquals("Found the correct releases", "1.0", ((Map)releases.get(0)).get("name"));
169       assertEquals("Found the correct releases", "2.0", ((Map)releases.get(1)).get("name"));
170    }
171 
172    /** Shutdown XmlRpc server */
173    public void tearDown(){
174       xmlrpc.shutdown();
175    }
176 
177 
178    // Private ------------------------------------------------------------------
179 
180    /** Make a param vector */
181    private Vector makeParam(Object obj){
182       Vector params = new Vector();
183       params.add(obj);
184       return params;
185    }
186 
187    /** Make a params vector */
188    private Vector makeParams(Object obj1, Object obj2){
189       Vector params = makeParam(obj1);
190       params.add(obj2);
191       return params;
192    }
193 }