Search in sources :

Example 11 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method speed.

@Test
public void speed() {
    SimpleSpeedTest z = new SimpleSpeedTest();
    int num = 4988;
    String elstr = "num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7)-z.abc(i)";
    int i = 5000;
    Context con = Lang.context();
    con.set("num", num);
    con.set("i", i);
    con.set("z", z);
    assertEquals(num + (i - 1 + 2 - 3 + 4 - 5 + 6 - 7) - z.abc(i), El.eval(con, elstr));
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 12 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method array.

/**
     * 数组测试
     */
@Test
public void array() {
    Context context = Lang.context();
    String[] str = new String[] { "a", "b", "c" };
    String[][] bb = new String[][] { { "a", "b" }, { "c", "d" } };
    context.set("a", str);
    context.set("b", bb);
    assertEquals("b", El.eval(context, "a[1]"));
    assertEquals("b", El.eval(context, "a[1].toString()"));
    assertEquals("b", El.eval(context, "a[2-1]"));
    assertEquals("d", El.eval(context, "b[1][1]"));
}
Also used : Context(org.nutz.lang.util.Context) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 13 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method list.

/**
     * list测试
     */
@Test
public void list() {
    Context context = Lang.context();
    List<String> list = new ArrayList<String>();
    context.set("b", list);
    assertEquals(0, El.eval(context, "b.size()"));
    list.add("");
    assertEquals(1, El.eval(context, "b.size()"));
    El.eval(context, "b.add('Q\nQ')");
    assertEquals(2, El.eval(context, "b.size()"));
}
Also used : Context(org.nutz.lang.util.Context) ArrayList(java.util.ArrayList) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 14 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class El2Test method testIssue303.

@Test
public void testIssue303() {
    Context context = Lang.context();
    Issue303 item = new Issue303("item");
    item.child = new Issue303("child");
    context.set("item", item);
    assertEquals("child", El.eval(context, "item.child.getName()"));
    assertEquals(0, El.eval(context, "item.list.size()"));
}
Also used : Context(org.nutz.lang.util.Context) Issue303(org.nutz.el.issue.Issue303) SimpleSpeedTest(org.nutz.el.speed.SimpleSpeedTest) Test(org.junit.Test)

Example 15 with Context

use of org.nutz.lang.util.Context in project nutz by nutzam.

the class TableName method render.

/**
     * 根据当前线程的参考对象,渲染一个动态表名
     * 
     * @param tableName
     *            动态表名
     * @return 渲染后的表名
     */
public static String render(Segment tableName) {
    Object obj = get();
    if (null == obj || !tableName.hasKey())
        return tableName.toString();
    Context context = Lang.context();
    if (isPrimitive(obj)) {
        for (String key : tableName.keys()) context.set(key, obj);
    } else if (obj instanceof Context) {
        for (String key : tableName.keys()) context.set(key, ((Context) obj).get(key));
    } else if (obj instanceof Map<?, ?>) {
        for (String key : tableName.keys()) context.set(key, ((Map<?, ?>) obj).get(key));
    } else {
        Mirror<?> mirror = Mirror.me(obj);
        for (String key : tableName.keys()) context.set(key, mirror.getValue(obj, key));
    }
    return tableName.render(context).toString();
}
Also used : Context(org.nutz.lang.util.Context) Map(java.util.Map)

Aggregations

Context (org.nutz.lang.util.Context)40 Test (org.junit.Test)29 SimpleSpeedTest (org.nutz.el.speed.SimpleSpeedTest)24 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 ServletContext (javax.servlet.ServletContext)3 El (org.nutz.el.El)3 Map (java.util.Map)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 BigDecimal (java.math.BigDecimal)1 ServerSocket (java.net.ServerSocket)1 Socket (java.net.Socket)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Ignore (org.junit.Ignore)1 ElException (org.nutz.el.ElException)1 Issue293 (org.nutz.el.issue.Issue293)1 Issue303 (org.nutz.el.issue.Issue303)1