You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ch...@apache.org on 2003/08/22 04:23:36 UTC

cvs commit: incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/transport AsyncTransportStress.java AsyncTransportTest.java

chirino     2003/08/21 19:23:36

  Added:       modules/core/src/test/org/apache/geronimo/remoting
                        IPerson.java MarshalingInterceptorsTest.java
                        Person.java RemotingInterceptorsTest.java
                        TransientValue.java
               modules/core/src/test/org/apache/geronimo/remoting/transport
                        AsyncTransportStress.java AsyncTransportTest.java
  Log:
  Commiting the intial versions of Interceptor based remoting framwork that will be used to remote EJBs.
  
  Revision  Changes    Path
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/IPerson.java
  
  Index: IPerson.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  package org.apache.geronimo.remoting;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  public interface IPerson {
  	public TransientValue getValue();
  	public void setValue(TransientValue value);
     /**
      * @return
      */
     public abstract IPerson getSpouse();
     /**
      * @param spouse
      */
     public abstract void setSpouse(IPerson spouse);
     /**
      * @return
      */
     public abstract String getFirstName();
     /**
      * @param firstName
      */
     public abstract void setFirstName(String firstName);
     /**
      * @return
      */
     public abstract String getLastName();
     /**
      * @param lastName
      */
     public abstract void setLastName(String lastName);
  }
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/MarshalingInterceptorsTest.java
  
  Index: MarshalingInterceptorsTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.remoting;
  
  import java.io.IOException;
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.ArrayList;
  
  import junit.framework.TestCase;
  
  import org.apache.geronimo.common.Interceptor;
  import org.apache.geronimo.proxy.ProxyContainer;
  import org.apache.geronimo.proxy.ReflexiveInterceptor;
  import org.apache.geronimo.remoting.transport.BytesMarshalledObject;
  
  /**
   * Unit test for the Marshaling/DeMarshaling Interceptors
   *
   * This test uses 2 classloaders to mock 2 seperate
   * application classloaders.
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  
  public class MarshalingInterceptorsTest extends TestCase {
  
      private static final String PERSON_CLASS = "org.apache.geronimo.remoting.Person";
      private static final String TRANSIENT_CLASS = "org.apache.geronimo.remoting.TransientValue";
      ArrayList severContainers = new ArrayList();
      URLClassLoader cl1, cl2;
  
  //    /**
  //     * creates the two classloaders that will be used during the tests.
  //     */
  //    public void setUp() throws MalformedURLException, IOException {
  //        URL url = new URL(System.getProperty("test-app.url"));
  //        System.out.println("Setting up the CP to: " + url);
  //
  //        cl1 = new URLClassLoader(new URL[] { url });
  //        cl2 = new URLClassLoader(new URL[] { url });
  //    }
      
      //
      // This is here until we can enable the tests.
      public void testNull() {        
      }
  
  //    /**
  //     * Verify that the classpath/classloader structure is
  //     * as expected.  If the application classes are in
  //     * the current classloader then that will throw all the 
  //     * tests off.
  //     * 
  //     * @throws Exception
  //     */
  //    public void testCheckClassLoaders() throws Exception {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //        assertFalse("Classpath for this test was incorrect.  The '"+PERSON_CLASS+"' cannot be in the test's classpath.", class1.equals(class2));
  //    }
  //
  //    /**
  //     * Verify that the helper methods used by this test work
  //     * as expected.  Everything should be ok when working in the
  //     * same classloader.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseSameCL() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl1.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        call(object1, "setSpouse", new Object[] { object2 });
  //    }
  //
  //    /**
  //     * Verify that the helper methods used by this test work
  //     * as expected.  We should see problems when you start mixing
  //     * Objects from different classloaders.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseOtherCL() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        try {
  //            call(object1, "setSpouse", new Object[] { object2 });
  //            fail("Call should fail due to argument type mismatch.");
  //        } catch (IllegalArgumentException e) {
  //        }
  //    }
  //
  //    /**
  //     * Verify that a proxy using working in a single app context works ok.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseWithProxy() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        // Simulate App1 creating a proxy.
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Object proxy1 = createProxy(object1);
  //        call(proxy1, "setSpouse", new Object[] { object1 });
  //    }
  //
  //    /**
  //     * App2 creates a proxy and serializes.  App1 context deserializes
  //     * the proxy and trys to use it.  Method calls should be getting
  //     * marshalled/demarshaled by the proxy to avoid getting an 
  //     * IllegalArgumentException.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseOtherCLWithSerializedProxy() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        // Simulate App2 creating a proxy.
  //        Thread.currentThread().setContextClassLoader(cl2);
  //        Object proxy2 = createProxy(object2);
  //        MarshalledObject mo = new BytesMarshalledObject(proxy2);
  //
  //        // Simulate App1 using the serialized proxy.    
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Object proxy1 = mo.get();
  //        call(proxy1, "setSpouse", new Object[] { object1 });
  //    }
  //
  //    /**
  //     * App1 creates a proxy.  It then creates a Transient calls that holds
  //     * a "value" property that is transient when serialized.  This allows to 
  //     * to see if the Transient object was serialized by getting the value back
  //     * and seeing if the "value" is null.  All this work is done in one classloader
  //     * so no serialization should occur.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetTransientWithOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Class class2 = cl1.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        Object proxy1 = createProxy(object1);
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, "foo");
  //    }
  //
  //    /**
  //     * Same as testSetTransientWithOptimizedProxy() but, the proxy is serialized before it is used.
  //     * It should still result in the method call not being serialized.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetTransientWithSerializedOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Class class2 = cl1.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        Object proxy1 = createProxy(object1);
  //        proxy1 = new BytesMarshalledObject(proxy1).get();
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, "foo");
  //    }
  //
  //    /**
  //     * Same as testSetTransientWithOptimizedProxy() but, the proxy is serialized before it is
  //     * by App2.  Since a different classloader is using the proxy, the metod call should
  //     * be serialized and we should see that the set "value" is null.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetTransientWithSerializedNonOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Object proxy1 = createProxy(object1);
  //        Thread.currentThread().setContextClassLoader(cl2);
  //        proxy1 = new BytesMarshalledObject(proxy1).get();
  //
  //        Class class2 = cl2.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, null);
  //    }
  //
  //    /**
  //     * Does a reflexive call on object1 my calling method with the provided args.
  //     * 
  //     * @param object1
  //     * @param string
  //     * @param objects
  //     */
  //    private Object call(Object object1, String method, Object[] args) throws Throwable {
  //        try {
  //            Class argTypes[] = getArgTypes(object1.getClass().getClassLoader(), args);
  //            Method m = findMethod(object1.getClass(), method);
  //            return m.invoke(object1, args);
  //        } catch (InvocationTargetException e) {
  //            throw e.getTargetException();
  //        }
  //    }
  //
  //    /**
  //     * Gets the Class[] for a given Object[] using the provided loader.
  //     * 
  //     * @param object1
  //     * @param args
  //     * @return
  //     */
  //    private Class[] getArgTypes(ClassLoader loader, Object[] args) throws Exception {
  //        Class rc[] = new Class[args.length];
  //        for (int i = 0; i < rc.length; i++) {
  //            rc[i] = loader.loadClass(args[i].getClass().getName());
  //        }
  //        return rc;
  //    }
  //
  //    /**
  //     * Finds the first method in class c whose name is name.
  //     * @param c
  //     * @param name
  //     * @return
  //     */
  //    private Method findMethod(Class c, String name) {
  //        Method[] methods = c.getMethods();
  //        for (int i = 0; i < methods.length; i++) {
  //            if (methods[i].getName().equals(name))
  //                return methods[i];
  //        }
  //        return null;
  //    }
  //
  //    /**
  //      * @param object1
  //      * @return
  //      */
  //    private Object createProxy(Object object1) throws Exception {
  //        
  //        ProxyContainer serverContainer = new ProxyContainer();        
  //
  //        DeMarshalingInterceptor dmi = new DeMarshalingInterceptor();
  //        dmi.setClassloader(object1.getClass().getClassLoader());
  //        Long dmiid = InterceptorRegistry.instance.register(dmi);
  //        serverContainer.addInterceptor(dmi);      
  //        Interceptor reflexive = new ReflexiveInterceptor(object1);
  //        serverContainer.addInterceptor(reflexive);
  //        severContainers.add(serverContainer);
  //        
  //        ProxyContainer clientContainer = new ProxyContainer();        
  //        IntraVMRoutingInterceptor ivmri = new IntraVMRoutingInterceptor();
  //        ivmri.setDeMarshalingInterceptorID(dmiid);
  //        clientContainer.addInterceptor(ivmri);
  //
  //        return clientContainer.createProxy(object1.getClass());
  //    }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/Person.java
  
  Index: Person.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  package org.apache.geronimo.remoting;
  
  import java.io.Serializable;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  public class Person implements Serializable, IPerson {
  	
  	private String firstName;
  	private String lastName;
  	private IPerson spouse;
  	private TransientValue value;
  	
     /**
      * @return
      */
     public TransientValue getValue() {
        return value;
     }
  
     /**
      * @param value
      */
     public void setValue(TransientValue value) {
        this.value = value;
     }
  
     /**
      * @return
      */
     public IPerson getSpouse() {
        return spouse;
     }
  
     /**
      * @param spouse
      */
     public void setSpouse(IPerson spouse) {
        this.spouse = spouse;
     }
  
     /**
      * @return
      */
     public String getFirstName() {
        return firstName;
     }
  
     /**
      * @param firstName
      */
     public void setFirstName(String firstName) {
        this.firstName = firstName;
     }
  
     /**
      * @return
      */
     public String getLastName() {
        return lastName;
     }
  
     /**
      * @param lastName
      */
     public void setLastName(String lastName) {
        this.lastName = lastName;
     }
  
  	public static void main(String[] args) {
        Person p = new Person();
        p.setSpouse(p);
     }
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/RemotingInterceptorsTest.java
  
  Index: RemotingInterceptorsTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.remoting;
  
  import java.lang.reflect.InvocationTargetException;
  import java.lang.reflect.Method;
  import java.net.URI;
  import java.net.URL;
  import java.net.URLClassLoader;
  import java.util.ArrayList;
  
  import junit.framework.TestCase;
  
  import org.apache.geronimo.common.Interceptor;
  import org.apache.geronimo.proxy.ProxyContainer;
  import org.apache.geronimo.proxy.ReflexiveInterceptor;
  import org.apache.geronimo.remoting.transport.BytesMarshalledObject;
  import org.apache.geronimo.remoting.transport.FragmentBasedInterceptorRouter;
  import org.apache.geronimo.remoting.transport.RemoteTransportInterceptor;
  import org.apache.geronimo.remoting.transport.TransportFactory;
  import org.apache.geronimo.remoting.transport.TransportServer;
  import org.apache.geronimo.remoting.transport.URISupport;
  
  /**
   * Unit test for the Marshaling/DeMarshaling Interceptors
   *
   * This test uses 2 classloaders to mock 2 seperate
   * application classloaders.
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  
  public class RemotingInterceptorsTest extends TestCase {
  
      private static final String PERSON_CLASS = "org.apache.geronimo.remoting.Person";
      private static final String TRANSIENT_CLASS = "org.apache.geronimo.remoting.TransientValue";
      ArrayList severContainers = new ArrayList();
      URLClassLoader cl1, cl2;
      
      TransportServer server;
      URI connectURI;    
  
  //    /**
  //     * creates the two classloaders that will be used during the tests.
  //     */
  //    public void setUp() throws Exception {
  //        URL url = new URL(System.getProperty("test-app.url"));
  //        System.out.println("Setting up the CP to: " + url);
  //
  //        cl1 = new URLClassLoader(new URL[] { url });
  //        cl2 = new URLClassLoader(new URL[] { url });
  //
  //        System.out.println("================================================");
  //        URI bindURI = new URI("async://0.0.0.0:0");
  //        TransportFactory tf = TransportFactory.getTransportFactory(bindURI);
  //        server = tf.createSever();
  //        server.bind(bindURI,new FragmentBasedInterceptorRouter());
  //        connectURI = server.getClientConnectURI();
  //        server.start();
  //
  //    }
  //    
  //    /**
  //     * @see junit.framework.TestCase#tearDown()
  //     */
  //    protected void tearDown() throws Exception {
  //        server.dispose();
  //    }    
  
      // This is here until we can enable the tests.
      public void testNull() {        
      }
  
  //    /**
  //     * Verify that the classpath/classloader structure is
  //     * as expected.  If the application classes are in
  //     * the current classloader then that will throw all the 
  //     * tests off.
  //     * 
  //     * @throws Exception
  //     */
  //    public void testCheckClassLoaders() throws Exception {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //        assertFalse("Classpath for this test was incorrect.  The '"+PERSON_CLASS+"' cannot be in the test's classpath.", class1.equals(class2));
  //    }
  //
  //    /**
  //     * Verify that the helper methods used by this test work
  //     * as expected.  Everything should be ok when working in the
  //     * same classloader.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseSameCL() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl1.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        call(object1, "setSpouse", new Object[] { object2 });
  //    }
  //
  //    /**
  //     * Verify that the helper methods used by this test work
  //     * as expected.  We should see problems when you start mixing
  //     * Objects from different classloaders.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseOtherCL() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        try {
  //            call(object1, "setSpouse", new Object[] { object2 });
  //            fail("Call should fail due to argument type mismatch.");
  //        } catch (IllegalArgumentException e) {
  //        }
  //    }
  //
  //    /**
  //     * Verify that a proxy using working in a single app context works ok.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseWithProxy() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        // Simulate App1 creating a proxy.
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Object proxy1 = createProxy(object1);
  //        call(proxy1, "setSpouse", new Object[] { object1 });
  //    }
  //
  //    /**
  //     * App2 creates a proxy and serializes.  App1 context deserializes
  //     * the proxy and trys to use it.  Method calls should be getting
  //     * marshalled/demarshaled by the proxy to avoid getting an 
  //     * IllegalArgumentException.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetSpouseOtherCLWithSerializedProxy() throws Throwable {
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Class class2 = cl2.loadClass(PERSON_CLASS);
  //
  //        Object object1 = class1.newInstance();
  //        Object object2 = class2.newInstance();
  //
  //        // Simulate App2 creating a proxy.
  //        Thread.currentThread().setContextClassLoader(cl2);
  //        Object proxy2 = createProxy(object2);
  //        MarshalledObject mo = new BytesMarshalledObject(proxy2);
  //
  //        // Simulate App1 using the serialized proxy.    
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Object proxy1 = mo.get();
  //        call(proxy1, "setSpouse", new Object[] { object1 });
  //    }
  //
  //    /**
  //     * App1 creates a proxy.  It then creates a Transient calls that holds
  //     * a "value" property that is transient when serialized.  This allows to 
  //     * to see if the Transient object was serialized by getting the value back
  //     * and seeing if the "value" is null.  All this work is done in one classloader
  //     * so no serialization should occur.
  //     * 
  //     * @throws Throwable
  //     */
  //    /* Disable until proxy knows how to do a local optimize.
  //    public void testSetTransientWithOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Class class2 = cl1.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        Object proxy1 = createProxy(object1);
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, "foo");
  //    }
  //    */
  //    
  //    /**
  //     * Same as testSetTransientWithOptimizedProxy() but, the proxy is serialized before it is used.
  //     * It should still result in the method call not being serialized.
  //     * 
  //     * @throws Throwable
  //     */
  //    /* Disable until proxy knows how to do a local optimize.
  //    public void testSetTransientWithSerializedOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Class class2 = cl1.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        Object proxy1 = createProxy(object1);
  //        proxy1 = new BytesMarshalledObject(proxy1).get();
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, "foo");
  //    }
  //    */
  //    
  //    /**
  //     * Same as testSetTransientWithOptimizedProxy() but, the proxy is serialized before it is
  //     * by App2.  Since a different classloader is using the proxy, the metod call should
  //     * be serialized and we should see that the set "value" is null.
  //     * 
  //     * @throws Throwable
  //     */
  //    public void testSetTransientWithSerializedNonOptimizedProxy() throws Throwable {
  //        Thread.currentThread().setContextClassLoader(cl1);
  //        Class class1 = cl1.loadClass(PERSON_CLASS);
  //        Object object1 = class1.newInstance();
  //
  //        Object proxy1 = createProxy(object1);
  //        Thread.currentThread().setContextClassLoader(cl2);
  //        proxy1 = new BytesMarshalledObject(proxy1).get();
  //
  //        Class class2 = cl2.loadClass(TRANSIENT_CLASS);
  //        Object object2 = class2.newInstance();
  //        call(object2, "setValue", new Object[] { "foo" });
  //
  //        call(proxy1, "setValue", new Object[] { object2 });
  //        Object rc = call(proxy1, "getValue", new Object[] {
  //        });
  //        rc = call(rc, "getValue", new Object[] {
  //        });
  //
  //        assertSame(rc, null);
  //    }
  //
  //    /**
  //     * Does a reflexive call on object1 my calling method with the provided args.
  //     * 
  //     * @param object1
  //     * @param string
  //     * @param objects
  //     */
  //    private Object call(Object object1, String method, Object[] args) throws Throwable {
  //        try {
  //            Class argTypes[] = getArgTypes(object1.getClass().getClassLoader(), args);
  //            Method m = findMethod(object1.getClass(), method);
  //            return m.invoke(object1, args);
  //        } catch (InvocationTargetException e) {
  //            throw e.getTargetException();
  //        }
  //    }
  //
  //    /**
  //     * Gets the Class[] for a given Object[] using the provided loader.
  //     * 
  //     * @param object1
  //     * @param args
  //     * @return
  //     */
  //    private Class[] getArgTypes(ClassLoader loader, Object[] args) throws Exception {
  //        Class rc[] = new Class[args.length];
  //        for (int i = 0; i < rc.length; i++) {
  //            rc[i] = loader.loadClass(args[i].getClass().getName());
  //        }
  //        return rc;
  //    }
  //
  //    /**
  //     * Finds the first method in class c whose name is name.
  //     * @param c
  //     * @param name
  //     * @return
  //     */
  //    private Method findMethod(Class c, String name) {
  //        Method[] methods = c.getMethods();
  //        for (int i = 0; i < methods.length; i++) {
  //            if (methods[i].getName().equals(name))
  //                return methods[i];
  //        }
  //        return null;
  //    }
  //
  //    /**
  //      * @param object1
  //      * @return
  //      */
  //    private Object createProxy(Object object1) throws Exception {
  //        
  //        ProxyContainer serverContainer = new ProxyContainer();        
  //
  //        DeMarshalingInterceptor dmi = new DeMarshalingInterceptor();
  //        dmi.setClassloader(object1.getClass().getClassLoader());
  //        Long dmiid = InterceptorRegistry.instance.register(dmi);
  //        serverContainer.addInterceptor(dmi);      
  //        Interceptor reflexive = new ReflexiveInterceptor(object1);
  //        serverContainer.addInterceptor(reflexive);
  //        severContainers.add(serverContainer);
  //        
  //        ProxyContainer clientContainer = new ProxyContainer();
  //        clientContainer.addInterceptor(new MarshalingInterceptor());
  //        RemoteTransportInterceptor rti = new RemoteTransportInterceptor();
  //        URI u = URISupport.setFragment(connectURI, ""+dmiid);        
  //        rti.setRemoteURI(u);
  //        clientContainer.addInterceptor(rti);
  //        
  //        return clientContainer.createProxy(object1.getClass());
  //    }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/TransientValue.java
  
  Index: TransientValue.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  package org.apache.geronimo.remoting;
  
  import java.io.Serializable;
  
  /**
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  public class TransientValue implements Serializable {
  	transient public Object value;
  	
     /**
      * @return
      */
     public Object getValue() {
        return value;
     }
  
     /**
      * @param value
      */
     public void setValue(Object value) {
        this.value = value;
     }
  
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/transport/AsyncTransportStress.java
  
  Index: AsyncTransportStress.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.remoting.transport;
  
  import java.net.URI;
  
  import junit.framework.TestCase;
  
  import org.apache.geronimo.remoting.MarshalledObject;
  
  import EDU.oswego.cs.dl.util.concurrent.CyclicBarrier;
  import EDU.oswego.cs.dl.util.concurrent.Semaphore;
  
  /**
   * Unit test for the Async Remoting Transport
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  
  public class AsyncTransportStress extends TestCase {
  
      TransportServer server;
      TransportClient client;
      URI connectURI;
      volatile MockRouter mockRouter;
  
      class MockRouter implements Router {
          
          Semaphore requestCounter=new Semaphore(0);
          Semaphore datagramCounter=new Semaphore(0);
  
          public Msg sendRequest(URI to, Msg request) throws TransportException {
              try {
                  requestCounter.release();
  
                  Msg response = request.createMsg();
                  response.pushMarshaledObject(request.popMarshaledObject());
                  return response;
  
              } catch (Exception e) {
                  throw new TransportException(e.getMessage());
              }
          }
  
          /**
           * @see org.apache.geronimo.remoting.transport.Router#sendDatagram(java.net.URI, org.apache.geronimo.remoting.transport.Msg)
           */
          public void sendDatagram(URI to, Msg request) throws TransportException {
              datagramCounter.release();
          }
      }
  
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception {
          System.out.println("================================================");
          URI bindURI = new URI("async://0.0.0.0:0");
          TransportFactory tf = TransportFactory.getTransportFactory(bindURI);
          server = tf.createSever();
          mockRouter = new MockRouter();
          server.bind(bindURI,mockRouter);
          connectURI = server.getClientConnectURI();
          server.start();
          client = tf.createClient();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      protected void tearDown() throws Exception {
          server.dispose();
      }
  
      public void testConcurrentRequests() throws Exception {
          
          final int WORKERS = 100;
          final int MESSAGE_COUNT=10;
          final CyclicBarrier barrier = new CyclicBarrier(WORKERS); 
  
          for( int i=0; i < WORKERS; i++ ) {                
          
              new Thread() {
                  /**
                   * @see java.lang.Thread#run()
                   */
                  public void run() {
                      try {
                          String text = "Hello World";
                          MarshalledObject object = client.createMarshalledObject();
                          object.set(text);
                          Msg msg = client.createMsg();
                          msg.pushMarshaledObject(object);
                                  
                          barrier.barrier();
                          
                          for(int i=0; i < MESSAGE_COUNT; i++ )         
                              client.sendRequest(connectURI,msg);
                          
                          
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }.start();
          }
          
          for( int j=0; j < WORKERS; j++ ) {                
              for(int i=0; i < MESSAGE_COUNT; i++ ) {
                  boolean b = mockRouter.requestCounter.attempt(30000000);         
                  if( !b )
                      fail("test timed out");
              }
          }
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/test/org/apache/geronimo/remoting/transport/AsyncTransportTest.java
  
  Index: AsyncTransportTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.remoting.transport;
  
  import java.net.URI;
  
  import junit.framework.TestCase;
  
  import org.apache.geronimo.remoting.MarshalledObject;
  
  import EDU.oswego.cs.dl.util.concurrent.Semaphore;
  
  /**
   * Unit test for the Async Remoting Transport
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/22 02:23:36 $
   */
  
  public class AsyncTransportTest extends TestCase {
  
      TransportServer server;
      TransportClient client;
      URI connectURI;
      MockRouter mockRouter;
  
      class MockRouter implements Router {
          
          Semaphore requestCounter=new Semaphore(0);
          Semaphore datagramCounter=new Semaphore(0);
  
          public Msg sendRequest(URI to, Msg request) throws TransportException {
              try {
                  requestCounter.release();
  
                  Msg response = request.createMsg();
                  response.pushMarshaledObject(request.popMarshaledObject());
                  return response;
  
              } catch (Exception e) {
                  throw new TransportException(e.getMessage());
              }
          }
  
          /**
           * @see org.apache.geronimo.remoting.transport.Router#sendDatagram(java.net.URI, org.apache.geronimo.remoting.transport.Msg)
           */
          public void sendDatagram(URI to, Msg request) throws TransportException {
              datagramCounter.release();
          }
      }
  
      /**
       * @see junit.framework.TestCase#setUp()
       */
      protected void setUp() throws Exception {
          System.out.println("================================================");
          URI bindURI = new URI("async://0.0.0.0:0");
          TransportFactory tf = TransportFactory.getTransportFactory(bindURI);
          server = tf.createSever();
          mockRouter = new MockRouter();
          server.bind(bindURI,mockRouter);
          connectURI = server.getClientConnectURI();
          server.start();
          client = tf.createClient();
      }
  
      /**
       * @see junit.framework.TestCase#tearDown()
       */
      protected void tearDown() throws Exception {
          server.dispose();
      }
  
      public void testRequestMessage() throws Exception {
          String text = "Hello World";
          
          MarshalledObject object = client.createMarshalledObject();
          object.set(text);
          Msg msg = client.createMsg();
          msg.pushMarshaledObject(object);
          
          Msg response = client.sendRequest(connectURI,msg);
          Object rc = response.popMarshaledObject().get();
          
          assertEquals(rc,text);
          assertTrue(mockRouter.requestCounter.permits()==1);
          assertTrue(mockRouter.datagramCounter.permits()==0);
      }
  
      public void testDatagramMessage() throws Exception {
  
          String text = "Hello World";
          
          MarshalledObject object = client.createMarshalledObject();
          object.set(text);
          Msg msg = client.createMsg();
          msg.pushMarshaledObject(object);
          
          client.sendDatagram(connectURI,msg);        
          assertTrue(mockRouter.datagramCounter.attempt(10000));
      }
  }