package com.qbq;
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import junit.framework.TestCase; import ognl.Ognl; import ognl.OgnlException;
public class TestOgnl extends TestCase { private void out(Object obj) { System.out.println(obj); }
public void testOgnl001() throws OgnlException { User user = new User(); user.setUsername("John"); String value = (String) Ognl.getValue("username", user); out(value); assertEquals("John", value); } public void testOgnl002() throws OgnlException { User user = new User(); Person p = new Person(); p.setName("Mike"); user.setPerson(p); String value = (String) Ognl.getValue("person.name", user); out(value); assertEquals("Mike", value); } public void testOgnl003() throws OgnlException { User user = new User(); Person p = new Person(); p.setName("Jim"); user.setPerson(p); String value = (String) Ognl.getValue("#root.person.name", user); out(value); assertEquals("Jim", value); } public void testOgnl004() throws OgnlException { Person p1 = new Person(); p1.setName("Jordan"); Person p2 = new Person(); p2.setName("Carter"); Map<String, Person> context = new HashMap<String, Person>(); context.put("p1", p1); context.put("p2", p2); String value = (String) Ognl.getValue("#p1.name + ' , ' + #p2.name", context, new Object()); out(value); assertEquals("Jordan , Carter", value); } public void testOgnl005() throws OgnlException { Person p1 = new Person(); p1.setName("Jordan"); Person p2 = new Person(); p2.setName("Carter"); Map<String, Person> context = new HashMap<String, Person>(); context.put("p1", p1); context.put("p2", p2); User root = new User(); root.setUsername("Miler"); String value = (String) Ognl.getValue("#p1.name + ' , ' + #p2.name + ' , ' + username", context, root); out(value); assertEquals("Jordan , Carter , Miler", value); } public void testOgnl006() throws OgnlException { User user = new User(); Ognl.setValue("username", user, "John"); out(user.getUsername()); assertEquals("John", user.getUsername()); } public void testOgnl007() throws OgnlException { User user = new User(); Map<String, User> context = new HashMap<String, User>(); context.put("u", user); Ognl.setValue("#u.username", context, new Object(), "John"); out(user.getUsername()); assertEquals("John", user.getUsername()); }
public void testOgnl008() throws OgnlException { User user = new User(); Map<String, User> context = new HashMap<String, User>(); context.put("u", user); Ognl.getValue("#u.username = 'John'", context, new Object()); out(user.getUsername()); assertEquals("John", user.getUsername()); }
public void testOgnl009() throws OgnlException { User user = new User(); Person person = new Person(); Map<String, Object> context = new HashMap<String, Object>(); context.put("u", user); context.put("p", person); Ognl.getValue("#u.username = 'John', #p.name = 'Mike'", context, new Object()); out(user.getUsername() + " , " + person.getName()); assertEquals("John", user.getUsername()); assertEquals("Mike", person.getName()); } public void testOgnl010() throws OgnlException { User user = new User(); user.setUsername("John"); String value = (String) Ognl.getValue("getUsername()", user); out(value); assertEquals("John", value); }
public void testOgnl011() throws OgnlException { User user = new User(); Ognl.getValue("setUsername('John')", user); out(user.getUsername()); assertEquals("John", user.getUsername()); } public void testOgnl012() throws OgnlException { Object root = new Object(); Map<String, Object> context = new HashMap<String, Object>(); List<Object> values = new ArrayList<Object>(); for(int i=0; i<11; i++){ values.add(i); } context.put("values", values); Ognl.getValue("@System@out.println(#values.size.(#this > 10 ? \"Bigger than 10\" : 'Not Bigger than 10'))", context, root); String value = (String) Ognl.getValue("#values.size.(#this > 10 ? \"Bigger than 10\" : 'Not Bigger than 10')", context, root); assertEquals("Bigger than 10", value); } public void testOgnl013() throws Exception{ User user = new User(); Ognl.getValue("setUsername('John')", user); Ognl.getValue("@System@out.println(#this.username)", user); String value = (String) Ognl.getValue("#this.username", user); assertEquals("John", value); } public void testOgnl014() throws Exception{ User user = new User(); Ognl.getValue("setUsername('John')", user); Ognl.getValue("@System@out.println(username.(#this.toLowerCase()))", user); String value = (String) Ognl.getValue("username.(#this.toLowerCase())", user); assertEquals("john", value); } public void testOgnl015() throws Exception{ Object root = new Object(); Map<String, Object> context = new HashMap<String, Object>(); User u = new User(); u.setUsername("John"); context.put("u", u); context.put("fact", "username");
String value = (String)Ognl.getValue("#fact(#u)", context, root); out(value); assertEquals("John", value); } public void testOgnl016() throws Exception{ Person person = new Person(); Map<String, Object> context = new HashMap<String, Object>(); User u = new User(); u.setUsername("John"); context.put("u", u); /** * 相当于调用person这个根对象的fact方法,并把#u的解释结果作为参数传入此方法 */ String value = (String)Ognl.getValue("fact(#u)", context, person); out(value); assertEquals("John", value); } public void testOgnl017() throws Exception{ Person person = new Person(); Map<String, Object> context = new HashMap<String, Object>(); User u = new User(); u.setUsername("John"); context.put("u", u); /** * 1、先计算表达式fact,得到结果是:username * 2、解释括号中的表达式#u,其结果是user对象 * 3、把括号中表达式的结果作为root对象,解释在第一步中得到的结果(即把第一步的结果看成另外一个表达式) */ String value = (String)Ognl.getValue("(fact)(#u)", context, person); out(value); assertEquals("John", value); } public void testOgnl018() throws Exception{ Object root = new Object(); Map<String, Object> context = new HashMap<String, Object>(); //用OGNL创建List对象
List<Object> listvalue = (List<Object>)Ognl.getValue("{100,'one hundred','yibai'}", context, root); context.put("listvalue", listvalue); //用OGNL创建数组
int[] intarray= (int[])Ognl.getValue("new int[]{1,2,3}", context, root); context.put("intarray", intarray); //用OGNL创建Map对象
Map<String, Object> mapvalue = (Map<String, Object>)Ognl.getValue("#{'listvalue':#listvalue,'intarray':#intarray}", context, root); context.put("mapvalue", mapvalue); Ognl.getValue("@System@out.println(#listvalue[2])", context, root); Ognl.getValue("@System@out.println(#intarray[0])", context, root); Ognl.getValue("@System@out.println(#mapvalue['intarray'][1])", context, root); Ognl.getValue("@System@out.println(#mapvalue.intarray[2])", context, root); assertEquals("yibai", Ognl.getValue("#listvalue[2]", context, root)); assertEquals(1, Ognl.getValue("#intarray[0]", context, root)); assertEquals(2, Ognl.getValue("#mapvalue['intarray'][1]", context, root)); assertEquals(3, Ognl.getValue("#mapvalue.intarray[2]", context, root)); } }
class Person { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } // For test 016
public String fact(User user) { return user.getUsername(); } // For test 017
public String fact = "username"; }
class User { private String username; private Person person; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } }
|