public void testModelOnSession() throws Exception {
    inter.setScope("session");
    inter.setName("king");

    User user = new User();
    user.setName("King George");
    Map session = new HashMap();
    ActionContext.getContext().setSession(session);
    ActionContext.getContext().getSession().put("king", user);

    ScopedModelDriven action = new MyUserScopedModelDrivenAction();
    MockActionInvocation mai = new MockActionInvocation();
    MockActionProxy map = new MockActionProxy();
    ActionConfig ac = new ActionConfig.Builder("", "", "").build();
    map.setConfig(ac);
    mai.setAction(action);
    mai.setProxy(map);

    inter.intercept(mai);
    inter.destroy();

    assertNotNull(action.getModel());
    assertNotNull(action.getScopeKey());
    assertEquals("king", action.getScopeKey());

    Object model = ActionContext.getContext().getSession().get(action.getScopeKey());
    assertNotNull(model);
    assertTrue("Model should be an User object", model instanceof User);
    assertEquals("King George", ((User) model).getName());
  }
  public void testNoScopedModelAction() throws Exception {
    Action action = new SimpleAction();
    MockActionInvocation mai = new MockActionInvocation();
    MockActionProxy map = new MockActionProxy();
    ActionConfig ac = new ActionConfig.Builder("", "", "").build();
    map.setConfig(ac);
    mai.setAction(action);
    mai.setProxy(map);

    inter.intercept(mai);
    inter.destroy();
    // nothing happends
  }
  protected void setUp() throws Exception {
    super.setUp();

    stringWriter = new StringWriter();
    writer = new PrintWriter(stringWriter);
    response = new StrutsMockHttpServletResponse();
    response.setWriter(writer);
    servletContext = new StrutsMockServletContext();
    stack = ActionContext.getContext().getValueStack();
    context = new ActionContext(stack.getContext());
    context.put(StrutsStatics.HTTP_RESPONSE, response);
    context.put(StrutsStatics.SERVLET_CONTEXT, servletContext);
    invocation = new MockActionInvocation();
    invocation.setStack(stack);
    invocation.setInvocationContext(context);
  }
  public void testMultiselectNoValue() throws Exception {
    param.put("user", "batman");
    param.put("email", "*****@*****.**");
    param.put("__multiselect_superpower", "");
    assertTrue(param.containsKey("__multiselect_superpower"));

    ai.getInvocationContext().setParameters(HttpParameters.create(param).build());

    interceptor.init();
    interceptor.intercept(ai);
    interceptor.destroy();

    HttpParameters parameters = ai.getInvocationContext().getParameters();
    assertFalse(parameters.contains("__multiselect_superpower"));
    assertEquals(
        3, parameters.getNames().size()); // should be 3 as __multiselect_ should be removed
    assertFalse(parameters.get("superpower").isDefined());
  }
  protected void setUp() throws Exception {
    super.setUp();
    param = new HashMap<>();

    interceptor = new MultiselectInterceptor();
    ai = new MockActionInvocation();
    ai.setInvocationContext(ActionContext.getContext());
    ActionContext.getContext().setParameters(HttpParameters.create(param).build());
  }
  public void testScopedModelDrivenAction() throws Exception {
    inter.setScope("request");

    ScopedModelDriven action = new MyUserScopedModelDrivenAction();
    MockActionInvocation mai = new MockActionInvocation();
    MockActionProxy map = new MockActionProxy();
    ActionConfig ac = new ActionConfig.Builder("", "", "").build();
    map.setConfig(ac);
    mai.setAction(action);
    mai.setProxy(map);

    inter.intercept(mai);
    inter.destroy();

    assertNotNull(action.getModel());
    assertNotNull(action.getScopeKey());
    assertEquals("com.opensymphony.xwork2.test.User", action.getScopeKey());

    Object model = ActionContext.getContext().get(action.getScopeKey());
    assertNotNull(model);
    assertTrue("Model should be an User object", model instanceof User);
  }
  public void testModelAlreadySetOnAction() throws Exception {
    inter.setScope("request");
    inter.setName("king");

    User user = new User();
    user.setName("King George");

    ScopedModelDriven action = new MyUserScopedModelDrivenAction();
    action.setModel(user);
    MockActionInvocation mai = new MockActionInvocation();
    MockActionProxy map = new MockActionProxy();
    ActionConfig ac = new ActionConfig.Builder("", "", "").build();
    map.setConfig(ac);
    mai.setAction(action);
    mai.setProxy(map);

    inter.intercept(mai);
    inter.destroy();

    assertNotNull(action.getModel());
    assertNull(action.getScopeKey()); // no scope key as nothing happended
  }
Example #8
0
  public void setUp() throws Exception {
    interceptor = new I18nInterceptor();
    interceptor.init();
    session = new HashMap();

    Map<String, Object> ctx = new HashMap<String, Object>();
    ctx.put(ActionContext.PARAMETERS, HttpParameters.createEmpty().build());
    ctx.put(ActionContext.SESSION, session);

    ac = new ActionContext(ctx);

    ServletActionContext.setContext(ac);
    ServletActionContext.setRequest(new MockHttpServletRequest());

    Action action =
        new Action() {
          public String execute() throws Exception {
            return SUCCESS;
          }
        };
    mai = new MockActionInvocation();
    ((MockActionInvocation) mai).setAction(action);
    ((MockActionInvocation) mai).setInvocationContext(ac);
  }