001: /*
002: * CoadunationUtil: The coadunation util library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * BeanWrapper.java
020: *
021: * This object is responsible for wrapping a bean and loading it into memory.
022: */
023:
024: // package path
025: package com.rift.coad.util.connection;
026:
027: // java imports
028: import java.lang.reflect.Proxy;
029: import java.lang.reflect.InvocationHandler;
030: import java.lang.reflect.Method;
031:
032: // junit imports
033: import junit.framework.*;
034:
035: // logging import
036: import org.apache.log4j.Logger;
037: import org.apache.log4j.BasicConfigurator;
038:
039: /**
040: * Test the connection handler.
041: *
042: * @author Brett Chaldecott
043: */
044: public class ConnectionHandlerTest extends TestCase {
045:
046: /**
047: * The test interface
048: */
049: public interface TestInter {
050: public String helloWorld(String msg)
051: throws java.rmi.RemoteException;
052: }
053:
054: /**
055: * The test interface implementation
056: */
057: public class TestInterImpl implements TestInter {
058:
059: /**
060: *
061: */
062: public String helloWorld(String msg)
063: throws java.rmi.RemoteException {
064: if (throwException) {
065: throw new java.rmi.RemoteException("This is a test ex");
066: }
067: System.out.println("Message is :" + msg);
068: called = true;
069: return "Bob is your uncle";
070: }
071: }
072:
073: public boolean called = false;
074: public boolean throwException = false;
075:
076: public ConnectionHandlerTest(String testName) {
077: super (testName);
078: BasicConfigurator.configure();
079: }
080:
081: protected void setUp() throws Exception {
082: }
083:
084: protected void tearDown() throws Exception {
085: }
086:
087: /**
088: * Test of class com.rift.coad.util.connection.ConnectionHandler.
089: */
090: public void testHandler() throws Exception {
091: System.out.println("Handler");
092:
093: TestInterImpl rmiRef = new TestInterImpl();
094: RMIConnection rmiConnection = new RMIConnection(null, "test");
095: ConnectionHandler handler = new ConnectionHandler(
096: rmiConnection, rmiRef);
097: TestInter testInter = (TestInter) Proxy.newProxyInstance(
098: TestInter.class.getClassLoader(),
099: new Class[] { TestInter.class }, handler);
100:
101: String result = testInter.helloWorld("test message");
102: System.out.println("Result message : " + result);
103:
104: if (called == false) {
105: fail("Failed to make the call");
106: }
107:
108: called = false;
109: throwException = true;
110:
111: try {
112: testInter.helloWorld("Test");
113: fail("Failed to generate an exception");
114: } catch (java.rmi.RemoteException ex) {
115: if (called) {
116: fail("The call landed");
117: }
118: }
119: }
120:
121: }
|