Esempio n. 1
0
  public void doTestPhaseMethodExpressionAndListenerWithPhaseId(UIViewRoot root, PhaseId phaseId)
      throws Exception {
    PhaseListenerBean phaseListener = new PhaseListenerBean(phaseId);
    PhaseListenerBean phaseListenerBean = new PhaseListenerBean(phaseId);
    facesContext.getExternalContext().getRequestMap().put("bean", phaseListenerBean);
    Class[] args = new Class[] {PhaseEvent.class};
    MethodExpression
        beforeExpression =
            facesContext
                .getApplication()
                .getExpressionFactory()
                .createMethodExpression(
                    facesContext.getELContext(), "#{bean.beforePhase}", null, args),
        afterExpression =
            facesContext
                .getApplication()
                .getExpressionFactory()
                .createMethodExpression(
                    facesContext.getELContext(), "#{bean.afterPhase}", null, args);
    root.setBeforePhaseListener(beforeExpression);
    root.setAfterPhaseListener(afterExpression);
    root.addPhaseListener(phaseListener);

    callRightLifecycleMethodGivenPhaseId(root, phaseId);

    assertTrue(phaseListenerBean.isBeforePhaseCalled());
    assertTrue(phaseListenerBean.isAfterPhaseCalled());
    assertTrue(phaseListener.isBeforePhaseCalled());
    assertTrue(phaseListener.isAfterPhaseCalled());
  }
Esempio n. 2
0
  public void testPhaseListenerExceptions() throws Exception {
    PhaseId[] ids = {
      PhaseId.APPLY_REQUEST_VALUES,
      PhaseId.PROCESS_VALIDATIONS,
      PhaseId.UPDATE_MODEL_VALUES,
      PhaseId.INVOKE_APPLICATION,
      PhaseId.RENDER_RESPONSE
    };
    Class[] args = new Class[] {PhaseEvent.class};
    MethodExpression beforeExpression =
        facesContext
            .getApplication()
            .getExpressionFactory()
            .createMethodExpression(facesContext.getELContext(), "#{bean.beforePhase}", null, args);
    MethodExpression afterExpression =
        facesContext
            .getApplication()
            .getExpressionFactory()
            .createMethodExpression(facesContext.getELContext(), "#{bean.afterPhase}", null, args);
    for (PhaseId id : ids) {
      UIViewRoot root =
          facesContext.getApplication().getViewHandler().createView(facesContext, null);
      PhaseListenerBean bean = new PhaseListenerBean(id, true, false);
      PhaseListenerBean pl1 = new PhaseListenerBean(id);
      PhaseListenerBean pl2 = new PhaseListenerBean(id);
      facesContext.getExternalContext().getRequestMap().put("bean", bean);
      root.setBeforePhaseListener(beforeExpression);
      root.setAfterPhaseListener(afterExpression);
      root.addPhaseListener(pl1);
      root.addPhaseListener(pl2);

      // validate behavior
      callRightLifecycleMethodGivenPhaseId(root, id);
      assertTrue(bean.isBeforePhaseCalled());
      assertTrue(!bean.isAfterPhaseCalled());
      assertTrue(!pl1.isBeforePhaseCalled());
      assertTrue(!pl1.isAfterPhaseCalled());
      assertTrue(!pl2.isBeforePhaseCalled());
      assertTrue(!pl2.isAfterPhaseCalled());

      // ensure PLs are invoked properly in the case of exceptions
      root = facesContext.getApplication().getViewHandler().createView(facesContext, null);
      bean = new PhaseListenerBean(id);
      pl1 = new PhaseListenerBean(id, true, false);
      pl2 = new PhaseListenerBean(id);
      facesContext.getExternalContext().getRequestMap().put("bean", bean);
      root.setBeforePhaseListener(beforeExpression);
      root.setAfterPhaseListener(afterExpression);
      root.addPhaseListener(pl1);
      root.addPhaseListener(pl2);

      // validate behavior
      callRightLifecycleMethodGivenPhaseId(root, id);
      assertTrue(bean.isBeforePhaseCalled());
      assertTrue(bean.isAfterPhaseCalled());
      assertTrue(pl1.isBeforePhaseCalled());
      assertTrue(!pl1.isAfterPhaseCalled());
      assertTrue(!pl2.isBeforePhaseCalled());
      assertTrue(!pl2.isAfterPhaseCalled());
    }
  }