Beispiel #1
1
  public void testCopySameType() {
    Foo foo1 = new Foo();
    Foo foo2 = new Foo();

    Map context = Ognl.createDefaultContext(foo1);

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

    foo1.setTitle("blah");
    foo1.setNumber(1);
    foo1.setPoints(new long[] {1, 2, 3});
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);

    ognlUtil.copy(foo1, foo2, context);

    assertEquals(foo1.getTitle(), foo2.getTitle());
    assertEquals(foo1.getNumber(), foo2.getNumber());
    assertEquals(foo1.getPoints(), foo2.getPoints());
    assertEquals(foo1.getBirthday(), foo2.getBirthday());
    assertEquals(foo1.isUseful(), foo2.isUseful());
  }
Beispiel #2
0
  public void test_Set_Chain_Indexed_Property() throws Exception {
    Root root = new Root();
    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);

    context.setRoot(root);
    context.setCurrentObject(root);

    SimpleNode node =
        (SimpleNode) Ognl.parseExpression("tab.searchCriteriaSelections[index1][index2]");
    node.setValue(context, root, Boolean.FALSE);
  }
Beispiel #3
0
  public void test_Set_Get_Multiple_Generic_Types_Property() throws Exception {
    BaseGeneric<GameGenericObject, Long> root = new GameGeneric();
    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);

    context.setRoot(root);
    context.setCurrentObject(root);

    SimpleNode node = (SimpleNode) Ognl.parseExpression("ids");
    node.setValue(context, root, new String[] {"0", "20", "43"});

    isEqual(new Long[] {new Long(0), new Long(20), new Long(43)}, root.getIds());
    isEqual(node.getValue(context, root), root.getIds());
  }
Beispiel #4
0
 public void testSetProperty() {
   Foo foo = new Foo();
   Map context = Ognl.createDefaultContext(foo);
   assertFalse(123456 == foo.getNumber());
   ognlUtil.setProperty("number", "123456", foo, context);
   assertEquals(123456, foo.getNumber());
 }
Beispiel #5
0
  public void test_Get_Generic_Property() throws Exception {
    GenericRoot root = new GenericRoot();
    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);

    context.setRoot(root);
    context.setCurrentObject(root);

    SimpleNode node = (SimpleNode) Ognl.parseExpression("cracker.param");
    node.setValue(context, root, "0");

    assertEquals(new Integer(0), node.getValue(context, root));

    node.setValue(context, root, "10");

    assertEquals(new Integer(10), node.getValue(context, root));
  }
Beispiel #6
0
  public void test_Get_Value_Body() throws Exception {
    ASTProperty p = new ASTProperty(0);
    p.setIndexedAccess(false);
    ASTConst pRef = new ASTConst(0);
    pRef.setValue("nested");
    pRef.jjtSetParent(p);
    p.jjtAddChild(pRef, 0);

    Map root = new Root().getMap();

    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);
    context.setRoot(root);
    context.setCurrentObject(root);
    context.setCurrentNode(pRef);

    assertEquals(root.getClass(), context.getCurrentType());
    assertEquals(null, context.getPreviousType());
    assertEquals(root, context.getCurrentObject());
    assertEquals(null, context.getCurrentAccessor());
    assertEquals(null, context.getPreviousAccessor());

    Object value = p.getValue(context, root);

    assertEquals(root.get("nested"), value);
    assertEquals(root.getClass(), context.getCurrentType());
    assertEquals(null, context.getPreviousType());
    assertEquals(null, context.getCurrentAccessor());
    assertEquals(null, context.getPreviousAccessor());
  }
Beispiel #7
0
  public void test_Get_Source() throws Throwable {
    ASTProperty p = new ASTProperty(0);
    p.setIndexedAccess(false);
    ASTConst pRef = new ASTConst(0);
    pRef.setValue("nested");
    pRef.jjtSetParent(p);
    p.jjtAddChild(pRef, 0);

    Map root = new Root().getMap();

    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);
    context.setRoot(root);
    context.setCurrentObject(root);
    context.setCurrentNode(pRef);

    assertEquals(".get(\"nested\")", p.toGetSourceString(context, root));
    assertEquals(Object.class, context.getCurrentType());
    assertEquals(Map.class, context.getCurrentAccessor());
    assertEquals(root.getClass(), context.getPreviousType());
    assertEquals(null, context.getPreviousAccessor());

    assertEquals(root.get("nested"), context.getCurrentObject());

    assert Map.class.isAssignableFrom(context.getCurrentAccessor());

    assertEquals(root.getClass(), context.getPreviousType());
    assertEquals(null, context.getPreviousAccessor());
  }
