@Test
 public void testInvokeWithSuperAAAB() {
   MethodExpression me11 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanAAA,beanB)}", null, null);
   Object r11 = me11.invoke(context, null);
   assertEquals("AAB: Hello AAA from B", r11.toString());
 }
Ejemplo n.º 2
0
 public void closeListener(CloseEvent event) {
   FacesContext context = FacesContext.getCurrentInstance();
   MethodExpression ajaxEventListener = (MethodExpression) getAttributes().get("closeListener");
   if (ajaxEventListener != null) {
     ajaxEventListener.invoke(context.getELContext(), new Object[] {event});
   }
 }
 @Test
 public void testInvokeWithSuperABNoReturnTypeNoParamTypes() {
   MethodExpression me2 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanA,beanB)}", null, null);
   Object r2 = me2.invoke(context, null);
   assertEquals("AB: Hello A from B", r2.toString());
 }
Ejemplo n.º 4
0
  /**
   * An action listener for tested JSF component. Can be modified dynamically.
   *
   * @param event event representing the activation of a user interface component
   */
  public void actionListener(ActionEvent event) {
    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    MethodExpression method = null;

    if (attributes.get("actionListener") == null) {
      return;
    }

    String listener = (String) attributes.get("actionListener").getValue();

    if (listener == null) {
      return;
    }

    RichBean.logToPage("* action listener invoked");

    // if no select options for "actionListener" are defined in property file and it is an EL
    // expression
    if (!hasSelectOptions("actionListener") && isStringEL(listener)) {
      method =
          getExpressionFactory()
              .createMethodExpression(
                  elContext, listener, void.class, new Class[] {ActionEvent.class});
      method.invoke(elContext, new Object[] {event});
    }

    // if select options for "actionListener" are defined in property file
    if (hasSelectOptions("actionListener")) {
      method =
          getExpressionFactory()
              .createMethodExpression(
                  elContext, getMethodEL(listener), void.class, new Class[] {ActionEvent.class});
      method.invoke(elContext, new Object[] {event});
    }
  }
 @Test
 public void testBug50449b() throws Exception {
   MethodExpression me1 =
       factory.createMethodExpression(context, "${beanB.sayHello('Tomcat')}", null, null);
   String actual = (String) me1.invoke(context, null);
   assertEquals("Hello Tomcat from B", actual);
 }
Ejemplo n.º 6
0
  /**
   * An action for tested JSF component. Can be modified dynamically.
   *
   * @return outcome of an action or null if no navigation should be performed
   */
  public String action() {
    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
    MethodExpression method = null;

    if (attributes.get("action") == null) {
      return null;
    }

    String outcome = (String) attributes.get("action").getValue();

    if (outcome == null) {
      return null;
    }

    RichBean.logToPage("* action invoked");

    // if no select options for "action" are defined in property file and it is an EL expression
    if (!hasSelectOptions("action") && isStringEL(outcome)) {
      method =
          getExpressionFactory()
              .createMethodExpression(elContext, outcome, String.class, new Class[0]);
      return (String) method.invoke(elContext, null);
    }

    // if select options for "action" are defined in property file
    if (hasSelectOptions("action")) {
      method =
          getExpressionFactory()
              .createMethodExpression(elContext, getMethodEL(outcome), String.class, new Class[0]);

      return (String) method.invoke(elContext, null);
    }

    return outcome;
  }
 @Test
 public void testInvokeWithSuperABBB() {
   MethodExpression me7 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanA,beanBBB)}", null, null);
   Object r7 = me7.invoke(context, null);
   assertEquals("ABB: Hello A from BBB", r7.toString());
 }
 @Test
 public void testBug53792c() {
   MethodExpression me =
       factory.createMethodExpression(
           context, "#{beanB.sayHello().length()}", null, new Class<?>[] {});
   Integer result = (Integer) me.invoke(context, null);
   assertEquals(beanB.sayHello().length(), result.intValue());
 }
 @Test
 public void testInvokeWithVarArgsAAABBB() throws Exception {
   MethodExpression me9 =
       factory.createMethodExpression(
           context, "${beanC.sayHello(beanAAA,beanBBB,beanBBB)}", null, null);
   Object r9 = me9.invoke(context, null);
   assertEquals("ABB[]: Hello AAA from BBB, BBB", r9.toString());
 }
