public void testFindConversionErrorMessage() {
    ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());

    String message = XWorkConverter.getConversionErrorMessage("birth", stack);
    assertNotNull(message);
    assertEquals("Invalid date for birth.", message);

    message = XWorkConverter.getConversionErrorMessage("foo", stack);
    assertNotNull(message);
    assertEquals("Invalid field value for field \"foo\".", message);
  }
Пример #2
0
    public void beforeResult(ActionInvocation invocation, String resultCode) {
      ValueStack stack = invocation.getStack();
      CompoundRoot root = stack.getRoot();

      boolean needsRefresh = true;
      Object newModel = action.getModel();

      // Check to see if the new model instance is already on the stack
      for (Object item : root) {
        if (item.equals(newModel)) {
          needsRefresh = false;
        }
      }

      // Add the new model on the stack
      if (needsRefresh) {

        // Clear off the old model instance
        if (originalModel != null) {
          root.remove(originalModel);
        }
        if (newModel != null) {
          stack.push(newModel);
        }
      }
    }
  /**
   * validation check를 할때 hashMap name이 view(jsp)에서 입력되었는지를 알수 있게 하기 위해서는 stack에 getter setter로 정의된
   * hashMap(여기에서는 reqMap이름을 쓴다)이름을 지정하고 hashMap에 저장된 값을 리턴한다.
   */
  @Override
  protected Object getFieldValue(String name, Object object) throws ValidationException {
    ValueStack stack = ActionContext.getContext().getValueStack();

    boolean pop = false;

    if (!(stack.getRoot().contains(object))) {
      stack.push(object);
      pop = true;
    }

    Object retVal = null;
    // HashMap reqMap의 값을 stack에 넣어준다.
    Object obj = (Map) stack.findValue("reqMap");

    // HashMap reqMap이 null이 아닐때
    if (obj != null) {
      Map m = (Map) obj;
      // stack에 있는 HashMap reqMap값을 name으로 가져와서 valueStack에 올려준다.
      retVal = m.get(name);
    }
    // HashMap reqMap이 null일때
    else {
      // getter setter을 사용한 name의 값을 valueStack에 올려준다.
      retVal = stack.findValue(name);
    }

    if (pop) {
      stack.pop();
    }

    return retVal;
  }
  public void testGenericPropertiesFromGetter() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    assertEquals(1, gb.getGetterList().size());
    assertEquals("42.42", stack.findValue("getterList.get(0).toString()"));
    assertEquals(new Double(42.42), stack.findValue("getterList.get(0)"));
    assertEquals(new Double(42.42), gb.getGetterList().get(0));
  }
  public void testFindConversionMappingForInterface() {
    ModelDrivenAnnotationAction2 action = new ModelDrivenAnnotationAction2();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());

    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    String value = "asdf:123";
    Object o =
        converter.convertValue(
            ognlStackContext, action.getModel(), null, "barObj", value, Bar.class);
    assertNotNull(o);
    assertTrue("class is: " + o.getClass(), o instanceof Bar);

    Bar b = (Bar) o;
    assertEquals(value, b.getTitle() + ":" + b.getSomethingElse());
  }
  protected Object getOverrideExpr(ActionInvocation invocation, Object value) {
    ValueStack stack = invocation.getStack();

    try {
      stack.push(value);

      return escape(stack.findString("top"));
    } finally {
      stack.pop();
    }
  }
  public void testGenericProperties() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    String[] value = new String[] {"123.12", "123.45"};
    stack.setValue("doubles", value);
    assertEquals(2, gb.getDoubles().size());
    assertEquals(Double.class, gb.getDoubles().get(0).getClass());
    assertEquals(new Double(123.12), gb.getDoubles().get(0));
    assertEquals(new Double(123.45), gb.getDoubles().get(1));
  }
  public void testFieldErrorMessageAddedWhenConversionFailsOnModelDriven() {
    ModelDrivenAnnotationAction action = new ModelDrivenAnnotationAction();
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);
    stack.push(action.getModel());

    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    String[] value = new String[] {"invalid date"};
    assertEquals(
        "Conversion should have failed.",
        OgnlRuntime.NoConversionPossible,
        converter.convertValue(ognlStackContext, action, null, "birth", value, Date.class));
    stack.pop();
    stack.pop();

    Map conversionErrors = (Map) ognlStackContext.get(ActionContext.CONVERSION_ERRORS);
    assertNotNull(conversionErrors);
    assertEquals(1, conversionErrors.size());
    assertNotNull(conversionErrors.get("birth"));
    assertEquals(value, conversionErrors.get("birth"));
  }
