public void setUp() { super.setUp(); UIViewRoot page = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null); page.setViewId("viewId"); page.setLocale(Locale.US); getFacesContext().setViewRoot(page); }
/** Test the built-in conversion for those renderers that have it. */ public void testEmptyStrings() { UIViewRoot root = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null); root.setLocale(Locale.US); UIInput text = new UIInput(), hidden = new UIInput(), secret = new UIInput(); text.setId("text"); hidden.setId("hidden"); secret.setId("secret"); text.setRendererType("Text"); hidden.setRendererType("Hidden"); secret.setRendererType("Secret"); root.getChildren().add(text); root.getChildren().add(hidden); root.getChildren().add(secret); TextRenderer textRenderer = new TextRenderer(); HiddenRenderer hiddenRenderer = new HiddenRenderer(); SecretRenderer secretRenderer = new SecretRenderer(); try { textRenderer.decode(getFacesContext(), text); hiddenRenderer.decode(getFacesContext(), hidden); secretRenderer.decode(getFacesContext(), secret); } catch (Throwable e) { assertTrue(false); } assertTrue(text.isValid()); assertTrue(hidden.isValid()); assertTrue(secret.isValid()); }
/* * This method provides custom logic of wrapping viewRoot instances. * This is done, because different environments casts current viewRoot * instance to their own implementation of UIViewRoot class. * So, we need to comply with inheritance hierarchy. */ private UIViewRoot getAjaxViewRoot(UIViewRoot root, FacesContext context) { UIViewRoot riRoot; Class ajax4jsfViewRootClass = null; try { ajax4jsfViewRootClass = Class.forName("org.ajax4jsf.framework.ajax.AjaxViewRoot"); } catch (ClassNotFoundException e) { // absence of the Ajax4jsf library is a valid case } boolean isAjax4jsf = (ajax4jsfViewRootClass != null); if (isAjax4jsf) throw new IllegalArgumentException( "OpenFaces warning: The old Ajax4jsf framework is not supported. Use RichFaces that now incorporates this framework instead."); Class richFacesAjaxViewRootClass = null; try { richFacesAjaxViewRootClass = Class.forName("org.ajax4jsf.component.AjaxViewRoot"); } catch (ClassNotFoundException e) { // absence of the RichFaces library is a valid case } if (Environment.isExoPortal()) riRoot = createExoAjaxViewRoot(root); else if (richFacesAjaxViewRootClass == null) riRoot = new AjaxViewRoot(); else if (Environment.isGateInPortal(context)) riRoot = createGateInAjaxViewRoot(root); else riRoot = createA4jAjaxViewRoot(root); // fill properties from default. riRoot.setViewId(root.getViewId()); riRoot.setLocale(root.getLocale()); String renderKitId = root.getRenderKitId(); // Fix facelets bug - for debug requests renderKitId is null ! if (null == renderKitId) { renderKitId = calculateRenderKitId(context); } riRoot.setRenderKitId(renderKitId); return riRoot; }
public static void setLocale(FacesContext context, Locale locale) { UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { viewRoot.setLocale(locale); } }
public void testResourceBundle() throws Exception { ResourceBundle rb = null; UIViewRoot root = new UIViewRoot(); root.setLocale(Locale.ENGLISH); getFacesContext().setViewRoot(root); // negative test, non-existant rb rb = application.getResourceBundle(getFacesContext(), "bogusName"); assertNull(rb); // basic test, existing rb rb = application.getResourceBundle(getFacesContext(), "testResourceBundle"); assertNotNull(rb); String value = rb.getString("value1"); assertEquals("Jerry", value); // switch locale to German getFacesContext().getViewRoot().setLocale(Locale.GERMAN); rb = application.getResourceBundle(getFacesContext(), "testResourceBundle"); assertNotNull(rb); value = rb.getString("value1"); assertEquals("Bernhard", value); // switch to a different rb rb = application.getResourceBundle(getFacesContext(), "testResourceBundle2"); assertNotNull(rb); value = rb.getString("label"); assertEquals("Abflug", value); }
private void restoreView(FacesContext context) throws FacesException { Application app = context.getApplication(); if (app instanceof ApplicationImpl) ((ApplicationImpl) app).initRequest(); ViewHandler view = app.getViewHandler(); view.initView(context); UIViewRoot viewRoot = context.getViewRoot(); if (viewRoot != null) { ExternalContext extContext = context.getExternalContext(); viewRoot.setLocale(extContext.getRequestLocale()); doSetBindings(context.getELContext(), viewRoot); return; } String viewId = calculateViewId(context); String renderKitId = view.calculateRenderKitId(context); RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); RenderKit renderKit = renderKitFactory.getRenderKit(context, renderKitId); ResponseStateManager stateManager = renderKit.getResponseStateManager(); if (stateManager.isPostback(context)) { viewRoot = view.restoreView(context, viewId); if (viewRoot != null) { doSetBindings(context.getELContext(), viewRoot); } else { // XXX: backward compat issues with ViewHandler and StateManager // throw new ViewExpiredException(L.l("{0} is an expired view", viewId)); context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } context.setViewRoot(viewRoot); } else { context.renderResponse(); viewRoot = view.createView(context, viewId); context.setViewRoot(viewRoot); } }
// // Methods from TestCase // public void setUp() { super.setUp(); ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); application = aFactory.getApplication(); UIViewRoot viewRoot = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null); viewRoot.setViewId("viewId"); viewRoot.setLocale(Locale.US); getFacesContext().setViewRoot(viewRoot); }
/** * @param context * @param resource * @throws IOException */ public void send(ResourceContext resourceContext, InternetResource resource) throws IOException { FacesContext facesContext = FacesContext.getCurrentInstance(); if (null != facesContext) { Lifecycle facesLifecycle = getFacesLifecycle(); PhaseListener[] phaseListeners = facesLifecycle.getPhaseListeners(); PhaseEvent restoreViewEvent = new PhaseEvent(facesContext, PhaseId.RESTORE_VIEW, this); processPhaseListeners(phaseListeners, restoreViewEvent, true); // Fix for a http://jira.jboss.org/jira/browse/RF-1056 if (facesContext.getResponseComplete()) { return; } // fix for a http://jira.jboss.com/jira/browse/RF-1064 . // viewRoot can be created outside. UIViewRoot savedViewRoot = facesContext.getViewRoot(); try { // create "dummy" viewRoot, to avoid problems in phase listeners. UIViewRoot root = new UIViewRoot(); String key = resource.getKey(); if (null != key && !key.startsWith("/")) { key = "/" + key; } root.setViewId(key); root.setLocale(Locale.getDefault()); root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); facesContext.setViewRoot(root); // We do not simulate other phases. facesContext.renderResponse(); // Invoke after restore view phase listeners processPhaseListeners(phaseListeners, restoreViewEvent, false); // Fix for a http://jira.jboss.org/jira/browse/RF-1056 if (!facesContext.getResponseComplete()) { // Invoke before render view phase listeners PhaseEvent renderViewEvent = new PhaseEvent(facesContext, PhaseId.RENDER_RESPONSE, this); try { processPhaseListeners(phaseListeners, renderViewEvent, true); sendResource(resourceContext, resource); } finally { processPhaseListeners(phaseListeners, renderViewEvent, false); } } } finally { if (null != savedViewRoot) { facesContext.setViewRoot(savedViewRoot); } } } else { sendResource(resourceContext, resource); } }
public void testImplicitObjects() { VariableResolver variableResolver = getFacesContext().getApplication().getVariableResolver(); Object result = null; // // test scope maps // // ApplicationMap assertTrue( variableResolver.resolveVariable(getFacesContext(), "applicationScope") == getFacesContext().getExternalContext().getApplicationMap()); // SessionMap assertTrue( variableResolver.resolveVariable(getFacesContext(), "sessionScope") == getFacesContext().getExternalContext().getSessionMap()); // RequestMap assertTrue( variableResolver.resolveVariable(getFacesContext(), "requestScope") == getFacesContext().getExternalContext().getRequestMap()); // // test request objects // // cookie assertTrue( variableResolver.resolveVariable(getFacesContext(), "cookie") == getFacesContext().getExternalContext().getRequestCookieMap()); // header assertTrue( variableResolver.resolveVariable(getFacesContext(), "header") == getFacesContext().getExternalContext().getRequestHeaderMap()); // headerValues assertTrue( variableResolver.resolveVariable(getFacesContext(), "headerValues") == getFacesContext().getExternalContext().getRequestHeaderValuesMap()); // parameter assertTrue( variableResolver.resolveVariable(getFacesContext(), "param") == getFacesContext().getExternalContext().getRequestParameterMap()); // parameterValues assertTrue( variableResolver.resolveVariable(getFacesContext(), "paramValues") == getFacesContext().getExternalContext().getRequestParameterValuesMap()); // // misc // // initParameter assertTrue( variableResolver.resolveVariable(getFacesContext(), "initParam") == getFacesContext().getExternalContext().getInitParameterMap()); // facesContext assertTrue( variableResolver.resolveVariable(getFacesContext(), "facesContext") == getFacesContext()); // tree // create a dummy root for the tree. UIViewRoot page = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null); page.setId("root"); page.setViewId("newTree"); page.setLocale(Locale.US); getFacesContext().setViewRoot(page); assertTrue( variableResolver.resolveVariable(getFacesContext(), "view") == getFacesContext().getViewRoot()); }
public void setCurrentLocale(String locale) { UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot(); viewRoot.setLocale(new Locale(locale)); }
public void testUISelectMany() throws Exception { // create the test bean TestBean bean = new TestBean(); getFacesContext().getExternalContext().getRequestMap().put("bean", bean); // create a dummy root for the tree. UIViewRoot root = Util.getViewHandler(getFacesContext()).createView(getFacesContext(), null); root.setId("root"); root.setLocale(Locale.US); getFacesContext().setViewRoot(root); // test model type of boolean [] UISelectMany booleanv = new UISelectMany(); booleanv.setId("bool"); booleanv.setRendererType("javax.faces.Checkbox"); booleanv.setValueExpression( "value", (getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.booleans}", Boolean.class))); root.getChildren().add(booleanv); booleanv.getChildren().add(newUISelectItem(Boolean.TRUE)); booleanv.getChildren().add(newUISelectItem(Boolean.FALSE)); booleanv.decode(getFacesContext()); booleanv.validate(getFacesContext()); booleanv.updateModel(getFacesContext()); assertNotNull(bean.getBooleans()); assertTrue(bean.getBooleans()[0] == false); assertTrue(bean.getBooleans()[1] == true); assertTrue(bean.getBooleans()[2] == false); // test model type of boolean [] booleanv = new UISelectMany(); booleanv.setId("bool2"); booleanv.setRendererType("javax.faces.Checkbox"); booleanv.setValueExpression( "value", (getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.booleans}", Object.class))); root.getChildren().add(booleanv); booleanv.getChildren().add(newUISelectItem(Boolean.TRUE)); booleanv.getChildren().add(newUISelectItem(Boolean.FALSE)); booleanv.decode(getFacesContext()); booleanv.validate(getFacesContext()); booleanv.updateModel(getFacesContext()); assertNotNull(bean.getBooleans()); assertTrue(bean.getBooleans()[0] == false); assertTrue(bean.getBooleans().length == 1); // test model type of byte [] UISelectMany bytev = new UISelectMany(); bytev.setId("byte"); bytev.setRendererType("javax.faces.Checkbox"); bytev.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.bytes}", Object.class)); bytev.getChildren().add(newUISelectItem(new Byte(Byte.MIN_VALUE))); bytev.getChildren().add(newUISelectItem(new Byte(Byte.MAX_VALUE))); bytev.getChildren().add(newUISelectItem(new Byte((byte) 1))); bytev.getChildren().add(newUISelectItem(new Byte((byte) -1))); root.getChildren().add(bytev); bytev.decode(getFacesContext()); bytev.validate(getFacesContext()); bytev.updateModel(getFacesContext()); assertNotNull(bean.getBytes()); assertTrue(bean.getBytes()[0] == Byte.MIN_VALUE); assertTrue(bean.getBytes()[1] == Byte.MAX_VALUE); assertTrue(bean.getBytes()[2] == 1); // test model type of char [] UISelectMany charv = new UISelectMany(); charv.setId("char"); charv.setRendererType("javax.faces.Checkbox"); charv.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.chars}", Object.class)); root.getChildren().add(charv); charv.getChildren().add(newUISelectItem(new Character('Q'))); charv.getChildren().add(newUISelectItem(new Character('A'))); charv.getChildren().add(newUISelectItem(new Character('Z'))); charv.getChildren().add(newUISelectItem(new Character('z'))); charv.decode(getFacesContext()); charv.validate(getFacesContext()); charv.updateModel(getFacesContext()); assertNotNull(bean.getChars()); assertTrue(bean.getChars()[0] == 'Q'); assertTrue(bean.getChars()[1] == 'A'); assertTrue(bean.getChars()[2] == 'z'); // test model type of short [] UISelectMany shortv = new UISelectMany(); shortv.setId("short"); shortv.setRendererType("javax.faces.Checkbox"); shortv.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.shorts}", Object.class)); root.getChildren().add(shortv); shortv.getChildren().add(newUISelectItem(new Short((short) (Byte.MAX_VALUE + 1)))); shortv.getChildren().add(newUISelectItem(new Short((short) 100))); shortv.getChildren().add(newUISelectItem(new Short(Short.MIN_VALUE))); shortv.getChildren().add(newUISelectItem(new Short(Short.MAX_VALUE))); shortv.decode(getFacesContext()); shortv.validate(getFacesContext()); shortv.updateModel(getFacesContext()); assertNotNull(bean.getShorts()); assertTrue(bean.getShorts()[0] == Short.MIN_VALUE); assertTrue(bean.getShorts()[1] == Short.MAX_VALUE); assertTrue(bean.getShorts()[2] == Byte.MAX_VALUE + 1); // test model type of int [] UISelectMany intv = new UISelectMany(); intv.setId("int"); intv.setRendererType("javax.faces.Checkbox"); intv.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression(getFacesContext().getELContext(), "#{bean.ints}", Object.class)); root.getChildren().add(intv); intv.getChildren().add(newUISelectItem(new Integer(Short.MAX_VALUE + 1))); intv.getChildren().add(newUISelectItem(new Integer(100))); intv.getChildren().add(newUISelectItem(new Integer(Integer.MIN_VALUE))); intv.getChildren().add(newUISelectItem(new Integer(Integer.MAX_VALUE))); intv.decode(getFacesContext()); intv.validate(getFacesContext()); intv.updateModel(getFacesContext()); assertNotNull(bean.getInts()); assertTrue(bean.getInts()[0] == Integer.MIN_VALUE); assertTrue(bean.getInts()[1] == Integer.MAX_VALUE); assertTrue(bean.getInts()[2] == Short.MAX_VALUE + 1); // test model type of float [] UISelectMany floatv = new UISelectMany(); floatv.setId("float"); floatv.setRendererType("javax.faces.Checkbox"); floatv.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.floats}", Object.class)); root.getChildren().add(floatv); floatv.getChildren().add(newUISelectItem(new Float(Integer.MAX_VALUE + 1))); floatv.getChildren().add(newUISelectItem(new Float(100))); floatv.getChildren().add(newUISelectItem(new Float(Float.MIN_VALUE))); floatv.getChildren().add(newUISelectItem(new Float(Float.MAX_VALUE))); floatv.decode(getFacesContext()); floatv.validate(getFacesContext()); floatv.updateModel(getFacesContext()); assertNotNull(bean.getFloats()); assertTrue(bean.getFloats()[0] == Float.MIN_VALUE); assertTrue(bean.getFloats()[1] == Float.MAX_VALUE); assertTrue(bean.getFloats()[2] == Integer.MAX_VALUE + 1); // test model type of long [] UISelectMany longv = new UISelectMany(); longv.setId("long"); longv.setRendererType("javax.faces.Checkbox"); longv.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.longs}", Object.class)); root.getChildren().add(longv); longv.getChildren().add(newUISelectItem(new Long(Integer.MAX_VALUE + 1))); longv.getChildren().add(newUISelectItem(new Long(100))); longv.getChildren().add(newUISelectItem(new Long(Long.MIN_VALUE))); longv.getChildren().add(newUISelectItem(new Long(Long.MAX_VALUE))); longv.decode(getFacesContext()); longv.validate(getFacesContext()); longv.updateModel(getFacesContext()); assertNotNull(bean.getLongs()); assertTrue(bean.getLongs()[0] == Long.MIN_VALUE); assertTrue(bean.getLongs()[1] == Long.MAX_VALUE); assertTrue(bean.getLongs()[2] == Integer.MAX_VALUE + 1); // test model type of double [] UISelectMany doublev = new UISelectMany(); doublev.setId("double"); doublev.setRendererType("javax.faces.Checkbox"); doublev.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.doubles}", Object.class)); root.getChildren().add(doublev); doublev.getChildren().add(newUISelectItem(new Double(Long.MAX_VALUE + 1))); doublev.getChildren().add(newUISelectItem(new Double(100))); doublev.getChildren().add(newUISelectItem(new Double(Double.MIN_VALUE))); doublev.getChildren().add(newUISelectItem(new Double(Double.MAX_VALUE))); doublev.decode(getFacesContext()); doublev.validate(getFacesContext()); doublev.updateModel(getFacesContext()); assertNotNull(bean.getDoubles()); assertTrue(bean.getDoubles()[0] == Double.MIN_VALUE); assertTrue(bean.getDoubles()[1] == Double.MAX_VALUE); assertTrue(bean.getDoubles()[2] == Long.MAX_VALUE + 1); // test model type of String [] UISelectMany str = new UISelectMany(); str.setId("str"); str.setRendererType("javax.faces.Checkbox"); str.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.strings}", Object.class)); root.getChildren().add(str); str.getChildren().add(newUISelectItem("value1")); str.getChildren().add(newUISelectItem("value2")); str.getChildren().add(newUISelectItem("value3")); str.getChildren().add(newUISelectItem("value4")); str.decode(getFacesContext()); str.validate(getFacesContext()); str.updateModel(getFacesContext()); assertNotNull(bean.getStrings()); assertTrue("value1".equals(bean.getStrings()[0])); // test model type of Date [] UISelectMany date = new UISelectMany(); Converter dateConv = Util.getConverterForIdentifer("javax.faces.DateTime", getFacesContext()); assertNotNull(dateConv); date.setConverter(dateConv); date.setId("date"); date.setRendererType("javax.faces.Checkbox"); date.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.dates}", Object.class)); root.getChildren().add(date); try { SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); df.setTimeZone(TimeZone.getTimeZone("GMT")); date.getChildren().add(newUISelectItem(df.parse("19670101"))); date.getChildren().add(newUISelectItem(df.parse("20030526"))); date.getChildren().add(newUISelectItem(df.parse("19460819"))); date.getChildren().add(newUISelectItem(df.parse("17760704"))); } catch (ParseException e) { assertTrue(e.getMessage(), false); } date.decode(getFacesContext()); date.validate(getFacesContext()); date.updateModel(getFacesContext()); assertNotNull(bean.getDates()); Object expected = null; try { SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); df.setTimeZone(TimeZone.getTimeZone("GMT")); expected = df.parse("19460819"); } catch (ParseException e) { assertTrue(e.getMessage(), false); } assertEquals("bean.getDates()[2] not as expected: ", expected, bean.getDates()[2]); // test model type of Number [] UISelectMany number = new UISelectMany(); Converter numberConv = Util.getConverterForIdentifer("javax.faces.Number", getFacesContext()); assertNotNull(numberConv); number.setConverter(numberConv); number.setId("num"); number.setRendererType("javax.faces.Checkbox"); number.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.numbers}", Object.class)); root.getChildren().add(number); number.getChildren().add(newUISelectItem(new Double(3.14))); number.getChildren().add(newUISelectItem(new Double(49.99))); number.getChildren().add(newUISelectItem(new Long(12))); number.getChildren().add(newUISelectItem(new Double(-145.5))); number.decode(getFacesContext()); number.validate(getFacesContext()); number.updateModel(getFacesContext()); assertNotNull(bean.getNumbers()); try { DecimalFormat df = new DecimalFormat("'$'##.##", new DecimalFormatSymbols(Locale.US)); expected = df.parse("$49.99"); } catch (ParseException e) { assertTrue(e.getMessage(), false); } assertTrue(expected.equals(bean.getNumbers()[2])); // test model type of List of Strings UISelectMany stringList = new UISelectMany(); stringList.setId("stringList"); stringList.setRendererType("javax.faces.Checkbox"); stringList.setValueExpression( "value", getFacesContext() .getApplication() .getExpressionFactory() .createValueExpression( getFacesContext().getELContext(), "#{bean.stringList}", Object.class)); root.getChildren().add(stringList); stringList.getChildren().add(newUISelectItem("value1")); stringList.getChildren().add(newUISelectItem("value2")); stringList.getChildren().add(newUISelectItem("value3")); stringList.getChildren().add(newUISelectItem("value4")); stringList.decode(getFacesContext()); stringList.validate(getFacesContext()); stringList.updateModel(getFacesContext()); assertNotNull(bean.getStringList()); assertTrue(bean.getStringList().get(0).equals("value1")); assertTrue(bean.getStringList().get(1).equals("value2")); assertTrue(bean.getStringList().get(2).equals("value3")); }