KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > config > ConfigTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.config;
19
20 import java.io.File JavaDoc;
21 import java.util.List JavaDoc;
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 /**
52  * @version $Revision: 473224 $
53  */

54 public class ConfigTest extends TestCase {
55     
56     private static final Log log = LogFactory.getLog(ConfigTest.class);
57     
58     protected static final String JavaDoc JOURNAL_ROOT = "target/test-data/";
59     protected static final String JavaDoc DERBY_ROOT = "target/test-data/";
60     protected static final String JavaDoc 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     /*
72      * IMPORTANT NOTE: Assertions checking for the existence of the derby directory will fail if the first derby
73      * directory is not created under target/test-data/. The test in unable to change the derby
74      * root directory for succeeding creation. It uses the first created directory as the root.
75      */

76
77     /*
78      * This tests creating a journal persistence adapter using the persistence adapter factory bean
79      */

80     public void testJournaledJDBCConfig() throws Exception JavaDoc {
81 // System.out.print("Checking journaled JDBC persistence adapter configuration... ");
82

83         File JavaDoc journalFile = new File JavaDoc(JOURNAL_ROOT + "testJournaledJDBCConfig/journal");
84         recursiveDelete(journalFile);
85
86         File JavaDoc derbyFile = new File JavaDoc(DERBY_ROOT + "testJournaledJDBCConfig/derbydb"); // Default derby name
87
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             // Check persistence factory configurations
102
// System.out.print("Checking persistence adapter factory settings... ");
103
JournalPersistenceAdapter pa = (JournalPersistenceAdapter) broker.getPersistenceAdapter();
104             
105             log.info("Success");
106         } finally {
107             if (broker != null) {
108                 broker.stop();
109             }
110         }
111     }
112
113     /*
114      * This tests creating a jdbc persistence adapter using xbeans-spring
115      */

116     public void testJdbcConfig() throws Exception JavaDoc {
117 // System.out.print("Checking jdbc persistence adapter configuration... ");
118
File JavaDoc derbyFile = new File JavaDoc(DERBY_ROOT + "testJDBCConfig/derbydb"); // Default derby name
119
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     /*
145      * This tests configuring the different broker properties using xbeans-spring
146      */

147     public void testBrokerConfig() throws Exception JavaDoc {
148         ActiveMQTopic dest;
149         BrokerService broker;
150
151         File JavaDoc journalFile = new File JavaDoc(JOURNAL_ROOT);
152         recursiveDelete(journalFile);
153
154         // Create broker from resource
155
// System.out.print("Creating broker... ");
156
broker = createBroker("org/apache/activemq/config/example.xml");
157         log.info("Success");
158
159         try {
160             // Check broker configuration
161
// System.out.print("Checking broker configurations... ");
162
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             // Check specific vm transport
172
// System.out.print("Checking vm connector... ");
173
assertEquals("Should have a specific VM Connector", "vm://javacoola", broker.getVmConnectorURI().toString());
174             log.info("Success");
175
176             // Check transport connectors list
177
// System.out.print("Checking transport connectors... ");
178
List JavaDoc 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             // Check network connectors
185
// System.out.print("Checking network connectors... ");
186
List JavaDoc networkConnectors = broker.getNetworkConnectors();
187             assertEquals("Should have a single network connector", 1, networkConnectors.size());
188             log.info("Success");
189
190             // Check dispatch policy configuration
191
// System.out.print("Checking dispatch policies... ");
192

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             // Check subscription policy configuration
207
// System.out.print("Checking subscription recovery policies... ");
208
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             // Check usage manager
238
// System.out.print("Checking memory manager configurations... ");
239
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     /*
254      * This tests creating a journal persistence adapter using xbeans-spring
255      */

256     public void testJournalConfig() throws Exception JavaDoc {
257 // System.out.print("Checking journal persistence adapter configuration... ");
258

259         File JavaDoc journalFile = new File JavaDoc(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     /*
281      * This tests creating a memory persistence adapter using xbeans-spring
282      */

283     public void testMemoryConfig() throws Exception JavaDoc {
284 // System.out.print("Checking memory persistence adapter configuration... ");
285

286         File JavaDoc journalFile = new File JavaDoc(JOURNAL_ROOT + "testMemoryConfig");
287         recursiveDelete(journalFile);
288
289         File JavaDoc derbyFile = new File JavaDoc(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 JavaDoc {
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     /*
336      * TODO: Create additional tests for forwarding bridges
337      */

338
339     protected static void recursiveDelete(File JavaDoc file) {
340         if( file.isDirectory() ) {
341             File JavaDoc[] 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 JavaDoc resource) throws Exception JavaDoc {
350         return createBroker(new ClassPathResource(resource));
351     }
352
353     protected BrokerService createBroker(Resource resource) throws Exception JavaDoc {
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         //Broker is already started by default when using the XML file
362
// broker.start();
363

364         return broker;
365     }
366 }
367
Popular Tags