Beispiel #8
0
  public void testCopyNull() {
    Foo foo = new Foo();
    Map context = Ognl.createDefaultContext(foo);
    ognlUtil.copy(null, null, context);

    ognlUtil.copy(foo, null, context);
    ognlUtil.copy(null, foo, context);
  }
Beispiel #9
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());
  }
Beispiel #10
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");
  }
Beispiel #11
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());
  }
Beispiel #12
0
  public void testGetTopTarget() throws Exception {
    Foo foo = new Foo();
    Map context = Ognl.createDefaultContext(foo);

    CompoundRoot root = new CompoundRoot();
    Object top = ognlUtil.getRealTarget("top", context, root);
    assertEquals(root, top); // top should be root

    root.push(foo);
    Object val = ognlUtil.getRealTarget("unknown", context, root);
    assertNull(val); // not found
  }
Beispiel #13
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");
  }
Beispiel #14
0
  public void testSetList() throws Exception {
    ChainingInterceptor foo = new ChainingInterceptor();
    ChainingInterceptor foo2 = new ChainingInterceptor();

    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);
    SimpleNode expression = (SimpleNode) Ognl.parseExpression("{'a','ruby','b','tom'}");

    Ognl.getValue(expression, context, "aksdj");

    final ValueStack stack = ActionContext.getContext().getValueStack();

    Object result = Ognl.getValue(ognlUtil.compile("{\"foo\",'ruby','b','tom'}"), context, foo);
    foo.setIncludes((Collection) result);

    assertEquals(4, foo.getIncludes().size());
    assertEquals("foo", foo.getIncludes().toArray()[0]);
    assertEquals("ruby", foo.getIncludes().toArray()[1]);
    assertEquals("b", "" + foo.getIncludes().toArray()[2]);
    assertEquals("tom", foo.getIncludes().toArray()[3]);

    Object result2 = Ognl.getValue(ognlUtil.compile("{\"foo\",'ruby','b','tom'}"), context, foo2);
    ognlUtil.setProperty("includes", result2, foo2, context);

    assertEquals(4, foo.getIncludes().size());
    assertEquals("foo", foo.getIncludes().toArray()[0]);
    assertEquals("ruby", foo.getIncludes().toArray()[1]);
    assertEquals("b", "" + foo.getIncludes().toArray()[2]);
    assertEquals("tom", foo.getIncludes().toArray()[3]);

    result = ActionContext.getContext().getValueStack().findValue("{\"foo\",'ruby','b','tom'}");

    foo.setIncludes((Collection) result);
    assertEquals(ArrayList.class, result.getClass());

    assertEquals(4, foo.getIncludes().size());
    assertEquals("foo", foo.getIncludes().toArray()[0]);
    assertEquals("ruby", foo.getIncludes().toArray()[1]);
    assertEquals("b", "" + foo.getIncludes().toArray()[2]);
    assertEquals("tom", foo.getIncludes().toArray()[3]);
  }
Beispiel #15
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]);
  }
Beispiel #16
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());
  }
Beispiel #17
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());
  }
Beispiel #18
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());
  }
Beispiel #19
0
  public void test_Complicated_List() throws Exception {
    Root root = new Root();
    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);

    SimpleNode node =
        (SimpleNode)
            Ognl.compileExpression(
                context,
                root,
                "{ new org.ognl.test.objects.MenuItem('Home', 'Main', "
                    + "{ new org.ognl.test.objects.MenuItem('Help', 'Help'), "
                    + "new org.ognl.test.objects.MenuItem('Contact', 'Contact') }), " // end first
                                                                                      // item
                    + "new org.ognl.test.objects.MenuItem('UserList', getMessages().getMessage('menu.members')), "
                    + "new org.ognl.test.objects.MenuItem('account/BetSlipList', getMessages().getMessage('menu.account'), "
                    + "{ new org.ognl.test.objects.MenuItem('account/BetSlipList', 'My Bets'), "
                    + "new org.ognl.test.objects.MenuItem('account/TransactionList', 'My Transactions') }), "
                    + "new org.ognl.test.objects.MenuItem('About', 'About'), "
                    + "new org.ognl.test.objects.MenuItem('admin/Admin', getMessages().getMessage('menu.admin'), "
                    + "{ new org.ognl.test.objects.MenuItem('admin/AddEvent', 'Add event'), "
                    + "new org.ognl.test.objects.MenuItem('admin/AddResult', 'Add result') })}");

    assertTrue(List.class.isAssignableFrom(node.getAccessor().get(context, root).getClass()));
  }
Beispiel #20
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());
  }
Beispiel #21
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());
  }
