// Negative tests (should throw exceptions) public void testNegative() throws Exception { VariableResolver variableResolver = getFacesContext().getApplication().getVariableResolver(); Object value = null; // ---------- NullPointerException Returns ---------- try { value = variableResolver.resolveVariable(getFacesContext(), null); fail("Should have thrown NullPointerException"); } catch (NullPointerException e) {; // Expected result } try { value = variableResolver.resolveVariable(null, "foo"); fail("Should have thrown NullPointerException"); } catch (NullPointerException e) {; // Expected result } try { value = variableResolver.resolveVariable(null, null); fail("Should have thrown NullPointerException"); } catch (NullPointerException e) {; // Expected result } }
public void testScopedLookup() { TestBean testBean = new TestBean(); InnerBean newInner, oldInner = new InnerBean(); testBean.setInner(oldInner); VariableResolver variableResolver = getFacesContext().getApplication().getVariableResolver(); Object result = null; getFacesContext().getExternalContext().getSessionMap().remove("TestBean"); // // Get tests // // application getFacesContext().getExternalContext().getApplicationMap().put("TestBean", testBean); result = variableResolver.resolveVariable(getFacesContext(), "TestBean"); assertTrue(result == testBean); getFacesContext().getExternalContext().getApplicationMap().remove("TestBean"); // session getFacesContext().getExternalContext().getSessionMap().put("TestBean", testBean); result = variableResolver.resolveVariable(getFacesContext(), "TestBean"); assertTrue(result == testBean); getFacesContext().getExternalContext().getSessionMap().remove("TestBean"); // session getFacesContext().getExternalContext().getRequestMap().put("TestBean", testBean); result = variableResolver.resolveVariable(getFacesContext(), "TestBean"); assertTrue(result == testBean); getFacesContext().getExternalContext().getRequestMap().remove("TestBean"); }
@Override @SuppressWarnings("deprecation") public Object getValue(ELContext context, Object base, Object property) throws ELException { // Don't call into the chain unless it's been decorated. if (legacyVR instanceof ChainAwareVariableResolver) { return null; } if (base != null) { return null; } if (base == null && property == null) { String message = MessageUtils.getExceptionMessageString( MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID, "base and property"); // ????? throw new PropertyNotFoundException(message); } context.setPropertyResolved(true); Object result = null; FacesContext facesContext = (FacesContext) context.getContext(FacesContext.class); String propString = property.toString(); Map<String, Object> stateMap = RequestStateManager.getStateMap(facesContext); try { // If we are already in the midst of an expression evaluation // that touched this resolver... //noinspection unchecked List<String> varNames = (List<String>) stateMap.get(RequestStateManager.REENTRANT_GUARD); if (varNames != null && !varNames.isEmpty() && varNames.contains(propString)) { // take no action and return. context.setPropertyResolved(false); return null; } // Make sure subsequent calls don't take action. if (varNames == null) { varNames = new ArrayList<>(); stateMap.put(RequestStateManager.REENTRANT_GUARD, varNames); } varNames.add(propString); result = legacyVR.resolveVariable(facesContext, propString); } catch (EvaluationException ex) { context.setPropertyResolved(false); throw new ELException(ex); } finally { // Make sure to remove the guard after the call returns //noinspection unchecked List<String> varNames = (List<String>) stateMap.get(RequestStateManager.REENTRANT_GUARD); if (varNames != null && !varNames.isEmpty()) { varNames.remove(propString); } // Make sure that the ELContext "resolved" indicator is set // in accordance wth the result of the resolution. context.setPropertyResolved(result != null); } return result; }
/** * This test verifies that if the variable resolver does not find a managed bean it tries to * instantiate it if it was added to the application's managed bean factory list. */ public void testManagedBean() throws Exception { String beanName = "com.sun.faces.TestBean"; ManagedBeanInfo beanInfo = new ManagedBeanInfo(beanName, beanName, "session", null, null, null, null); ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); ApplicationImpl application = (ApplicationImpl) aFactory.getApplication(); ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance(); BeanManager manager = associate.getBeanManager(); manager.register(beanInfo); VariableResolver variableResolver = application.getVariableResolver(); Object result = variableResolver.resolveVariable(getFacesContext(), beanName); assertTrue(result instanceof TestBean); }
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()); }