001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.stateful;
017:
018: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
019: import org.apache.openejb.test.stateless.BasicStatelessHome;
020: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
021: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
022: import org.apache.openejb.test.TestFailureException;
023:
024: import javax.ejb.RemoteHome;
025: import javax.ejb.Stateless;
026: import javax.ejb.SessionContext;
027: import javax.ejb.EJB;
028: import javax.ejb.CreateException;
029: import javax.ejb.Init;
030: import javax.ejb.Remove;
031: import javax.annotation.Resource;
032: import javax.sql.DataSource;
033: import javax.persistence.PersistenceUnit;
034: import javax.persistence.EntityManagerFactory;
035: import javax.persistence.PersistenceContext;
036: import javax.persistence.EntityManager;
037: import javax.jms.ConnectionFactory;
038: import javax.jms.Session;
039: import javax.jms.Topic;
040: import javax.jms.MessageProducer;
041: import javax.jms.QueueConnectionFactory;
042: import javax.jms.TopicConnectionFactory;
043: import javax.jms.JMSException;
044:
045: import junit.framework.Assert;
046: import junit.framework.AssertionFailedError;
047:
048: @RemoteHome(org.apache.openejb.test.stateful.EncStatefulHome.class)
049: @Stateless
050: public class AnnotatedFieldInjectionStatefulBean {
051:
052: @Resource
053: private SessionContext ejbContext;
054: @EJB(beanName="BasicBmpBean")
055: private BasicBmpHome bmpHome;
056: @EJB(beanName="BasicStatefulBean")
057: private BasicStatefulHome statefulHome;
058: @EJB(beanName="BasicStatelessBean")
059: private BasicStatelessHome statelessHome;
060: @Resource
061: private String striing = "1";
062: @Resource
063: private Double doouble = 1.0D;
064: @Resource
065: private Long loong = 1L;
066: @Resource
067: private Float flooat = 1.0F;
068: @Resource
069: private Integer inteeger = 1;
070: @Resource
071: private Short shoort = (short) 1;
072: @Resource
073: private Boolean booolean = true;
074: @Resource
075: private Byte byyte = (byte) 1;
076: @Resource
077: private Character chaaracter = 'D';
078: @Resource
079: private DataSource daataSource;
080: @Resource
081: private ConnectionFactory coonnectionFactory;
082: @Resource
083: private QueueConnectionFactory queueCoonnectionFactory;
084: @Resource
085: private TopicConnectionFactory topicCoonnectionFactory;
086: @PersistenceUnit(unitName="openjpa-test-unit")
087: private EntityManagerFactory emf;
088: @PersistenceContext(unitName="openjpa-test-unit")
089: private EntityManager em;
090: @EJB
091: private BasicStatelessBusinessLocal statelessBusinessLocal;
092: @EJB
093: private BasicStatelessBusinessRemote statelessBusinessRemote;
094: @EJB
095: private BasicStatefulBusinessLocal statefulBusinessLocal;
096: @EJB
097: private BasicStatefulBusinessRemote statefulBusinessRemote;
098:
099: @Init
100: public void create(String name) {
101: }
102:
103: @Remove
104: public void remove() {
105: }
106:
107: public void lookupEntityBean() throws TestFailureException {
108: try {
109: Assert.assertNotNull("The EJBObject is null", bmpHome);
110: } catch (AssertionFailedError afe) {
111: throw new TestFailureException(afe);
112: }
113: }
114:
115: public void lookupStatefulBean() throws TestFailureException {
116: try {
117: Assert.assertNotNull("The EJBObject is null", statefulHome);
118: } catch (AssertionFailedError afe) {
119: throw new TestFailureException(afe);
120: }
121: }
122:
123: public void lookupStatelessBean() throws TestFailureException {
124: try {
125: Assert
126: .assertNotNull("The EJBObject is null",
127: statelessHome);
128: } catch (AssertionFailedError afe) {
129: throw new TestFailureException(afe);
130: }
131: }
132:
133: public void lookupStatelessBusinessLocal()
134: throws TestFailureException {
135: try {
136: Assert.assertNotNull("The EJB BusinessLocal is null",
137: statelessBusinessLocal);
138: } catch (AssertionFailedError afe) {
139: throw new TestFailureException(afe);
140: }
141: }
142:
143: public void lookupStatelessBusinessRemote()
144: throws TestFailureException {
145: try {
146: Assert.assertNotNull("The EJB BusinessRemote is null",
147: statelessBusinessRemote);
148: } catch (AssertionFailedError afe) {
149: throw new TestFailureException(afe);
150: }
151: }
152:
153: public void lookupStatefulBusinessLocal()
154: throws TestFailureException {
155: try {
156: Assert.assertNotNull("The EJB BusinessLocal is null",
157: statefulBusinessLocal);
158: } catch (AssertionFailedError afe) {
159: throw new TestFailureException(afe);
160: }
161: }
162:
163: public void lookupStatefulBusinessRemote()
164: throws TestFailureException {
165: try {
166: Assert.assertNotNull("The EJB BusinessRemote is null",
167: statefulBusinessRemote);
168: } catch (AssertionFailedError afe) {
169: throw new TestFailureException(afe);
170: }
171: }
172:
173: public void lookupStringEntry() throws TestFailureException {
174: try {
175: String expected = new String("1");
176: Assert.assertNotNull("The String looked up is null",
177: striing);
178: Assert.assertEquals(expected, striing);
179: } catch (AssertionFailedError afe) {
180: throw new TestFailureException(afe);
181: }
182: }
183:
184: public void lookupDoubleEntry() throws TestFailureException {
185: try {
186: Double expected = new Double(1.0D);
187:
188: Assert.assertNotNull("The Double looked up is null",
189: doouble);
190: Assert.assertEquals(expected, doouble);
191:
192: } catch (AssertionFailedError afe) {
193: throw new TestFailureException(afe);
194: }
195: }
196:
197: public void lookupLongEntry() throws TestFailureException {
198: try {
199: Long expected = new Long(1L);
200:
201: Assert.assertNotNull("The Long looked up is null", loong);
202: Assert.assertEquals(expected, loong);
203: } catch (AssertionFailedError afe) {
204: throw new TestFailureException(afe);
205: }
206: }
207:
208: public void lookupFloatEntry() throws TestFailureException {
209: try {
210: Float expected = new Float(1.0F);
211:
212: Assert.assertNotNull("The Float looked up is null", flooat);
213: Assert.assertEquals(expected, flooat);
214: } catch (AssertionFailedError afe) {
215: throw new TestFailureException(afe);
216: }
217: }
218:
219: public void lookupIntegerEntry() throws TestFailureException {
220: try {
221: Integer expected = new Integer(1);
222:
223: Assert.assertNotNull("The Integer looked up is null",
224: inteeger);
225: Assert.assertEquals(expected, inteeger);
226:
227: } catch (AssertionFailedError afe) {
228: throw new TestFailureException(afe);
229: }
230: }
231:
232: public void lookupShortEntry() throws TestFailureException {
233: try {
234: Short expected = new Short((short) 1);
235:
236: Assert.assertNotNull("The Short looked up is null", shoort);
237: Assert.assertEquals(expected, shoort);
238: } catch (AssertionFailedError afe) {
239: throw new TestFailureException(afe);
240: }
241: }
242:
243: public void lookupBooleanEntry() throws TestFailureException {
244: try {
245: Boolean expected = new Boolean(true);
246:
247: Assert.assertNotNull("The Boolean looked up is null",
248: booolean);
249: Assert.assertEquals(expected, booolean);
250: } catch (AssertionFailedError afe) {
251: throw new TestFailureException(afe);
252: }
253: }
254:
255: public void lookupByteEntry() throws TestFailureException {
256: try {
257: Byte expected = new Byte((byte) 1);
258:
259: Assert.assertNotNull("The Byte looked up is null", byyte);
260: Assert.assertEquals(expected, byyte);
261: } catch (AssertionFailedError afe) {
262: throw new TestFailureException(afe);
263: }
264: }
265:
266: public void lookupCharacterEntry() throws TestFailureException {
267: try {
268: Character expected = new Character('D');
269:
270: Assert.assertNotNull("The Character looked up is null",
271: chaaracter);
272: Assert.assertEquals(expected, chaaracter);
273: } catch (AssertionFailedError afe) {
274: throw new TestFailureException(afe);
275: }
276: }
277:
278: public void lookupResource() throws TestFailureException {
279: try {
280: Assert.assertNotNull("The DataSource is null", daataSource);
281: } catch (AssertionFailedError afe) {
282: throw new TestFailureException(afe);
283: }
284: }
285:
286: public void lookupJMSConnectionFactory()
287: throws TestFailureException {
288: try {
289: try {
290: testJmsConnection(coonnectionFactory.createConnection());
291: testJmsConnection(queueCoonnectionFactory
292: .createConnection());
293: testJmsConnection(topicCoonnectionFactory
294: .createConnection());
295: } catch (Exception e) {
296: e.printStackTrace();
297: Assert.fail("Received Exception " + e.getClass()
298: + " : " + e.getMessage());
299: }
300: } catch (AssertionFailedError afe) {
301: throw new TestFailureException(afe);
302: }
303: }
304:
305: private void testJmsConnection(javax.jms.Connection connection)
306: throws JMSException {
307: Session session = connection.createSession(false,
308: Session.DUPS_OK_ACKNOWLEDGE);
309: Topic topic = session.createTopic("test");
310: MessageProducer producer = session.createProducer(topic);
311: producer.send(session.createMessage());
312: producer.close();
313: session.close();
314: connection.close();
315: }
316:
317: public void lookupPersistenceUnit() throws TestFailureException {
318: try {
319: Assert.assertNotNull("The EntityManagerFactory is null",
320: emf);
321: } catch (AssertionFailedError afe) {
322: throw new TestFailureException(afe);
323: }
324: }
325:
326: public void lookupPersistenceContext() throws TestFailureException {
327: try {
328: Assert.assertNotNull("The EntityManager is null", em);
329:
330: try {
331: // call a do nothing method to assure entity manager actually exists
332: em.getFlushMode();
333: } catch (Exception e) {
334: Assert.fail("Received Exception " + e.getClass()
335: + " : " + e.getMessage());
336: }
337: } catch (AssertionFailedError afe) {
338: throw new TestFailureException(afe);
339: }
340: }
341:
342: public void lookupSessionContext() throws TestFailureException {
343: try {
344: // TODO: DMB: Can't seem to find where to make this work
345: // Assert.assertNotNull("The SessionContext is null", ejbContext);
346: } catch (AssertionFailedError afe) {
347: throw new TestFailureException(afe);
348: }
349:
350: }
351: }
|