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 //**********************************************************************************************************************
40 // Fields
41 //**********************************************************************************************************************
42
43 private static WebServer server;
44 private static XmlRpcClient client;
45
46 //**********************************************************************************************************************
47 // Static Methods
48 //**********************************************************************************************************************
49
50 public static Test suite()
51 {
52 return new TestSetup(new TestSuite(TestXmlRpcInvoker.class))
53 {
54 public void run( final TestResult testResult )
55 {
56 Protectable p = new Protectable()
57 {
58 public void protect() throws Throwable
59 {
60 try
61 {
62 setUp();
63 basicRun(testResult);
64 }
65 finally
66 {
67 tearDown();
68 }
69 }
70 };
71 testResult.runProtected(this, p);
72 }
73
74 protected void setUp() throws Exception
75 {
76 server = new WebServer(9999);
77 server.addHandler("echo", new EchoImpl());
78 server.start();
79 client = new XmlRpcClientLite("http://localhost:9999/RPC2");
80 }
81
82 protected void tearDown() throws Exception
83 {
84 server.shutdown();
85 }
86 };
87 }
88
89 //**********************************************************************************************************************
90 // Other Methods
91 //**********************************************************************************************************************
92
93 public void testInvalidHandlerName()
94 {
95 final XmlRpcInvoker handler = new XmlRpcInvoker(client, "invalid");
96 final Echo echo = ( Echo ) new CglibProxyFactory()
97 .createInvokerProxy(handler, new Class[] {Echo.class});
98 try
99 {
100 echo.echoBack("Hello");
101 fail();
102 }
103 catch( InvokerException e )
104 {
105 }
106 }
107
108 public void testValidInvocation() throws Exception
109 {
110 final XmlRpcInvoker handler = new XmlRpcInvoker(client, "echo");
111 final Echo echo = ( Echo ) new CglibProxyFactory()
112 .createInvokerProxy(handler, new Class[] {Echo.class});
113 assertEquals("Hello", echo.echoBack("Hello"));
114 }
115 }