Пример #9
0
 // 进入修改界面
 public String viewUpdate() {
   // 查到所有部门
   List<Dept> listDept = deptService.getAll();
   request.put("listDept", listDept);
   // 根据点击进入更新页面获得的参数id,查到需要修改的员工的初始信息
   int id = employee.getId();
   Employee emp = employeeService.findById(id);
   ValueStack vsStack = ActionContext.getContext().getValueStack();
   // 移除栈顶
   vsStack.pop();
   // 把获得的员工信息加入栈
   vsStack.push(emp);
   return "edit";
 }
  // TODO: Fixme... This test does not work with GenericsObjectDeterminer!
  public void testStringToCollectionConversion() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    AnnotationUser user = new AnnotationUser();
    stack.push(user);

    stack.setValue("list", "asdf");
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
  }
Пример #11
0
  @Override
  public String intercept(ActionInvocation invocation) throws Exception {
    Object action = invocation.getAction();

    if (action instanceof ModelDriven) {
      ModelDriven modelDriven = (ModelDriven) action;
      ValueStack stack = invocation.getStack();
      Object model = modelDriven.getModel();
      if (model != null) {
        stack.push(model);
      }
      if (refreshModelBeforeResult) {
        invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
      }
    }
    return invocation.invoke();
  }
  public void testGenericPropertiesFromSetter() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("genericMap[123.12]", "66");
    stack.setValue("genericMap[456.12]", "42");

    assertEquals(2, gb.getGenericMap().size());
    assertEquals("66", stack.findValue("genericMap.get(123.12).toString()"));
    assertEquals("42", stack.findValue("genericMap.get(456.12).toString()"));
    assertEquals(66, stack.findValue("genericMap.get(123.12)"));
    assertEquals(42, stack.findValue("genericMap.get(456.12)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(66)"));
    assertEquals(true, stack.findValue("genericMap.containsValue(42)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(123.12)"));
    assertEquals(true, stack.findValue("genericMap.containsKey(456.12)"));
  }
Пример #13
0
  /** Test that type conversion is performed on indexed collection properties. */
  public void testSetIndexedValue() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map<String, Object> stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.TRUE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    User user = new User();
    stack.push(user);

    // indexed string w/ existing array
    user.setList(new ArrayList<String>());
    user.getList().add("");

    String[] foo = new String[] {"asdf"};
    stack.setValue("list[0]", foo);
    assertNotNull(user.getList());
    assertEquals(1, user.getList().size());
    assertEquals(String.class, user.getList().get(0).getClass());
    assertEquals("asdf", user.getList().get(0));
  }