Ejemplo n.º 10
0
 public MethodExpression createMethodExpression(
     ELContext context, String expression, Class expectedReturnType, Class[] expectedParamTypes) {
   ExpressionBuilder builder = new ExpressionBuilder(expression, context);
   MethodExpression me = builder.createMethodExpression(expectedReturnType, expectedParamTypes);
   if (expectedParamTypes == null && !me.isParmetersProvided()) {
     throw new NullPointerException(MessageFactory.get("error.method.nullParms"));
   }
   return me;
 }
Ejemplo n.º 11
0
 public Object invokeMethodExpression(
     String expr, Class returnType, Class[] argTypes, Object[] args) {
   FacesContext fc = FacesContext.getCurrentInstance();
   ELContext elctx = fc.getELContext();
   ExpressionFactory elFactory = fc.getApplication().getExpressionFactory();
   MethodExpression methodExpr =
       elFactory.createMethodExpression(elctx, expr, returnType, argTypes);
   return methodExpr.invoke(elctx, args);
 }
Ejemplo n.º 12
0
  protected void encodeSummaryRow(FacesContext context, DataTable table, SummaryRow summaryRow)
      throws IOException {
    MethodExpression me = summaryRow.getListener();
    if (me != null) {
      me.invoke(context.getELContext(), new Object[] {table.getSortBy()});
    }

    summaryRow.encodeAll(context);
  }
 @Test
 public void testBugPrimitives() throws Exception {
   MethodExpression me =
       factory.createMethodExpression(context, "${beanA.setValLong(5)}", null, null);
   me.invoke(context, null);
   ValueExpression ve =
       factory.createValueExpression(context, "#{beanA.valLong}", java.lang.String.class);
   assertEquals("5", ve.getValue(context));
 }
 @Test
 public void testInvokeWithSuperAAABB() {
   // The Java compiler reports this as ambiguous. Using the parameter that
   // matches exactly seems reasonable to limit the scope of the method
   // search so the EL will find a match.
   MethodExpression me12 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanAAA,beanBB)}", null, null);
   Object r12 = me12.invoke(context, null);
   assertEquals("ABB: Hello AAA from BB", r12.toString());
 }
