001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.resource.activemq;
018:
019: import java.net.URI;
020: import java.net.URISyntaxException;
021: import java.util.Collections;
022: import java.util.Hashtable;
023: import java.util.Map;
024: import java.util.Properties;
025: import javax.naming.Context;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import javax.naming.spi.InitialContextFactory;
029: import javax.sql.DataSource;
030:
031: import junit.framework.TestCase;
032: import org.apache.activemq.broker.BrokerFactory;
033: import org.apache.activemq.broker.BrokerService;
034: import org.apache.activemq.broker.TransportConnector;
035: import org.apache.activemq.network.jms.JmsConnector;
036: import org.apache.activemq.store.PersistenceAdapter;
037: import org.apache.activemq.store.jdbc.JDBCPersistenceAdapter;
038: import org.apache.activemq.store.memory.MemoryPersistenceAdapter;
039: import org.apache.openejb.util.URISupport;
040: import org.apache.openejb.core.CoreContainerSystem;
041: import org.apache.openejb.loader.SystemInstance;
042: import org.apache.openejb.spi.ContainerSystem;
043: import org.apache.xbean.naming.context.ImmutableContext;
044: import org.hsqldb.jdbc.jdbcDataSource;
045:
046: public class OpenEjbBrokerFactoryTest extends TestCase {
047: public void testBrokerUri() throws Exception {
048: assertEquals(
049: "openejb:broker:(tcp://localhost:61616)?persistent=false",
050: getBrokerUri("broker:(tcp://localhost:61616)"));
051: assertEquals(
052: "openejb:broker:(tcp://localhost:61616)?useJmx=false&persistent=false",
053: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false"));
054: assertEquals(
055: "openejb:broker:(tcp://localhost:61616)?useJmx=false&persistent=false",
056: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false&persistent=true"));
057: assertEquals(
058: "openejb:broker:(tcp://localhost:61616)?useJmx=false&persistent=false",
059: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false&persistent=false"));
060: }
061:
062: private String getBrokerUri(String brokerUri)
063: throws URISyntaxException {
064: URISupport.CompositeData compositeData = URISupport
065: .parseComposite(new URI(brokerUri));
066: compositeData.getParameters().put("persistent", "false");
067: return "openejb:" + compositeData.toURI();
068: }
069:
070: public void testBrokerDoubleCreate() throws Exception {
071: BrokerService broker = BrokerFactory
072: .createBroker(new URI(
073: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false")));
074: stopBroker(broker);
075:
076: broker = BrokerFactory
077: .createBroker(new URI(
078: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false")));
079: stopBroker(broker);
080:
081: }
082:
083: public void testNoDataSource() throws Exception {
084: BrokerService broker = BrokerFactory
085: .createBroker(new URI(
086: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false")));
087: assertNotNull("broker is null", broker);
088:
089: PersistenceAdapter persistenceAdapter = broker
090: .getPersistenceAdapter();
091: assertNotNull("persistenceAdapter is null", persistenceAdapter);
092:
093: assertTrue(
094: "persistenceAdapter should be an instance of MemoryPersistenceAdapter",
095: persistenceAdapter instanceof MemoryPersistenceAdapter);
096:
097: stopBroker(broker);
098: }
099:
100: public void testDirectDataSource() throws Exception {
101: Properties properties = new Properties();
102:
103: DataSource dataSource = new jdbcDataSource();
104: properties.put("DataSource", dataSource);
105:
106: OpenEjbBrokerFactory.setThreadProperties(properties);
107: BrokerService broker = null;
108: try {
109: broker = BrokerFactory
110: .createBroker(new URI(
111: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false")));
112: assertNotNull("broker is null", broker);
113:
114: PersistenceAdapter persistenceAdapter = broker
115: .getPersistenceAdapter();
116: assertNotNull("persistenceAdapter is null",
117: persistenceAdapter);
118:
119: assertTrue(
120: "persistenceAdapter should be an instance of JDBCPersistenceAdapter",
121: persistenceAdapter instanceof JDBCPersistenceAdapter);
122: JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;
123:
124: assertSame(dataSource, jdbcPersistenceAdapter
125: .getDataSource());
126: } finally {
127: stopBroker(broker);
128: OpenEjbBrokerFactory.setThreadProperties(null);
129: }
130: }
131:
132: public void testLookupDataSource() throws Exception {
133: Properties properties = new Properties();
134:
135: DataSource dataSource = new jdbcDataSource();
136: MockInitialContextFactory.install(Collections.singletonMap(
137: "java:openejb/Resource/TestDs", dataSource));
138: assertSame(dataSource, new InitialContext()
139: .lookup("java:openejb/Resource/TestDs"));
140:
141: CoreContainerSystem containerSystem = new CoreContainerSystem();
142: containerSystem.getJNDIContext().bind(
143: "java:openejb/Resource/TestDs", dataSource);
144: SystemInstance.get().setComponent(ContainerSystem.class,
145: containerSystem);
146:
147: properties.put("DataSource", "TestDs");
148:
149: OpenEjbBrokerFactory.setThreadProperties(properties);
150: BrokerService broker = null;
151: try {
152: broker = BrokerFactory
153: .createBroker(new URI(
154: getBrokerUri("broker:(tcp://localhost:61616)?useJmx=false")));
155: assertNotNull("broker is null", broker);
156:
157: PersistenceAdapter persistenceAdapter = broker
158: .getPersistenceAdapter();
159: assertNotNull("persistenceAdapter is null",
160: persistenceAdapter);
161:
162: assertTrue(
163: "persistenceAdapter should be an instance of JDBCPersistenceAdapter",
164: persistenceAdapter instanceof JDBCPersistenceAdapter);
165: JDBCPersistenceAdapter jdbcPersistenceAdapter = (JDBCPersistenceAdapter) persistenceAdapter;
166:
167: assertSame(dataSource, jdbcPersistenceAdapter
168: .getDataSource());
169: } finally {
170: stopBroker(broker);
171: OpenEjbBrokerFactory.setThreadProperties(null);
172: }
173: }
174:
175: public static class MockInitialContextFactory implements
176: InitialContextFactory {
177: private static ImmutableContext immutableContext;
178:
179: public static void install(Map bindings) throws NamingException {
180: immutableContext = new ImmutableContext(bindings);
181: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
182: MockInitialContextFactory.class.getName());
183: new InitialContext();
184: }
185:
186: public Context getInitialContext(Hashtable<?, ?> environment)
187: throws NamingException {
188: return immutableContext;
189: }
190: }
191:
192: private void stopBroker(BrokerService broker) throws Exception {
193: if (broker == null)
194: return;
195:
196: if (broker.getJmsBridgeConnectors() != null) {
197: for (JmsConnector connector : broker
198: .getJmsBridgeConnectors()) {
199: connector.stop();
200: }
201: }
202: for (Object o : broker.getTransportConnectors()) {
203: TransportConnector tc = (TransportConnector) o;
204: tc.stop();
205:
206: }
207: broker.stop();
208: }
209:
210: }
|