Пример #14
0
  /** XW-281 */
  public void testSetBigIndexedValue() {
    ValueStack stack = ActionContext.getContext().getValueStack();
    Map stackContext = stack.getContext();
    stackContext.put(ReflectionContextState.CREATE_NULL_OBJECTS, Boolean.FALSE);
    stackContext.put(ReflectionContextState.DENY_METHOD_EXECUTION, Boolean.TRUE);
    stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);

    User user = new User();
    stack.push(user);

    // indexed string w/ existing array
    user.setList(new ArrayList());

    String[] foo = new String[] {"asdf"};
    ((OgnlValueStack) stack).setDevMode("true");
    try {
      stack.setValue("list.1114778947765", foo);
      fail("non-valid expression: list.1114778947765");
    } catch (RuntimeException ex) {; // it's oke
    }

    try {
      stack.setValue("1114778947765", foo);
      fail("non-valid expression: 1114778947765");
    } catch (RuntimeException ex) {;
    }

    try {
      stack.setValue("1234", foo);
      fail("non-valid expression: 1114778947765");
    } catch (RuntimeException ex) {;
    }

    ((OgnlValueStack) stack).setDevMode("false");
    stack.setValue("list.1114778947765", foo);
    stack.setValue("1114778947765", foo);
    stack.setValue("1234", foo);
  }
  // FIXME: Implement nested Generics such as: List of Generics List, Map of Generic keys/values,
  // etc...
  public void no_testGenericPropertiesWithNestedGenerics() {
    GenericsBean gb = new GenericsBean();
    ValueStack stack = ac.getValueStack();
    stack.push(gb);

    stack.setValue("extendedMap[123.12]", new String[] {"1", "2", "3", "4"});
    stack.setValue("extendedMap[456.12]", new String[] {"5", "6", "7", "8", "9"});

    System.out.println("gb.getExtendedMap(): " + gb.getExtendedMap());

    assertEquals(2, gb.getExtendedMap().size());
    System.out.println(stack.findValue("extendedMap"));
    assertEquals(4, stack.findValue("extendedMap.get(123.12).size"));
    assertEquals(5, stack.findValue("extendedMap.get(456.12).size"));

    assertEquals("1", stack.findValue("extendedMap.get(123.12).get(0)"));
    assertEquals("5", stack.findValue("extendedMap.get(456.12).get(0)"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(123.12).get(0).class"));
    assertEquals(Integer.class, stack.findValue("extendedMap.get(456.12).get(0).class"));

    assertEquals(List.class, stack.findValue("extendedMap.get(123.12).class"));
    assertEquals(List.class, stack.findValue("extendedMap.get(456.12).class"));
  }
  public void testFieldErrorMessageAddedForComplexProperty() {
    SimpleAnnotationAction action = new SimpleAnnotationAction();
    action.setBean(new AnnotatedTestBean());

    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(action);

    Map<String, Object> ognlStackContext = stack.getContext();
    ognlStackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE);
    ognlStackContext.put(XWorkConverter.CONVERSION_PROPERTY_FULLNAME, "bean.birth");

    String[] value = new String[] {"invalid date"};
    assertEquals(
        "Conversion should have failed.",
        OgnlRuntime.NoConversionPossible,
        converter.convertValue(
            ognlStackContext, action.getBean(), null, "birth", value, Date.class));
    stack.pop();

    Map conversionErrors = (Map) stack.getContext().get(ActionContext.CONVERSION_ERRORS);
    assertNotNull(conversionErrors);
    assertTrue(conversionErrors.size() == 1);
    assertEquals(value, conversionErrors.get("bean.birth"));
  }
  public void testValidate() throws Exception {
    /* MockAction.class */
    // reference number
    ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
    final RequiredStringValidator referenceNumberRequiredStringValidator =
        new RequiredStringValidator();
    referenceNumberRequiredStringValidator.setFieldName("referenceNumber");
    referenceNumberRequiredStringValidator.setDefaultMessage("Reference number is required");
    referenceNumberRequiredStringValidator.setValueStack(stack);

    // order
    final RequiredFieldValidator orderRequiredValidator = new RequiredFieldValidator();
    orderRequiredValidator.setFieldName("order");
    orderRequiredValidator.setDefaultMessage("Order is required");
    orderRequiredValidator.setValueStack(stack);

    // customer
    final RequiredFieldValidator customerRequiredValidator = new RequiredFieldValidator();
    customerRequiredValidator.setFieldName("customer");
    customerRequiredValidator.setDefaultMessage("Customer is required");
    customerRequiredValidator.setValueStack(stack);
    final VisitorFieldValidator customerVisitorValidator = new VisitorFieldValidator();
    customerVisitorValidator.setAppendPrefix(true);
    customerVisitorValidator.setFieldName("customer");
    customerVisitorValidator.setValueStack(stack);

    /* Customer.class */
    // customer -> name
    final RequiredStringValidator customerNameRequiredStringValidator =
        new RequiredStringValidator();
    customerNameRequiredStringValidator.setFieldName("name");
    customerNameRequiredStringValidator.setDefaultMessage("Name is required");
    customerNameRequiredStringValidator.setValueStack(stack);

    // customer -> age
    final RequiredFieldValidator customerAgeRequiredValidator = new RequiredFieldValidator();
    customerAgeRequiredValidator.setFieldName("age");
    customerAgeRequiredValidator.setDefaultMessage("Age is required");
    customerAgeRequiredValidator.setValueStack(stack);

    // customer -> Address
    final RequiredFieldValidator customerAddressRequiredFieldValidator =
        new RequiredFieldValidator();
    customerAddressRequiredFieldValidator.setFieldName("address");
    customerAddressRequiredFieldValidator.setDefaultMessage("Address is required");
    customerAddressRequiredFieldValidator.setValueStack(stack);

    final VisitorFieldValidator customerAddressVisitorFieldValidator = new VisitorFieldValidator();
    customerAddressVisitorFieldValidator.setFieldName("address");
    customerAddressVisitorFieldValidator.setAppendPrefix(true);
    // customerAddressVisitorFieldValidator.setDefaultMessage("");
    customerAddressVisitorFieldValidator.setValueStack(stack);

    /* Address.class */
    // customer -> Address -> street
    final RequiredStringValidator customerAddressStreetRequiredFieldValidator =
        new RequiredStringValidator();
    customerAddressStreetRequiredFieldValidator.setFieldName("street");
    customerAddressStreetRequiredFieldValidator.setDefaultMessage("Street is required");
    customerAddressStreetRequiredFieldValidator.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator.setValueStack(stack);

    final RequiredStringValidator customerAddressStreetRequiredFieldValidator2 =
        new RequiredStringValidator();
    customerAddressStreetRequiredFieldValidator2.setFieldName("street");
    customerAddressStreetRequiredFieldValidator2.setDefaultMessage("Street is required 2");
    customerAddressStreetRequiredFieldValidator2.setShortCircuit(true);
    customerAddressStreetRequiredFieldValidator2.setValueStack(stack);

    // customer -> Address -> pobox
    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator =
        new RequiredStringValidator();
    customerAddressPoboxRequiredFieldValidator.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator.setDefaultMessage("PO Box is required");
    customerAddressPoboxRequiredFieldValidator.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator.setValueStack(stack);

    final RequiredStringValidator customerAddressPoboxRequiredFieldValidator2 =
        new RequiredStringValidator();
    customerAddressPoboxRequiredFieldValidator2.setFieldName("pobox");
    customerAddressPoboxRequiredFieldValidator2.setDefaultMessage("PO Box is required 2");
    customerAddressPoboxRequiredFieldValidator2.setShortCircuit(false);
    customerAddressPoboxRequiredFieldValidator2.setValueStack(stack);

    final List<Validator> validatorsForMockAction =
        new ArrayList<Validator>() {
          {
            add(referenceNumberRequiredStringValidator);
            add(orderRequiredValidator);
            add(customerRequiredValidator);
            add(customerVisitorValidator);
          }
        };

    final List<Validator> validatorsForCustomer =
        new ArrayList<Validator>() {
          {
            add(customerNameRequiredStringValidator);
            add(customerAgeRequiredValidator);
            add(customerAddressRequiredFieldValidator);
            add(customerAddressVisitorFieldValidator);
          }
        };

    final List<Validator> validatorsForAddress =
        new ArrayList<Validator>() {
          {
            add(customerAddressStreetRequiredFieldValidator);
            add(customerAddressStreetRequiredFieldValidator2);
            add(customerAddressPoboxRequiredFieldValidator);
            add(customerAddressPoboxRequiredFieldValidator2);
          }
        };

    DefaultActionValidatorManager validatorManager =
        new DefaultActionValidatorManager() {
          @Override
          public List<Validator> getValidators(Class clazz, String context, String method) {
            if (clazz.isAssignableFrom(MockAction.class)) {
              return validatorsForMockAction;
            } else if (clazz.isAssignableFrom(Customer.class)) {
              return validatorsForCustomer;
            } else if (clazz.isAssignableFrom(Address.class)) {
              return validatorsForAddress;
            }
            return Collections.emptyList();
          }
        };
    customerVisitorValidator.setActionValidatorManager(validatorManager);
    customerAddressVisitorFieldValidator.setActionValidatorManager(validatorManager);

    MockAction action = new MockAction();
    stack.push(action);
    validatorManager.validate(action, "ctx");

    assertFalse(action.hasActionErrors());
    assertFalse(action.hasActionMessages());
    assertTrue(action.hasFieldErrors());
    assertTrue(action.getFieldErrors().containsKey("referenceNumber"));
    assertEquals((action.getFieldErrors().get("referenceNumber")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("order"));
    assertEquals((action.getFieldErrors().get("order")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.name"));
    assertEquals((action.getFieldErrors().get("customer.name")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.age"));
    assertEquals((action.getFieldErrors().get("customer.age")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.street"));
    assertEquals((action.getFieldErrors().get("customer.address.street")).size(), 1);
    assertTrue(action.getFieldErrors().containsKey("customer.address.pobox"));
    assertEquals((action.getFieldErrors().get("customer.address.pobox")).size(), 2);
  }
 public void testValueStackWithTypeParameter() {
   ValueStack stack = ActionContext.getContext().getValueStack();
   stack.push(new Foo1());
   Bar1 bar = (Bar1) stack.findValue("bar", Bar1.class);
   assertNotNull(bar);
 }