Ejemplo n.º 1
0
  public void testNullProperties() {
    Foo foo = new Foo();
    foo.setALong(88);

    Map context = Ognl.createDefaultContext(foo);

    ognlUtil.setProperties(null, foo, context);
    assertEquals(88, foo.getALong());

    Map props = new HashMap();
    props.put("aLong", "99");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(99, foo.getALong());
  }
Ejemplo n.º 2
0
  public void testStringToLong() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("aLong", "123");

    ognlUtil.setProperties(props, foo, context);
    assertEquals(123, foo.getALong());

    props.put("aLong", new String[] {"123"});

    foo.setALong(0);
    ognlUtil.setProperties(props, foo, context);
    assertEquals(123, foo.getALong());
  }
Ejemplo n.º 3
0
  public void testSetPropertiesBoolean() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("useful", "true");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(true, foo.isUseful());

    props = new HashMap();
    props.put("useful", "false");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(false, foo.isUseful());
  }
Ejemplo n.º 4
0
  public void testOgnlHandlesCrapAtTheEndOfANumber() {
    Foo foo = new Foo();
    Map<String, Object> context = Ognl.createDefaultContext(foo);

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("aLong", "123a");

    ognlUtil.setProperties(props, foo, context);
    assertEquals(0, foo.getALong());
  }
Ejemplo n.º 5
0
  public void testNoExceptionForUnmatchedGetterAndSetterWithThrowPropertyException() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("myIntegerProperty", new Integer(1234));

    TestObject testObject = new TestObject();

    // this used to fail in OGNL versions < 2.7
    ognlUtil.setProperties(props, testObject, true);
    assertEquals(1234, props.get("myIntegerProperty"));
  }
Ejemplo n.º 6
0
  public void testSetPropertiesString() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("title", "this is a title");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(foo.getTitle(), "this is a title");
  }
Ejemplo n.º 7
0
  public void testSetPropertiesInt() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("number", "2");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(2, foo.getNumber());
  }
Ejemplo n.º 8
0
  public void testDeepSetting() {
    Foo foo = new Foo();
    foo.setBar(new Bar());

    Map<String, Object> context = Ognl.createDefaultContext(foo);

    Map<String, Object> props = new HashMap();
    props.put("bar.title", "i am barbaz");
    ognlUtil.setProperties(props, foo, context);

    assertEquals(foo.getBar().getTitle(), "i am barbaz");
  }
Ejemplo n.º 9
0
  public void testExceptionForWrongPropertyNameWithThrowPropertyException() {
    Map<String, Object> props = new HashMap<String, Object>();
    props.put("myStringProperty", "testString");

    TestObject testObject = new TestObject();

    try {
      ognlUtil.setProperties(props, testObject, true);
      fail("Should rise NoSuchPropertyException because of wrong property name");
    } catch (Exception e) {
      // expected
    }
  }
Ejemplo n.º 10
0
  public void testSetPropertiesLongArray() {
    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("points", new String[] {"1", "2"});
    ognlUtil.setProperties(props, foo, context);

    assertNotNull(foo.getPoints());
    assertEquals(2, foo.getPoints().length);
    assertEquals(1, foo.getPoints()[0]);
    assertEquals(2, foo.getPoints()[1]);
  }
Ejemplo n.º 11
0
  public void testCanSetADependentObject() throws Exception {
    String dogName = "fido";

    OgnlRuntime.setNullHandler(
        Owner.class,
        new NullHandler() {
          public Object nullMethodResult(Map map, Object o, String s, Object[] objects) {
            return null;
          }

          public Object nullPropertyValue(Map map, Object o, Object o1) {
            String methodName = o1.toString();
            String getter =
                "set" + methodName.substring(0, 1).toUpperCase() + methodName.substring(1);
            Method[] methods = o.getClass().getDeclaredMethods();
            System.out.println(getter);

            for (Method method : methods) {
              String name = method.getName();

              if (!getter.equals(name) || (method.getParameterTypes().length != 1)) {
                continue;
              } else {
                Class clazz = method.getParameterTypes()[0];

                try {
                  Object param = clazz.newInstance();
                  method.invoke(o, new Object[] {param});

                  return param;
                } catch (Exception e) {
                  throw new RuntimeException(e);
                }
              }
            }

            return null;
          }
        });

    Owner owner = new Owner();
    Map context = Ognl.createDefaultContext(owner);
    Map props = new HashMap();
    props.put("dog.name", dogName);

    ognlUtil.setProperties(props, owner, context);
    assertNotNull("expected Ognl to create an instance of Dog", owner.getDog());
    assertEquals(dogName, owner.getDog().getName());
  }
Ejemplo n.º 12
0
  public void testCanSetDependentObjectArray() {
    EmailAction action = new EmailAction();
    Map<String, Object> context = Ognl.createDefaultContext(action);

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("email[0].address", "addr1");
    props.put("email[1].address", "addr2");
    props.put("email[2].address", "addr3");

    ognlUtil.setProperties(props, action, context);
    assertEquals(3, action.email.size());
    assertEquals("addr1", action.email.get(0).toString());
    assertEquals("addr2", action.email.get(1).toString());
    assertEquals("addr3", action.email.get(2).toString());
  }
Ejemplo n.º 13
0
  public void testSetPropertiesDate() {
    Locale orig = Locale.getDefault();
    Locale.setDefault(Locale.US);

    Foo foo = new Foo();

    Map context = Ognl.createDefaultContext(foo);

    Map props = new HashMap();
    props.put("birthday", "02/12/1982");
    // US style test
    ognlUtil.setProperties(props, foo, context);

    Calendar cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.FEBRUARY);
    cal.set(Calendar.DAY_OF_MONTH, 12);
    cal.set(Calendar.YEAR, 1982);

    assertEquals(cal.getTime(), foo.getBirthday());

    Locale.setDefault(Locale.UK);
    // UK style test
    props.put("event", "18/10/2006 14:23:45");
    props.put("meeting", "09/09/2006 14:30");
    ognlUtil.setProperties(props, foo, context);

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.OCTOBER);
    cal.set(Calendar.DAY_OF_MONTH, 18);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 23);
    cal.set(Calendar.SECOND, 45);

    assertEquals(cal.getTime(), foo.getEvent());

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.SEPTEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 9);
    cal.set(Calendar.YEAR, 2006);
    cal.set(Calendar.HOUR_OF_DAY, 14);
    cal.set(Calendar.MINUTE, 30);

    assertEquals(cal.getTime(), foo.getMeeting());

    Locale.setDefault(orig);

    Locale.setDefault(orig);

    // test RFC 3339 date format for JSON
    props.put("event", "1996-12-19T16:39:57Z");
    ognlUtil.setProperties(props, foo, context);

    cal = Calendar.getInstance();
    cal.clear();
    cal.set(Calendar.MONTH, Calendar.DECEMBER);
    cal.set(Calendar.DAY_OF_MONTH, 19);
    cal.set(Calendar.YEAR, 1996);
    cal.set(Calendar.HOUR_OF_DAY, 16);
    cal.set(Calendar.MINUTE, 39);
    cal.set(Calendar.SECOND, 57);

    assertEquals(cal.getTime(), foo.getEvent());

    // test setting a calendar property
    props.put("calendar", "1996-12-19T16:39:57Z");
    ognlUtil.setProperties(props, foo, context);
    assertEquals(cal, foo.getCalendar());
  }