KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > util > connection > ConnectionHandlerTest


1 /*
2  * CoadunationUtil: The coadunation util library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library 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. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * BeanWrapper.java
20  *
21  * This object is responsible for wrapping a bean and loading it into memory.
22  */

23
24 // package path
25
package com.rift.coad.util.connection;
26
27 // java imports
28
import java.lang.reflect.Proxy JavaDoc;
29 import java.lang.reflect.InvocationHandler JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31
32 // junit imports
33
import junit.framework.*;
34
35 // logging import
36
import org.apache.log4j.Logger;
37 import org.apache.log4j.BasicConfigurator;
38
39 /**
40  * Test the connection handler.
41  *
42  * @author Brett Chaldecott
43  */

44 public class ConnectionHandlerTest extends TestCase {
45     
46     /**
47      * The test interface
48      */

49     public interface TestInter {
50         public String JavaDoc helloWorld(String JavaDoc msg) throws java.rmi.RemoteException JavaDoc;
51     }
52     
53     /**
54      * The test interface implementation
55      */

56     public class TestInterImpl implements TestInter {
57         
58         /**
59          *
60          */

61         public String JavaDoc helloWorld(String JavaDoc msg) throws java.rmi.RemoteException JavaDoc {
62             if (throwException) {
63                 throw new java.rmi.RemoteException JavaDoc("This is a test ex");
64             }
65             System.out.println("Message is :" + msg);
66             called = true;
67             return "Bob is your uncle";
68         }
69     }
70     
71     public boolean called = false;
72     public boolean throwException = false;
73     
74     public ConnectionHandlerTest(String JavaDoc testName) {
75         super(testName);
76         BasicConfigurator.configure();
77     }
78
79     protected void setUp() throws Exception JavaDoc {
80     }
81
82     protected void tearDown() throws Exception JavaDoc {
83     }
84
85     /**
86      * Test of class com.rift.coad.util.connection.ConnectionHandler.
87      */

88     public void testHandler() throws Exception JavaDoc {
89         System.out.println("Handler");
90         
91         TestInterImpl rmiRef = new TestInterImpl();
92         RMIConnection rmiConnection = new RMIConnection(null,"test");
93         ConnectionHandler handler = new ConnectionHandler(rmiConnection,rmiRef);
94         TestInter testInter = (TestInter)Proxy.newProxyInstance(
95                 TestInter.class.getClassLoader(),
96                 new Class JavaDoc[] {TestInter.class},handler);
97         
98         String JavaDoc result = testInter.helloWorld("test message");
99         System.out.println("Result message : " + result);
100         
101         if (called == false) {
102             fail("Failed to make the call");
103         }
104         
105         called = false;
106         throwException = true;
107         
108         try {
109             testInter.helloWorld("Test");
110             fail("Failed to generate an exception");
111         } catch (java.rmi.RemoteException JavaDoc ex) {
112             if (called) {
113                 fail("The call landed");
114             }
115         }
116     }
117     
118 }
119
Popular Tags