1 15 package org.apache.tapestry.web; 16 17 import java.net.MalformedURLException ; 18 import java.net.URL ; 19 import java.util.List ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.apache.tapestry.web.ServletWebContext; 24 import org.apache.tapestry.web.WebContext; 25 import org.easymock.MockControl; 26 27 33 public class TestServletWebContext extends BaseWebTestCase 34 { 35 36 public void testGetInitParameterNames() 37 { 38 MockControl control = newControl(ServletContext .class); 39 ServletContext context = (ServletContext ) control.getMock(); 40 41 context.getInitParameterNames(); 42 control.setReturnValue(newEnumeration()); 43 44 replayControls(); 45 46 WebContext wc = new ServletWebContext(context); 47 48 List l = wc.getInitParameterNames(); 49 50 checkList(l); 51 52 verifyControls(); 53 } 54 55 public void testGetInitParameterValue() 56 { 57 String value = "William Orbit"; 58 59 MockControl control = newControl(ServletContext .class); 60 ServletContext context = (ServletContext ) control.getMock(); 61 62 context.getInitParameter("artist"); 63 control.setReturnValue(value); 64 65 replayControls(); 66 67 WebContext wc = new ServletWebContext(context); 68 69 assertSame(value, wc.getInitParameterValue("artist")); 70 71 verifyControls(); 72 } 73 74 public void testGetAttributeNames() 75 { 76 MockControl control = newControl(ServletContext .class); 77 ServletContext context = (ServletContext ) control.getMock(); 78 79 context.getAttributeNames(); 80 control.setReturnValue(newEnumeration()); 81 82 replayControls(); 83 84 WebContext wc = new ServletWebContext(context); 85 86 List l = wc.getAttributeNames(); 87 88 checkList(l); 89 90 verifyControls(); 91 } 92 93 public void testGetAttribute() 94 { 95 Object attribute = new Object (); 96 97 MockControl control = newControl(ServletContext .class); 98 ServletContext context = (ServletContext ) control.getMock(); 99 100 context.getAttribute("attr"); 101 control.setReturnValue(attribute); 102 103 replayControls(); 104 105 WebContext wc = new ServletWebContext(context); 106 107 assertSame(attribute, wc.getAttribute("attr")); 108 109 verifyControls(); 110 } 111 112 public void testSetAttribute() 113 { 114 Object attribute = new Object (); 115 116 MockControl control = newControl(ServletContext .class); 117 ServletContext context = (ServletContext ) control.getMock(); 118 119 context.setAttribute("name", attribute); 120 121 replayControls(); 122 123 WebContext wc = new ServletWebContext(context); 124 125 wc.setAttribute("name", attribute); 126 127 verifyControls(); 128 } 129 130 public void testSetAttributeToNull() 131 { 132 MockControl control = newControl(ServletContext .class); 133 ServletContext context = (ServletContext ) control.getMock(); 134 135 context.removeAttribute("tonull"); 136 137 replayControls(); 138 139 WebContext wc = new ServletWebContext(context); 140 141 wc.setAttribute("tonull", null); 142 143 verifyControls(); 144 } 145 146 public void testGetResource() throws Exception 147 { 148 URL url = new URL ("http://jakarta.apache.org/tapestry"); 149 150 MockControl control = newControl(ServletContext .class); 151 ServletContext context = (ServletContext ) control.getMock(); 152 153 context.getResource("/tapestry"); 154 control.setReturnValue(url); 155 156 replayControls(); 157 158 WebContext wc = new ServletWebContext(context); 159 160 assertSame(url, wc.getResource("/tapestry")); 161 162 verifyControls(); 163 } 164 165 public void testGetResourceFailure() throws Exception 166 { 167 Throwable t = new MalformedURLException ("Like this ever happens."); 168 169 MockControl control = newControl(ServletContext .class); 170 ServletContext context = (ServletContext ) control.getMock(); 171 172 context.getResource("/tapestry"); 173 control.setThrowable(t); 174 175 replayControls(); 176 177 interceptLogging(ServletWebContext.class.getName()); 178 179 WebContext wc = new ServletWebContext(context); 180 181 assertNull(wc.getResource("/tapestry")); 182 183 verifyControls(); 184 185 assertLoggedMessage("Error getting context resource '/tapestry': Like this ever happens."); 186 } 187 } | Popular Tags |