    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * 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.codehaus.activemq.util;
    19
    20  import junit.framework.TestCase;
    21  import org.codehaus.activemq.transport.ember.EmberTransportChannelFactory;
    22  import org.codehaus.activemq.transport.ember.EmberTransportServerChannelFactory;
    23  import org.codehaus.activemq.transport.gnet.GTransportChannelFactory;
    24  import org.codehaus.activemq.transport.gnet.GTransportServerChannelFactory;
    25  import org.codehaus.activemq.transport.jrms.JRMSTransportChannelFactory;
    26  import org.codehaus.activemq.transport.jrms.JRMSTransportServerChannelFactory;
    27  import org.codehaus.activemq.transport.jxta.JxtaTransportChannelFactory;
    28  import org.codehaus.activemq.transport.jxta.JxtaTransportServerChannelFactory;
    29  import org.codehaus.activemq.transport.multicast.MulticastTransportChannelFactory;
    30  import org.codehaus.activemq.transport.multicast.MulticastTransportServerChannelFactory;
    31  import org.codehaus.activemq.transport.ssl.SslTransportChannelFactory;
    32  import org.codehaus.activemq.transport.ssl.SslTransportServerChannelFactory;
    33  import org.codehaus.activemq.transport.tcp.TcpTransportChannelFactory;
    34  import org.codehaus.activemq.transport.tcp.TcpTransportServerChannelFactory;
    35  import org.codehaus.activemq.transport.udp.UdpTransportChannelFactory;
    36  import org.codehaus.activemq.transport.udp.UdpTransportServerChannelFactory;
    37  import org.codehaus.activemq.transport.vm.VmTransportChannelFactory;
    38  import org.codehaus.activemq.transport.vm.VmTransportServerChannelFactory;
    39
    40  /***
    41   * @version $Revision: 1.1 $
    42   */
    43  public class FactoryFinderTest extends TestCase {
    44      FactoryFinder clientFinder = new FactoryFinder("META-INF/services/org/codehaus/activemq/transport/");
    45      FactoryFinder serverFinder = new FactoryFinder("META-INF/services/org/codehaus/activemq/transport/server/");
    46
    47      public void testClientFactory() throws Exception {
    48          assertClient("gnet", GTransportChannelFactory.class);
    49          assertClient("jrms", JRMSTransportChannelFactory.class);
    50          assertClient("jxta", JxtaTransportChannelFactory.class);
    51          assertClient("multicast", MulticastTransportChannelFactory.class);
    52          assertClient("nio", EmberTransportChannelFactory.class);
    53          assertClient("ssl", SslTransportChannelFactory.class);
    54          assertClient("tcp", TcpTransportChannelFactory.class);
    55          assertClient("udp", UdpTransportChannelFactory.class);
    56          assertClient("vm", VmTransportChannelFactory.class);
    57      }
    58
    59      public void testServerFactory() throws Exception {
    60          assertServer("gnet", GTransportServerChannelFactory.class);
    61          assertServer("jrms", JRMSTransportServerChannelFactory.class);
    62          assertServer("jxta", JxtaTransportServerChannelFactory.class);
    63          assertServer("multicast", MulticastTransportServerChannelFactory.class);
    64          assertServer("nio", EmberTransportServerChannelFactory.class);
    65          assertServer("ssl", SslTransportServerChannelFactory.class);
    66          assertServer("tcp", TcpTransportServerChannelFactory.class);
    67          assertServer("udp", UdpTransportServerChannelFactory.class);
    68          assertServer("vm", VmTransportServerChannelFactory.class);
    69      }
    70
    71      protected void assertClient(String key, Class type) throws Exception {
    72          assertFinder(key, type, clientFinder);
    73      }
    74
    75      protected void assertServer(String key, Class type) throws Exception {
    76          assertFinder(key, type, serverFinder);
    77      }
    78
    79      protected void assertFinder(String key, Class type, FactoryFinder finder) throws Exception {
    80          Object factory = finder.newInstance(key);
    81          assertTrue("Returned non null object", factory != null);
    82          assertTrue("Is an instance of: " + type, type.isInstance(factory));
    83
    84          // lets do it one more time to test the cache returns the same value
    85          factory = finder.newInstance(key);
    86          assertTrue("Is an instance of: " + type, type.isInstance(factory));
    87      }
    88  }