⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ognlutiltest.java

📁 在Struts2中的jar包xwork的源代码.版本为2.0.7
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.util;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.XWorkTestCase;import com.opensymphony.xwork2.XWorkException;import com.opensymphony.xwork2.interceptor.ChainingInterceptor;import com.opensymphony.xwork2.test.User;import ognl.*;import java.lang.reflect.Method;import java.text.*;import java.util.*;/** * Unit test of {@link OgnlUtil}. *  * @version $Date: 2007-02-01 12:10:10 +0100 (Do, 01 Feb 2007) $ $Id: OgnlUtilTest.java 1321 2007-02-01 11:10:10Z phil $ */public class OgnlUtilTest extends XWorkTestCase {    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 (int i = 0; i < methods.length; i++) {                    String name = methods[i].getName();                    if (!getter.equals(name) || (methods[i].getParameterTypes().length != 1)) {                        continue;                    } else {                        Class clazz = methods[i].getParameterTypes()[0];                        try {                            Object param = clazz.newInstance();                            methods[i].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());    }    public void testCanSetDependentObjectArray() {        EmailAction action = new EmailAction();        Map context = Ognl.createDefaultContext(action);        Map props = new HashMap();        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());    }    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());    }    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 context = Ognl.createDefaultContext(foo1);        List excludes = new ArrayList();        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 includes = new ArrayList();        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));    }    public void testCopyUnevenObjects() {        Foo foo = new Foo();        Bar bar = new Bar();        Map 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());    }    public void testDeepSetting() {        Foo foo = new Foo();        foo.setBar(new Bar());        Map context = Ognl.createDefaultContext(foo);        Map props = new HashMap();        props.put("bar.title", "i am barbaz");        OgnlUtil.setProperties(props, foo, context);        assertEquals(foo.getBar().getTitle(), "i am barbaz");    }    public void testExceptionForUnmatchedGetterAndSetterWithThrowPropertyException() {        Map props = new HashMap();        props.put("myIntegerProperty", new Integer(1234));        TestObject testObject = new TestObject();        try {            OgnlUtil.setProperties(props, testObject, true);            fail("should rise IllegalAccessException because of Wrong getter method");        } catch (Exception e) {            //expected        }    }    public void testExceptionForWrongPropertyNameWithThrowPropertyException() {        Map props = new HashMap();        props.put("myStringProperty", "testString");        TestObject testObject = new TestObject();        try {            OgnlUtil.setProperties(props, testObject, true);            fail("Should rise NoSuchPropertyException because of wrong property name");        } catch (Exception e) {            //expected        }    }    public void testOgnlHandlesCrapAtTheEndOfANumber() {        Foo foo = new Foo();        Map context = Ognl.createDefaultContext(foo);        Map props = new HashMap();        props.put("aLong", "123a");        OgnlUtil.setProperties(props, foo, context);        assertEquals(0, foo.getALong());    }    /**     * Test that type conversion is performed on indexed collection properties.     */    public void testSetIndexedValue() {        ValueStack stack = ValueStackFactory.getFactory().createValueStack();        Map stackContext = stack.getContext();        stackContext.put(InstantiatingNullHandler.CREATE_NULL_OBJECTS, Boolean.TRUE);        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());        user.getList().add("");        String[] foo = new String[]{"asdf"};        stack.setValue("list[0]", foo);        assertNotNull(user.getList());        assertEquals(1, user.getList().size());        assertEquals(String.class, user.getList().get(0).getClass());        assertEquals("asdf", user.getList().get(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());    }    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();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -