📄 ognlutiltest.java
字号:
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()); } 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()); } 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]); } 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"); } 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()); } 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]); } 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()); } 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()); } 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); } 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 } public void testGetBeanMap() throws Exception { Bar bar = new Bar(); bar.setTitle("I have beer"); Foo foo = new Foo(); foo.setALong(123); foo.setNumber(44); foo.setBar(bar); foo.setTitle("Hello Santa"); foo.setUseful(true); // just do some of the 15 tests Map beans = OgnlUtil.getBeanMap(foo); assertNotNull(beans); assertEquals(17, beans.size()); assertEquals("Hello Santa", beans.get("title")); assertEquals(new Long("123"), beans.get("ALong")); assertEquals(new Integer("44"), beans.get("number")); assertEquals(bar, beans.get("bar")); assertEquals(Boolean.TRUE, beans.get("useful")); } public void testGetBeanMapNoReadMethod() throws Exception { MyWriteBar bar = new MyWriteBar(); bar.setBar("Sams"); Map beans = OgnlUtil.getBeanMap(bar); assertEquals(2, beans.size()); assertEquals(new Integer("1"), beans.get("id")); assertEquals("There is no read method for bar", beans.get("bar")); } /** * XW-281 */ public void testSetBigIndexedValue() { ValueStack stack = ValueStackFactory.getFactory().createValueStack(); Map stackContext = stack.getContext(); stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.FALSE); stackContext.put(XWorkMethodAccessor.DENY_METHOD_EXECUTION, Boolean.TRUE); stackContext.put(XWorkConverter.REPORT_CONVERSION_ERRORS, Boolean.TRUE); User user = new User(); stack.push(user); // indexed string w/ existing array user.setList(new ArrayList()); String[] foo = new String[]{"asdf"}; ((OgnlValueStack)stack).setDevMode("true"); try { stack.setValue("list.1114778947765", foo); fail("non-valid expression: list.1114778947765"); } catch(RuntimeException ex) { ; // it's oke } try { stack.setValue("1114778947765", foo); fail("non-valid expression: 1114778947765"); } catch(RuntimeException ex) { ; } try { stack.setValue("1234", foo); fail("non-valid expression: 1114778947765"); } catch(RuntimeException ex) { ; } ((OgnlValueStack)stack).setDevMode("false"); stack.setValue("list.1114778947765", foo); stack.setValue("1114778947765", foo); stack.setValue("1234", foo); } public static class Email { String address; public void setAddress(String address) { this.address = address; } public String toString() { return address; } } static class TestObject { private Integer myIntegerProperty; private Long myLongProperty; private String myStrProperty; public void setMyIntegerProperty(Integer myIntegerProperty) { this.myIntegerProperty = myIntegerProperty; } public String getMyIntegerProperty() { return myIntegerProperty.toString(); } public void setMyLongProperty(Long myLongProperty) { this.myLongProperty = myLongProperty; } public Long getMyLongProperty() { return myLongProperty; } public void setMyStrProperty(String myStrProperty) { this.myStrProperty = myStrProperty; } public String getMyStrProperty() { return myStrProperty; } } class EmailAction { public List email = new OgnlList(Email.class); public List getEmail() { return this.email; } } class OgnlList extends ArrayList { private Class clazz; public OgnlList(Class clazz) { this.clazz = clazz; } public synchronized Object get(int index) { while (index >= this.size()) { try { this.add(clazz.newInstance()); } catch (Exception e) { throw new XWorkException(e); } } return super.get(index); } } private class MyWriteBar { private int id; public int getId() { return id; } public void setBar(String name) { if ("Sams".equals(name)) id = 1; else id = 999; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -