public void testDateConversion() throws ParseException {
    java.sql.Date sqlDate = new java.sql.Date(System.currentTimeMillis());
    assertEquals(sqlDate, converter.convertValue(context, null, null, null, sqlDate, Date.class));

    SimpleDateFormat format = new SimpleDateFormat("mm/dd/yyyy hh:mm:ss");
    Date date = format.parse("01/10/2001 00:00:00");
    String dateStr = (String) converter.convertValue(context, null, null, null, date, String.class);
    Date date2 = (Date) converter.convertValue(context, null, null, null, dateStr, Date.class);
    assertEquals(date, date2);
  }
 public void testLocalizedDateConversion() throws Exception {
   Date date = new Date(System.currentTimeMillis());
   Locale locale = Locale.GERMANY;
   DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
   String dateString = df.format(date);
   context.put(ActionContext.LOCALE, locale);
   assertEquals(dateString, converter.convertValue(context, null, null, null, date, String.class));
 }
 public void testStringArrayToSet() {
   Set<String> list = new HashSet<>();
   list.add("foo");
   list.add("bar");
   list.add("baz");
   assertEquals(
       list,
       converter.convertValue(
           context, null, null, null, new String[] {"foo", "bar", "bar", "baz"}, Set.class));
 }
 public void testStringArrayToCollection() {
   List<String> list = new ArrayList<>();
   list.add("foo");
   list.add("bar");
   list.add("baz");
   assertEquals(
       list,
       converter.convertValue(
           context, null, null, null, new String[] {"foo", "bar", "baz"}, Collection.class));
 }
  public void testStringToCustomTypeUsingCustomConverter() {
    // the converter needs to be registered as the Bar.class converter
    // it won't be detected from the Foo-conversion.properties
    // because the Foo-conversion.properties file is only used when converting a property of Foo
    converter.registerConverter(Bar.class.getName(), new FooBarConverter());

    Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
    assertNotNull("conversion failed", bar);
    assertEquals(123, bar.getSomethingElse());
    assertEquals("blah", bar.getTitle());
  }
  public void testStringArrayToPrimitives() throws OgnlException {
    long[] longs =
        (long[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, long[].class);
    assertNotNull(longs);
    assertTrue(Arrays.equals(new long[] {123, 456}, longs));

    int[] ints =
        (int[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, int[].class);
    assertNotNull(ints);
    assertTrue(Arrays.equals(new int[] {123, 456}, ints));

    double[] doubles =
        (double[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, double[].class);
    assertNotNull(doubles);
    assertTrue(Arrays.equals(new double[] {123, 456}, doubles));

    float[] floats =
        (float[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, float[].class);
    assertNotNull(floats);
    assertTrue(Arrays.equals(new float[] {123, 456}, floats));

    boolean[] booleans =
        (boolean[])
            converter.convertValue(
                context, null, null, null, new String[] {"true", "false"}, boolean[].class);
    assertNotNull(booleans);
    assertTrue(Arrays.equals(new boolean[] {true, false}, booleans));
  }
  public void testStringArrayToPrimitiveWrappers() {
    Long[] longs =
        (Long[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, Long[].class);
    assertNotNull(longs);
    assertTrue(Arrays.equals(new Long[] {123L, 456L}, longs));

    Integer[] ints =
        (Integer[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, Integer[].class);
    assertNotNull(ints);
    assertTrue(Arrays.equals(new Integer[] {123, 456}, ints));

    Double[] doubles =
        (Double[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, Double[].class);
    assertNotNull(doubles);
    assertTrue(Arrays.equals(new Double[] {123D, 456D}, doubles));

    Float[] floats =
        (Float[])
            converter.convertValue(
                context, null, null, null, new String[] {"123", "456"}, Float[].class);
    assertNotNull(floats);
    assertTrue(Arrays.equals(new Float[] {123F, 456F}, floats));

    Boolean[] booleans =
        (Boolean[])
            converter.convertValue(
                context, null, null, null, new String[] {"true", "false"}, Boolean[].class);
    assertNotNull(booleans);
    assertTrue(Arrays.equals(new Boolean[] {Boolean.TRUE, Boolean.FALSE}, booleans));
  }
 public void testStringToPrimitives() {
   assertEquals(
       new Long(123), converter.convertValue(context, null, null, null, "123", long.class));
   assertEquals(
       new Integer(123), converter.convertValue(context, null, null, null, "123", int.class));
   assertEquals(
       new Double(123.5),
       converter.convertValue(context, null, null, null, "123.5", double.class));
   assertEquals(
       new Float(123.5), converter.convertValue(context, null, null, null, "123.5", float.class));
   assertEquals(false, converter.convertValue(context, null, null, null, "false", boolean.class));
   assertEquals(true, converter.convertValue(context, null, null, null, "true", boolean.class));
   assertEquals(
       new BigDecimal(123.5),
       converter.convertValue(context, null, null, null, "123.5", BigDecimal.class));
   assertEquals(
       new BigInteger("123"),
       converter.convertValue(context, null, null, null, "123", BigInteger.class));
 }
 public void testStringToPrimitiveWrappers() {
   assertEquals(
       new Long(123), converter.convertValue(context, null, null, null, "123", Long.class));
   assertEquals(
       new Integer(123), converter.convertValue(context, null, null, null, "123", Integer.class));
   assertEquals(
       new Double(123.5),
       converter.convertValue(context, null, null, null, "123.5", Double.class));
   assertEquals(
       new Float(123.5), converter.convertValue(context, null, null, null, "123.5", float.class));
   assertEquals(false, converter.convertValue(context, null, null, null, "false", Boolean.class));
   assertEquals(true, converter.convertValue(context, null, null, null, "true", Boolean.class));
 }
  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());
  }
  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"));
  }
  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 testArrayToNumberConversion() {
   String[] value = new String[] {"12345"};
   assertEquals(
       new Integer(12345),
       converter.convertValue(context, null, null, null, value, Integer.class));
   assertEquals(
       new Long(12345), converter.convertValue(context, null, null, null, value, Long.class));
   value[0] = "123.45";
   assertEquals(
       new Float(123.45), converter.convertValue(context, null, null, null, value, Float.class));
   assertEquals(
       new Double(123.45), converter.convertValue(context, null, null, null, value, Double.class));
   value[0] = "1234567890123456789012345678901234567890";
   assertEquals(
       new BigInteger(value[0]),
       converter.convertValue(context, null, null, null, value, BigInteger.class));
   value[0] = "1234567890123456789.012345678901234567890";
   assertEquals(
       new BigDecimal(value[0]),
       converter.convertValue(context, null, null, null, value, BigDecimal.class));
 }