KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > web > servlets > ENCTester


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.web.servlets;
23
24 import java.net.URL JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import javax.jms.JMSException JavaDoc;
29 import javax.jms.Queue JavaDoc;
30 import javax.jms.QueueConnectionFactory JavaDoc;
31 import javax.jms.Topic JavaDoc;
32 import javax.mail.Session JavaDoc;
33 import javax.naming.Context JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.sql.DataSource JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.test.web.mock.EntityHome;
41 import org.jboss.test.web.mock.StatelessSessionHome;
42 import org.jboss.test.web.mock.StatelessSessionLocalHome;
43
44 /**
45  * A common test bean used by the enc servlets to validate their enc configuration.
46  *
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 58438 $
49  */

50 public class ENCTester
51 {
52    private Logger log;
53    ENCTester(Logger log)
54    {
55       this.log = log;
56    }
57
58    void testENC() throws ServletException JavaDoc
59    {
60       try
61       {
62          // Obtain the environment naming context.
63
Context JavaDoc initCtx = new InitialContext JavaDoc();
64          Hashtable JavaDoc env = initCtx.getEnvironment();
65          Iterator JavaDoc keys = env.keySet().iterator();
66          log.info("InitialContext.env:");
67          while( keys.hasNext() )
68          {
69             Object JavaDoc key = keys.next();
70             log.info("Key: "+key+", value: "+env.get(key));
71          }
72          Context JavaDoc myEnv = (Context JavaDoc) initCtx.lookup("java:comp/env");
73          testEjbRefs(initCtx, myEnv);
74          testJdbcDataSource(initCtx, myEnv);
75          testMail(initCtx, myEnv);
76          testJMS(initCtx, myEnv);
77          testURL(initCtx, myEnv);
78          testEnvEntries(initCtx, myEnv);
79          testMessageDestinationRefs(initCtx, myEnv);
80       }
81       catch (NamingException JavaDoc e)
82       {
83          log.debug("Lookup failed", e);
84          throw new ServletException JavaDoc("Lookup failed, ENC tests failed", e);
85       }
86       catch (JMSException JavaDoc e)
87       {
88          log.debug("JMS access failed", e);
89          throw new ServletException JavaDoc("JMS access failed, ENC tests failed", e);
90       }
91       catch (RuntimeException JavaDoc e)
92       {
93          log.debug("Runtime error", e);
94          throw new ServletException JavaDoc("Runtime error, ENC tests failed", e);
95       }
96    }
97
98    private void testEnvEntries(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
99    {
100       // Basic env values
101
Integer JavaDoc i = (Integer JavaDoc) myEnv.lookup("Ints/i0");
102       log.debug("Ints/i0 = " + i);
103       i = (Integer JavaDoc) initCtx.lookup("java:comp/env/Ints/i1");
104       log.debug("Ints/i1 = " + i);
105       Float JavaDoc f = (Float JavaDoc) myEnv.lookup("Floats/f0");
106       log.debug("Floats/f0 = " + f);
107       f = (Float JavaDoc) initCtx.lookup("java:comp/env/Floats/f1");
108       log.debug("Floats/f1 = " + f);
109       String JavaDoc s = (String JavaDoc) myEnv.lookup("Strings/s0");
110       log.debug("Strings/s0 = " + s);
111       s = (String JavaDoc) initCtx.lookup("java:comp/env/Strings/s1");
112       log.debug("Strings/s1 = " + s);
113       s = (String JavaDoc) initCtx.lookup("java:comp/env/ejb/catalog/CatalogDAOClass");
114       log.debug("ejb/catalog/CatalogDAOClass = " + s);
115    }
116
117    private void testEjbRefs(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
118    {
119       //do lookup on bean specified without ejb-link
120
Object JavaDoc ejb = initCtx.lookup("java:comp/env/ejb/bean3");
121       if ((ejb instanceof StatelessSessionHome) == false)
122          throw new NamingException JavaDoc("ejb/bean3 is not a StatelessSessionHome");
123       log.debug("ejb/bean3 = " + ejb);
124
125
126       ejb = initCtx.lookup("java:comp/env/ejb/CtsBmp");
127       if ((ejb instanceof EntityHome) == false)
128          throw new NamingException JavaDoc("ejb/CtsBmp is not a EntityHome");
129
130       //lookup of local-ejb-ref bean specified without ejb-link
131
ejb = initCtx.lookup("java:comp/env/ejb/local/bean3");
132       if ((ejb instanceof StatelessSessionLocalHome) == false)
133          throw new NamingException JavaDoc("ejb/local/bean3 is not a StatelessSessionLocalHome");
134       log.debug("ejb/local/bean3 = " + ejb);
135    }
136
137    private void testJdbcDataSource(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
138    {
139       // JDBC DataSource
140
DataSource JavaDoc ds = (DataSource JavaDoc) myEnv.lookup("jdbc/DefaultDS");
141       log.debug("jdbc/DefaultDS = " + ds);
142    }
143
144    private void testMail(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
145    {
146       // JavaMail Session
147
Session JavaDoc session = (Session JavaDoc) myEnv.lookup("mail/DefaultMail");
148       log.debug("mail/DefaultMail = " + session);
149    }
150
151    private void testJMS(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
152    {
153       // JavaMail Session
154
QueueConnectionFactory JavaDoc qf = (QueueConnectionFactory JavaDoc) myEnv.lookup("jms/QueFactory");
155       log.debug("jms/QueFactory = " + qf);
156    }
157
158    private void testURL(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc
159    {
160       // URLs
161
URL JavaDoc home1 = (URL JavaDoc) myEnv.lookup("url/JBossHome");
162       log.debug("url/JBossHome = " + home1);
163       URL JavaDoc home2 = (URL JavaDoc) initCtx.lookup("java:comp/env/url/JBossHome");
164       log.debug("url/JBossHome = " + home2);
165       if( home1.equals(home2) == false )
166          throw new NamingException JavaDoc("url/JBossHome != java:comp/env/url/JBossHome");
167    }
168
169    private void testMessageDestinationRefs(Context JavaDoc initCtx, Context JavaDoc myEnv) throws NamingException JavaDoc, JMSException JavaDoc
170    {
171       Object JavaDoc obj = myEnv.lookup("mdr/ConsumesLink");
172       log.debug("mdr/ConsumesLink = " + obj);
173       if ((obj instanceof Queue JavaDoc) == false)
174          throw new RuntimeException JavaDoc("mdr/ConsumesLink is not a javax.jms.Queue");
175       Queue JavaDoc queue = (Queue JavaDoc) obj;
176       if ("QUEUE.testQueue".equals(queue.getQueueName()))
177          throw new RuntimeException JavaDoc("Excepted QUEUE.testQueue, got " + queue);
178       
179       obj = myEnv.lookup("mdr/ProducesLink");
180       log.debug("mdr/ProducesLink = " + obj);
181       if ((obj instanceof Topic JavaDoc) == false)
182          throw new RuntimeException JavaDoc("mdr/ProducesLink is not a javax.jms.Topic");
183       Topic JavaDoc topic = (Topic JavaDoc) obj;
184       if ("TOPIC.testTopic".equals(topic.getTopicName()))
185          throw new RuntimeException JavaDoc("Excepted TOPIC.testTopic got " + topic);
186
187       obj = myEnv.lookup("mdr/ConsumesProducesJNDIName");
188       log.debug("mdr/ConsumesProducesJNDIName = " + obj);
189       if ((obj instanceof Queue JavaDoc) == false)
190          throw new RuntimeException JavaDoc("mdr/ConsumesProducesJNDIName is not a javax.jms.Queue");
191       queue = (Queue JavaDoc) obj;
192       if ("QUEUE.A".equals(queue.getQueueName()))
193          throw new RuntimeException JavaDoc("Excepted QUEUE.A, got " + queue);
194    }
195
196 }
197
Popular Tags