    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.config;
    19
    20  import junit.framework.TestCase;
    21  import org.codehaus.activemq.XmlConfigHelper;
    22  import org.codehaus.activemq.service.RedeliveryPolicy;
    23  import org.codehaus.activemq.broker.Broker;
    24  import org.codehaus.activemq.broker.BrokerContainer;
    25  import org.codehaus.activemq.broker.BrokerContainerFactory;
    26  import org.codehaus.activemq.broker.BrokerContext;
    27  import org.codehaus.activemq.broker.impl.BrokerConnectorImpl;
    28  import org.codehaus.activemq.security.SecurityAdapter;
    29  import org.codehaus.activemq.spring.ActiveMQBeanDefinitionReader;
    30  import org.codehaus.activemq.spring.ActiveMQBeanFactory;
    31  import org.codehaus.activemq.spring.ActiveMQDtdResolver;
    32  import org.codehaus.activemq.spring.SpringBrokerContainerFactory;
    33  import org.codehaus.activemq.transport.NetworkChannel;
    34  import org.codehaus.activemq.transport.NetworkConnector;
    35  import org.codehaus.activemq.transport.TransportServerChannel;
    36  import org.codehaus.activemq.transport.tcp.TcpTransportServerChannel;
    37  import org.codehaus.activemq.transport.vm.VmTransportServerChannel;
    38  import org.springframework.beans.factory.xml.XmlBeanFactory;
    39  import org.springframework.core.io.ClassPathResource;
    40  import org.springframework.core.io.FileSystemResource;
    41  import org.springframework.core.io.Resource;
    42  import org.w3c.dom.Document;
    43
    44  import javax.jms.JMSException;
    45  import javax.xml.parsers.DocumentBuilder;
    46  import javax.xml.parsers.DocumentBuilderFactory;
    47  import javax.xml.transform.Transformer;
    48  import javax.xml.transform.dom.DOMSource;
    49  import javax.xml.transform.stream.StreamResult;
    50  import javax.xml.transform.stream.StreamSource;
    51  import java.io.File;
    52  import java.util.List;
    53
    54  /***
    55   * @version $Revision: 1.17 $
    56   */
    57  public class ConfigTest extends TestCase {
    58      public void testConfig() {
    59          ActiveMQBeanFactory factory = new ActiveMQBeanFactory("Cheese", new ClassPathResource("org/codehaus/activemq/config/example.xml"));
    60
    61          Object value = factory.getBean("broker");
    62
    63          assertTrue("Should have created a broker!", value != null);
    64          assertTrue("Should be a broker container: " + value, value instanceof BrokerContainer);
    65
    66          BrokerContainer container = (BrokerContainer) value;
    67          Broker broker = container.getBroker();
    68          assertTrue("Should have a broker!", broker != null);
    69
    70          assertEquals("Broker name not set!", "localhost", broker.getBrokerName());
    71
    72          Object transport = factory.getBean("transport");
    73          assertTrue("Made transport", transport != null);
    74
    75          List connectors = container.getTransportConnectors();
    76          assertEquals("Should have created more connectors", 3, connectors.size());
    77
    78          BrokerConnectorImpl connector1 = (BrokerConnectorImpl) connectors.get(0);
    79          TransportServerChannel serverChannel1 = connector1.getServerChannel();
    80          assertTrue(serverChannel1 instanceof VmTransportServerChannel);
    81
    82          BrokerConnectorImpl connector2 = (BrokerConnectorImpl) connectors.get(1);
    83          TransportServerChannel serverChannel2 = connector2.getServerChannel();
    84          assertTrue(serverChannel2 instanceof TcpTransportServerChannel);
    85          TcpTransportServerChannel tcpChannel2 = (TcpTransportServerChannel) serverChannel2;
    86          assertEquals("backlog", 1000, tcpChannel2.getBacklog());
    87          assertEquals("maxOutstandingMessages", 50, tcpChannel2.getMaxOutstandingMessages());
    88          assertTrue("useAsyncSend", tcpChannel2.isUseAsyncSend());
    89
    90
    91          List networkConnectors = container.getNetworkConnectors();
    92          assertEquals("Should have a single network connector", 1, networkConnectors.size());
    93          NetworkConnector networkConnector = (NetworkConnector) networkConnectors.get(0);
    94
    95          List networkChannels = networkConnector.getNetworkChannels();
    96          assertEquals("Should have 2 network channels", 2, networkChannels.size());
    97
    98          NetworkChannel networkChannel = (NetworkChannel) networkChannels.get(0);
    99          assertEquals("URL not equal", "tcp://somehost:61616", networkChannel.getUri());
    100         assertEquals("remoteUserName", "James", networkChannel.getRemoteUserName());
    101         assertEquals("remotePassword", "Stewey", networkChannel.getRemotePassword());
    102
    103         System.out.println("Created network channel: " + networkChannel);
    104
    105         SecurityAdapter securityAdapter = broker.getSecurityAdapter();
    106         assertTrue("Should have created a security adapter", securityAdapter != null);
    107
    108         RedeliveryPolicy redeliveryPolicy = broker.getRedeliveryPolicy();
    109         assertTrue("Should have created a redeliveryPolicy", redeliveryPolicy != null);
    110         assertEquals("isBackOffMode", true, redeliveryPolicy.isBackOffMode());
    111         assertEquals("getMaximumRetryCount", 10, redeliveryPolicy.getMaximumRetryCount());
    112     }
    113
    114     public void testBerkeleyDBConfig() throws JMSException {
    115         createBroker(new FileSystemResource("src/sample-conf/bdb-example.xml"));
    116
    117         File file = new File("target/berkeleydb");
    118         assertTrue("Created the file for the persistent store: " + file, file.exists());
    119     }
    120
    121
    122     public void testHowlConfig() throws JMSException {
    123         createBroker(new FileSystemResource("src/sample-conf/howl-example.xml"));
    124
    125         File file = new File("target/howlTest/berkeleydb");
    126         assertTrue("Created the file for the persistent store: " + file, file.exists());
    127     }
    128
    129     public void testJdbmConfig() throws JMSException {
    130         createBroker(new FileSystemResource("src/sample-conf/jdbm-example.xml"));
    131
    132         File file = new File("target/XmlTest/jdbm");
    133         assertTrue("Created the file for the persistent store: " + file, file.exists());
    134     }
    135
    136     public void testVmConfig() throws JMSException {
    137         createBroker(new FileSystemResource("src/sample-conf/vm-example.xml"));
    138     }
    139
    140     public void testTransform() throws Exception {
    141         ClassPathResource resource = new ClassPathResource("org/codehaus/activemq/activemq-to-spring.xsl");
    142         StreamSource source = new StreamSource(resource.getInputStream(), resource.getURL().toString());
    143         Transformer transformer = ActiveMQBeanDefinitionReader.createTransformer(source);
    144
    145         DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    146         builder.setEntityResolver(new ActiveMQDtdResolver());
    147         Document document = builder.parse(new ClassPathResource("org/codehaus/activemq/config/example.xml").getFile());
    148
    149         transformer.transform(new DOMSource(document), new StreamResult(new File("target/example-spring.xml")));
    150     }
    151
    152     public void testSpring() throws Exception {
    153         XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("org/codehaus/activemq/config/spring-test.xml"));
    154
    155         Object transport = factory.getBean("transport");
    156         assertTrue("Made transport", transport != null);
    157
    158         System.out.println("Created transport: " + transport);
    159     }
    160
    161     public void testXmlConfigHelper() throws Exception {
    162         createBroker("file:src/sample-conf/vm-example.xml");
    163         createBroker("org/codehaus/activemq/config/config.xml");
    164     }
    165
    166     protected BrokerContainer createBroker(String resource) throws JMSException {
    167         BrokerContainerFactory factory = XmlConfigHelper.createBrokerContainerFactory(resource);
    168         return createBroker(factory);
    169     }
    170
    171     protected BrokerContainer createBroker(Resource resource) throws JMSException {
    172         SpringBrokerContainerFactory factory = new SpringBrokerContainerFactory();
    173         factory.setResource(resource);
    174         return createBroker(factory);
    175
    176     }
    177
    178     protected BrokerContainer createBroker(BrokerContainerFactory factory) throws JMSException {
    179         assertTrue("Factory is null", factory != null);
    180         String brokerName = getName();
    181         BrokerContainer container = factory.createBrokerContainer(brokerName, BrokerContext.getInstance());
    182
    183         assertTrue("Should have a broker container!", container != null);
    184
    185         Broker broker = container.getBroker();
    186         assertTrue("Should have a broker!", broker != null);
    187
    188         assertEquals("Broker name not set!", brokerName, broker.getBrokerName());
    189
    190         container.start();
    191         container.stop();
    192         return container;
    193     }
    194 }