Example #1
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());
  }