1 18 package org.apache.activemq.config; 19 20 import java.io.File ; 21 import java.util.List ; 22 23 import junit.framework.TestCase; 24 25 import org.apache.activemq.broker.BrokerService; 26 import org.apache.activemq.broker.TransportConnector; 27 import org.apache.activemq.broker.region.policy.FixedSizedSubscriptionRecoveryPolicy; 28 import org.apache.activemq.broker.region.policy.LastImageSubscriptionRecoveryPolicy; 29 import org.apache.activemq.broker.region.policy.NoSubscriptionRecoveryPolicy; 30 import org.apache.activemq.broker.region.policy.RoundRobinDispatchPolicy; 31 import org.apache.activemq.broker.region.policy.SimpleDispatchPolicy; 32 import org.apache.activemq.broker.region.policy.StrictOrderDispatchPolicy; 33 import org.apache.activemq.broker.region.policy.SubscriptionRecoveryPolicy; 34 import org.apache.activemq.broker.region.policy.TimedSubscriptionRecoveryPolicy; 35 import org.apache.activemq.command.ActiveMQTopic; 36 import org.apache.activemq.memory.UsageManager; 37 import org.apache.activemq.store.PersistenceAdapter; 38 import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter; 39 import org.apache.activemq.store.journal.JournalPersistenceAdapter; 40 import org.apache.activemq.store.memory.MemoryPersistenceAdapter; 41 import org.apache.activemq.transport.tcp.TcpTransportServer; 42 import org.apache.activemq.wireformat.ObjectStreamWireFormat; 43 import org.apache.activemq.xbean.BrokerFactoryBean; 44 import org.apache.commons.logging.Log; 45 import org.apache.commons.logging.LogFactory; 46 import org.apache.derby.jdbc.EmbeddedDataSource; 47 import org.springframework.core.io.ClassPathResource; 48 import org.springframework.core.io.FileSystemResource; 49 import org.springframework.core.io.Resource; 50 51 54 public class ConfigTest extends TestCase { 55 56 private static final Log log = LogFactory.getLog(ConfigTest.class); 57 58 protected static final String JOURNAL_ROOT = "target/test-data/"; 59 protected static final String DERBY_ROOT = "target/test-data/"; 60 protected static final String CONF_ROOT = "src/test/resources/org/apache/activemq/config/sample-conf/"; 61 62 static { 63 System.setProperty("javax.net.ssl.trustStore", "src/test/resources/client.keystore"); 64 System.setProperty("javax.net.ssl.trustStorePassword", "password"); 65 System.setProperty("javax.net.ssl.trustStoreType", "jks"); 66 System.setProperty("javax.net.ssl.keyStore", "src/test/resources/server.keystore"); 67 System.setProperty("javax.net.ssl.keyStorePassword", "password"); 68 System.setProperty("javax.net.ssl.keyStoreType", "jks"); 69 } 70 71 76 77 80 public void testJournaledJDBCConfig() throws Exception { 81 83 File journalFile = new File (JOURNAL_ROOT + "testJournaledJDBCConfig/journal"); 84 recursiveDelete(journalFile); 85 86 File derbyFile = new File (DERBY_ROOT + "testJournaledJDBCConfig/derbydb"); recursiveDelete(derbyFile); 88 89 BrokerService broker; 90 broker = createBroker(new FileSystemResource(CONF_ROOT + "journaledjdbc-example.xml")); 91 try { 92 assertEquals("Broker Config Error (brokerName)", "brokerJournaledJDBCConfigTest", broker.getBrokerName()); 93 94 PersistenceAdapter adapter = broker.getPersistenceAdapter(); 95 96 assertTrue("Should have created a journal persistence adapter", adapter instanceof JournalPersistenceAdapter); 97 assertTrue("Should have created a derby directory at " + derbyFile.getAbsolutePath(), derbyFile.exists()); 98 assertTrue("Should have created a journal directory at " + journalFile.getAbsolutePath(), journalFile.exists()); 99 100 101 JournalPersistenceAdapter pa = (JournalPersistenceAdapter) broker.getPersistenceAdapter(); 104 105 log.info("Success"); 106 } finally { 107 if (broker != null) { 108 broker.stop(); 109 } 110 } 111 } 112 113 116 public void testJdbcConfig() throws Exception { 117 File derbyFile = new File (DERBY_ROOT + "testJDBCConfig/derbydb"); recursiveDelete(derbyFile); 120 121 BrokerService broker; 122 broker = createBroker(new FileSystemResource(CONF_ROOT + "jdbc-example.xml")); 123 try { 124 assertEquals("Broker Config Error (brokerName)", "brokerJdbcConfigTest", broker.getBrokerName()); 125 126 PersistenceAdapter adapter = broker.getPersistenceAdapter(); 127 128 assertTrue("Should have created a jdbc persistence adapter", adapter instanceof JDBCPersistenceAdapter); 129 assertEquals("JDBC Adapter Config Error (cleanupPeriod)", 60000, 130 ((JDBCPersistenceAdapter)adapter).getCleanupPeriod()); 131 assertTrue("Should have created an EmbeddedDataSource", 132 ((JDBCPersistenceAdapter)adapter).getDataSource() instanceof EmbeddedDataSource); 133 assertTrue("Should have created a DefaultWireFormat", 134 ((JDBCPersistenceAdapter)adapter).getWireFormat() instanceof ObjectStreamWireFormat); 135 136 log.info("Success"); 137 } finally { 138 if (broker != null) { 139 broker.stop(); 140 } 141 } 142 } 143 144 147 public void testBrokerConfig() throws Exception { 148 ActiveMQTopic dest; 149 BrokerService broker; 150 151 File journalFile = new File (JOURNAL_ROOT); 152 recursiveDelete(journalFile); 153 154 broker = createBroker("org/apache/activemq/config/example.xml"); 157 log.info("Success"); 158 159 try { 160 assertEquals("Broker Config Error (brokerName)", "brokerConfigTest", broker.getBrokerName()); 163 assertEquals("Broker Config Error (populateJMSXUserID)", false, broker.isPopulateJMSXUserID()); 164 assertEquals("Broker Config Error (useLoggingForShutdownErrors)", true, broker.isUseLoggingForShutdownErrors()); 165 assertEquals("Broker Config Error (useJmx)", true, broker.isUseJmx()); 166 assertEquals("Broker Config Error (persistent)", false, broker.isPersistent()); 167 assertEquals("Broker Config Error (useShutdownHook)", false, broker.isUseShutdownHook()); 168 assertEquals("Broker Config Error (deleteAllMessagesOnStartup)", true, broker.isDeleteAllMessagesOnStartup()); 169 log.info("Success"); 170 171 assertEquals("Should have a specific VM Connector", "vm://javacoola", broker.getVmConnectorURI().toString()); 174 log.info("Success"); 175 176 List connectors = broker.getTransportConnectors(); 179 assertTrue("Should have created at least 3 connectors", (connectors.size() >= 3)); 180 assertTrue ("1st connector should be TcpTransportServer", ((TransportConnector)connectors.get(0)).getServer() instanceof TcpTransportServer); 181 assertTrue ("2nd connector should be TcpTransportServer", ((TransportConnector)connectors.get(1)).getServer() instanceof TcpTransportServer); 182 assertTrue ("3rd connector should be TcpTransportServer", ((TransportConnector)connectors.get(2)).getServer() instanceof TcpTransportServer); 183 184 List networkConnectors = broker.getNetworkConnectors(); 187 assertEquals("Should have a single network connector", 1, networkConnectors.size()); 188 log.info("Success"); 189 190 193 dest = new ActiveMQTopic("Topic.SimpleDispatch"); 194 assertTrue("Should have a simple dispatch policy for " + dest.getTopicName(), 195 broker.getDestinationPolicy().getEntryFor(dest).getDispatchPolicy() instanceof SimpleDispatchPolicy); 196 197 dest = new ActiveMQTopic("Topic.RoundRobinDispatch"); 198 assertTrue("Should have a round robin dispatch policy for " + dest.getTopicName(), 199 broker.getDestinationPolicy().getEntryFor(dest).getDispatchPolicy() instanceof RoundRobinDispatchPolicy); 200 201 dest = new ActiveMQTopic("Topic.StrictOrderDispatch"); 202 assertTrue("Should have a strict order dispatch policy for " + dest.getTopicName(), 203 broker.getDestinationPolicy().getEntryFor(dest).getDispatchPolicy() instanceof StrictOrderDispatchPolicy); 204 log.info("Success"); 205 206 SubscriptionRecoveryPolicy subsPolicy; 209 210 dest = new ActiveMQTopic("Topic.FixedSizedSubs"); 211 subsPolicy = broker.getDestinationPolicy().getEntryFor(dest).getSubscriptionRecoveryPolicy(); 212 assertTrue("Should have a fixed sized subscription recovery policy for " + dest.getTopicName(), 213 subsPolicy instanceof FixedSizedSubscriptionRecoveryPolicy); 214 assertEquals("FixedSizedSubsPolicy Config Error (maximumSize)", 2000000, 215 ((FixedSizedSubscriptionRecoveryPolicy)subsPolicy).getMaximumSize()); 216 assertEquals("FixedSizedSubsPolicy Config Error (useSharedBuffer)", false, 217 ((FixedSizedSubscriptionRecoveryPolicy)subsPolicy).isUseSharedBuffer()); 218 219 dest = new ActiveMQTopic("Topic.LastImageSubs"); 220 subsPolicy = broker.getDestinationPolicy().getEntryFor(dest).getSubscriptionRecoveryPolicy(); 221 assertTrue("Should have a last image subscription recovery policy for " + dest.getTopicName(), 222 subsPolicy instanceof LastImageSubscriptionRecoveryPolicy); 223 224 dest = new ActiveMQTopic("Topic.NoSubs"); 225 subsPolicy = broker.getDestinationPolicy().getEntryFor(dest).getSubscriptionRecoveryPolicy(); 226 assertTrue("Should have no subscription recovery policy for " + dest.getTopicName(), 227 subsPolicy instanceof NoSubscriptionRecoveryPolicy); 228 229 dest = new ActiveMQTopic("Topic.TimedSubs"); 230 subsPolicy = broker.getDestinationPolicy().getEntryFor(dest).getSubscriptionRecoveryPolicy(); 231 assertTrue("Should have a timed subscription recovery policy for " + dest.getTopicName(), 232 subsPolicy instanceof TimedSubscriptionRecoveryPolicy); 233 assertEquals("TimedSubsPolicy Config Error (recoverDuration)", 25000, 234 ((TimedSubscriptionRecoveryPolicy)subsPolicy).getRecoverDuration()); 235 log.info("Success"); 236 237 UsageManager memMgr = broker.getMemoryManager(); 240 assertTrue("Should have a memory manager", memMgr != null); 241 assertEquals("UsageManager Config Error (limit)", 200000, memMgr.getLimit()); 242 assertEquals("UsageManager Config Error (percentUsageMinDelta)", 20, memMgr.getPercentUsageMinDelta()); 243 log.info("Success"); 244 245 log.info("Success"); 246 } finally { 247 if (broker != null) { 248 broker.stop(); 249 } 250 } 251 } 252 253 256 public void testJournalConfig() throws Exception { 257 259 File journalFile = new File (JOURNAL_ROOT + "testJournalConfig/journal"); 260 recursiveDelete(journalFile); 261 262 BrokerService broker; 263 broker = createBroker(new FileSystemResource(CONF_ROOT + "journal-example.xml")); 264 try { 265 assertEquals("Broker Config Error (brokerName)", "brokerJournalConfigTest", broker.getBrokerName()); 266 267 PersistenceAdapter adapter = broker.getPersistenceAdapter(); 268 269 assertTrue("Should have created a journal persistence adapter", adapter instanceof JournalPersistenceAdapter); 270 assertTrue("Should have created a journal directory at " + journalFile.getAbsolutePath(), journalFile.exists()); 271 272 log.info("Success"); 273 } finally { 274 if (broker != null) { 275 broker.stop(); 276 } 277 } 278 } 279 280 283 public void testMemoryConfig() throws Exception { 284 286 File journalFile = new File (JOURNAL_ROOT + "testMemoryConfig"); 287 recursiveDelete(journalFile); 288 289 File derbyFile = new File (DERBY_ROOT + "testMemoryConfig"); 290 recursiveDelete(derbyFile); 291 292 BrokerService broker; 293 broker = createBroker(new FileSystemResource(CONF_ROOT + "memory-example.xml")); 294 295 try { 296 assertEquals("Broker Config Error (brokerName)", "brokerMemoryConfigTest", broker.getBrokerName()); 297 298 PersistenceAdapter adapter = broker.getPersistenceAdapter(); 299 300 assertTrue("Should have created a memory persistence adapter", adapter instanceof MemoryPersistenceAdapter); 301 assertTrue("Should have not created a derby directory at " + derbyFile.getAbsolutePath(), !derbyFile.exists()); 302 assertTrue("Should have not created a journal directory at " + journalFile.getAbsolutePath(), !journalFile.exists()); 303 304 log.info("Success"); 305 } finally { 306 if (broker != null) { 307 broker.stop(); 308 } 309 } 310 311 } 312 313 public void testXmlConfigHelper() throws Exception { 314 BrokerService broker; 315 316 broker = createBroker(new FileSystemResource(CONF_ROOT + "memory-example.xml")); 317 try { 318 assertEquals("Broker Config Error (brokerName)", "brokerMemoryConfigTest", broker.getBrokerName()); 319 } finally { 320 if (broker != null) { 321 broker.stop(); 322 } 323 } 324 325 broker = createBroker("org/apache/activemq/config/config.xml"); 326 try { 327 assertEquals("Broker Config Error (brokerName)", "brokerXmlConfigHelper", broker.getBrokerName()); 328 } finally { 329 if (broker != null) { 330 broker.stop(); 331 } 332 } 333 } 334 335 338 339 protected static void recursiveDelete(File file) { 340 if( file.isDirectory() ) { 341 File [] files = file.listFiles(); 342 for (int i = 0; i < files.length; i++) { 343 recursiveDelete(files[i]); 344 } 345 } 346 file.delete(); 347 } 348 349 protected BrokerService createBroker(String resource) throws Exception { 350 return createBroker(new ClassPathResource(resource)); 351 } 352 353 protected BrokerService createBroker(Resource resource) throws Exception { 354 BrokerFactoryBean factory = new BrokerFactoryBean(resource); 355 factory.afterPropertiesSet(); 356 357 BrokerService broker = factory.getBroker(); 358 359 assertTrue("Should have a broker!", broker != null); 360 361 364 return broker; 365 } 366 } 367 | Popular Tags |