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.broker.Broker; 22 import org.codehaus.activemq.broker.BrokerContainer; 23 import org.codehaus.activemq.broker.impl.BrokerConnectorImpl; 24 import org.codehaus.activemq.spring.ActiveMQBeanDefinitionReader; 25 import org.codehaus.activemq.spring.ActiveMQBeanFactory; 26 import org.codehaus.activemq.spring.ActiveMQDtdResolver; 27 import org.codehaus.activemq.spring.SpringBrokerContainerFactory; 28 import org.codehaus.activemq.transport.TransportServerChannel; 29 import org.codehaus.activemq.transport.tcp.TcpTransportServerChannel; 30 import org.codehaus.activemq.transport.vm.VmTransportServerChannel; 31 import org.springframework.beans.factory.xml.XmlBeanFactory; 32 import org.springframework.core.io.ClassPathResource; 33 import org.springframework.core.io.FileSystemResource; 34 import org.springframework.core.io.Resource; 35 import org.w3c.dom.Document; 36 37 import javax.jms.JMSException; 38 import javax.xml.parsers.DocumentBuilder; 39 import javax.xml.parsers.DocumentBuilderFactory; 40 import javax.xml.transform.Transformer; 41 import javax.xml.transform.dom.DOMSource; 42 import javax.xml.transform.stream.StreamResult; 43 import javax.xml.transform.stream.StreamSource; 44 import java.io.File; 45 import java.util.List; 46 47 /*** 48 * @version $Revision: 1.9 $ 49 */ 50 public class ConfigTest extends TestCase { 51 public void testConfig() { 52 ActiveMQBeanFactory factory = new ActiveMQBeanFactory("Cheese", new ClassPathResource("org/codehaus/activemq/config/example.xml")); 53 54 Object value = factory.getBean("broker"); 55 56 assertTrue("Should have created a broker!", value != null); 57 assertTrue("Should be a broker container: " + value, value instanceof BrokerContainer); 58 59 BrokerContainer container = (BrokerContainer) value; 60 Broker broker = container.getBroker(); 61 assertTrue("Should have a broker!", broker != null); 62 63 assertEquals("Broker name not set!", "localhost", broker.getBrokerName()); 64 65 Object transport = factory.getBean("transport"); 66 assertTrue("Made transport", transport != null); 67 68 List connectors = container.getConnectors(); 69 assertEquals("Should have created more connectors", 3, connectors.size()); 70 71 BrokerConnectorImpl connector1 = (BrokerConnectorImpl) connectors.get(0); 72 TransportServerChannel serverChannel1 = connector1.getServerChannel(); 73 assertTrue(serverChannel1 instanceof VmTransportServerChannel); 74 75 BrokerConnectorImpl connector2 = (BrokerConnectorImpl) connectors.get(1); 76 TransportServerChannel serverChannel2 = connector2.getServerChannel(); 77 assertTrue(serverChannel2 instanceof TcpTransportServerChannel); 78 TcpTransportServerChannel tcpChannel2 = (TcpTransportServerChannel) serverChannel2; 79 assertEquals("backlog", 1000, tcpChannel2.getBacklog()); 80 assertEquals("maxOutstandingMessages", 50, tcpChannel2.getMaxOutstandingMessages()); 81 assertTrue("useAsyncSend", tcpChannel2.isUseAsyncSend()); 82 } 83 84 public void testBerkeleyDBConfig() throws JMSException { 85 createBroker(new FileSystemResource("src/sample-conf/bdb-example.xml")); 86 87 File file = new File("target/berkeleydb"); 88 assertTrue("Created the file for the persistent store: " + file, file.exists()); 89 } 90 91 92 public void testHowlConfig() throws JMSException { 93 createBroker(new FileSystemResource("src/sample-conf/howl-example.xml")); 94 95 File file = new File("target/howlTest/berkeleydb"); 96 assertTrue("Created the file for the persistent store: " + file, file.exists()); 97 } 98 99 public void testJdbmConfig() throws JMSException { 100 createBroker(new FileSystemResource("src/sample-conf/jdbm-example.xml")); 101 102 File file = new File("target/XmlTest/jdbm"); 103 assertTrue("Created the file for the persistent store: " + file, file.exists()); 104 } 105 106 public void testVmConfig() throws JMSException { 107 createBroker(new FileSystemResource("src/sample-conf/vm-example.xml")); 108 } 109 110 public void testTransform() throws Exception { 111 ClassPathResource resource = new ClassPathResource("org/codehaus/activemq/activemq-to-spring.xsl"); 112 StreamSource source = new StreamSource(resource.getInputStream(), resource.getURL().toString()); 113 Transformer transformer = ActiveMQBeanDefinitionReader.createTransformer(source); 114 115 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 116 builder.setEntityResolver(new ActiveMQDtdResolver()); 117 Document document = builder.parse(new ClassPathResource("org/codehaus/activemq/config/example.xml").getFile()); 118 119 transformer.transform(new DOMSource(document), new StreamResult(new File("target/example-spring.xml"))); 120 } 121 122 public void testSpring() throws Exception { 123 XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("org/codehaus/activemq/config/spring-test.xml")); 124 125 Object transport = factory.getBean("transport"); 126 assertTrue("Made transport", transport != null); 127 128 System.out.println("Created transport: " + transport); 129 } 130 131 132 protected BrokerContainer createBroker(Resource resource) throws JMSException { 133 String brokerName = getName(); 134 SpringBrokerContainerFactory factory = new SpringBrokerContainerFactory(); 135 factory.setResource(resource); 136 BrokerContainer container = factory.createBrokerContainer(brokerName); 137 138 assertTrue("Should have a broker container!", container != null); 139 140 Broker broker = container.getBroker(); 141 assertTrue("Should have a broker!", broker != null); 142 143 assertEquals("Broker name not set!", brokerName, broker.getBrokerName()); 144 145 container.start(); 146 container.stop(); 147 return container; 148 149 } 150 }