1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.naming.Binding ; 22 import javax.naming.Context ; 23 import javax.naming.InitialContext ; 24 import javax.naming.NamingEnumeration ; 25 import javax.naming.NamingException ; 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 43 44 public class Jndi02 extends HttpServlet { 45 46 String names[] = 48 { "booleanEntry", "byteEntry", "doubleEntry", "floatEntry", 49 "integerEntry", "longEntry", "stringEntry", "nested" }; 50 51 52 public void destroy() { 55 56 try { 57 SessionBean sb = new SessionBean(); 58 log("OK Accessing SessionBean"); 59 } catch (Throwable t) { 60 log("FAIL Accessing SessionBean", t); 61 } 62 63 try { 64 SharedSessionBean sb = new SharedSessionBean(); 65 log("OK Accessing SharedSessionBean"); 66 } catch (Throwable t) { 67 log("FAIL Accessing SharedSessionBean", t); 68 } 69 70 try { 71 UnpSharedSessionBean sb = new UnpSharedSessionBean(); 72 log("OK Accessing UnpSharedSessionBean"); 73 } catch (Throwable t) { 74 log("FAIL Accessing UnpSharedSessionBean", t); 75 } 76 77 try { 78 UnsharedSessionBean sb = new UnsharedSessionBean(); 79 log("OK Accessing UnsharedSessionBean"); 80 } catch (Throwable 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 response.setContentType("text/plain"); 92 PrintWriter writer = response.getWriter(); 93 StringBuffer sb = new StringBuffer (); 94 boolean ok = true; 95 Object value = null; 96 97 Context initContext = null; 99 try { 100 initContext = new InitialContext (); 101 } catch (NamingException e) { 102 log("Create initContext", e); 103 sb.append(" Cannot create initContext."); 104 ok = false; 105 } 106 107 Context envContext = null; 109 try { 110 if (ok) { 111 value = initContext.lookup("java:comp/env"); 112 envContext = (Context ) value; 113 if (envContext == null) { 114 sb.append(" Missing envContext."); 115 ok = false; 116 } 117 } 118 } catch (ClassCastException e) { 119 sb.append(" envContext class is "); 120 sb.append(value.getClass().getName()); 121 sb.append("."); 122 ok = false; 123 } catch (NamingException e) { 124 log("Create envContext", e); 125 sb.append(" Cannot create envContext."); 126 ok = false; 127 } 128 129 try { 131 if (ok) { 132 value = envContext.lookup("booleanEntry"); 133 Boolean booleanValue = (Boolean ) value; 134 if (!(booleanValue.booleanValue() == true)) { 135 sb.append(" booleanValue is "); 136 sb.append(booleanValue); 137 sb.append("."); 138 } 139 } 140 } catch (ClassCastException e) { 141 sb.append(" booleanValue class is "); 142 sb.append(value.getClass().getName()); 143 sb.append("."); 144 } catch (NullPointerException e) { 145 sb.append(" booleanValue is missing."); 146 } catch (NamingException e) { 147 log("Get booleanValue", e); 148 sb.append(" Cannot get booleanValue."); 149 } 150 151 try { 153 if (ok) { 154 value = envContext.lookup("byteEntry"); 155 Byte byteValue = (Byte ) value; 156 if (!(byteValue.byteValue() == 123)) { 157 sb.append(" byteValue is "); 158 sb.append(byteValue); 159 sb.append("."); 160 } 161 } 162 } catch (ClassCastException e) { 163 sb.append(" byteValue class is "); 164 sb.append(value.getClass().getName()); 165 sb.append("."); 166 } catch (NullPointerException e) { 167 sb.append(" byteValue is missing."); 168 } catch (NamingException e) { 169 log("Get byteValue", e); 170 sb.append(" Cannot get byteValue."); 171 } 172 173 try { 175 if (ok) { 176 value = envContext.lookup("doubleEntry"); 177 Double doubleValue = (Double ) value; 178 if (!(doubleValue.doubleValue() == 123.45)) { 179 sb.append(" doubleValue is "); 180 sb.append(doubleValue); 181 sb.append("."); 182 } 183 } 184 } catch (ClassCastException e) { 185 sb.append(" doubleValue class is "); 186 sb.append(value.getClass().getName()); 187 sb.append("."); 188 } catch (NullPointerException e) { 189 sb.append(" doubleValue is missing."); 190 } catch (NamingException e) { 191 log("Get doubleValue", e); 192 sb.append(" Cannot get doubleValue."); 193 } 194 195 try { 197 if (ok) { 198 value = envContext.lookup("floatEntry"); 199 Float floatValue = (Float ) 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 e) { 209 sb.append(" floatValue class is "); 210 sb.append(value.getClass().getName()); 211 sb.append("."); 212 } catch (NullPointerException e) { 213 sb.append(" floatValue is missing."); 214 } catch (NamingException e) { 215 log("Get floatValue", e); 216 sb.append(" Cannot get floatValue."); 217 } 218 219 try { 221 if (ok) { 222 value = envContext.lookup("integerEntry"); 223 Integer integerValue = (Integer ) value; 224 if (!(integerValue.intValue() == 12345)) { 225 sb.append(" integerValue is "); 226 sb.append(integerValue); 227 sb.append("."); 228 } 229 } 230 } catch (ClassCastException e) { 231 sb.append(" integerValue class is "); 232 sb.append(value.getClass().getName()); 233 sb.append("."); 234 } catch (NullPointerException e) { 235 sb.append(" integerValue is missing."); 236 } catch (NamingException e) { 237 log("Get integerValue", e); 238 sb.append(" Cannot get integerValue."); 239 } 240 241 try { 243 if (ok) { 244 value = envContext.lookup("longEntry"); 245 Long longValue = (Long ) value; 246 if (!(longValue.longValue() == 54321)) { 247 sb.append(" longValue is "); 248 sb.append(longValue); 249 sb.append("."); 250 } 251 } 252 } catch (ClassCastException e) { 253 sb.append(" longValue class is "); 254 sb.append(value.getClass().getName()); 255 sb.append("."); 256 } catch (NullPointerException e) { 257 sb.append(" longValue is missing."); 258 } catch (NamingException e) { 259 log("Get longValue", e); 260 sb.append(" Cannot get longValue."); 261 } 262 263 try { 265 if (ok) { 266 value = envContext.lookup("stringEntry"); 267 String stringValue = (String ) value; 268 if (!"String Value".equals(stringValue)) { 269 sb.append(" stringValue is "); 270 sb.append(stringValue); 271 sb.append("."); 272 } 273 } 274 } catch (ClassCastException e) { 275 sb.append(" stringValue class is "); 276 sb.append(value.getClass().getName()); 277 sb.append("."); 278 } catch (NullPointerException e) { 279 sb.append(" stringValue is missing."); 280 } catch (NamingException e) { 281 log("Get stringValue", e); 282 sb.append(" Cannot get stringValue."); 283 } 284 285 try { 287 if (ok) { 288 value = envContext.lookup("nested/nestedEntry"); 289 String stringValue = (String ) value; 290 if (!"Nested Value".equals(stringValue)) { 291 sb.append(" stringValue is "); 292 sb.append(stringValue); 293 sb.append("."); 294 } 295 } 296 } catch (ClassCastException e) { 297 sb.append(" stringValue class is "); 298 sb.append(value.getClass().getName()); 299 sb.append("."); 300 } catch (NullPointerException e) { 301 sb.append(" stringValue is missing."); 302 } catch (NamingException e) { 303 log("Get stringValue", e); 304 sb.append(" Cannot get stringValue."); 305 } 306 307 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 namingEnum = 314 initContext.listBindings("java:comp/env"); 315 while (namingEnum.hasMore()) { 316 Binding binding = (Binding ) namingEnum.next(); 317 String 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 e) { 344 log("Enumerate envContext", e); 345 sb.append(" Cannot enumerate envContext"); 346 } 347 348 if (sb.length() < 1) 350 writer.println("Jndi02 PASSED"); 351 else { 352 writer.print("Jndi02 FAILED -"); 353 writer.println(sb); 354 } 355 356 while (true) { 358 String message = StaticLogger.read(); 359 if (message == null) 360 break; 361 writer.println(message); 362 } 363 StaticLogger.reset(); 364 365 } 366 367 } 368 | Popular Tags |