1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.proxy.invoker;
19  
20  import junit.extensions.TestSetup;
21  import junit.framework.Protectable;
22  import junit.framework.Test;
23  import junit.framework.TestCase;
24  import junit.framework.TestResult;
25  import junit.framework.TestSuite;
26  import org.apache.commons.proxy.exception.InvokerException;
27  import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
28  import org.apache.commons.proxy.util.Echo;
29  import org.apache.commons.proxy.util.EchoImpl;
30  import org.apache.xmlrpc.WebServer;
31  import org.apache.xmlrpc.XmlRpcClient;
32  import org.apache.xmlrpc.XmlRpcClientLite;
33  
34  /**
35   * @author James Carman
36   */
37  public class TestXmlRpcInvoker extends TestCase
38  {
39      private static WebServer server;
40      private static XmlRpcClient client;
41  
42      public static Test suite()
43      {
44          return new TestSetup( new TestSuite( TestXmlRpcInvoker.class ) )
45          {
46              public void run( final TestResult testResult )
47              {
48                  Protectable p = new Protectable()
49                  {
50                      public void protect() throws Throwable
51                      {
52                          try
53                          {
54                              setUp();
55                              basicRun( testResult );
56                          }
57                          finally
58                          {
59                              tearDown();
60                          }
61                      }
62                  };
63                  testResult.runProtected( this, p );
64              }
65  
66              protected void setUp() throws Exception
67              {
68                  server = new WebServer( 9999 );
69                  server.addHandler( "echo", new EchoImpl() );
70                  server.start();
71                  client = new XmlRpcClientLite( "http://localhost:9999/RPC2" );
72              }
73  
74              protected void tearDown() throws Exception
75              {
76                  server.shutdown();
77              }
78          };
79      }
80  
81      public void testInvalidHandlerName()
82      {
83          final XmlRpcInvoker handler = new XmlRpcInvoker( client, "invalid" );
84          final Echo echo = ( Echo ) new CglibProxyFactory()
85                  .createInvokerProxy( handler, new Class[]{ Echo.class } );
86          try
87          {
88              echo.echoBack( "Hello" );
89              fail();
90          }
91          catch( InvokerException e )
92          {
93          }
94      }
95  
96      public void testValidInvocation() throws Exception
97      {
98          final XmlRpcInvoker handler = new XmlRpcInvoker( client, "echo" );
99          final Echo echo = ( Echo ) new CglibProxyFactory()
100                 .createInvokerProxy( handler, new Class[]{ Echo.class } );
101         assertEquals( "Hello", echo.echoBack( "Hello" ) );
102 
103     }
104 }