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.assembler.classic;
018:
019: import junit.framework.TestCase;
020: import org.apache.openejb.config.ConfigurationFactory;
021: import org.apache.openejb.config.EjbModule;
022: import org.apache.openejb.jee.ActivationConfig;
023: import org.apache.openejb.jee.ActivationConfigProperty;
024: import org.apache.openejb.jee.EjbJar;
025: import org.apache.openejb.jee.MessageDrivenBean;
026: import org.apache.openejb.test.mdb.BasicMdbBean;
027:
028: import javax.jms.MessageListener;
029: import javax.resource.spi.ResourceAdapter;
030: import javax.resource.spi.BootstrapContext;
031: import javax.resource.spi.ResourceAdapterInternalException;
032: import javax.resource.spi.ActivationSpec;
033: import javax.resource.spi.UnavailableException;
034: import javax.resource.spi.InvalidPropertyException;
035: import javax.resource.spi.endpoint.MessageEndpointFactory;
036: import javax.resource.spi.endpoint.MessageEndpoint;
037: import javax.resource.ResourceException;
038: import javax.transaction.xa.XAResource;
039: import java.util.Properties;
040:
041: /**
042: * @version $Rev: 610927 $ $Date: 2008-01-10 12:48:37 -0800 $
043: */
044: public class MdbConfigTest extends TestCase {
045: public void test() throws Exception {
046: ConfigurationFactory config = new ConfigurationFactory();
047: Assembler assembler = new Assembler();
048:
049: // System services
050: assembler.createProxyFactory(config
051: .configureService(ProxyFactoryInfo.class));
052: assembler.createTransactionManager(config
053: .configureService(TransactionServiceInfo.class));
054: assembler.createSecurityService(config
055: .configureService(SecurityServiceInfo.class));
056:
057: // JMS persistence datasource
058: ResourceInfo dataSourceInfo = config.configureService(
059: "Default Unmanaged JDBC Database", ResourceInfo.class);
060: dataSourceInfo.properties.setProperty("JdbcUrl",
061: "jdbc:hsqldb:mem:MdbConfigTest");
062: assembler.createResource(dataSourceInfo);
063:
064: // JMS
065: assembler.createResource(config.configureService(
066: "Default JMS Resource Adapter", ResourceInfo.class));
067:
068: // JMS Container
069: MdbContainerInfo mdbContainerInfo = config
070: .configureService(MdbContainerInfo.class);
071: assembler.createContainer(mdbContainerInfo);
072:
073: // FakeRA
074: ResourceInfo resourceInfo = new ResourceInfo();
075: resourceInfo.service = "Resource";
076: resourceInfo.className = FakeRA.class.getName();
077: resourceInfo.id = "FakeRA";
078: resourceInfo.properties = new Properties();
079: assembler.createResource(resourceInfo);
080:
081: // FakeRA container
082: ContainerInfo containerInfo = config
083: .configureService(MdbContainerInfo.class);
084: containerInfo.id = "FakeContainer";
085: containerInfo.displayName = "Fake Container";
086: containerInfo.properties.setProperty("ResourceAdapter",
087: "FakeRA");
088: containerInfo.properties.setProperty(
089: "MessageListenerInterface", FakeMessageListener.class
090: .getName());
091: containerInfo.properties.setProperty("ActivationSpecClass",
092: FakeActivationSpec.class.getName());
093: assembler.createContainer(containerInfo);
094:
095: // generate ejb jar application
096: EjbJar ejbJar = new EjbJar();
097: ejbJar.addEnterpriseBean(createJaxbMdb("JmsMdb",
098: BasicMdbBean.class.getName(), MessageListener.class
099: .getName()));
100: ejbJar.addEnterpriseBean(createJaxbMdb("FakeMdb", FakeMdb.class
101: .getName(), FakeMessageListener.class.getName()));
102: EjbModule ejbModule = new EjbModule(
103: getClass().getClassLoader(), "FakeEjbJar", "fake.jar",
104: ejbJar, null);
105:
106: // configure and deploy it
107: EjbJarInfo info = config.configureApplication(ejbModule);
108: assembler.createEjbJar(info);
109: }
110:
111: public static class FakeMdb implements FakeMessageListener {
112: public void doIt(Properties properties) {
113: }
114: }
115:
116: public static interface FakeMessageListener {
117: public void doIt(Properties properties);
118: }
119:
120: public static class FakeRA implements ResourceAdapter {
121: public boolean started;
122:
123: public void start(BootstrapContext bootstrapContext)
124: throws ResourceAdapterInternalException {
125: assertFalse("Already started", started);
126: assertNotNull("bootstrapContext is null", bootstrapContext);
127: assertNotNull("bootstrapContext.getWorkManager() is null",
128: bootstrapContext.getWorkManager());
129: assertNotNull("bootstrapContext.getXATerminator() is null",
130: bootstrapContext.getXATerminator());
131: try {
132: assertNotNull("bootstrapContext.createTimer() is null",
133: bootstrapContext.createTimer());
134: } catch (UnavailableException e) {
135: throw new ResourceAdapterInternalException(
136: "bootstrapContext.createTimer() threw an exception",
137: e);
138: }
139: }
140:
141: public void stop() {
142: assertTrue("RA was not started", started);
143: }
144:
145: public void endpointActivation(
146: MessageEndpointFactory messageEndpointFactory,
147: ActivationSpec activationSpec) throws ResourceException {
148: assertNotNull("messageEndpointFactory is null",
149: messageEndpointFactory);
150: assertNotNull("activationSpec is null", activationSpec);
151: assertTrue(
152: "activationSpec should be an instance of FakeActivationSpec",
153: activationSpec instanceof FakeActivationSpec);
154:
155: MessageEndpoint endpoint = messageEndpointFactory
156: .createEndpoint(null);
157: assertNotNull("endpoint is null", endpoint);
158: assertTrue(
159: "endpoint should be an instance of FakeMessageListener",
160: endpoint instanceof FakeMessageListener);
161: }
162:
163: public void endpointDeactivation(
164: MessageEndpointFactory messageEndpointFactory,
165: ActivationSpec activationSpec) {
166: assertNotNull("messageEndpointFactory is null",
167: messageEndpointFactory);
168: assertNotNull("activationSpec is null", activationSpec);
169: assertTrue(
170: "activationSpec should be an instance of FakeActivationSpec",
171: activationSpec instanceof FakeActivationSpec);
172: }
173:
174: public XAResource[] getXAResources(
175: ActivationSpec[] activationSpecs)
176: throws ResourceException {
177: return new XAResource[0];
178: }
179: }
180:
181: public static class FakeActivationSpec implements ActivationSpec {
182: private FakeRA fakeRA;
183: protected boolean validated;
184:
185: public void validate() throws InvalidPropertyException {
186: validated = true;
187: }
188:
189: public FakeRA getResourceAdapter() {
190: return fakeRA;
191: }
192:
193: public void setResourceAdapter(ResourceAdapter resourceAdapter) {
194: assertNotNull("resourceAdapter is null", resourceAdapter);
195: assertTrue(
196: "resourceAdapter should be an instance of FakeRA",
197: resourceAdapter instanceof FakeRA);
198: this .fakeRA = (FakeRA) resourceAdapter;
199: assertTrue("ActivationSpec has not been validated",
200: validated);
201: }
202: }
203:
204: private MessageDrivenBean createJaxbMdb(String ejbName,
205: String mdbClass, String messageListenerInterface) {
206: MessageDrivenBean bean = new MessageDrivenBean(ejbName);
207: bean.setEjbClass(mdbClass);
208: bean.setMessagingType(messageListenerInterface);
209:
210: ActivationConfig activationConfig = new ActivationConfig();
211: activationConfig.getActivationConfigProperty().add(
212: new ActivationConfigProperty("destination", ejbName));
213: activationConfig.getActivationConfigProperty().add(
214: new ActivationConfigProperty("destinationType",
215: "javax.jms.Queue"));
216: bean.setActivationConfig(activationConfig);
217:
218: return bean;
219: }
220: }
|