001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.invoker;
019    
020    import junit.extensions.TestSetup;
021    import junit.framework.Protectable;
022    import junit.framework.Test;
023    import junit.framework.TestCase;
024    import junit.framework.TestResult;
025    import junit.framework.TestSuite;
026    import org.apache.commons.proxy.exception.InvokerException;
027    import org.apache.commons.proxy.factory.cglib.CglibProxyFactory;
028    import org.apache.commons.proxy.util.Echo;
029    import org.apache.commons.proxy.util.EchoImpl;
030    import org.apache.xmlrpc.WebServer;
031    import org.apache.xmlrpc.XmlRpcClient;
032    import org.apache.xmlrpc.XmlRpcClientLite;
033    
034    /**
035     * @author James Carman
036     */
037    public class TestXmlRpcInvoker extends TestCase
038    {
039        private static WebServer server;
040        private static XmlRpcClient client;
041    
042        public static Test suite()
043        {
044            return new TestSetup( new TestSuite( TestXmlRpcInvoker.class ) )
045            {
046                public void run( final TestResult testResult )
047                {
048                    Protectable p = new Protectable()
049                    {
050                        public void protect() throws Throwable
051                        {
052                            try
053                            {
054                                setUp();
055                                basicRun( testResult );
056                            }
057                            finally
058                            {
059                                tearDown();
060                            }
061                        }
062                    };
063                    testResult.runProtected( this, p );
064                }
065    
066                protected void setUp() throws Exception
067                {
068                    server = new WebServer( 9999 );
069                    server.addHandler( "echo", new EchoImpl() );
070                    server.start();
071                    client = new XmlRpcClientLite( "http://localhost:9999/RPC2" );
072                }
073    
074                protected void tearDown() throws Exception
075                {
076                    server.shutdown();
077                }
078            };
079        }
080    
081        public void testInvalidHandlerName()
082        {
083            final XmlRpcInvoker handler = new XmlRpcInvoker( client, "invalid" );
084            final Echo echo = ( Echo ) new CglibProxyFactory()
085                    .createInvokerProxy( handler, new Class[]{ Echo.class } );
086            try
087            {
088                echo.echoBack( "Hello" );
089                fail();
090            }
091            catch( InvokerException e )
092            {
093            }
094        }
095    
096        public void testValidInvocation() throws Exception
097        {
098            final XmlRpcInvoker handler = new XmlRpcInvoker( client, "echo" );
099            final Echo echo = ( Echo ) new CglibProxyFactory()
100                    .createInvokerProxy( handler, new Class[]{ Echo.class } );
101            assertEquals( "Hello", echo.echoBack( "Hello" ) );
102    
103        }
104    }