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