1 16 17 package org.apache.tester; 18 19 20 import java.io.*; 21 import javax.servlet.*; 22 import javax.servlet.http.*; 23 import org.apache.tester.shared.SharedSessionBean; 24 import org.apache.tester.unshared.UnsharedSessionBean; 25 26 27 35 36 public class Context01 extends HttpServlet { 37 38 public void doGet(HttpServletRequest request, HttpServletResponse response) 39 throws IOException, ServletException { 40 41 response.setContentType("text/plain"); 42 boolean ok = true; 43 PrintWriter writer = response.getWriter(); 44 ServletContext context = getServletContext(); 45 46 if (ok) { 48 if (context.getAttribute("context01") != null) { 49 writer.println("Context01 FAILED - Attribute already exists"); 50 ok = false; 51 } 52 } 53 54 if (ok) { 56 ContextBean bean = new ContextBean(); 57 bean.setStringProperty("Context01"); 58 context.setAttribute("context01", bean); 59 } 60 61 if (ok) { 63 Object bean = context.getAttribute("context01"); 64 if (bean == null) { 65 writer.println("Context01 FAILED - Cannot retrieve attribute"); 66 ok = false; 67 } 68 if (ok) { 69 if (!(bean instanceof ContextBean)) { 70 writer.println("Context01 FAILED - Bean instance of " + 71 bean.getClass().getName()); 72 ok = false; 73 } 74 } 75 if (ok) { 76 String value = ((ContextBean) bean).getStringProperty(); 77 if (!"Context01".equals(value)) { 78 writer.println("Context01 FAILED - Value = " + value); 79 ok = false; 80 } 81 } 82 if (ok) { 83 String lifecycle = ((ContextBean) bean).getLifecycle(); 84 if (!"/add".equals(lifecycle)) { 85 writer.println("Context01 FAILED - Bean lifecycle is " + 86 lifecycle); 87 ok = false; 88 } 89 } 90 } 91 92 if (ok) { 94 ContextBean bean = (ContextBean) context.getAttribute("context01"); 95 context.setAttribute("context01", bean); 96 String lifecycle = bean.getLifecycle(); 97 if (!"/add/rep".equals(lifecycle)) { 98 writer.println("Context01 FAILED - Bean lifecycle is " + 99 lifecycle); 100 ok = false; 101 } 102 } 103 104 if (ok) { 106 ContextBean bean = (ContextBean) context.getAttribute("context01"); 107 context.removeAttribute("context01"); 108 String lifecycle = bean.getLifecycle(); 109 if (!"/add/rep/rem".equals(lifecycle)) { 110 writer.println("Context01 FAILED - Bean lifecycle is " + 111 lifecycle); 112 ok = false; 113 } 114 } 115 116 context.setAttribute("context01", new ContextBean()); 118 119 if (ok) { 121 context.setAttribute("FOO", "BAR"); 122 context.setAttribute("FOO", null); 123 if (context.getAttribute("FOO") != null) { 124 writer.println("Context01 FAILED - setAttribute(name,null)"); 125 ok = false; 126 } 127 } 128 129 if (ok) 131 writer.println("Context01 PASSED"); 132 while (true) { 133 String message = StaticLogger.read(); 134 if (message == null) 135 break; 136 writer.println(message); 137 } 138 StaticLogger.reset(); 139 140 } 141 142 } 143 | Popular Tags |