Ejemplo n.º 15
0
 @Override
 public void broadcast(FacesEvent event) {
   super.broadcast(event);
   if (this.equals(event.getComponent()) && event instanceof FormChageEvent) {
     MethodExpression listener = (MethodExpression) getAttributes().get("listener");
     if (listener != null) {
       listener.invoke(FacesContext.getCurrentInstance().getELContext(), null);
     }
   }
 }
 @Test
 public void testInvokeWithSuper() {
   MethodExpression me =
       factory.createMethodExpression(
           context, "${beanA.setBean(beanBB)}", null, new Class<?>[] {TesterBeanB.class});
   me.invoke(context, null);
   ValueExpression ve = factory.createValueExpression(context, "${beanA.bean.name}", String.class);
   Object r = ve.getValue(context);
   assertEquals("BB", r);
 }
  @Test
  public void testInvoke() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("B");

    context
        .getVariableMapper()
        .setVariable("beanB", factory.createValueExpression(beanB, TesterBeanB.class));

    MethodExpression me1 =
        factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 =
        factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class, new Class<?>[] {String.class});
    MethodExpression me3 =
        factory.createMethodExpression(
            context, "${beanB.sayHello}", String.class, new Class<?>[] {String.class});

    assertEquals("B", me1.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, null));
    assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] {"JUnit2"}));
    assertEquals("Hello JUnit2 from B", me3.invoke(context, new Object[] {"JUnit2"}));
    assertEquals("Hello JUnit from B", me2.invoke(context, new Object[] {null}));
    assertEquals("Hello  from B", me3.invoke(context, new Object[] {null}));
  }
 /*
  * This is also tested implicitly in numerous places elsewhere in this class.
  */
 @Test
 public void testBug49655() throws Exception {
   // This is the call the failed
   MethodExpression me =
       factory.createMethodExpression(context, "#{beanA.setName('New value')}", null, null);
   // The rest is to check it worked correctly
   me.invoke(context, null);
   ValueExpression ve =
       factory.createValueExpression(context, "#{beanA.name}", java.lang.String.class);
   assertEquals("New value", ve.getValue(context));
 }
 @Test
 public void testInvokeWithSuperABReturnTypeParamTypes() {
   MethodExpression me5 =
       factory.createMethodExpression(
           context,
           "${beanC.sayHello(beanA,beanB)}",
           String.class,
           new Class<?>[] {TesterBeanA.class, TesterBeanB.class});
   Object r5 = me5.invoke(context, null);
   assertEquals("AB: Hello A from B", r5.toString());
 }
 @Override
 public Object evaluateMethodExpression(String expression, Object... values)
     throws UnsupportedEvaluationException {
   String el = toELExpression(expression);
   FacesContext facesContext = getFacesContext();
   ELContext elContext = facesContext.getELContext();
   ExpressionFactory expressionFactory = facesContext.getApplication().getExpressionFactory();
   MethodExpression methodExpression =
       expressionFactory.createMethodExpression(
           elContext, el, Object.class, new Class[values.length]);
   return methodExpression.invoke(elContext, values);
 }
  @Test
  public void testBug52970() {
    MethodExpression me =
        factory.createMethodExpression(
            context, "${beanEnum.submit('APPLE')}", null, new Class<?>[] {TesterBeanEnum.class});
    me.invoke(context, null);

    ValueExpression ve =
        factory.createValueExpression(context, "#{beanEnum.lastSubmitted}", TesterEnum.class);
    TesterEnum actual = (TesterEnum) ve.getValue(context);
    assertEquals(TesterEnum.APPLE, actual);
  }
  @Test
  public void testBug52445a() {
    MethodExpression me =
        factory.createMethodExpression(
            context, "${beanA.setBean(beanBB)}", null, new Class<?>[] {TesterBeanB.class});
    me.invoke(context, null);

    MethodExpression me1 =
        factory.createMethodExpression(context, "${beanA.bean.sayHello()}", null, null);
    String actual = (String) me1.invoke(context, null);
    assertEquals("Hello from BB", actual);
  }
 @Test
 public void testInvokeWithVarArgsAB() throws Exception {
   MethodExpression me1 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanA,beanB,beanB)}", null, null);
   Exception e = null;
   try {
     me1.invoke(context, null);
   } catch (Exception e1) {
     e = e1;
   }
   // Expected to fail
   assertNotNull(e);
 }
 @Test
 public void testInvokeWithSuperAAABBB() {
   MethodExpression me13 =
       factory.createMethodExpression(context, "${beanC.sayHello(beanAAA,beanBBB)}", null, null);
   Exception e = null;
   try {
     me13.invoke(context, null);
   } catch (Exception e1) {
     e = e1;
   }
   // Expected to fail
   assertNotNull(e);
 }
  /**
   * @param writer
   * @param component
   * @param context
   * @param scripts
   * @param styles
   * @throws java.io.IOException
   */
  public static void writeRequiredIncludes(
      ResponseWriter writer,
      UIComponent component,
      FacesContext context,
      String[] scripts,
      String[] styles)
      throws IOException {

    /*
     * Prepare access to the managed bean NitobiIncludes
     */
    ELContext elContext = context.getELContext();
    ExpressionFactory expressionFactory = context.getApplication().getExpressionFactory();
    MethodExpression methodExpression =
        expressionFactory.createMethodExpression(
            elContext,
            "#{NitobiIncludes.registerInclude}",
            Boolean.TYPE,
            new Class[] {String.class});

    /*
     * Include the toolkit if needed
     */
    if ((Boolean) methodExpression.invoke(elContext, new Object[] {NitobiIncludes.TOOLKIT})) {
      writeScriptInclude(writer, component, NitobiIncludes.TOOLKIT);
    }

    /*
     * Include the scripts
     */
    if (scripts != null) {
      for (String include : scripts) {
        if ((Boolean) methodExpression.invoke(elContext, new Object[] {include})) {
          writeScriptInclude(writer, component, include);
        }
      }
    }

    /*
     * Include the styles
     */
    if (styles != null) {
      for (String style : styles) {
        if ((Boolean) methodExpression.invoke(elContext, new Object[] {style})) {
          writeCSSInclude(writer, component, style);
        }
      }
    }
  }
