public void testCAllExecute2Times() throws Exception {
    setToken(request);
    ActionProxy proxy = buildProxy(getActionName());
    assertEquals(Action.SUCCESS, proxy.execute());

    ActionProxy proxy2 = buildProxy(getActionName());
    // must not call setToken
    // double post will result in a invalid.token return code
    assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy2.execute());
  }
  public void testCAllExecute2Times() throws Exception {
    setToken(request);
    ActionProxy proxy = buildProxy(getActionName());
    assertEquals(Action.SUCCESS, proxy.execute());

    ActionProxy proxy2 = buildProxy(getActionName());
    // must not call setToken
    // double post will just return success and render the same view as the first execute
    // see TokenInterceptor where a 2nd call will return invalid.token code instead
    assertEquals(Action.SUCCESS, proxy2.execute());
  }
  /**
   * Test adding SmartPoints to Groups.
   *
   * @throws Exception the exception
   */
  public void testImportEcoMode() throws Exception {
    SessionAuthenticationTestUtil.setAuthenticationTest();
    SessionAuthenticationTestUtil.setMockSession();

    request.setSession(SessionAuthenticationTestUtil.setSessionTest());

    /**
     * *********************************
     *
     * <p>TESTING FAILURE
     *
     * <p>*********************************
     */
    logger.debug("TESTING FAILURE");

    ActionProxy proxy = getActionProxy("/systemsettings/ajax.importEcoMode.action");

    EcoModeAjaxAction action = (EcoModeAjaxAction) proxy.getAction();

    action.setServletRequest(request);

    EcoModeBCFMockImpl bcf = (EcoModeBCFMockImpl) action.getEcoModeBCF();
    bcf.setMode(BaseMockImpl.MODE_FAILURE);

    /** Asserts * */
    String actionResponse = proxy.execute();
    assertEquals(MESSAGE_STRUTS_OUTCOME, EXCEPTION, actionResponse);

    /**
     * *********************************
     *
     * <p>TESTING SUCCESS
     *
     * <p>*********************************
     */
    logger.debug("TESTING SUCCESS");

    proxy = getActionProxy("/systemsettings/ajax.importEcoMode.action");
    action = (EcoModeAjaxAction) proxy.getAction();
    SessionAuthenticationTestUtil.setMockSession();
    action.setServletRequest(request);

    action.setUploadEcoModeFile(new File("arquivo.csv"));
    action.setUploadTag("1,2,3,4");
    // Create REQUEST mock ---- END

    bcf = (EcoModeBCFMockImpl) action.getEcoModeBCF();
    bcf.setMode(BaseMockImpl.MODE_SUCCESS);

    /** Asserts * */
    actionResponse = proxy.execute();
    assertEquals(MESSAGE_STRUTS_OUTCOME, EXCEPTION, actionResponse);
  }
  public void testRangeValidation() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("longFoo", "200");

    HashMap<String, Object> extraContext = new HashMap<String, Object>();
    extraContext.put(ActionContext.PARAMETERS, params);

    try {
      ActionProxy proxy =
          actionProxyFactory.createActionProxy(
              "", MockConfigurationProvider.VALIDATION_ACTION_NAME, extraContext);
      proxy.execute();
      assertTrue(((ValidationAware) proxy.getAction()).hasFieldErrors());

      Map errors = ((ValidationAware) proxy.getAction()).getFieldErrors();
      List errorMessages = (List) errors.get("longFoo");
      assertEquals(1, errorMessages.size());

      String errorMessage = (String) errorMessages.get(0);
      assertNotNull(errorMessage);
    } catch (Exception e) {
      e.printStackTrace();
      fail();
    }
  }
 /**
  * Tests creating empty action flow configuration.
  *
  * @throws Exception when something goes wrong.
  */
 @Test
 public void testNoFlow() throws Exception {
   ActionProxy proxy = getActionProxy("/noFlow/noFlow");
   Assert.assertNotNull(proxy);
   proxy.getInvocation().getInvocationContext().setSession(new HashMap<String, Object>());
   proxy.execute();
 }
  public void testNoTokenInSession() throws Exception {
    assertEquals(oldContext, ActionContext.getContext());

    ActionProxy proxy = buildProxy(getActionName());
    setToken(request);
    ActionContext.getContext().getSession().clear();
    assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
  }
  @Test
  public void testDestroy() throws Exception {
    initServletMockObjects();
    request.setParameter("id", "101");

    ActionProxy proxy = getActionProxy("/user/destroy");
    proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
  }
  @Test
  public void testCreate() throws Exception {
    initServletMockObjects();
    request.setParameter("user.firstName", "first_name");
    request.setParameter("user.lastName", "last_name");

    ActionProxy proxy = getActionProxy("/user/create");
    proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
  }
  @Test
  public void testNewn() throws Exception {
    initServletMockObjects();

    ActionProxy proxy = getActionProxy("/user/new");
    UserAction userAction = (UserAction) proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    User user = userAction.getUser();
    assertNotNull(user);
  }
  public void testAnnotatedMethodSuccess3() throws Exception {
    HashMap<String, Object> params = new HashMap<String, Object>();

    // make it not fail
    params.put("param1", "key1");

    HashMap<String, Object> extraContext = new HashMap<String, Object>();
    extraContext.put(ActionContext.PARAMETERS, params);

    ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
    proxy.execute();
    assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());
  }
  public void testNotAnnotatedMethodSuccess2() throws Exception {
    HashMap<String, Object> params = new HashMap<String, Object>();

    HashMap<String, Object> extraContext = new HashMap<String, Object>();
    extraContext.put(ActionContext.PARAMETERS, params);

    ActionProxy proxy =
        actionProxyFactory.createActionProxy("", "notAnnotatedMethod", extraContext);
    proxy.execute();
    assertFalse(((ValidationAware) proxy.getAction()).hasActionErrors());

    Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
    assertEquals(0, errors.size());
  }
  @Test
  public void testList() throws Exception {
    initServletMockObjects();
    request.setParameter("search.firstName", "first_name_page");
    request.setParameter("search.lastName", "last_name_page");

    ActionProxy proxy = getActionProxy("/user/list");
    UserAction userAction = (UserAction) proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    List<UserView> listLogin = userAction.getListLogin();
    assertEquals(3, listLogin.size());
  }
  public void testAnnotatedMethodFailure() throws Exception {
    HashMap<String, Object> params = new HashMap<String, Object>();

    HashMap<String, Object> extraContext = new HashMap<String, Object>();
    extraContext.put(ActionContext.PARAMETERS, params);

    ActionProxy proxy = actionProxyFactory.createActionProxy("", "annotatedMethod", extraContext);
    proxy.execute();
    assertTrue(((ValidationAware) proxy.getAction()).hasActionErrors());
    Collection errors = ((ValidationAware) proxy.getAction()).getActionErrors();
    assertEquals(1, errors.size());

    assertEquals("Need param1 or param2.", errors.iterator().next());
  }
  public void testExecute() throws Exception {

    request.setParameter("emplid", "3");

    ActionProxy proxy = getActionProxy("personDelete");

    PersonDeleter action = (PersonDeleter) proxy.getAction();

    assertNotNull(action);

    String result = proxy.execute();

    assertEquals(
        "Result of calling execute method is not success but it should be.", "success", result);
  }
  @Test
  public void testEdit() throws Exception {
    initServletMockObjects();
    request.setParameter("id", "102");

    ActionProxy proxy = getActionProxy("/user/edit");
    UserAction userAction = (UserAction) proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    User user = userAction.getUser();
    assertNotNull(user);
    assertEquals("first_name_102", user.getFirstName());
    assertEquals("last_name_102", user.getLastName());
  }
  public void testInput() throws Exception {

    request.setParameter("emplid", "2");

    ActionProxy proxy = getActionProxy("inputPersonUpdate");

    PersonUpdater action = (PersonUpdater) proxy.getAction();

    assertNotNull(action);

    String result = proxy.execute();

    assertEquals("Result of calling input method is not input but it should be.", "input", result);

    Person person = action.getPerson();

    assertEquals("Person's last name is not Cole but should be.", "Cole", person.getLastName());
  }
  public void testExecute() throws Exception {

    request.setParameter("person.emplid", "2");
    request.setParameter("person.firstName", "Kate");
    request.setParameter("person.lastName", "Cole");

    ActionProxy proxy = getActionProxy("executePersonUpdate");

    PersonUpdater action = (PersonUpdater) proxy.getAction();

    assertNotNull(action);

    String result = proxy.execute();

    assertEquals(
        "Result of calling execute method is not success but it should be.", "success", result);

    Person person = action.getPerson();

    assertEquals("Person's first name is not Kate but should be.", "Kate", person.getFirstName());
  }
  @Test
  public void testExecute() throws Exception {

    ActionMapping mapping = getActionMapping("/system/user-list");
    assertNotNull(mapping);
    assertEquals("/system", mapping.getNamespace());
    assertEquals("user-list", mapping.getName());

    log.info("Answer value is " + mapping.getName());

    ActionProxy proxy = getActionProxy("/system/user-list");

    UserListAction action = (UserListAction) proxy.getAction();

    assertNotNull(action);

    String result = proxy.execute();

    assertEquals(
        "Result of calling execute method is not success but it should be.", "success", result);
  }
  /**
   * Tests creating action flow override configuration.
   *
   * @throws Exception when something goes wrong.
   */
  @Test
  public void testCorrectFlowOverride() throws Exception {
    ActionProxy proxy = getActionProxy("/correctFlowOverride/correctFlowOverride");
    Assert.assertNotNull(proxy);
    proxy.getInvocation().getInvocationContext().setSession(new HashMap<String, Object>());
    proxy.execute();

    ActionConfig overriddenViewConf =
        configuration
            .getRuntimeConfiguration()
            .getActionConfig("/correctFlowOverride", "savePhone-2ViewOverride");

    Assert.assertNotNull(overriddenViewConf);
    Assert.assertEquals("phone", overriddenViewConf.getMethodName());
    Assert.assertEquals(
        "anotherPhone",
        overriddenViewConf
            .getResults()
            .get(Action.SUCCESS)
            .getParams()
            .get(ServletDispatcherResult.DEFAULT_PARAM));

    ActionConfig actionConfig =
        configuration
            .getRuntimeConfiguration()
            .getActionConfig("/correctFlowOverride", "saveName-1ViewOverride");

    Assert.assertNotNull(actionConfig);
    Assert.assertEquals("executeOverride", actionConfig.getMethodName());
    Assert.assertEquals(
        "name",
        actionConfig
            .getResults()
            .get(Action.SUCCESS)
            .getParams()
            .get(ServletDispatcherResult.DEFAULT_PARAM));
  }
  @Test
  public void testCURD() throws Exception {
    initServletMockObjects();
    ActionProxy proxy = getActionProxy("/referance/new");
    ReferanceAction referanceAction = (ReferanceAction) proxy.getAction();
    String result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    initServletMockObjects();
    request.setParameter("referanceBean.name", "name");
    request.setParameter("referanceBean.code", "code");
    request.setParameter("referanceBean.textEN", "textEN");
    request.setParameter("referanceBean.textCN", "textCN");
    request.setParameter("referanceBean.category", "category");

    proxy = getActionProxy("/referance/create");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    initServletMockObjects();
    proxy = getActionProxy("/referance/list");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
    List<ReferanceView> listReferance = referanceAction.getListReferance();
    assertEquals(2, listReferance.size());
    Page page = referanceAction.getPage();
    assertEquals(1, page.getPage());

    Long idEN = listReferance.get(0).getId();
    Long idCN = listReferance.get(1).getId();

    initServletMockObjects();
    request.setParameter("id", Long.toString(idEN));
    proxy = getActionProxy("/referance/edit");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
    Referance referance = referanceAction.getReferance();
    assertEquals("name", referance.getName());
    assertEquals("code", referance.getCode());
    assertEquals("textEN", referance.getText());
    assertEquals("en", referance.getLang());
    assertEquals("category", referance.getCategory());

    initServletMockObjects();
    request.setParameter("referance.id", Long.toString(idEN));
    request.setParameter("referance.name", "name-updated");
    request.setParameter("referance.code", "code-updated");
    request.setParameter("referance.text", "textEN-updated");
    request.setParameter("referance.lang", "en");
    request.setParameter("referance.category", "category-updated");
    proxy = getActionProxy("/referance/update");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    initServletMockObjects();
    request.setParameter("id", Long.toString(idEN));
    proxy = getActionProxy("/referance/show");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
    referance = referanceAction.getReferance();
    assertEquals("name-updated", referance.getName());
    assertEquals("code-updated", referance.getCode());
    assertEquals("textEN-updated", referance.getText());
    assertEquals("en", referance.getLang());
    assertEquals("category-updated", referance.getCategory());

    initServletMockObjects();
    request.setParameter("id", Long.toString(idEN));
    proxy = getActionProxy("/referance/destroy");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);

    initServletMockObjects();
    request.setParameter("id", Long.toString(idCN));
    proxy = getActionProxy("/referance/destroy");
    referanceAction = (ReferanceAction) proxy.getAction();
    result = proxy.execute();
    assertEquals(Action.SUCCESS, result);
  }
