KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tester > Jndi02


1 /*
2  * Copyright 1999, 2000, 2001 ,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.tester;
18
19
20 import java.io.*;
21 import javax.naming.Binding JavaDoc;
22 import javax.naming.Context JavaDoc;
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingEnumeration JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28 import org.apache.tester.SessionBean;
29 import org.apache.tester.shared.SharedSessionBean;
30 import org.apache.tester.unpshared.UnpSharedSessionBean;
31 import org.apache.tester.unshared.UnsharedSessionBean;
32
33
34 /**
35  * Positive test for looking up environment entries from the naming context
36  * provided by the servlet container. The looked-up values are initialized
37  * via <code>&lt;env-entry&gt;</code> elements in the web application
38  * deployment descriptor.
39  *
40  * @author Craig R. McClanahan
41  * @version $Revision: 1.3 $ $Date: 2004/08/31 14:41:22 $
42  */

43
44 public class Jndi02 extends HttpServlet {
45
46     // Names of the known <env-entry> elements
47
String JavaDoc names[] =
48     { "booleanEntry", "byteEntry", "doubleEntry", "floatEntry",
49       "integerEntry", "longEntry", "stringEntry", "nested" };
50
51
52     // Reference some application classes for the first time in destroy()
53
// and log the results
54
public void destroy() {
55
56         try {
57             SessionBean sb = new SessionBean();
58             log("OK Accessing SessionBean");
59         } catch (Throwable JavaDoc t) {
60             log("FAIL Accessing SessionBean", t);
61         }
62
63         try {
64             SharedSessionBean sb = new SharedSessionBean();
65             log("OK Accessing SharedSessionBean");
66         } catch (Throwable JavaDoc t) {
67             log("FAIL Accessing SharedSessionBean", t);
68         }
69
70         try {
71             UnpSharedSessionBean sb = new UnpSharedSessionBean();
72             log("OK Accessing UnpSharedSessionBean");
73         } catch (Throwable JavaDoc t) {
74             log("FAIL Accessing UnpSharedSessionBean", t);
75         }
76
77         try {
78             UnsharedSessionBean sb = new UnsharedSessionBean();
79             log("OK Accessing UnsharedSessionBean");
80         } catch (Throwable JavaDoc t) {
81             log("FAIL Accessing UnsharedSessionBean", t);
82         }
83
84     }
85
86
87     public void doGet(HttpServletRequest request, HttpServletResponse response)
88         throws IOException, ServletException {
89
90         // Prepare to render our output
91
response.setContentType("text/plain");
92         PrintWriter writer = response.getWriter();
93         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
94         boolean ok = true;
95         Object JavaDoc value = null;
96
97         // Look up the initial context provided by our servlet container
98
Context JavaDoc initContext = null;
99         try {
100             initContext = new InitialContext JavaDoc();
101         } catch (NamingException JavaDoc e) {
102             log("Create initContext", e);
103             sb.append(" Cannot create initContext.");
104             ok = false;
105         }
106
107         // Look up the environment context provided to our web application
108
Context JavaDoc envContext = null;
109         try {
110             if (ok) {
111                 value = initContext.lookup("java:comp/env");
112                 envContext = (Context JavaDoc) value;
113                 if (envContext == null) {
114                     sb.append(" Missing envContext.");
115                     ok = false;
116                 }
117             }
118         } catch (ClassCastException JavaDoc e) {
119             sb.append(" envContext class is ");
120             sb.append(value.getClass().getName());
121             sb.append(".");
122             ok = false;
123         } catch (NamingException JavaDoc e) {
124             log("Create envContext", e);
125             sb.append(" Cannot create envContext.");
126             ok = false;
127         }
128
129         // Validate the booleanEntry environment entry
130
try {
131             if (ok) {
132                 value = envContext.lookup("booleanEntry");
133                 Boolean JavaDoc booleanValue = (Boolean JavaDoc) value;
134                 if (!(booleanValue.booleanValue() == true)) {
135                     sb.append(" booleanValue is ");
136                     sb.append(booleanValue);
137                     sb.append(".");
138                 }
139             }
140         } catch (ClassCastException JavaDoc e) {
141             sb.append(" booleanValue class is ");
142             sb.append(value.getClass().getName());
143             sb.append(".");
144         } catch (NullPointerException JavaDoc e) {
145             sb.append(" booleanValue is missing.");
146         } catch (NamingException JavaDoc e) {
147             log("Get booleanValue", e);
148             sb.append(" Cannot get booleanValue.");
149         }
150
151         // Validate the byteEntry environment entry
152
try {
153             if (ok) {
154                 value = envContext.lookup("byteEntry");
155                 Byte JavaDoc byteValue = (Byte JavaDoc) value;
156                 if (!(byteValue.byteValue() == 123)) {
157                     sb.append(" byteValue is ");
158                     sb.append(byteValue);
159                     sb.append(".");
160                 }
161             }
162         } catch (ClassCastException JavaDoc e) {
163             sb.append(" byteValue class is ");
164             sb.append(value.getClass().getName());
165             sb.append(".");
166         } catch (NullPointerException JavaDoc e) {
167             sb.append(" byteValue is missing.");
168         } catch (NamingException JavaDoc e) {
169             log("Get byteValue", e);
170             sb.append(" Cannot get byteValue.");
171         }
172
173         // Validate the doubleEntry environment entry
174
try {
175             if (ok) {
176                 value = envContext.lookup("doubleEntry");
177                 Double JavaDoc doubleValue = (Double JavaDoc) value;
178                 if (!(doubleValue.doubleValue() == 123.45)) {
179                     sb.append(" doubleValue is ");
180                     sb.append(doubleValue);
181                     sb.append(".");
182                 }
183             }
184         } catch (ClassCastException JavaDoc e) {
185             sb.append(" doubleValue class is ");
186             sb.append(value.getClass().getName());
187             sb.append(".");
188         } catch (NullPointerException JavaDoc e) {
189             sb.append(" doubleValue is missing.");
190         } catch (NamingException JavaDoc e) {
191             log("Get doubleValue", e);
192             sb.append(" Cannot get doubleValue.");
193         }
194
195         // Validate the floatEntry environment entry
196
try {
197             if (ok) {
198                 value = envContext.lookup("floatEntry");
199                 Float JavaDoc floatValue = (Float JavaDoc) value;
200                 float difference = floatValue.floatValue() - ((float) 54.32);
201                 if ((difference < ((float) -0.01)) ||
202                     (difference > ((float) 0.01))) {
203                     sb.append(" floatValue is ");
204                     sb.append(floatValue);
205                     sb.append(".");
206                 }
207             }
208         } catch (ClassCastException JavaDoc e) {
209             sb.append(" floatValue class is ");
210             sb.append(value.getClass().getName());
211             sb.append(".");
212         } catch (NullPointerException JavaDoc e) {
213             sb.append(" floatValue is missing.");
214         } catch (NamingException JavaDoc e) {
215             log("Get floatValue", e);
216             sb.append(" Cannot get floatValue.");
217         }
218
219         // Validate the integerEntry environment entry
220
try {
221             if (ok) {
222                 value = envContext.lookup("integerEntry");
223                 Integer JavaDoc integerValue = (Integer JavaDoc) value;
224                 if (!(integerValue.intValue() == 12345)) {
225                     sb.append(" integerValue is ");
226                     sb.append(integerValue);
227                     sb.append(".");
228                 }
229             }
230         } catch (ClassCastException JavaDoc e) {
231             sb.append(" integerValue class is ");
232             sb.append(value.getClass().getName());
233             sb.append(".");
234         } catch (NullPointerException JavaDoc e) {
235             sb.append(" integerValue is missing.");
236         } catch (NamingException JavaDoc e) {
237             log("Get integerValue", e);
238             sb.append(" Cannot get integerValue.");
239         }
240
241         // Validate the longEntry environment entry
242
try {
243             if (ok) {
244                 value = envContext.lookup("longEntry");
245                 Long JavaDoc longValue = (Long JavaDoc) value;
246                 if (!(longValue.longValue() == 54321)) {
247                     sb.append(" longValue is ");
248                     sb.append(longValue);
249                     sb.append(".");
250                 }
251             }
252         } catch (ClassCastException JavaDoc e) {
253             sb.append(" longValue class is ");
254             sb.append(value.getClass().getName());
255             sb.append(".");
256         } catch (NullPointerException JavaDoc e) {
257             sb.append(" longValue is missing.");
258         } catch (NamingException JavaDoc e) {
259             log("Get longValue", e);
260             sb.append(" Cannot get longValue.");
261         }
262
263         // Validate the stringEntry environment entry
264
try {
265             if (ok) {
266                 value = envContext.lookup("stringEntry");
267                 String JavaDoc stringValue = (String JavaDoc) value;
268                 if (!"String Value".equals(stringValue)) {
269                     sb.append(" stringValue is ");
270                     sb.append(stringValue);
271                     sb.append(".");
272                 }
273             }
274         } catch (ClassCastException JavaDoc e) {
275             sb.append(" stringValue class is ");
276             sb.append(value.getClass().getName());
277             sb.append(".");
278         } catch (NullPointerException JavaDoc e) {
279             sb.append(" stringValue is missing.");
280         } catch (NamingException JavaDoc e) {
281             log("Get stringValue", e);
282             sb.append(" Cannot get stringValue.");
283         }
284
285         // Validate the nestedEntry environment entry
286
try {
287             if (ok) {
288                 value = envContext.lookup("nested/nestedEntry");
289                 String JavaDoc stringValue = (String JavaDoc) value;
290                 if (!"Nested Value".equals(stringValue)) {
291                     sb.append(" stringValue is ");
292                     sb.append(stringValue);
293                     sb.append(".");
294                 }
295             }
296         } catch (ClassCastException JavaDoc e) {
297             sb.append(" stringValue class is ");
298             sb.append(value.getClass().getName());
299             sb.append(".");
300         } catch (NullPointerException JavaDoc e) {
301             sb.append(" stringValue is missing.");
302         } catch (NamingException JavaDoc e) {
303             log("Get stringValue", e);
304             sb.append(" Cannot get stringValue.");
305         }
306
307         // Validate that we can enumerate the contents of our environment
308
try {
309             if (ok) {
310                 int counts[] = new int[names.length];
311                 for (int i = 0; i < names.length; i++)
312                     counts[i] = 0;
313                 NamingEnumeration JavaDoc namingEnum =
314                     initContext.listBindings("java:comp/env");
315                 while (namingEnum.hasMore()) {
316                     Binding JavaDoc binding = (Binding JavaDoc) namingEnum.next();
317                     String JavaDoc name = binding.getName();
318                     boolean found = false;
319                     for (int i = 0; i < names.length; i++) {
320                         if (name.equals(names[i])) {
321                             counts[i]++;
322                             found = true;
323                             break;
324                         }
325                     }
326                     if (!found)
327                         StaticLogger.write("Found binding for '" + name + "'");
328                 }
329                 for (int i = 0; i < names.length; i++) {
330                     if (counts[i] < 1) {
331                         sb.append(" Missing binding for ");
332                         sb.append(names[i]);
333                         sb.append(".");
334                     } else if (counts[i] > 1) {
335                         sb.append(" Found ");
336                         sb.append(counts[i]);
337                         sb.append(" bindings for ");
338                         sb.append(names[i]);
339                         sb.append(".");
340                     }
341                 }
342             }
343         } catch (NamingException JavaDoc e) {
344             log("Enumerate envContext", e);
345             sb.append(" Cannot enumerate envContext");
346         }
347
348         // Report our ultimate success or failure
349
if (sb.length() < 1)
350             writer.println("Jndi02 PASSED");
351         else {
352             writer.print("Jndi02 FAILED -");
353             writer.println(sb);
354         }
355
356         // Add wrapper messages as required
357
while (true) {
358             String JavaDoc message = StaticLogger.read();
359             if (message == null)
360                 break;
361             writer.println(message);
362         }
363         StaticLogger.reset();
364
365     }
366
367 }
368
Popular Tags