1 package com.sun.facelets.tag.jsf.core; 2 3 import java.util.Date ; 4 import java.util.Locale ; 5 6 import javax.faces.component.UICommand; 7 import javax.faces.component.UIComponent; 8 import javax.faces.component.UIData; 9 import javax.faces.component.UIInput; 10 import javax.faces.component.UIOutput; 11 import javax.faces.component.UIViewRoot; 12 import javax.faces.component.html.HtmlGraphicImage; 13 import javax.faces.context.FacesContext; 14 import javax.faces.event.ActionListener; 15 import javax.faces.validator.Validator; 16 17 import com.sun.facelets.Facelet; 18 import com.sun.facelets.FaceletFactory; 19 import com.sun.facelets.FaceletTestCase; 20 21 public class CoreTestCase extends FaceletTestCase { 22 23 public void testActionListenerHandler() throws Exception { 24 ActionListener listener = new ActionListenerImpl(); 25 FacesContext faces = FacesContext.getCurrentInstance(); 26 faces.getExternalContext().getRequestMap().put("actionListener", 27 listener); 28 29 FaceletFactory f = FaceletFactory.getInstance(); 30 Facelet at = f.getFacelet("actionListener.xml"); 31 32 UIViewRoot root = faces.getViewRoot(); 33 at.apply(faces, root); 34 35 UICommand action1 = (UICommand) root.findComponent("action1"); 36 UICommand action2 = (UICommand) root.findComponent("action2"); 37 38 assertNotNull("action1", action1); 39 assertNotNull("action2", action2); 40 41 assertEquals("action1 listeners", 1, 42 action1.getActionListeners().length); 43 assertEquals("action2 listeners", 2, 44 action2.getActionListeners().length); 45 46 assertEquals("action2 binding", listener, 47 action2.getActionListeners()[0]); 48 } 49 50 public void testAttributeHandler() throws Exception { 51 String title = "Dog in a Funny Hat"; 52 FacesContext faces = FacesContext.getCurrentInstance(); 53 faces.getExternalContext().getRequestMap().put("title", title); 54 55 FaceletFactory f = FaceletFactory.getInstance(); 56 Facelet at = f.getFacelet("attribute.xml"); 57 58 UIViewRoot root = faces.getViewRoot(); 59 at.apply(faces, root); 60 61 HtmlGraphicImage graphic1 = (HtmlGraphicImage) root 62 .findComponent("graphic1"); 63 HtmlGraphicImage graphic2 = (HtmlGraphicImage) root 64 .findComponent("graphic2"); 65 66 assertNotNull("graphic1", graphic1); 67 assertNotNull("graphic2", graphic2); 68 69 assertEquals("graphic1 title", "literal", graphic1.getTitle()); 70 assertEquals("graphic2 title", title, graphic2.getTitle()); 71 } 72 73 public void testConvertDateTimeHandler() throws Exception { 74 Date now = new Date (1000 * 360 * 60 * 24 * 7); 75 FacesContext faces = FacesContext.getCurrentInstance(); 76 faces.getExternalContext().getRequestMap().put("now", now); 77 78 FaceletFactory f = FaceletFactory.getInstance(); 79 Facelet at = f.getFacelet("convertDateTime.xml"); 80 81 UIViewRoot root = faces.getViewRoot(); 82 at.apply(faces, root); 83 84 UIOutput out1 = (UIOutput) root.findComponent("form:out1"); 85 UIOutput out2 = (UIOutput) root.findComponent("form:out2"); 86 UIOutput out3 = (UIOutput) root.findComponent("form:out3"); 87 UIOutput out4 = (UIOutput) root.findComponent("form:out4"); 88 UIOutput out5 = (UIOutput) root.findComponent("form:out5"); 89 90 assertNotNull("out1", out1); 91 assertNotNull("out2", out2); 92 assertNotNull("out3", out3); 93 assertNotNull("out4", out4); 94 assertNotNull("out5", out5); 95 96 assertNotNull("out1 converter", out1.getConverter()); 97 assertNotNull("out2 converter", out2.getConverter()); 98 assertNotNull("out3 converter", out3.getConverter()); 99 assertNotNull("out4 converter", out4.getConverter()); 100 assertNotNull("out5 converter", out5.getConverter()); 101 102 assertEquals("out1 value", "12/24/69", out1.getConverter().getAsString( 103 faces, out1, now)); 104 assertEquals("out2 value", "12/24/69 6:57:12 AM", out2.getConverter() 105 .getAsString(faces, out2, now)); 106 assertEquals("out3 value", "Dec 24, 1969", out3.getConverter() 107 .getAsString(faces, out3, now)); 108 assertEquals("out4 value", "6:57:12 AM", out4.getConverter() 109 .getAsString(faces, out4, now)); 110 assertEquals("out5 value", "0:57 AM, CST", out5.getConverter() 111 .getAsString(faces, out5, now)); 112 } 113 114 public void testConvertDelegateHandler() throws Exception { 115 FacesContext faces = FacesContext.getCurrentInstance(); 116 117 FaceletFactory f = FaceletFactory.getInstance(); 118 Facelet at = f.getFacelet("converter.xml"); 119 120 UIViewRoot root = faces.getViewRoot(); 121 at.apply(faces, root); 122 123 UIOutput out1 = (UIOutput) root.findComponent("out1"); 124 125 assertNotNull("out1", out1); 126 127 assertNotNull("out1 converter", out1.getConverter()); 128 129 assertEquals("out1 value", new Double (42.5), out1.getConverter().getAsObject(faces, out1, out1.getLocalValue().toString())); 130 } 131 132 public void testConvertNumberHandler() throws Exception { 133 FacesContext faces = FacesContext.getCurrentInstance(); 134 135 FaceletFactory f = FaceletFactory.getInstance(); 136 Facelet at = f.getFacelet("convertNumber.xml"); 137 138 UIViewRoot root = faces.getViewRoot(); 139 at.apply(faces, root); 140 141 UIOutput out1 = (UIOutput) root.findComponent("out1"); 142 UIOutput out2 = (UIOutput) root.findComponent("out2"); 143 UIOutput out3 = (UIOutput) root.findComponent("out3"); 144 UIOutput out4 = (UIOutput) root.findComponent("out4"); 145 UIOutput out5 = (UIOutput) root.findComponent("out5"); 146 147 assertNotNull("out1", out1); 148 assertNotNull("out2", out2); 149 assertNotNull("out3", out3); 150 assertNotNull("out4", out4); 151 assertNotNull("out5", out5); 152 153 assertNotNull("out1 converter", out1.getConverter()); 154 assertNotNull("out2 converter", out2.getConverter()); 155 assertNotNull("out3 converter", out3.getConverter()); 156 assertNotNull("out4 converter", out4.getConverter()); 157 assertNotNull("out5 converter", out5.getConverter()); 158 159 assertEquals("out1 value", "12", out1.getConverter().getAsString( 160 faces, out1, new Double (12.001))); 161 assertEquals("out2 value", "$12.00", out2.getConverter() 162 .getAsString(faces, out2, new Double (12.00))); 163 assertEquals("out3 value", "00,032", out3.getConverter() 164 .getAsString(faces, out3, new Double (32))); 165 assertEquals("out4 value", "0.67", out4.getConverter() 166 .getAsString(faces, out4, new Double (2.0/3.0))); 167 assertEquals("out5 value", "67%", out5.getConverter() 168 .getAsString(faces, out5, new Double (0.67))); 169 } 170 171 public void testFacetHandler() throws Exception { 172 FacesContext faces = FacesContext.getCurrentInstance(); 173 174 FaceletFactory f = FaceletFactory.getInstance(); 175 Facelet at = f.getFacelet("facet.xml"); 176 177 UIViewRoot root = faces.getViewRoot(); 178 at.apply(faces, root); 179 180 UIData data = (UIData) root.findComponent("table"); 181 182 assertNotNull("data", data); 183 184 UIComponent footer = data.getFooter(); 185 186 assertNotNull("footer", footer); 187 } 188 189 public void testLoadBundleHandler() throws Exception { 190 FacesContext faces = FacesContext.getCurrentInstance(); 191 192 FaceletFactory f = FaceletFactory.getInstance(); 193 Facelet at = f.getFacelet("loadBundle.xml"); 194 195 UIViewRoot root = faces.getViewRoot(); 196 at.apply(faces, root); 197 198 Object value = faces.getExternalContext().getRequestMap().get("foo"); 199 200 assertNotNull("bundle loaded into request", value); 201 } 202 203 public void testValidateDelegateHandler() throws Exception { 204 FacesContext faces = FacesContext.getCurrentInstance(); 205 206 FaceletFactory f = FaceletFactory.getInstance(); 207 Facelet at = f.getFacelet("validator.xml"); 208 209 UIViewRoot root = faces.getViewRoot(); 210 at.apply(faces, root); 211 212 UIInput input = (UIInput) root.findComponent("form:input"); 213 214 assertNotNull("input", input); 215 216 assertEquals("input validator", 1, input.getValidators().length); 217 218 Validator v = input.getValidators()[0]; 219 220 v.validate(faces, input, "4333"); 221 } 222 223 public void testValidateDoubleRangeHandler() throws Exception { 224 FacesContext faces = FacesContext.getCurrentInstance(); 225 226 FaceletFactory f = FaceletFactory.getInstance(); 227 Facelet at = f.getFacelet("validateDoubleRange.xml"); 228 229 UIViewRoot root = faces.getViewRoot(); 230 at.apply(faces, root); 231 232 UIInput input = (UIInput) root.findComponent("form:input"); 233 234 assertNotNull("input", input); 235 236 assertEquals("input validator", 1, input.getValidators().length); 237 238 Validator v = input.getValidators()[0]; 239 240 v.validate(faces, input, new Double (1.8)); 241 } 242 243 public void testValidateLengthHandler() throws Exception { 244 FacesContext faces = FacesContext.getCurrentInstance(); 245 246 FaceletFactory f = FaceletFactory.getInstance(); 247 Facelet at = f.getFacelet("validateLength.xml"); 248 249 UIViewRoot root = faces.getViewRoot(); 250 at.apply(faces, root); 251 252 UIInput input = (UIInput) root.findComponent("form:input"); 253 254 assertNotNull("input", input); 255 256 assertEquals("input validator", 1, input.getValidators().length); 257 258 Validator v = input.getValidators()[0]; 259 260 v.validate(faces, input, "beans"); 261 } 262 263 public void testValidateLongRangeHandler() throws Exception { 264 FacesContext faces = FacesContext.getCurrentInstance(); 265 266 FaceletFactory f = FaceletFactory.getInstance(); 267 Facelet at = f.getFacelet("validateLongRange.xml"); 268 269 UIViewRoot root = faces.getViewRoot(); 270 at.apply(faces, root); 271 272 UIInput input = (UIInput) root.findComponent("form:input"); 273 274 assertNotNull("input", input); 275 276 assertEquals("input validator", 1, input.getValidators().length); 277 278 Validator v = input.getValidators()[0]; 279 280 v.validate(faces, input, new Long (2000)); 281 } 282 283 public void testValueChangeListenerHandler() throws Exception { 284 FacesContext faces = FacesContext.getCurrentInstance(); 285 286 FaceletFactory f = FaceletFactory.getInstance(); 287 Facelet at = f.getFacelet("valueChangeListener.xml"); 288 289 UIViewRoot root = faces.getViewRoot(); 290 at.apply(faces, root); 291 292 UIInput input = (UIInput) root.findComponent("form:input"); 293 294 assertNotNull("input", input); 295 296 assertEquals("input listener", 1, input.getValueChangeListeners().length); 297 } 298 299 public void testViewHandler() throws Exception { 300 301 FacesContext faces = FacesContext.getCurrentInstance(); 302 303 FaceletFactory f = FaceletFactory.getInstance(); 304 Facelet at = f.getFacelet("view.xml"); 305 306 UIViewRoot root = faces.getViewRoot(); 307 at.apply(faces, root); 308 309 assertEquals("german locale", Locale.GERMAN, root.getLocale()); 310 } 311 312 } 313 | Popular Tags |