Beispiel #22
0
  public void testCopyUnevenObjects() {
    Foo foo = new Foo();
    Bar bar = new Bar();

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

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

    foo.setTitle("blah");
    foo.setNumber(1);
    foo.setPoints(new long[] {1, 2, 3});
    foo.setBirthday(cal.getTime());
    foo.setUseful(false);

    ognlUtil.copy(foo, bar, context);

    assertEquals(foo.getTitle(), bar.getTitle());
    assertEquals(0, bar.getSomethingElse());
  }
Beispiel #23
0
  public void testIncudeExcludes() {

    Foo foo1 = new Foo();
    Foo foo2 = new Foo();

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

    foo1.setPoints(new long[] {1, 2, 3});
    foo1.setBirthday(cal.getTime());
    foo1.setUseful(false);

    foo1.setTitle("foo1 title");
    foo1.setNumber(1);

    foo2.setTitle("foo2 title");
    foo2.setNumber(2);

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

    List<String> excludes = new ArrayList<String>();
    excludes.add("title");
    excludes.add("number");

    ognlUtil.copy(foo1, foo2, context, excludes, null);
    // these values should remain unchanged in foo2
    assertEquals(foo2.getTitle(), "foo2 title");
    assertEquals(foo2.getNumber(), 2);

    // these values should be changed/copied
    assertEquals(foo1.getPoints(), foo2.getPoints());
    assertEquals(foo1.getBirthday(), foo2.getBirthday());
    assertEquals(foo1.isUseful(), foo2.isUseful());

    Bar b1 = new Bar();
    Bar b2 = new Bar();

    b1.setTitle("bar1 title");
    b1.setSomethingElse(10);

    b1.setId(new Long(1));

    b2.setTitle("");
    b2.setId(new Long(2));

    context = Ognl.createDefaultContext(b1);
    List<String> includes = new ArrayList<String>();
    includes.add("title");
    includes.add("somethingElse");

    ognlUtil.copy(b1, b2, context, null, includes);
    // includes properties got copied
    assertEquals(b1.getTitle(), b2.getTitle());
    assertEquals(b1.getSomethingElse(), b2.getSomethingElse());

    // id properties did not
    assertEquals(b2.getId(), new Long(2));
  }
Beispiel #24
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());
  }
Beispiel #25
0
  public void test_Indexed_Object_Type() throws Throwable {
    // ASTChain chain = new ASTChain(0);

    ASTProperty listp = new ASTProperty(0);
    listp.setIndexedAccess(false);
    // listp.jjtSetParent(chain);

    ASTConst listc = new ASTConst(0);
    listc.setValue("list");
    listc.jjtSetParent(listp);
    listp.jjtAddChild(listc, 0);

    // chain.jjtAddChild(listp, 0);

    ASTProperty p = new ASTProperty(0);
    p.setIndexedAccess(true);

    ASTProperty pindex = new ASTProperty(0);

    ASTConst pRef = new ASTConst(0);
    pRef.setValue("genericIndex");
    pRef.jjtSetParent(pindex);
    pindex.jjtAddChild(pRef, 0);

    p.jjtAddChild(pindex, 0);
    // chain.jjtAddChild(p, 1);

    Root root = new Root();

    OgnlContext context = (OgnlContext) Ognl.createDefaultContext(null);
    context.setRoot(root);
    context.setCurrentObject(root);
    context.setCurrentNode(listp);

    assertEquals(".getList()", listp.toGetSourceString(context, root));
    assertEquals(List.class, context.getCurrentType());
    assertEquals(Root.class, context.getCurrentAccessor());
    assertEquals(null, context.getPreviousAccessor());
    assertEquals(root.getClass(), context.getPreviousType());
    assertEquals(root.getList(), context.getCurrentObject());

    // re test with chain

    context = (OgnlContext) Ognl.createDefaultContext(null);
    context.setRoot(root);
    context.setCurrentObject(root);

    ASTChain chain = new ASTChain(0);
    listp.jjtSetParent(chain);
    chain.jjtAddChild(listp, 0);

    context.setCurrentNode(chain);

    assertEquals(".getList()", chain.toGetSourceString(context, root));
    assertEquals(List.class, context.getCurrentType());
    assertEquals(Root.class, context.getCurrentAccessor());
    assertEquals(null, context.getPreviousAccessor());
    assertEquals(Root.class, context.getPreviousType());
    assertEquals(root.getList(), context.getCurrentObject());

    // test with only getIndex

    assertEquals(
        ".get(ognl.OgnlOps#getIntValue(((org.ognl.test.objects.Root)$2)..getGenericIndex().toString()))",
        p.toGetSourceString(context, root.getList()));
    assertEquals(root.getArray(), context.getCurrentObject());
    assertEquals(Object.class, context.getCurrentType());
  }