Exemple #21
0
  /**
   * Load Action class for mapping and invoke the appropriate Action method, or go directly to the
   * Result.
   *
   * <p>This method first creates the action context from the given parameters, and then loads an
   * <tt>ActionProxy</tt> from the given action name and namespace. After that, the Action method is
   * executed and output channels through the response object. Actions not found are sent back to
   * the user via the {@link Dispatcher#sendError} method, using the 404 return code. All other
   * errors are reported by throwing a ServletException.
   *
   * @param request the HttpServletRequest object
   * @param response the HttpServletResponse object
   * @param mapping the action mapping object
   * @throws ServletException when an unknown error occurs (not a 404, but typically something that
   *     would end up as a 5xx by the servlet container)
   * @param context Our ServletContext object
   */
  public void serviceAction(
      HttpServletRequest request,
      HttpServletResponse response,
      ServletContext context,
      ActionMapping mapping)
      throws ServletException {

    Map<String, Object> extraContext = createContextMap(request, response, mapping, context);

    // If there was a previous value stack, then create a new copy and pass it in to be used by the
    // new Action
    ValueStack stack =
        (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
    boolean nullStack = stack == null;
    if (nullStack) {
      ActionContext ctx = ActionContext.getContext();
      if (ctx != null) {
        stack = ctx.getValueStack();
      }
    }
    if (stack != null) {
      extraContext.put(ActionContext.VALUE_STACK, valueStackFactory.createValueStack(stack));
    }

    String timerKey = "Handling request from Dispatcher";
    try {
      UtilTimerStack.push(timerKey);
      String namespace = mapping.getNamespace();
      String name = mapping.getName();
      String method = mapping.getMethod();

      Configuration config = configurationManager.getConfiguration();
      ActionProxy proxy =
          config
              .getContainer()
              .getInstance(ActionProxyFactory.class)
              .createActionProxy(namespace, name, method, extraContext, true, false);

      request.setAttribute(
          ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());

      // if the ActionMapping says to go straight to a result, do it!
      if (mapping.getResult() != null) {
        Result result = mapping.getResult();
        result.execute(proxy.getInvocation());
      } else {
        proxy.execute();
      }

      // If there was a previous value stack then set it back onto the request
      if (!nullStack) {
        request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);
      }
    } catch (ConfigurationException e) {
      // WW-2874 Only log error if in devMode
      if (devMode) {
        String reqStr = request.getRequestURI();
        if (request.getQueryString() != null) {
          reqStr = reqStr + "?" + request.getQueryString();
        }
        LOG.error("Could not find action or result\n" + reqStr, e);
      } else {
        if (LOG.isWarnEnabled()) {
          LOG.warn("Could not find action or result", e);
        }
      }
      sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);
    } catch (Exception e) {
      if (handleException || devMode) {
        sendError(request, response, context, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e);
      } else {
        throw new ServletException(e);
      }
    } finally {
      UtilTimerStack.pop(timerKey);
    }
  }
 public void testNoTokenInParams() throws Exception {
   ActionProxy proxy = buildProxy(getActionName());
   assertEquals(TokenInterceptor.INVALID_TOKEN_CODE, proxy.execute());
 }
 public void testTokenInterceptorSuccess() throws Exception {
   setToken(request);
   ActionProxy proxy = buildProxy(getActionName());
   assertEquals(Action.SUCCESS, proxy.execute());
 }
 public void testNullTokenName() throws Exception {
   setToken((String) null);
   ActionProxy proxy = buildProxy(getActionName());
   proxy.execute();
 }