Ejemplo n.º 26
0
 @Test
 public void testMethExpr() {
   MethodExpression meth = null;
   ELContext ctxt = elm.getELContext();
   try {
     meth = factory.createMethodExpression(ctxt, "#{str.length}", Object.class, null);
   } catch (NullPointerException ex) {
     // Do nothing
   }
   assertTrue(meth == null);
   meth = factory.createMethodExpression(ctxt, "#{'abc'.length()}", Object.class, null);
   Object result = meth.invoke(ctxt, new Object[] {"abcde"});
   System.out.println("'abc'.length() called, equals " + result);
   assertEquals(result, new Integer(3));
 }
  @Test
  public void testIsParametersProvided() {
    TesterBeanB beanB = new TesterBeanB();
    beanB.setName("Tomcat");
    ValueExpression var = factory.createValueExpression(beanB, TesterBeanB.class);
    context.getVariableMapper().setVariable("beanB", var);

    MethodExpression me1 =
        factory.createMethodExpression(
            context, "${beanB.getName}", String.class, new Class<?>[] {});
    MethodExpression me2 =
        factory.createMethodExpression(
            context, "${beanB.sayHello('JUnit')}", String.class, new Class<?>[] {String.class});

    assertFalse(me1.isParmetersProvided());
    assertTrue(me2.isParmetersProvided());
  }
  @Test
  public void processAction_stringNavigationOutcome() {
    when(methodExpression.invoke(any(), any())).thenReturn(new Navigation("/index.xhtml"));

    new ReelsActionListener().processAction(actionEvent);

    verify(navigationHandler, times(1)).handleNavigation(any(), any(), any());
    verify(facesContext, times(1)).renderResponse();
  }
Ejemplo n.º 29
0
  private void invokeCallback(StateItem item) {
    if (!"".equals(item.getCallback())) {
      Application application = FacesContext.getCurrentInstance().getApplication();
      ExpressionFactory expressionFactory = application.getExpressionFactory();
      ELContext el = FacesContext.getCurrentInstance().getELContext();
      try {
        MethodExpression me =
            expressionFactory.createMethodExpression(
                el, item.getCallback(), Void.class, new Class[] {});

        me.invoke(el, null);
      } catch (NullPointerException ex) {
        ex.printStackTrace();
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
Ejemplo n.º 30
0
 /**
  * Notify afterPhase listener that is registered on the View Root.
  *
  * @param context the FacesContext for the current request
  * @param lifecycle lifecycle instance
  */
 private void notifyAfter(FacesContext context, Lifecycle lifecycle) {
   UIViewRoot viewRoot = context.getViewRoot();
   MethodExpression afterPhase = viewRoot.getAfterPhaseListener();
   if (null != afterPhase) {
     try {
       PhaseEvent event = new PhaseEvent(context, PhaseId.RESTORE_VIEW, lifecycle);
       afterPhase.invoke(context.getELContext(), new Object[] {event});
     } catch (Exception e) {
       if (LOGGER.isLoggable(Level.SEVERE)) {
         LOGGER.log(
             Level.SEVERE,
             "severe.component.unable_to_process_expression",
             new Object[] {afterPhase.getExpressionString(), ("afterPhase")});
       }
       return;
     }
   }
 }