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.servlet;
018:
019: import java.io.IOException;
020: import java.io.PrintStream;
021: import java.lang.reflect.Method;
022: import javax.annotation.Resource;
023: import javax.ejb.EJB;
024: import javax.jms.ConnectionFactory;
025: import javax.jms.JMSException;
026: import javax.jms.MessageProducer;
027: import javax.jms.QueueConnectionFactory;
028: import javax.jms.Session;
029: import javax.jms.Topic;
030: import javax.jms.TopicConnectionFactory;
031: import javax.persistence.EntityManager;
032: import javax.persistence.EntityManagerFactory;
033: import javax.persistence.PersistenceContext;
034: import javax.persistence.PersistenceUnit;
035: import javax.servlet.ServletException;
036: import javax.servlet.ServletOutputStream;
037: import javax.servlet.http.HttpServlet;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import javax.sql.DataSource;
041:
042: import junit.framework.Assert;
043: import org.apache.openejb.test.entity.bmp.BasicBmpHome;
044: import org.apache.openejb.test.stateful.BasicStatefulBusinessLocal;
045: import org.apache.openejb.test.stateful.BasicStatefulBusinessRemote;
046: import org.apache.openejb.test.stateful.BasicStatefulHome;
047: import org.apache.openejb.test.stateless.BasicStatelessBusinessLocal;
048: import org.apache.openejb.test.stateless.BasicStatelessBusinessRemote;
049: import org.apache.openejb.test.stateless.BasicStatelessHome;
050:
051: public class AnnotatedServlet extends HttpServlet implements
052: JndiTestServlet {
053: @EJB(beanName="BasicBmpBean")
054: private BasicBmpHome bmpHome;
055: @EJB(beanName="BasicStatefulBean")
056: private BasicStatefulHome statefulHome;
057: @EJB(beanName="BasicStatelessBean")
058: private BasicStatelessHome statelessHome;
059: @Resource
060: private String striing;
061: @Resource
062: private Double doouble;
063: @Resource
064: private Long loong;
065: @Resource
066: private Float flooat;
067: @Resource
068: private Integer inteeger;
069: @Resource
070: private Short shoort;
071: @Resource
072: private Boolean booolean;
073: @Resource
074: private Byte byyte;
075: @Resource
076: private Character chaaracter;
077: @Resource
078: private DataSource daataSource;
079: @Resource
080: private ConnectionFactory coonnectionFactory;
081: @Resource
082: private QueueConnectionFactory queueCoonnectionFactory;
083: @Resource
084: private TopicConnectionFactory topicCoonnectionFactory;
085: @PersistenceUnit(unitName="openjpa-test-unit")
086: private EntityManagerFactory emf;
087: @PersistenceContext(unitName="openjpa-test-unit")
088: private EntityManager em;
089: @EJB
090: private BasicStatelessBusinessLocal statelessBusinessLocal;
091: @EJB
092: private BasicStatelessBusinessRemote statelessBusinessRemote;
093: @EJB
094: private BasicStatefulBusinessLocal statefulBusinessLocal;
095: @EJB
096: private BasicStatefulBusinessRemote statefulBusinessRemote;
097:
098: protected void service(HttpServletRequest request,
099: HttpServletResponse response) throws ServletException,
100: IOException {
101: response.setContentType("text/plain");
102: ServletOutputStream out = response.getOutputStream();
103: PrintStream printStream = new PrintStream(out);
104:
105: String methodName = request.getParameter("method");
106: if (methodName == null) {
107: testAll(printStream);
108: } else {
109: try {
110: Method method = getClass().getMethod(methodName);
111: method.invoke(this );
112: } catch (Throwable e) {
113: // response.setStatus(580);
114: printStream.println("FAILED");
115: e.printStackTrace(printStream);
116: }
117: }
118: printStream.flush();
119: }
120:
121: public void testAll(PrintStream printStream) {
122: for (Method method : JndiTestServlet.class.getMethods()) {
123: try {
124: method.invoke(this );
125: printStream.println(method.getName() + " PASSED");
126: } catch (Throwable e) {
127: printStream.println(method.getName() + " FAILED");
128: e.printStackTrace(printStream);
129: printStream.flush();
130: }
131: printStream.println();
132: }
133: }
134:
135: public void lookupEntityBean() {
136: Assert.assertNotNull("The EJBObject is null", bmpHome);
137: }
138:
139: public void lookupStatefulBean() {
140: Assert.assertNotNull("The EJBObject is null", statefulHome);
141: }
142:
143: public void lookupStatelessBean() {
144: Assert.assertNotNull("The EJBObject is null", statelessHome);
145: }
146:
147: public void lookupStatelessBusinessLocal() {
148: Assert.assertNotNull("The EJB BusinessLocal is null",
149: statelessBusinessLocal);
150: }
151:
152: public void lookupStatelessBusinessRemote() {
153: Assert.assertNotNull("The EJB BusinessRemote is null",
154: statelessBusinessRemote);
155: }
156:
157: public void lookupStatefulBusinessLocal() {
158: Assert.assertNotNull("The EJB BusinessLocal is null",
159: statefulBusinessLocal);
160: }
161:
162: public void lookupStatefulBusinessRemote() {
163: Assert.assertNotNull("The EJB BusinessRemote is null",
164: statefulBusinessRemote);
165: }
166:
167: public void lookupStringEntry() {
168: String expected = "1";
169: Assert.assertNotNull("The String looked up is null", striing);
170: Assert.assertEquals(expected, striing);
171: }
172:
173: public void lookupDoubleEntry() {
174: Double expected = 1.0D;
175:
176: Assert.assertNotNull("The Double looked up is null", doouble);
177: Assert.assertEquals(expected, doouble);
178: }
179:
180: public void lookupLongEntry() {
181: Long expected = 1L;
182:
183: Assert.assertNotNull("The Long looked up is null", loong);
184: Assert.assertEquals(expected, loong);
185: }
186:
187: public void lookupFloatEntry() {
188: Float expected = 1.0F;
189:
190: Assert.assertNotNull("The Float looked up is null", flooat);
191: Assert.assertEquals(expected, flooat);
192: }
193:
194: public void lookupIntegerEntry() {
195: Integer expected = 1;
196:
197: Assert.assertNotNull("The Integer looked up is null", inteeger);
198: Assert.assertEquals(expected, inteeger);
199: }
200:
201: public void lookupShortEntry() {
202: Short expected = (short) 1;
203:
204: Assert.assertNotNull("The Short looked up is null", shoort);
205: Assert.assertEquals(expected, shoort);
206: }
207:
208: public void lookupBooleanEntry() {
209: Boolean expected = true;
210:
211: Assert.assertNotNull("The Boolean looked up is null", booolean);
212: Assert.assertEquals(expected, booolean);
213: }
214:
215: public void lookupByteEntry() {
216: Byte expected = (byte) 1;
217:
218: Assert.assertNotNull("The Byte looked up is null", byyte);
219: Assert.assertEquals(expected, byyte);
220: }
221:
222: public void lookupCharacterEntry() {
223: Character expected = 'D';
224:
225: Assert.assertNotNull("The Character looked up is null",
226: chaaracter);
227: Assert.assertEquals(expected, chaaracter);
228: }
229:
230: public void lookupResource() {
231: Assert.assertNotNull("The DataSource is null", daataSource);
232: }
233:
234: public void lookupJMSConnectionFactory() {
235: try {
236: testJmsConnection(coonnectionFactory.createConnection());
237: testJmsConnection(queueCoonnectionFactory
238: .createConnection());
239: testJmsConnection(topicCoonnectionFactory
240: .createConnection());
241: } catch (Exception e) {
242: e.printStackTrace();
243: Assert.fail("Received Exception " + e.getClass() + " : "
244: + e.getMessage());
245: }
246: }
247:
248: private void testJmsConnection(javax.jms.Connection connection)
249: throws JMSException {
250: Session session = connection.createSession(false,
251: Session.DUPS_OK_ACKNOWLEDGE);
252: Topic topic = session.createTopic("test");
253: MessageProducer producer = session.createProducer(topic);
254: producer.send(session.createMessage());
255: producer.close();
256: session.close();
257: connection.close();
258: }
259:
260: public void lookupPersistenceUnit() {
261: Assert.assertNotNull("The EntityManagerFactory is null", emf);
262: }
263:
264: public void lookupPersistenceContext() {
265: Assert.assertNotNull("The EntityManager is null", em);
266:
267: try {
268: // call a do nothing method to assure entity manager actually exists
269: em.getFlushMode();
270: } catch (Exception e) {
271: Assert.fail("Received Exception " + e.getClass() + " : "
272: + e.getMessage());
273: }
274: }
275: }
|