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.test.mdb;
018:
019: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
020: import org.apache.openejb.test.stateful.BasicStatefulHome;
021: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
022: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
023: import org.apache.openejb.test.stateless.BasicStatelessHome;
024: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
025: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
026: import org.apache.openejb.test.TestFailureException;
027:
028: import javax.ejb.EJB;
029: import javax.ejb.MessageDriven;
030: import javax.ejb.MessageDrivenContext;
031: import javax.ejb.EJBException;
032: import javax.ejb.ActivationConfigProperty;
033: import javax.annotation.Resource;
034: import javax.annotation.PostConstruct;
035: import javax.sql.DataSource;
036: import javax.jms.ConnectionFactory;
037: import javax.jms.QueueConnectionFactory;
038: import javax.jms.TopicConnectionFactory;
039: import javax.jms.JMSException;
040: import javax.jms.Session;
041: import javax.jms.Topic;
042: import javax.jms.MessageProducer;
043: import javax.jms.MessageListener;
044: import javax.jms.Message;
045: import javax.persistence.PersistenceUnit;
046: import javax.persistence.EntityManagerFactory;
047: import javax.persistence.PersistenceContext;
048: import javax.persistence.EntityManager;
049:
050: import junit.framework.Assert;
051: import junit.framework.AssertionFailedError;
052:
053: @MessageDriven(activationConfig={@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue"),@ActivationConfigProperty(propertyName="destination",propertyValue="AnnotatedFieldInjectionMdb")})
054: public class AnnotatedFieldInjectionMdbBean implements MessageListener {
055: @Resource
056: private MessageDrivenContext mdbContext;
057: @EJB(beanName="BasicBmpBean")
058: private BasicBmpHome bmpHome;
059: @EJB(beanName="BasicStatefulBean")
060: private BasicStatefulHome statefulHome;
061: @EJB(beanName="BasicStatelessBean")
062: private BasicStatelessHome statelessHome;
063: @Resource
064: private String striing = "1";
065: @Resource
066: private Double doouble = 1.0D;
067: @Resource
068: private Long loong = 1L;
069: @Resource
070: private Float flooat = 1.0F;
071: @Resource
072: private Integer inteeger = 1;
073: @Resource
074: private Short shoort = (short) 1;
075: @Resource
076: private Boolean booolean = true;
077: @Resource
078: private Byte byyte = (byte) 1;
079: @Resource
080: private Character chaaracter = 'D';
081: @Resource
082: private DataSource daataSource;
083: @Resource
084: private ConnectionFactory coonnectionFactory;
085: @Resource
086: private QueueConnectionFactory queueCoonnectionFactory;
087: @Resource
088: private TopicConnectionFactory topicCoonnectionFactory;
089: @PersistenceUnit(unitName="openjpa-test-unit")
090: private EntityManagerFactory emf;
091: @PersistenceContext(unitName="openjpa-test-unit")
092: private EntityManager em;
093: @EJB
094: private BasicStatelessBusinessLocal statelessBusinessLocal;
095: @EJB
096: private BasicStatelessBusinessRemote statelessBusinessRemote;
097: @EJB
098: private BasicStatefulBusinessLocal statefulBusinessLocal;
099: @EJB
100: private BasicStatefulBusinessRemote statefulBusinessRemote;
101:
102: private MdbInvoker mdbInvoker;
103:
104: @PostConstruct
105: public void start() {
106: try {
107: mdbInvoker = new MdbInvoker(coonnectionFactory, this );
108: } catch (Exception e) {
109: throw new EJBException(e);
110: }
111: }
112:
113: public void onMessage(Message message) {
114: try {
115: // System.out.println("\n" +
116: // "***************************************\n" +
117: // "Got message: " + message + "\n" +
118: // "***************************************\n\n");
119: try {
120: message.acknowledge();
121: } catch (JMSException e) {
122: e.printStackTrace();
123: }
124: mdbInvoker.onMessage(message);
125: } catch (Throwable e) {
126: e.printStackTrace();
127: }
128: }
129:
130: public void lookupEntityBean() throws TestFailureException {
131: try {
132: Assert.assertNotNull("The EJBObject is null", bmpHome);
133: } catch (AssertionFailedError afe) {
134: throw new TestFailureException(afe);
135: }
136: }
137:
138: public void lookupStatefulBean() throws TestFailureException {
139: try {
140: Assert.assertNotNull("The EJBObject is null", statefulHome);
141: } catch (AssertionFailedError afe) {
142: throw new TestFailureException(afe);
143: }
144: }
145:
146: public void lookupStatelessBean() throws TestFailureException {
147: try {
148: Assert
149: .assertNotNull("The EJBObject is null",
150: statelessHome);
151: } catch (AssertionFailedError afe) {
152: throw new TestFailureException(afe);
153: }
154: }
155:
156: public void lookupStatelessBusinessLocal()
157: throws TestFailureException {
158: try {
159: Assert.assertNotNull("The EJB BusinessLocal is null",
160: statelessBusinessLocal);
161: } catch (AssertionFailedError afe) {
162: throw new TestFailureException(afe);
163: }
164: }
165:
166: public void lookupStatelessBusinessRemote()
167: throws TestFailureException {
168: try {
169: Assert.assertNotNull("The EJB BusinessRemote is null",
170: statelessBusinessRemote);
171: } catch (AssertionFailedError afe) {
172: throw new TestFailureException(afe);
173: }
174: }
175:
176: public void lookupStatefulBusinessLocal()
177: throws TestFailureException {
178: try {
179: Assert.assertNotNull("The EJB BusinessLocal is null",
180: statefulBusinessLocal);
181: } catch (AssertionFailedError afe) {
182: throw new TestFailureException(afe);
183: }
184: }
185:
186: public void lookupStatefulBusinessRemote()
187: throws TestFailureException {
188: try {
189: Assert.assertNotNull("The EJB BusinessRemote is null",
190: statefulBusinessRemote);
191: } catch (AssertionFailedError afe) {
192: throw new TestFailureException(afe);
193: }
194: }
195:
196: public void lookupStringEntry() throws TestFailureException {
197: try {
198: String expected = new String("1");
199: Assert.assertNotNull("The String looked up is null",
200: striing);
201: Assert.assertEquals(expected, striing);
202: } catch (AssertionFailedError afe) {
203: throw new TestFailureException(afe);
204: }
205: }
206:
207: public void lookupDoubleEntry() throws TestFailureException {
208: try {
209: Double expected = new Double(1.0D);
210:
211: Assert.assertNotNull("The Double looked up is null",
212: doouble);
213: Assert.assertEquals(expected, doouble);
214:
215: } catch (AssertionFailedError afe) {
216: throw new TestFailureException(afe);
217: }
218: }
219:
220: public void lookupLongEntry() throws TestFailureException {
221: try {
222: Long expected = new Long(1L);
223:
224: Assert.assertNotNull("The Long looked up is null", loong);
225: Assert.assertEquals(expected, loong);
226: } catch (AssertionFailedError afe) {
227: throw new TestFailureException(afe);
228: }
229: }
230:
231: public void lookupFloatEntry() throws TestFailureException {
232: try {
233: Float expected = new Float(1.0F);
234:
235: Assert.assertNotNull("The Float looked up is null", flooat);
236: Assert.assertEquals(expected, flooat);
237: } catch (AssertionFailedError afe) {
238: throw new TestFailureException(afe);
239: }
240: }
241:
242: public void lookupIntegerEntry() throws TestFailureException {
243: try {
244: Integer expected = new Integer(1);
245:
246: Assert.assertNotNull("The Integer looked up is null",
247: inteeger);
248: Assert.assertEquals(expected, inteeger);
249:
250: } catch (AssertionFailedError afe) {
251: throw new TestFailureException(afe);
252: }
253: }
254:
255: public void lookupShortEntry() throws TestFailureException {
256: try {
257: Short expected = new Short((short) 1);
258:
259: Assert.assertNotNull("The Short looked up is null", shoort);
260: Assert.assertEquals(expected, shoort);
261: } catch (AssertionFailedError afe) {
262: throw new TestFailureException(afe);
263: }
264: }
265:
266: public void lookupBooleanEntry() throws TestFailureException {
267: try {
268: Boolean expected = new Boolean(true);
269:
270: Assert.assertNotNull("The Boolean looked up is null",
271: booolean);
272: Assert.assertEquals(expected, booolean);
273: } catch (AssertionFailedError afe) {
274: throw new TestFailureException(afe);
275: }
276: }
277:
278: public void lookupByteEntry() throws TestFailureException {
279: try {
280: Byte expected = new Byte((byte) 1);
281:
282: Assert.assertNotNull("The Byte looked up is null", byyte);
283: Assert.assertEquals(expected, byyte);
284: } catch (AssertionFailedError afe) {
285: throw new TestFailureException(afe);
286: }
287: }
288:
289: public void lookupCharacterEntry() throws TestFailureException {
290: try {
291: Character expected = new Character('D');
292:
293: Assert.assertNotNull("The Character looked up is null",
294: chaaracter);
295: Assert.assertEquals(expected, chaaracter);
296: } catch (AssertionFailedError afe) {
297: throw new TestFailureException(afe);
298: }
299: }
300:
301: public void lookupResource() throws TestFailureException {
302: try {
303: Assert.assertNotNull("The DataSource is null", daataSource);
304: } catch (AssertionFailedError afe) {
305: throw new TestFailureException(afe);
306: }
307: }
308:
309: public void lookupJMSConnectionFactory()
310: throws TestFailureException {
311: try {
312: try {
313: testJmsConnection(coonnectionFactory.createConnection());
314: testJmsConnection(queueCoonnectionFactory
315: .createConnection());
316: testJmsConnection(topicCoonnectionFactory
317: .createConnection());
318: } catch (Exception e) {
319: e.printStackTrace();
320: Assert.fail("Received Exception " + e.getClass()
321: + " : " + e.getMessage());
322: }
323: } catch (AssertionFailedError afe) {
324: throw new TestFailureException(afe);
325: }
326: }
327:
328: private void testJmsConnection(javax.jms.Connection connection)
329: throws JMSException {
330: Session session = connection.createSession(false,
331: Session.DUPS_OK_ACKNOWLEDGE);
332: Topic topic = session.createTopic("test");
333: MessageProducer producer = session.createProducer(topic);
334: producer.send(session.createMessage());
335: producer.close();
336: session.close();
337: connection.close();
338: }
339:
340: public void lookupPersistenceUnit() throws TestFailureException {
341: try {
342: Assert.assertNotNull("The EntityManagerFactory is null",
343: emf);
344: } catch (AssertionFailedError afe) {
345: throw new TestFailureException(afe);
346: }
347: }
348:
349: public void lookupPersistenceContext() throws TestFailureException {
350: try {
351: Assert.assertNotNull("The EntityManager is null", em);
352:
353: try {
354: // call a do nothing method to assure entity manager actually exists
355: em.getFlushMode();
356: } catch (Exception e) {
357: Assert.fail("Received Exception " + e.getClass()
358: + " : " + e.getMessage());
359: }
360: } catch (AssertionFailedError afe) {
361: throw new TestFailureException(afe);
362: }
363: }
364:
365: public void lookupMessageDrivenContext()
366: throws TestFailureException {
367: try {
368: Assert.assertNotNull("The MessageDrivenContext is null",
369: mdbContext);
370: } catch (AssertionFailedError afe) {
371: throw new TestFailureException(afe);
372: }
373:
374: }
375: }
|