@Test
 public void shouldNotInstantiateIfLastTerm() throws OgnlException, NoSuchMethodException {
   OgnlRuntime.setNullHandler(House.class, handler);
   final TypeConverter typeConverter = mockery.mock(TypeConverter.class);
   final House house = new House();
   final Mouse tom = new Mouse();
   mockery.checking(
       new Expectations() {
         {
           one(typeConverter)
               .convertValue(
                   context,
                   house,
                   House.class.getDeclaredMethod("setMouse", Mouse.class),
                   "mouse",
                   "22",
                   Mouse.class);
           will(returnValue(tom));
         }
       });
   Ognl.setTypeConverter(context, typeConverter);
   Ognl.setValue("mouse", context, house, "22");
   MatcherAssert.assertThat(house.getMouse(), Matchers.is(Matchers.equalTo(tom)));
   mockery.assertIsSatisfied();
 }
 @Test
 public void shouldInstantiateAnObjectIfRequiredToSetAProperty() throws OgnlException {
   OgnlRuntime.setNullHandler(House.class, handler);
   House house = new House();
   Ognl.setValue("mouse.name", context, house, "James");
   MatcherAssert.assertThat(house.getMouse().getName(), Matchers.is(Matchers.equalTo("James")));
 }
 @Test
 public void shouldInstantiateAListOfStrings() throws OgnlException {
   mockery.checking(
       new Expectations() {
         {
           one(removal).add((Collection<?>) with(an(Collection.class)));
         }
       });
   OgnlRuntime.setNullHandler(House.class, handler);
   OgnlRuntime.setNullHandler(Mouse.class, handler);
   House house = new House();
   Ognl.setValue("mouse.eyeColors[0]", context, house, "Blue");
   Ognl.setValue("mouse.eyeColors[1]", context, house, "Green");
   MatcherAssert.assertThat(
       house.getMouse().getEyeColors().get(0), Matchers.is(Matchers.equalTo("Blue")));
   MatcherAssert.assertThat(
       house.getMouse().getEyeColors().get(1), Matchers.is(Matchers.equalTo("Green")));
 }
예제 #4
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());
  }